blob: f4f43360f5e1465e9c4fd4d538a9e8cd4f9ed70f [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"
Stephen Hines0e2c34f2015-03-23 12:09:02 -070022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#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
Douglas Gregorb892d702013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
Stephen Hines176edba2014-12-01 14:53:08 -0800120 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000121 // Have we seen this protocol before?
Stephen Hines176edba2014-12-01 14:53:08 -0800122 if (!Known.insert(Proto).second)
Douglas Gregorb892d702013-01-21 19:42:21 +0000123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb892d702013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
Stephen Hines651f13c2014-04-23 16:59:28 -0700135 for (auto *P : Proto->protocols())
136 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb892d702013-01-21 19:42:21 +0000137}
138
John McCalld226f652010-08-21 09:40:31 +0000139Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000140 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000141 FieldDeclarator &FD,
142 ObjCDeclSpec &ODS,
143 Selector GetterSel,
144 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000145 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000146 tok::ObjCKeywordKind MethodImplKind,
147 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000148 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000149 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
150 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000151 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000152
Bill Wendlingad017fa2012-12-20 19:22:21 +0000153 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000154 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000155 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000156 // property is defaulted to 'assign' if it is readwrite and is
157 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000158 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000159 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000160 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
161 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
162 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
163 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000165
Douglas Gregoraabd0942013-01-21 19:05:22 +0000166 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000167 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700168 ObjCPropertyDecl *Res = nullptr;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000169 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000170 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000171 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000172 FD, GetterSel, SetterSel,
173 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000174 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000175 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000176 isOverridingProperty, TSI,
177 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000178 if (!Res)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700179 return nullptr;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000180 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000181 }
182
183 if (!Res) {
184 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
185 GetterSel, SetterSel, isAssign, isReadWrite,
186 Attributes, ODS.getPropertyAttributes(),
187 TSI, MethodImplKind);
188 if (lexicalDC)
189 Res->setLexicalDeclContext(lexicalDC);
190 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000191
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000192 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000193 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000194 (isa<ObjCInterfaceDecl>(ClassDecl) ||
195 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000196
David Blaikie4e4d0842012-03-11 07:00:24 +0000197 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000198 checkARCPropertyDecl(*this, Res);
199
Douglas Gregorb892d702013-01-21 19:42:21 +0000200 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000201 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000202 // For a class, compare the property against a property in our superclass.
203 bool FoundInSuper = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700204 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
205 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000206 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000207 for (unsigned I = 0, N = R.size(); I != N; ++I) {
208 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000209 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb892d702013-01-21 19:42:21 +0000210 FoundInSuper = true;
211 break;
212 }
213 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 if (FoundInSuper)
215 break;
216 else
217 CurrentInterfaceDecl = Super;
Douglas Gregorb892d702013-01-21 19:42:21 +0000218 }
219
220 if (FoundInSuper) {
221 // Also compare the property against a property in our protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 for (auto *P : CurrentInterfaceDecl->protocols()) {
223 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
Stephen Hines651f13c2014-04-23 16:59:28 -0700227 for (auto *P : IFace->all_referenced_protocols()) {
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000229 }
230 }
231 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700232 for (auto *P : Cat->protocols())
233 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000234 } else {
235 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Stephen Hines651f13c2014-04-23 16:59:28 -0700236 for (auto *P : Proto->protocols())
237 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000238 }
239
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000240 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000241 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000242}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000243
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000244static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000245makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000246 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000247 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000248 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000249 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000250 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000251 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000252 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000253 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000254 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000255 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000256 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000257 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000258 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000259 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000260 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000261 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000262 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000263 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000264 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000265 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000266 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000267 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000268 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000269 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000270 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
271
272 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
273}
274
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000275static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000276 SourceLocation LParenLoc, SourceLocation &Loc) {
277 if (LParenLoc.isMacroID())
278 return false;
279
280 SourceManager &SM = Context.getSourceManager();
281 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
282 // Try to load the file buffer.
283 bool invalidTemp = false;
284 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
285 if (invalidTemp)
286 return false;
287 const char *tokenBegin = file.data() + locInfo.second;
288
289 // Lex from the start of the given location.
290 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
291 Context.getLangOpts(),
292 file.begin(), tokenBegin, file.end());
293 Token Tok;
294 do {
295 lexer.LexFromRawLexer(Tok);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700296 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000297 Loc = Tok.getLocation();
298 return true;
299 }
300 } while (Tok.isNot(tok::r_paren));
301 return false;
302
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000303}
304
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000305static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000306 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
307 ObjCPropertyDecl::OBJC_PR_retain |
308 ObjCPropertyDecl::OBJC_PR_copy |
309 ObjCPropertyDecl::OBJC_PR_weak |
310 ObjCPropertyDecl::OBJC_PR_strong |
311 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
312}
313
Douglas Gregoraabd0942013-01-21 19:05:22 +0000314ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000315Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000316 SourceLocation AtLoc,
317 SourceLocation LParenLoc,
318 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000319 Selector GetterSel, Selector SetterSel,
320 const bool isAssign,
321 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000322 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000323 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000324 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000325 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000326 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000327 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000328 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000329 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000330 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000331 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
332
Douglas Gregord3297242013-01-16 23:00:23 +0000333 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000334 // Check for duplicate declaration of this property in current and
335 // other class extensions.
Stephen Hines651f13c2014-04-23 16:59:28 -0700336 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +0000337 if (ObjCPropertyDecl *prevDecl
Stephen Hines651f13c2014-04-23 16:59:28 -0700338 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000339 Diag(AtLoc, diag::err_duplicate_property);
340 Diag(prevDecl->getLocation(), diag::note_property_declare);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700341 return nullptr;
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000342 }
343 }
Douglas Gregord3297242013-01-16 23:00:23 +0000344 }
345
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000346 // Create a new ObjCPropertyDecl with the DeclContext being
347 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000348 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000349 ObjCPropertyDecl *PDecl =
350 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000351 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000352 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000353 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000354 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000356 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000358 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
359 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
360 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000362 // Set setter/getter selector name. Needed later.
363 PDecl->setGetterName(GetterSel);
364 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000365 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000366 DC->addDecl(PDecl);
367
368 // We need to look in the @interface to see if the @property was
369 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000370 if (!CCPrimary) {
371 Diag(CDecl->getLocation(), diag::err_continuation_class);
372 *isOverridingProperty = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700373 return nullptr;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000374 }
375
376 // Find the property in continuation class's primary class only.
377 ObjCPropertyDecl *PIDecl =
378 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
379
380 if (!PIDecl) {
381 // No matching property found in the primary class. Just fall thru
382 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000383 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000384 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000385 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000386 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000387
388 // A case of continuation class adding a new property in the class. This
389 // is not what it was meant for. However, gcc supports it and so should we.
390 // Make sure setter/getters are declared here.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700391 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
392 /* redeclaredProperty = */ nullptr,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000393 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000394 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
395 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000396 if (ASTMutationListener *L = Context.getASTMutationListener())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700397 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
398 CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000399 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000400 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000401 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
402 bool IncompatibleObjC = false;
403 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000404 // Relax the strict type matching for property type in continuation class.
405 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000406 // as it narrows the object type in its primary class property. Note that
407 // this conversion is safe only because the wider type is for a 'readonly'
408 // property in primary class and 'narrowed' type for a 'readwrite' property
409 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000410 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
411 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
412 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
413 ConvertedType, IncompatibleObjC))
414 || IncompatibleObjC) {
415 Diag(AtLoc,
416 diag::err_type_mismatch_continuation_class) << PDecl->getType();
417 Diag(PIDecl->getLocation(), diag::note_property_declare);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700418 return nullptr;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000419 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000420 }
421
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000422 // The property 'PIDecl's readonly attribute will be over-ridden
423 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000424 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000425 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +0000426 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
427 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000428 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000429 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000430 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000431 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
432 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000433 Diag(AtLoc, diag::warn_property_attr_mismatch);
434 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000435 }
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000436 else if (getLangOpts().ObjCAutoRefCount) {
437 QualType PrimaryPropertyQT =
438 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
439 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Bill Wendlingd75c6a72013-11-20 06:43:12 +0000440 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000441 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
442 PrimaryPropertyQT.getObjCLifetime();
443 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Bill Wendlingd75c6a72013-11-20 06:43:12 +0000444 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
445 !PropertyIsWeak) {
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000446 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
447 Diag(PIDecl->getLocation(), diag::note_property_declare);
448 }
449 }
450 }
451
Ted Kremenek9944c762010-03-18 01:22:36 +0000452 DeclContext *DC = cast<DeclContext>(CCPrimary);
453 if (!ObjCPropertyDecl::findPropertyDecl(DC,
454 PIDecl->getDeclName().getAsIdentifierInfo())) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700455 // In mrr mode, 'readwrite' property must have an explicit
456 // memory attribute. If none specified, select the default (assign).
457 if (!getLangOpts().ObjCAutoRefCount) {
458 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
459 ObjCDeclSpec::DQ_PR_retain |
460 ObjCDeclSpec::DQ_PR_strong |
461 ObjCDeclSpec::DQ_PR_copy |
462 ObjCDeclSpec::DQ_PR_unsafe_unretained |
463 ObjCDeclSpec::DQ_PR_weak)))
464 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
465 }
466
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000467 // Protocol is not in the primary class. Must build one for it.
468 ObjCDeclSpec ProtocolPropertyODS;
469 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
470 // and ObjCPropertyDecl::PropertyAttributeKind have identical
471 // values. Should consolidate both into one enum type.
472 ProtocolPropertyODS.
473 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
474 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000475 // Must re-establish the context from class extension to primary
476 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000477 ContextRAII SavedContext(*this, CCPrimary);
478
John McCalld226f652010-08-21 09:40:31 +0000479 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000480 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000481 PIDecl->getGetterName(),
482 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000483 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000484 MethodImplKind,
485 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000486 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000487 }
488 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000489 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000490 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000494 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
495 PIDecl->setSetterName(SetterSel);
496 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000497 // Tailor the diagnostics for the common case where a readwrite
498 // property is declared both in the @interface and the continuation.
499 // This is a common error where the user often intended the original
500 // declaration to be readonly.
501 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000502 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000503 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
504 ? diag::err_use_continuation_class_redeclaration_readwrite
505 : diag::err_use_continuation_class;
506 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000507 << CCPrimary->getDeclName();
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700509 return nullptr;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000510 }
511 *isOverridingProperty = true;
512 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000513 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000514 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
515 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000516 if (ASTMutationListener *L = Context.getASTMutationListener())
517 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000518 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000519}
520
521ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
522 ObjCContainerDecl *CDecl,
523 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000524 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000525 FieldDeclarator &FD,
526 Selector GetterSel,
527 Selector SetterSel,
528 const bool isAssign,
529 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000530 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000531 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000532 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000533 tok::ObjCKeywordKind MethodImplKind,
534 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000535 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000536 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000537
538 // Issue a warning if property is 'assign' as default and its object, which is
539 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000540 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000541 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000542 if (const ObjCObjectPointerType *ObjPtrTy =
543 T->getAs<ObjCObjectPointerType>()) {
544 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
545 if (IDecl)
546 if (ObjCProtocolDecl* PNSCopying =
547 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
548 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
549 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000550 }
Eli Friedman27d46442013-07-09 01:38:07 +0000551
552 if (T->isObjCObjectType()) {
553 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700554 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman27d46442013-07-09 01:38:07 +0000555 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
556 << FixItHint::CreateInsertion(StarLoc, "*");
557 T = Context.getObjCObjectPointerType(T);
558 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
559 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
560 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000561
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000562 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000563 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
564 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000565 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000566
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000567 if (ObjCPropertyDecl *prevDecl =
568 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000570 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000571 PDecl->setInvalidDecl();
572 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000573 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000574 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000575 if (lexicalDC)
576 PDecl->setLexicalDeclContext(lexicalDC);
577 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000578
579 if (T->isArrayType() || T->isFunctionType()) {
580 Diag(AtLoc, diag::err_property_type) << T;
581 PDecl->setInvalidDecl();
582 }
583
584 ProcessDeclAttributes(S, PDecl, FD.D);
585
586 // Regardless of setter/getter attribute, we save the default getter/setter
587 // selector names in anticipation of declaration of setter/getter methods.
588 PDecl->setGetterName(GetterSel);
589 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000590 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000591 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000592
Bill Wendlingad017fa2012-12-20 19:22:21 +0000593 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
595
Bill Wendlingad017fa2012-12-20 19:22:21 +0000596 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000597 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
598
Bill Wendlingad017fa2012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
601
602 if (isReadWrite)
603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
604
Bill Wendlingad017fa2012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
607
Bill Wendlingad017fa2012-12-20 19:22:21 +0000608 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
610
Bill Wendlingad017fa2012-12-20 19:22:21 +0000611 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
613
Bill Wendlingad017fa2012-12-20 19:22:21 +0000614 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
616
Bill Wendlingad017fa2012-12-20 19:22:21 +0000617 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
619
Ted Kremenek28685ab2010-03-12 00:46:40 +0000620 if (isAssign)
621 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
622
John McCall265941b2011-09-13 18:31:23 +0000623 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000624 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000626 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000628
John McCallf85e1932011-06-15 23:02:42 +0000629 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000630 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
632 if (isAssign)
633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
634
Ted Kremenek28685ab2010-03-12 00:46:40 +0000635 if (MethodImplKind == tok::objc_required)
636 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
637 else if (MethodImplKind == tok::objc_optional)
638 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000639
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000640 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000641}
642
John McCallf85e1932011-06-15 23:02:42 +0000643static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
644 ObjCPropertyDecl *property,
645 ObjCIvarDecl *ivar) {
646 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
647
John McCallf85e1932011-06-15 23:02:42 +0000648 QualType ivarType = ivar->getType();
649 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000650
John McCall265941b2011-09-13 18:31:23 +0000651 // The lifetime implied by the property's attributes.
652 Qualifiers::ObjCLifetime propertyLifetime =
653 getImpliedARCOwnership(property->getPropertyAttributes(),
654 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000655
John McCall265941b2011-09-13 18:31:23 +0000656 // We're fine if they match.
657 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000658
John McCall265941b2011-09-13 18:31:23 +0000659 // These aren't valid lifetimes for object ivars; don't diagnose twice.
660 if (ivarLifetime == Qualifiers::OCL_None ||
661 ivarLifetime == Qualifiers::OCL_Autoreleasing)
662 return;
John McCallf85e1932011-06-15 23:02:42 +0000663
John McCalld64c2eb2012-08-20 23:36:59 +0000664 // If the ivar is private, and it's implicitly __unsafe_unretained
665 // becaues of its type, then pretend it was actually implicitly
666 // __strong. This is only sound because we're processing the
667 // property implementation before parsing any method bodies.
668 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
669 propertyLifetime == Qualifiers::OCL_Strong &&
670 ivar->getAccessControl() == ObjCIvarDecl::Private) {
671 SplitQualType split = ivarType.split();
672 if (split.Quals.hasObjCLifetime()) {
673 assert(ivarType->isObjCARCImplicitlyUnretainedType());
674 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
675 ivarType = S.Context.getQualifiedType(split);
676 ivar->setType(ivarType);
677 return;
678 }
679 }
680
John McCall265941b2011-09-13 18:31:23 +0000681 switch (propertyLifetime) {
682 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000683 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000684 << property->getDeclName()
685 << ivar->getDeclName()
686 << ivarLifetime;
687 break;
John McCallf85e1932011-06-15 23:02:42 +0000688
John McCall265941b2011-09-13 18:31:23 +0000689 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000690 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000691 << property->getDeclName()
692 << ivar->getDeclName();
693 break;
John McCallf85e1932011-06-15 23:02:42 +0000694
John McCall265941b2011-09-13 18:31:23 +0000695 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000696 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000697 << property->getDeclName()
698 << ivar->getDeclName()
699 << ((property->getPropertyAttributesAsWritten()
700 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
701 break;
John McCallf85e1932011-06-15 23:02:42 +0000702
John McCall265941b2011-09-13 18:31:23 +0000703 case Qualifiers::OCL_Autoreleasing:
704 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000705
John McCall265941b2011-09-13 18:31:23 +0000706 case Qualifiers::OCL_None:
707 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000708 return;
709 }
710
711 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000712 if (propertyImplLoc.isValid())
713 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000714}
715
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000716/// setImpliedPropertyAttributeForReadOnlyProperty -
717/// This routine evaludates life-time attributes for a 'readonly'
718/// property with no known lifetime of its own, using backing
719/// 'ivar's attribute, if any. If no backing 'ivar', property's
720/// life-time is assumed 'strong'.
721static void setImpliedPropertyAttributeForReadOnlyProperty(
722 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
723 Qualifiers::ObjCLifetime propertyLifetime =
724 getImpliedARCOwnership(property->getPropertyAttributes(),
725 property->getType());
726 if (propertyLifetime != Qualifiers::OCL_None)
727 return;
728
729 if (!ivar) {
730 // if no backing ivar, make property 'strong'.
731 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
732 return;
733 }
734 // property assumes owenership of backing ivar.
735 QualType ivarType = ivar->getType();
736 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
737 if (ivarLifetime == Qualifiers::OCL_Strong)
738 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
739 else if (ivarLifetime == Qualifiers::OCL_Weak)
740 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
741 return;
742}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000743
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000744/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
745/// in inherited protocols with mismatched types. Since any of them can
746/// be candidate for synthesis.
Benjamin Kramerb1a4d372013-05-23 15:53:44 +0000747static void
748DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
749 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000750 ObjCPropertyDecl *Property) {
751 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700752 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
753 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000754 PDecl->collectInheritedProtocolProperties(Property, PropMap);
755 }
756 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
757 while (SDecl) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700758 for (const auto *PI : SDecl->all_referenced_protocols()) {
759 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000760 PDecl->collectInheritedProtocolProperties(Property, PropMap);
761 }
762 SDecl = SDecl->getSuperClass();
763 }
764
765 if (PropMap.empty())
766 return;
767
768 QualType RHSType = S.Context.getCanonicalType(Property->getType());
769 bool FirsTime = true;
770 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
771 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
772 ObjCPropertyDecl *Prop = I->second;
773 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
774 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
775 bool IncompatibleObjC = false;
776 QualType ConvertedType;
777 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
778 || IncompatibleObjC) {
779 if (FirsTime) {
780 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
781 << Property->getType();
782 FirsTime = false;
783 }
784 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
785 << Prop->getType();
786 }
787 }
788 }
789 if (!FirsTime && AtLoc.isValid())
790 S.Diag(AtLoc, diag::note_property_synthesize);
791}
792
Ted Kremenek28685ab2010-03-12 00:46:40 +0000793/// ActOnPropertyImplDecl - This routine performs semantic checks and
794/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000795/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000796///
John McCalld226f652010-08-21 09:40:31 +0000797Decl *Sema::ActOnPropertyImplDecl(Scope *S,
798 SourceLocation AtLoc,
799 SourceLocation PropertyLoc,
800 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000801 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000802 IdentifierInfo *PropertyIvar,
803 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000804 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000805 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000806 // Make sure we have a context for the property implementation declaration.
807 if (!ClassImpDecl) {
808 Diag(AtLoc, diag::error_missing_property_context);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700809 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000810 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000811 if (PropertyIvarLoc.isInvalid())
812 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000813 SourceLocation PropertyDiagLoc = PropertyLoc;
814 if (PropertyDiagLoc.isInvalid())
815 PropertyDiagLoc = ClassImpDecl->getLocStart();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700816 ObjCPropertyDecl *property = nullptr;
817 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000818 // Find the class or category class where this property must have
819 // a declaration.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700820 ObjCImplementationDecl *IC = nullptr;
821 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000822 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
823 IDecl = IC->getClassInterface();
824 // We always synthesize an interface for an implementation
825 // without an interface decl. So, IDecl is always non-zero.
826 assert(IDecl &&
827 "ActOnPropertyImplDecl - @implementation without @interface");
828
829 // Look for this property declaration in the @implementation's @interface
830 property = IDecl->FindPropertyDeclaration(PropertyId);
831 if (!property) {
832 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700833 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000834 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000835 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000836 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
837 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000838 if (AtLoc.isValid())
839 Diag(AtLoc, diag::warn_implicit_atomic_property);
840 else
841 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
842 Diag(property->getLocation(), diag::note_property_declare);
843 }
844
Ted Kremenek28685ab2010-03-12 00:46:40 +0000845 if (const ObjCCategoryDecl *CD =
846 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
847 if (!CD->IsClassExtension()) {
848 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
849 Diag(property->getLocation(), diag::note_property_declare);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700850 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000851 }
852 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000853 if (Synthesize&&
854 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
855 property->hasAttr<IBOutletAttr>() &&
856 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000857 bool ReadWriteProperty = false;
858 // Search into the class extensions and see if 'readonly property is
859 // redeclared 'readwrite', then no warning is to be issued.
Stephen Hines651f13c2014-04-23 16:59:28 -0700860 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000861 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
862 if (!R.empty())
863 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
864 PIkind = ExtProp->getPropertyAttributesAsWritten();
865 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
866 ReadWriteProperty = true;
867 break;
868 }
869 }
870 }
871
872 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000873 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Stephen Hines651f13c2014-04-23 16:59:28 -0700874 << property;
Fariborz Jahanian12564342013-02-08 23:32:30 +0000875 SourceLocation readonlyLoc;
876 if (LocPropertyAttribute(Context, "readonly",
877 property->getLParenLoc(), readonlyLoc)) {
878 SourceLocation endLoc =
879 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
880 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
881 Diag(property->getLocation(),
882 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
883 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
884 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000885 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000886 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000887 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
888 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000889
Ted Kremenek28685ab2010-03-12 00:46:40 +0000890 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
891 if (Synthesize) {
892 Diag(AtLoc, diag::error_synthesize_category_decl);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700893 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000894 }
895 IDecl = CatImplClass->getClassInterface();
896 if (!IDecl) {
897 Diag(AtLoc, diag::error_missing_property_interface);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700898 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000899 }
900 ObjCCategoryDecl *Category =
901 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
902
903 // If category for this implementation not found, it is an error which
904 // has already been reported eralier.
905 if (!Category)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700906 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000907 // Look for this property declaration in @implementation's category
908 property = Category->FindPropertyDeclaration(PropertyId);
909 if (!property) {
910 Diag(PropertyLoc, diag::error_bad_category_property_decl)
911 << Category->getDeclName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700912 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000913 }
914 } else {
915 Diag(AtLoc, diag::error_bad_property_context);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700916 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000917 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700918 ObjCIvarDecl *Ivar = nullptr;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000919 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000920 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000921 // Check that we have a valid, previously declared ivar for @synthesize
922 if (Synthesize) {
923 // @synthesize
924 if (!PropertyIvar)
925 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000926 // Check that this is a previously declared 'ivar' in 'IDecl' interface
927 ObjCInterfaceDecl *ClassDeclared;
928 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
929 QualType PropType = property->getType();
930 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000931
932 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000933 diag::err_incomplete_synthesized_property,
934 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000935 Diag(property->getLocation(), diag::note_property_declare);
936 CompleteTypeErr = true;
937 }
938
David Blaikie4e4d0842012-03-11 07:00:24 +0000939 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000940 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000941 ObjCPropertyDecl::OBJC_PR_readonly) &&
942 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000943 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
944 }
945
John McCallf85e1932011-06-15 23:02:42 +0000946 ObjCPropertyDecl::PropertyAttributeKind kind
947 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000948
949 // Add GC __weak to the ivar type if the property is weak.
950 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000951 getLangOpts().getGC() != LangOptions::NonGC) {
952 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000953 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000954 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000955 Diag(property->getLocation(), diag::note_property_declare);
956 } else {
957 PropertyIvarType =
958 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000959 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000960 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000961 if (AtLoc.isInvalid()) {
962 // Check when default synthesizing a property that there is
963 // an ivar matching property name and issue warning; since this
964 // is the most common case of not using an ivar used for backing
965 // property in non-default synthesis case.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700966 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000967 ObjCIvarDecl *originalIvar =
968 IDecl->lookupInstanceVariable(property->getIdentifier(),
969 ClassDeclared);
970 if (originalIvar) {
971 Diag(PropertyDiagLoc,
972 diag::warn_autosynthesis_property_ivar_match)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700973 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000974 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000975 Diag(property->getLocation(), diag::note_property_declare);
976 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000977 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000978 }
979
980 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000981 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000982 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000983 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000984 !PropertyIvarType.getObjCLifetime() &&
985 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000986
John McCall265941b2011-09-13 18:31:23 +0000987 // It's an error if we have to do this and the user didn't
988 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000989 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000990 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000991 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000992 diag::err_arc_objc_property_default_assign_on_object);
993 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000994 } else {
995 Qualifiers::ObjCLifetime lifetime =
996 getImpliedARCOwnership(kind, PropertyIvarType);
997 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000998 if (lifetime == Qualifiers::OCL_Weak) {
999 bool err = false;
1000 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001001 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1002 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1003 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001004 Diag(property->getLocation(),
1005 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1006 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1007 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001008 err = true;
1009 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001010 }
John McCall0a7dd782012-08-21 02:47:43 +00001011 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001012 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001013 Diag(property->getLocation(), diag::note_property_declare);
1014 }
John McCallf85e1932011-06-15 23:02:42 +00001015 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001016
John McCallf85e1932011-06-15 23:02:42 +00001017 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001018 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001019 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1020 }
John McCallf85e1932011-06-15 23:02:42 +00001021 }
1022
1023 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001024 !getLangOpts().ObjCAutoRefCount &&
1025 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001026 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001027 Diag(property->getLocation(), diag::note_property_declare);
1028 }
1029
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001030 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001031 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001032 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001033 ObjCIvarDecl::Private,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001034 (Expr *)nullptr, true);
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001035 if (RequireNonAbstractType(PropertyIvarLoc,
1036 PropertyIvarType,
1037 diag::err_abstract_type_in_decl,
1038 AbstractSynthesizedIvarType)) {
1039 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001040 Ivar->setInvalidDecl();
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001041 } else if (CompleteTypeErr)
1042 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001043 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001044 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001045
John McCall260611a2012-06-20 06:18:46 +00001046 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001047 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1048 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001049 // Note! I deliberately want it to fall thru so, we have a
1050 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001051 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001052 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001053 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001054 << property->getDeclName() << Ivar->getDeclName()
1055 << ClassDeclared->getDeclName();
1056 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001057 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001058 // Note! I deliberately want it to fall thru so more errors are caught.
1059 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001060 property->setPropertyIvarDecl(Ivar);
1061
Ted Kremenek28685ab2010-03-12 00:46:40 +00001062 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1063
1064 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001065 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001066 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001067 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001068 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001069 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001070 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001071 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001072 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001073 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1074 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001075 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001076 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001077 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001078 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001079 << property->getDeclName() << PropType
1080 << Ivar->getDeclName() << IvarType;
1081 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001082 // Note! I deliberately want it to fall thru so, we have a
1083 // a property implementation and to avoid future warnings.
1084 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001085 else {
1086 // FIXME! Rules for properties are somewhat different that those
1087 // for assignments. Use a new routine to consolidate all cases;
1088 // specifically for property redeclarations as well as for ivars.
1089 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1090 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1091 if (lhsType != rhsType &&
1092 lhsType->isArithmeticType()) {
1093 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1094 << property->getDeclName() << PropType
1095 << Ivar->getDeclName() << IvarType;
1096 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1097 // Fall thru - see previous comment
1098 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001099 }
1100 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001101 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001102 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001103 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001104 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001105 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001106 // Fall thru - see previous comment
1107 }
John McCallf85e1932011-06-15 23:02:42 +00001108 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001109 if ((property->getType()->isObjCObjectPointerType() ||
1110 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001111 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001112 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001113 << property->getDeclName() << Ivar->getDeclName();
1114 // Fall thru - see previous comment
1115 }
1116 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001117 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001118 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001119 } else if (PropertyIvar)
1120 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001121 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001122
Ted Kremenek28685ab2010-03-12 00:46:40 +00001123 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1124 ObjCPropertyImplDecl *PIDecl =
1125 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1126 property,
1127 (Synthesize ?
1128 ObjCPropertyImplDecl::Synthesize
1129 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001130 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001131
Fariborz Jahanian74414712012-05-15 18:12:51 +00001132 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001133 PIDecl->setInvalidDecl();
1134
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001135 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1136 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001137 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001138 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001139 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1140 // returned by the getter as it must conform to C++'s copy-return rules.
1141 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001142 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001143 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1144 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001145 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001146 VK_LValue, PropertyDiagLoc);
Eli Friedman9a14db32012-10-18 20:14:08 +00001147 MarkDeclRefReferenced(SelfExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001148 Expr *LoadSelfExpr =
1149 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001150 CK_LValueToRValue, SelfExpr, nullptr,
1151 VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001152 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001153 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001154 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001155 LoadSelfExpr, true, true);
1156 ExprResult Res = PerformCopyInitialization(
1157 InitializedEntity::InitializeResult(PropertyDiagLoc,
1158 getterMethod->getReturnType(),
1159 /*NRVO=*/false),
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001160 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001161 if (!Res.isInvalid()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001162 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001163 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001164 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001165 PIDecl->setGetterCXXConstructor(ResExpr);
1166 }
1167 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001168 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1169 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1170 Diag(getterMethod->getLocation(),
1171 diag::warn_property_getter_owning_mismatch);
1172 Diag(property->getLocation(), diag::note_property_declare);
1173 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001174 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1175 switch (getterMethod->getMethodFamily()) {
1176 case OMF_retain:
1177 case OMF_retainCount:
1178 case OMF_release:
1179 case OMF_autorelease:
1180 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1181 << 1 << getterMethod->getSelector();
1182 break;
1183 default:
1184 break;
1185 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001186 }
1187 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1188 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001189 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1190 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001191 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001192 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001193 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1194 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001195 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001196 VK_LValue, PropertyDiagLoc);
Eli Friedman9a14db32012-10-18 20:14:08 +00001197 MarkDeclRefReferenced(SelfExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001198 Expr *LoadSelfExpr =
1199 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001200 CK_LValueToRValue, SelfExpr, nullptr,
1201 VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001202 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001203 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001204 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001205 LoadSelfExpr, true, true);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001206 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1207 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001208 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001209 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1210 VK_LValue, PropertyDiagLoc);
1211 MarkDeclRefReferenced(rhs);
1212 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001213 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001214 if (property->getPropertyAttributes() &
1215 ObjCPropertyDecl::OBJC_PR_atomic) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001216 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001217 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001218 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1219 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001220 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001221 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001222 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001223 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001224 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001225 Diag(FuncDecl->getLocStart(),
1226 diag::note_callee_decl) << FuncDecl;
1227 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001228 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001229 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001230 }
1231 }
1232
Ted Kremenek28685ab2010-03-12 00:46:40 +00001233 if (IC) {
1234 if (Synthesize)
1235 if (ObjCPropertyImplDecl *PPIDecl =
1236 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1237 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1238 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1239 << PropertyIvar;
1240 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1241 }
1242
1243 if (ObjCPropertyImplDecl *PPIDecl
1244 = IC->FindPropertyImplDecl(PropertyId)) {
1245 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1246 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001247 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001248 }
1249 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001250 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001251 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001252 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001253 // Diagnose if an ivar was lazily synthesdized due to a previous
1254 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001255 // but it requires an ivar of different name.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001256 ObjCInterfaceDecl *ClassDeclared=nullptr;
1257 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001258 if (!Synthesize)
1259 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1260 else {
1261 if (PropertyIvar && PropertyIvar != PropertyId)
1262 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1263 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001264 // Issue diagnostics only if Ivar belongs to current class.
1265 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001266 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001267 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1268 << PropertyId;
1269 Ivar->setInvalidDecl();
1270 }
1271 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001272 } else {
1273 if (Synthesize)
1274 if (ObjCPropertyImplDecl *PPIDecl =
1275 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001276 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001277 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1278 << PropertyIvar;
1279 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1280 }
1281
1282 if (ObjCPropertyImplDecl *PPIDecl =
1283 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001284 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001285 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001286 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001287 }
1288 CatImplClass->addPropertyImplementation(PIDecl);
1289 }
1290
John McCalld226f652010-08-21 09:40:31 +00001291 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001292}
1293
1294//===----------------------------------------------------------------------===//
1295// Helper methods.
1296//===----------------------------------------------------------------------===//
1297
Ted Kremenek9d64c152010-03-12 00:38:38 +00001298/// DiagnosePropertyMismatch - Compares two properties for their
1299/// attributes and types and warns on a variety of inconsistencies.
1300///
1301void
1302Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1303 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001304 const IdentifierInfo *inheritedName,
1305 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001306 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001307 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001308 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001309 SuperProperty->getPropertyAttributes();
1310
1311 // We allow readonly properties without an explicit ownership
1312 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1313 // to be overridden by a property with any explicit ownership in the subclass.
1314 if (!OverridingProtocolProperty &&
1315 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1316 ;
1317 else {
1318 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1319 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1320 Diag(Property->getLocation(), diag::warn_readonly_property)
1321 << Property->getDeclName() << inheritedName;
1322 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1323 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001324 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001325 << Property->getDeclName() << "copy" << inheritedName;
1326 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1327 unsigned CAttrRetain =
1328 (CAttr &
1329 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1330 unsigned SAttrRetain =
1331 (SAttr &
1332 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1333 bool CStrong = (CAttrRetain != 0);
1334 bool SStrong = (SAttrRetain != 0);
1335 if (CStrong != SStrong)
1336 Diag(Property->getLocation(), diag::warn_property_attribute)
1337 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1338 }
John McCallf85e1932011-06-15 23:02:42 +00001339 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340
1341 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001342 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001343 Diag(Property->getLocation(), diag::warn_property_attribute)
1344 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001345 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1346 }
1347 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001348 Diag(Property->getLocation(), diag::warn_property_attribute)
1349 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001350 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1351 }
1352 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 Diag(Property->getLocation(), diag::warn_property_attribute)
1354 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001355 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1356 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001357
1358 QualType LHSType =
1359 Context.getCanonicalType(SuperProperty->getType());
1360 QualType RHSType =
1361 Context.getCanonicalType(Property->getType());
1362
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001363 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001364 // Do cases not handled in above.
1365 // FIXME. For future support of covariant property types, revisit this.
1366 bool IncompatibleObjC = false;
1367 QualType ConvertedType;
1368 if (!isObjCPointerConversion(RHSType, LHSType,
1369 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001370 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001371 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1372 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001373 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1374 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001375 }
1376}
1377
1378bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1379 ObjCMethodDecl *GetterMethod,
1380 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001381 if (!GetterMethod)
1382 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001383 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001384 QualType PropertyIvarType = property->getType().getNonReferenceType();
1385 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1386 if (!compat) {
1387 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1388 isa<ObjCObjectPointerType>(GetterType))
1389 compat =
1390 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001391 GetterType->getAs<ObjCObjectPointerType>(),
1392 PropertyIvarType->getAs<ObjCObjectPointerType>());
1393 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001394 != Compatible) {
1395 Diag(Loc, diag::error_property_accessor_type)
1396 << property->getDeclName() << PropertyIvarType
1397 << GetterMethod->getSelector() << GetterType;
1398 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1399 return true;
1400 } else {
1401 compat = true;
1402 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1403 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1404 if (lhsType != rhsType && lhsType->isArithmeticType())
1405 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001406 }
1407 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001408
1409 if (!compat) {
1410 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1411 << property->getDeclName()
1412 << GetterMethod->getSelector();
1413 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1414 return true;
1415 }
1416
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 return false;
1418}
1419
Ted Kremenek9d64c152010-03-12 00:38:38 +00001420/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001421/// the class and its conforming protocols; but not those in its super class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001422static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1423 ObjCContainerDecl::PropertyMap &PropMap,
1424 ObjCContainerDecl::PropertyMap &SuperPropMap,
1425 bool IncludeProtocols = true) {
1426
Ted Kremenek9d64c152010-03-12 00:38:38 +00001427 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001428 for (auto *Prop : IDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001429 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001430 if (IncludeProtocols) {
1431 // Scan through class's protocols.
1432 for (auto *PI : IDecl->all_referenced_protocols())
1433 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001435 }
1436 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1437 if (!CATDecl->IsClassExtension())
Stephen Hines651f13c2014-04-23 16:59:28 -07001438 for (auto *Prop : CATDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001439 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001440 if (IncludeProtocols) {
1441 // Scan through class's protocols.
1442 for (auto *PI : CATDecl->protocols())
1443 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1444 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001445 }
1446 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001447 for (auto *Prop : PDecl->properties()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001448 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1449 // Exclude property for protocols which conform to class's super-class,
1450 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001451 if (!PropertyFromSuper ||
1452 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001453 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1454 if (!PropEntry)
1455 PropEntry = Prop;
1456 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001457 }
1458 // scan through protocol's protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001459 for (auto *PI : PDecl->protocols())
1460 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001461 }
1462}
1463
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001464/// CollectSuperClassPropertyImplementations - This routine collects list of
1465/// properties to be implemented in super class(s) and also coming from their
1466/// conforming protocols.
1467static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001468 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001469 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001470 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001471 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001472 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001473 SDecl = SDecl->getSuperClass();
1474 }
1475 }
1476}
1477
Fariborz Jahanian26202292013-02-14 19:07:19 +00001478/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1479/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1480/// declared in class 'IFace'.
1481bool
1482Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1483 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1484 if (!IV->getSynthesize())
1485 return false;
1486 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1487 Method->isInstanceMethod());
1488 if (!IMD || !IMD->isPropertyAccessor())
1489 return false;
1490
1491 // look up a property declaration whose one of its accessors is implemented
1492 // by this method.
Stephen Hines651f13c2014-04-23 16:59:28 -07001493 for (const auto *Property : IFace->properties()) {
1494 if ((Property->getGetterName() == IMD->getSelector() ||
1495 Property->getSetterName() == IMD->getSelector()) &&
1496 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahanian26202292013-02-14 19:07:19 +00001497 return true;
1498 }
1499 return false;
1500}
1501
Stephen Hines651f13c2014-04-23 16:59:28 -07001502static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1503 ObjCPropertyDecl *Prop) {
1504 bool SuperClassImplementsGetter = false;
1505 bool SuperClassImplementsSetter = false;
1506 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1507 SuperClassImplementsSetter = true;
1508
1509 while (IDecl->getSuperClass()) {
1510 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1511 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1512 SuperClassImplementsGetter = true;
1513
1514 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1515 SuperClassImplementsSetter = true;
1516 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1517 return true;
1518 IDecl = IDecl->getSuperClass();
1519 }
1520 return false;
1521}
Fariborz Jahanian26202292013-02-14 19:07:19 +00001522
James Dennett699c9042012-06-15 07:13:21 +00001523/// \brief Default synthesizes all properties which must be synthesized
1524/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001525void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1526 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001527
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001529 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1530 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001531 if (PropMap.empty())
1532 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001533 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001534 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1535
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001536 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1537 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001538 // Is there a matching property synthesize/dynamic?
1539 if (Prop->isInvalidDecl() ||
1540 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1541 continue;
1542 // Property may have been synthesized by user.
1543 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1544 continue;
1545 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1546 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1547 continue;
1548 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1549 continue;
1550 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001551 if (ObjCPropertyImplDecl *PID =
1552 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Stephen Hines176edba2014-12-01 14:53:08 -08001553 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1554 << Prop->getIdentifier();
1555 if (!PID->getLocation().isInvalid())
1556 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001557 continue;
1558 }
Stephen Hines176edba2014-12-01 14:53:08 -08001559 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Stephen Hines651f13c2014-04-23 16:59:28 -07001560 if (ObjCProtocolDecl *Proto =
1561 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001562 // We won't auto-synthesize properties declared in protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001563 // Suppress the warning if class's superclass implements property's
1564 // getter and implements property's setter (if readwrite property).
Stephen Hines176edba2014-12-01 14:53:08 -08001565 // Or, if property is going to be implemented in its super class.
1566 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001567 Diag(IMPDecl->getLocation(),
1568 diag::warn_auto_synthesizing_protocol_property)
1569 << Prop << Proto;
1570 Diag(Prop->getLocation(), diag::note_property_declare);
1571 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001572 continue;
1573 }
Stephen Hines176edba2014-12-01 14:53:08 -08001574 // If property to be implemented in the super class, ignore.
1575 if (PropInSuperClass) {
1576 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1577 (PropInSuperClass->getPropertyAttributes() &
1578 ObjCPropertyDecl::OBJC_PR_readonly) &&
1579 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1580 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1581 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1582 << Prop->getIdentifier();
1583 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1584 }
1585 else {
1586 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1587 << Prop->getIdentifier();
1588 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1589 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1590 }
1591 continue;
1592 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001593 // We use invalid SourceLocations for the synthesized ivars since they
1594 // aren't really synthesized at a particular location; they just exist.
1595 // Saying that they are located at the @implementation isn't really going
1596 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001597 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1598 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1599 true,
1600 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001601 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001602 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001603 if (PIDecl) {
1604 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001605 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001606 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001607 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001608}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001609
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001610void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001611 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001612 return;
1613 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1614 if (!IC)
1615 return;
1616 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001617 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001618 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001619}
1620
Stephen Hines651f13c2014-04-23 16:59:28 -07001621static void DiagnoseUnimplementedAccessor(Sema &S,
1622 ObjCInterfaceDecl *PrimaryClass,
1623 Selector Method,
1624 ObjCImplDecl* IMPDecl,
1625 ObjCContainerDecl *CDecl,
1626 ObjCCategoryDecl *C,
1627 ObjCPropertyDecl *Prop,
1628 Sema::SelectorSet &SMap) {
1629 // When reporting on missing property setter/getter implementation in
1630 // categories, do not report when they are declared in primary class,
1631 // class's protocol, or one of it super classes. This is because,
1632 // the class is going to implement them.
1633 if (!SMap.count(Method) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001634 (PrimaryClass == nullptr ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001635 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1636 S.Diag(IMPDecl->getLocation(),
1637 isa<ObjCCategoryDecl>(CDecl) ?
1638 diag::warn_setter_getter_impl_required_in_category :
1639 diag::warn_setter_getter_impl_required)
1640 << Prop->getDeclName() << Method;
1641 S.Diag(Prop->getLocation(),
1642 diag::note_property_declare);
1643 if (S.LangOpts.ObjCDefaultSynthProperties &&
1644 S.LangOpts.ObjCRuntime.isNonFragile())
1645 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1646 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1647 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1648 }
1649}
1650
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001651void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Stephen Hines651f13c2014-04-23 16:59:28 -07001652 ObjCContainerDecl *CDecl,
1653 bool SynthesizeProperties) {
1654 ObjCContainerDecl::PropertyMap PropMap;
1655 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1656
1657 if (!SynthesizeProperties) {
1658 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1659 // Gather properties which need not be implemented in this class
1660 // or category.
1661 if (!IDecl)
1662 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1663 // For categories, no need to implement properties declared in
1664 // its primary class (and its super classes) if property is
1665 // declared in one of those containers.
1666 if ((IDecl = C->getClassInterface())) {
1667 ObjCInterfaceDecl::PropertyDeclOrder PO;
1668 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1669 }
1670 }
1671 if (IDecl)
1672 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1673
1674 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1675 }
1676
1677 // Scan the @interface to see if any of the protocols it adopts
1678 // require an explicit implementation, via attribute
1679 // 'objc_protocol_requires_explicit_implementation'.
1680 if (IDecl) {
1681 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
1682
1683 for (auto *PDecl : IDecl->all_referenced_protocols()) {
1684 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1685 continue;
1686 // Lazily construct a set of all the properties in the @interface
1687 // of the class, without looking at the superclass. We cannot
1688 // use the call to CollectImmediateProperties() above as that
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001689 // utilizes information from the super class's properties as well
Stephen Hines651f13c2014-04-23 16:59:28 -07001690 // as scans the adopted protocols. This work only triggers for protocols
1691 // with the attribute, which is very rare, and only occurs when
1692 // analyzing the @implementation.
1693 if (!LazyMap) {
1694 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1695 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1696 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1697 /* IncludeProtocols */ false);
1698 }
1699 // Add the properties of 'PDecl' to the list of properties that
1700 // need to be implemented.
1701 for (auto *PropDecl : PDecl->properties()) {
1702 if ((*LazyMap)[PropDecl->getIdentifier()])
1703 continue;
1704 PropMap[PropDecl->getIdentifier()] = PropDecl;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001705 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001706 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001707 }
1708
Ted Kremenek9d64c152010-03-12 00:38:38 +00001709 if (PropMap.empty())
1710 return;
1711
1712 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Stephen Hines651f13c2014-04-23 16:59:28 -07001713 for (const auto *I : IMPDecl->property_impls())
David Blaikie262bc182012-04-30 02:36:29 +00001714 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001715
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001716 SelectorSet InsMap;
1717 // Collect property accessors implemented in current implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001718 for (const auto *I : IMPDecl->instance_methods())
1719 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001720
1721 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001722 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001723 if (C && !C->IsClassExtension())
1724 if ((PrimaryClass = C->getClassInterface()))
1725 // Report unimplemented properties in the category as well.
1726 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1727 // When reporting on missing setter/getters, do not report when
1728 // setter/getter is implemented in category's primary class
1729 // implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001730 for (const auto *I : IMP->instance_methods())
1731 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001732 }
1733
Anna Zaksb36ea372012-10-18 19:17:53 +00001734 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001735 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1736 ObjCPropertyDecl *Prop = P->second;
1737 // Is there a matching propery synthesize/dynamic?
1738 if (Prop->isInvalidDecl() ||
1739 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001740 PropImplMap.count(Prop) ||
1741 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001742 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001743
1744 // Diagnose unimplemented getters and setters.
1745 DiagnoseUnimplementedAccessor(*this,
1746 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1747 if (!Prop->isReadOnly())
1748 DiagnoseUnimplementedAccessor(*this,
1749 PrimaryClass, Prop->getSetterName(),
1750 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001751 }
1752}
1753
1754void
1755Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1756 ObjCContainerDecl* IDecl) {
1757 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001758 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001759 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07001760 for (const auto *Property : IDecl->properties()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001761 ObjCMethodDecl *GetterMethod = nullptr;
1762 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001763 bool LookedUpGetterSetter = false;
1764
Bill Wendlingad017fa2012-12-20 19:22:21 +00001765 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001766 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001767
John McCall265941b2011-09-13 18:31:23 +00001768 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1769 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001770 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1771 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1772 LookedUpGetterSetter = true;
1773 if (GetterMethod) {
1774 Diag(GetterMethod->getLocation(),
1775 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001776 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001777 Diag(Property->getLocation(), diag::note_property_declare);
1778 }
1779 if (SetterMethod) {
1780 Diag(SetterMethod->getLocation(),
1781 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001782 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001783 Diag(Property->getLocation(), diag::note_property_declare);
1784 }
1785 }
1786
Ted Kremenek9d64c152010-03-12 00:38:38 +00001787 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001788 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1789 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001790 continue;
1791 if (const ObjCPropertyImplDecl *PIDecl
1792 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1793 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1794 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001795 if (!LookedUpGetterSetter) {
1796 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1797 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001798 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001799 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1800 SourceLocation MethodLoc =
1801 (GetterMethod ? GetterMethod->getLocation()
1802 : SetterMethod->getLocation());
1803 Diag(MethodLoc, diag::warn_atomic_property_rule)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001804 << Property->getIdentifier() << (GetterMethod != nullptr)
1805 << (SetterMethod != nullptr);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001806 // fixit stuff.
1807 if (!AttributesAsWritten) {
1808 if (Property->getLParenLoc().isValid()) {
1809 // @property () ... case.
1810 SourceRange PropSourceRange(Property->getAtLoc(),
1811 Property->getLParenLoc());
1812 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1813 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1814 }
1815 else {
1816 //@property id etc.
1817 SourceLocation endLoc =
1818 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1819 endLoc = endLoc.getLocWithOffset(-1);
1820 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1821 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1822 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1823 }
1824 }
1825 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1826 // @property () ... case.
1827 SourceLocation endLoc = Property->getLParenLoc();
1828 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1829 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1830 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1831 }
1832 else
1833 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001834 Diag(Property->getLocation(), diag::note_property_declare);
1835 }
1836 }
1837 }
1838}
1839
John McCallf85e1932011-06-15 23:02:42 +00001840void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001841 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001842 return;
1843
Stephen Hines651f13c2014-04-23 16:59:28 -07001844 for (const auto *PID : D->property_impls()) {
John McCallf85e1932011-06-15 23:02:42 +00001845 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001846 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1847 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001848 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1849 if (!method)
1850 continue;
1851 ObjCMethodFamily family = method->getMethodFamily();
1852 if (family == OMF_alloc || family == OMF_copy ||
1853 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001854 if (getLangOpts().ObjCAutoRefCount)
Stephen Hines651f13c2014-04-23 16:59:28 -07001855 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCallf85e1932011-06-15 23:02:42 +00001856 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001857 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001858
1859 // Look for a getter explicitly declared alongside the property.
1860 // If we find one, use its location for the note.
1861 SourceLocation noteLoc = PD->getLocation();
1862 SourceLocation fixItLoc;
1863 for (auto *getterRedecl : method->redecls()) {
1864 if (getterRedecl->isImplicit())
1865 continue;
1866 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1867 continue;
1868 noteLoc = getterRedecl->getLocation();
1869 fixItLoc = getterRedecl->getLocEnd();
1870 }
1871
1872 Preprocessor &PP = getPreprocessor();
1873 TokenValue tokens[] = {
1874 tok::kw___attribute, tok::l_paren, tok::l_paren,
1875 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1876 PP.getIdentifierInfo("none"), tok::r_paren,
1877 tok::r_paren, tok::r_paren
1878 };
1879 StringRef spelling = "__attribute__((objc_method_family(none)))";
1880 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1881 if (!macroName.empty())
1882 spelling = macroName;
1883
1884 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1885 << method->getDeclName() << spelling;
1886 if (fixItLoc.isValid()) {
1887 SmallString<64> fixItText(" ");
1888 fixItText += spelling;
1889 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1890 }
John McCallf85e1932011-06-15 23:02:42 +00001891 }
1892 }
1893 }
1894}
1895
Stephen Hines651f13c2014-04-23 16:59:28 -07001896void Sema::DiagnoseMissingDesignatedInitOverrides(
1897 const ObjCImplementationDecl *ImplD,
1898 const ObjCInterfaceDecl *IFD) {
1899 assert(IFD->hasDesignatedInitializers());
1900 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1901 if (!SuperD)
1902 return;
1903
1904 SelectorSet InitSelSet;
1905 for (const auto *I : ImplD->instance_methods())
1906 if (I->getMethodFamily() == OMF_init)
1907 InitSelSet.insert(I->getSelector());
1908
1909 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1910 SuperD->getDesignatedInitializers(DesignatedInits);
1911 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1912 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1913 const ObjCMethodDecl *MD = *I;
1914 if (!InitSelSet.count(MD->getSelector())) {
1915 Diag(ImplD->getLocation(),
1916 diag::warn_objc_implementation_missing_designated_init_override)
1917 << MD->getSelector();
1918 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1919 }
1920 }
1921}
1922
John McCall5de74d12010-11-10 07:01:40 +00001923/// AddPropertyAttrs - Propagates attributes from a property to the
1924/// implicitly-declared getter or setter for that property.
1925static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1926 ObjCPropertyDecl *Property) {
1927 // Should we just clone all attributes over?
Stephen Hines651f13c2014-04-23 16:59:28 -07001928 for (const auto *A : Property->attrs()) {
1929 if (isa<DeprecatedAttr>(A) ||
1930 isa<UnavailableAttr>(A) ||
1931 isa<AvailabilityAttr>(A))
1932 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001933 }
John McCall5de74d12010-11-10 07:01:40 +00001934}
1935
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1937/// have the property type and issue diagnostics if they don't.
1938/// Also synthesize a getter/setter method if none exist (and update the
1939/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1940/// methods is the "right" thing to do.
1941void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001942 ObjCContainerDecl *CD,
1943 ObjCPropertyDecl *redeclaredProperty,
1944 ObjCContainerDecl *lexicalDC) {
1945
Ted Kremenek9d64c152010-03-12 00:38:38 +00001946 ObjCMethodDecl *GetterMethod, *SetterMethod;
1947
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001948 if (CD->isInvalidDecl())
1949 return;
1950
Ted Kremenek9d64c152010-03-12 00:38:38 +00001951 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1952 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1953 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1954 property->getLocation());
1955
1956 if (SetterMethod) {
1957 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1958 property->getPropertyAttributes();
1959 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001960 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1961 Context.VoidTy)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001962 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1963 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001964 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001965 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1966 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001967 Diag(property->getLocation(),
1968 diag::warn_accessor_property_type_mismatch)
1969 << property->getDeclName()
1970 << SetterMethod->getSelector();
1971 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1972 }
1973 }
1974
1975 // Synthesize getter/setter methods if none exist.
1976 // Find the default getter and if one not found, add one.
1977 // FIXME: The synthesized property we set here is misleading. We almost always
1978 // synthesize these methods unless the user explicitly provided prototypes
1979 // (which is odd, but allowed). Sema should be typechecking that the
1980 // declarations jive in that situation (which it is not currently).
1981 if (!GetterMethod) {
1982 // No instance method of same name as property getter name was found.
1983 // Declare a getter method and add it to the list of methods
1984 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001985 SourceLocation Loc = redeclaredProperty ?
1986 redeclaredProperty->getLocation() :
1987 property->getLocation();
1988
1989 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1990 property->getGetterName(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001991 property->getType(), nullptr, CD,
1992 /*isInstance=*/true, /*isVariadic=*/false,
1993 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001994 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995 (property->getPropertyImplementation() ==
1996 ObjCPropertyDecl::Optional) ?
1997 ObjCMethodDecl::Optional :
1998 ObjCMethodDecl::Required);
1999 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00002000
2001 AddPropertyAttrs(*this, GetterMethod, property);
2002
Ted Kremenek23173d72010-05-18 21:09:07 +00002003 // FIXME: Eventually this shouldn't be needed, as the lexical context
2004 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00002005 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002006 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002007 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Stephen Hines651f13c2014-04-23 16:59:28 -07002008 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2009 Loc));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00002010
2011 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2012 GetterMethod->addAttr(
Stephen Hines651f13c2014-04-23 16:59:28 -07002013 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
2014
2015 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002016 GetterMethod->addAttr(
2017 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2018 SA->getName(), Loc));
John McCallb8463812013-04-04 01:38:37 +00002019
2020 if (getLangOpts().ObjCAutoRefCount)
2021 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002022 } else
2023 // A user declared getter will be synthesize when @synthesize of
2024 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002025 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002026 property->setGetterMethodDecl(GetterMethod);
2027
2028 // Skip setter if property is read-only.
2029 if (!property->isReadOnly()) {
2030 // Find the default setter and if one not found, add one.
2031 if (!SetterMethod) {
2032 // No instance method of same name as property setter name was found.
2033 // Declare a setter method and add it to the list of methods
2034 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002035 SourceLocation Loc = redeclaredProperty ?
2036 redeclaredProperty->getLocation() :
2037 property->getLocation();
2038
2039 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002040 ObjCMethodDecl::Create(Context, Loc, Loc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002041 property->getSetterName(), Context.VoidTy,
2042 nullptr, CD, /*isInstance=*/true,
2043 /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002044 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002045 /*isImplicitlyDeclared=*/true,
2046 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002047 (property->getPropertyImplementation() ==
2048 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00002049 ObjCMethodDecl::Optional :
2050 ObjCMethodDecl::Required);
2051
Ted Kremenek9d64c152010-03-12 00:38:38 +00002052 // Invent the arguments for the setter. We don't bother making a
2053 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002054 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2055 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002056 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00002057 property->getType().getUnqualifiedType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002058 /*TInfo=*/nullptr,
John McCalld931b082010-08-26 03:08:43 +00002059 SC_None,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002060 nullptr);
Dmitri Gribenko55431692013-05-05 00:41:58 +00002061 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00002062
2063 AddPropertyAttrs(*this, SetterMethod, property);
2064
Ted Kremenek9d64c152010-03-12 00:38:38 +00002065 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002066 // FIXME: Eventually this shouldn't be needed, as the lexical context
2067 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002068 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002069 SetterMethod->setLexicalDeclContext(lexicalDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07002070 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002071 SetterMethod->addAttr(
2072 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2073 SA->getName(), Loc));
John McCallb8463812013-04-04 01:38:37 +00002074 // It's possible for the user to have set a very odd custom
2075 // setter selector that causes it to have a method family.
2076 if (getLangOpts().ObjCAutoRefCount)
2077 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002078 } else
2079 // A user declared setter will be synthesize when @synthesize of
2080 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002081 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002082 property->setSetterMethodDecl(SetterMethod);
2083 }
2084 // Add any synthesized methods to the global pool. This allows us to
2085 // handle the following, which is supported by GCC (and part of the design).
2086 //
2087 // @interface Foo
2088 // @property double bar;
2089 // @end
2090 //
2091 // void thisIsUnfortunate() {
2092 // id foo;
2093 // double bar = [foo bar];
2094 // }
2095 //
2096 if (GetterMethod)
2097 AddInstanceMethodToGlobalPool(GetterMethod);
2098 if (SetterMethod)
2099 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002100
2101 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2102 if (!CurrentClass) {
2103 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2104 CurrentClass = Cat->getClassInterface();
2105 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2106 CurrentClass = Impl->getClassInterface();
2107 }
2108 if (GetterMethod)
2109 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2110 if (SetterMethod)
2111 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002112}
2113
John McCalld226f652010-08-21 09:40:31 +00002114void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002115 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002116 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002117 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002118 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002119 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002120 return;
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002121
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002122 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2123 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "readonly" << "readwrite";
2126
2127 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2128 QualType PropertyTy = PropertyDecl->getType();
2129 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002130
Stephen Hines651f13c2014-04-23 16:59:28 -07002131 // 'readonly' property with no obvious lifetime.
2132 // its life time will be determined by its backing ivar.
2133 if (getLangOpts().ObjCAutoRefCount &&
2134 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2135 PropertyTy->isObjCRetainableType() &&
2136 !PropertyOwnership)
2137 return;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002138
2139 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002140 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002141 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2142 !PropertyTy->isObjCRetainableType() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002143 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002144 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002145 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2146 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2147 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002148 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002149 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002150 }
2151
2152 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002153 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2154 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002155 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2156 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002157 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002158 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002159 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002160 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2161 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002162 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002163 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002164 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2166 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002167 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002168 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002169 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002170 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002173 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002174 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002175 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002176 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002177 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2178 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002179 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2180 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002181 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002182 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002183 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002184 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2185 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002186 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002187 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002188 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002189 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2190 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002191 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002192 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002193 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002194 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002195 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2196 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002197 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002198 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002199 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2200 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002201 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2202 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002203 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002204 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002205 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002206 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2207 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002208 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002209 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002210 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002211 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2212 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002213 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002214 }
2215 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002216 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2217 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002218 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2219 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002220 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002221 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002222 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2223 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002224 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2225 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002226 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002227 }
2228
Bill Wendlingad017fa2012-12-20 19:22:21 +00002229 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2230 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002231 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2232 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002233 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002234 }
2235
Ted Kremenek9d64c152010-03-12 00:38:38 +00002236 // Warn if user supplied no assignment attribute, property is
2237 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002238 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002239 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2240 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2241 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002242 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002243 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002244 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002245 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002246 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002247 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002248 bool isAnyClassTy =
2249 (PropertyTy->isObjCClassType() ||
2250 PropertyTy->isObjCQualifiedClassType());
2251 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2252 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002253 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002254 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002255 else if (propertyInPrimaryClass) {
2256 // Don't issue warning on property with no life time in class
2257 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002258 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002259 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002260 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002261
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002262 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002263 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002264 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002265 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002266 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002267
2268 // FIXME: Implement warning dependent on NSCopying being
2269 // implemented. See also:
2270 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2271 // (please trim this list while you are at it).
2272 }
2273
Bill Wendlingad017fa2012-12-20 19:22:21 +00002274 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2275 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002276 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002277 && PropertyTy->isBlockPointerType())
2278 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002279 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2280 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2281 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002282 PropertyTy->isBlockPointerType())
2283 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002284
Bill Wendlingad017fa2012-12-20 19:22:21 +00002285 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2286 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002287 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2288
Ted Kremenek9d64c152010-03-12 00:38:38 +00002289}