blob: 39c817286ca9ed23c6ee56012d4834bae51ab278 [file] [log] [blame]
Ted Kremenek7a7a0802010-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 McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-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 McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-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 McCall31168b02011-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 McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-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 McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahanian8d1ca5a12012-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 Gregorb8982092013-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,
120 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
121 // Have we seen this protocol before?
122 if (!Known.insert(Proto))
123 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 Jahanianb809a0e2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000135 for (auto *P : Proto->protocols())
136 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000137}
138
John McCall48871652010-08-21 09:40:31 +0000139Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000140 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000141 FieldDeclarator &FD,
142 ObjCDeclSpec &ODS,
143 Selector GetterSel,
144 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000145 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000146 tok::ObjCKeywordKind MethodImplKind,
147 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000148 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000149 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
150 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000151 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000154 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000155 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000156 // property is defaulted to 'assign' if it is readwrite and is
157 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000158 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000159 (isReadWrite &&
Bill Wendling44426052012-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 Jahanianb24b5682011-03-28 23:47:18 +0000165
Douglas Gregor90d34422013-01-21 19:05:22 +0000166 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000167 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregor90d34422013-01-21 19:05:22 +0000168 ObjCPropertyDecl *Res = 0;
169 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000170 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000171 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000172 FD, GetterSel, SetterSel,
173 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000174 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000175 ODS.getPropertyAttributes(),
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000176 isOverridingProperty, TSI,
177 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000178 if (!Res)
179 return 0;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000180 }
Douglas Gregor90d34422013-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 Kremenekcba58492010-09-23 21:18:05 +0000191
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000192 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000193 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000194 (isa<ObjCInterfaceDecl>(ClassDecl) ||
195 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000196
David Blaikiebbafb8a2012-03-11 07:00:24 +0000197 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000198 checkARCPropertyDecl(*this, Res);
199
Douglas Gregorb8982092013-01-21 19:42:21 +0000200 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000202 // For a class, compare the property against a property in our superclass.
203 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000204 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
205 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000206 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-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 Jahanianb809a0e2013-10-04 18:06:08 +0000209 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000210 FoundInSuper = true;
211 break;
212 }
213 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000214 if (FoundInSuper)
215 break;
216 else
217 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000218 }
219
220 if (FoundInSuper) {
221 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000222 for (auto *P : CurrentInterfaceDecl->protocols()) {
223 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000227 for (auto *P : IFace->all_referenced_protocols()) {
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000229 }
230 }
231 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
232 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
233 PEnd = Cat->protocol_end();
234 P != PEnd; ++P) {
235 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
236 }
237 } else {
238 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000239 for (auto *P : Proto->protocols())
240 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000241 }
242
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000243 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000244 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000245}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000246
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000247static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000248makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000249 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
274
275 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
276}
277
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000278static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000279 SourceLocation LParenLoc, SourceLocation &Loc) {
280 if (LParenLoc.isMacroID())
281 return false;
282
283 SourceManager &SM = Context.getSourceManager();
284 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
285 // Try to load the file buffer.
286 bool invalidTemp = false;
287 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
288 if (invalidTemp)
289 return false;
290 const char *tokenBegin = file.data() + locInfo.second;
291
292 // Lex from the start of the given location.
293 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
294 Context.getLangOpts(),
295 file.begin(), tokenBegin, file.end());
296 Token Tok;
297 do {
298 lexer.LexFromRawLexer(Tok);
299 if (Tok.is(tok::raw_identifier) &&
300 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
301 Loc = Tok.getLocation();
302 return true;
303 }
304 } while (Tok.isNot(tok::r_paren));
305 return false;
306
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000307}
308
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000309static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000310 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
311 ObjCPropertyDecl::OBJC_PR_retain |
312 ObjCPropertyDecl::OBJC_PR_copy |
313 ObjCPropertyDecl::OBJC_PR_weak |
314 ObjCPropertyDecl::OBJC_PR_strong |
315 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
316}
317
Douglas Gregor90d34422013-01-21 19:05:22 +0000318ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000319Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000320 SourceLocation AtLoc,
321 SourceLocation LParenLoc,
322 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000323 Selector GetterSel, Selector SetterSel,
324 const bool isAssign,
325 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000326 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000327 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000328 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000329 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000330 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000331 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000332 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000333 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000334 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000335 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
336
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000337 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000338 // Check for duplicate declaration of this property in current and
339 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000340 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000341 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000342 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000343 Diag(AtLoc, diag::err_duplicate_property);
344 Diag(prevDecl->getLocation(), diag::note_property_declare);
345 return 0;
346 }
347 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000348 }
349
Ted Kremenek959e8302010-03-12 02:31:10 +0000350 // Create a new ObjCPropertyDecl with the DeclContext being
351 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000352 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000353 ObjCPropertyDecl *PDecl =
354 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000355 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000356 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000357 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000358 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000359 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000360 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000362 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
363 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
364 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
365 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000366 // Set setter/getter selector name. Needed later.
367 PDecl->setGetterName(GetterSel);
368 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000369 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000370 DC->addDecl(PDecl);
371
372 // We need to look in the @interface to see if the @property was
373 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000374 if (!CCPrimary) {
375 Diag(CDecl->getLocation(), diag::err_continuation_class);
376 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000377 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000378 }
379
380 // Find the property in continuation class's primary class only.
381 ObjCPropertyDecl *PIDecl =
382 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
383
384 if (!PIDecl) {
385 // No matching property found in the primary class. Just fall thru
386 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000387 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000388 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000389 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000390 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000391
392 // A case of continuation class adding a new property in the class. This
393 // is not what it was meant for. However, gcc supports it and so should we.
394 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000395 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000396 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000397 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
398 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000399 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000400 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
401 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000402 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000403 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
404 bool IncompatibleObjC = false;
405 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000406 // Relax the strict type matching for property type in continuation class.
407 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000408 // as it narrows the object type in its primary class property. Note that
409 // this conversion is safe only because the wider type is for a 'readonly'
410 // property in primary class and 'narrowed' type for a 'readwrite' property
411 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000412 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
413 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
414 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
415 ConvertedType, IncompatibleObjC))
416 || IncompatibleObjC) {
417 Diag(AtLoc,
418 diag::err_type_mismatch_continuation_class) << PDecl->getType();
419 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000420 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000421 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000422 }
423
Ted Kremenek959e8302010-03-12 02:31:10 +0000424 // The property 'PIDecl's readonly attribute will be over-ridden
425 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000426 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000428 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
429 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000430 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000431 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000432 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000433 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
434 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 Diag(AtLoc, diag::warn_property_attr_mismatch);
436 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000437 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000438 else if (getLangOpts().ObjCAutoRefCount) {
439 QualType PrimaryPropertyQT =
440 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
441 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000442 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000443 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
444 PrimaryPropertyQT.getObjCLifetime();
445 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000446 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
447 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000448 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
449 Diag(PIDecl->getLocation(), diag::note_property_declare);
450 }
451 }
452 }
453
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000454 DeclContext *DC = cast<DeclContext>(CCPrimary);
455 if (!ObjCPropertyDecl::findPropertyDecl(DC,
456 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000457 // In mrr mode, 'readwrite' property must have an explicit
458 // memory attribute. If none specified, select the default (assign).
459 if (!getLangOpts().ObjCAutoRefCount) {
460 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
461 ObjCDeclSpec::DQ_PR_retain |
462 ObjCDeclSpec::DQ_PR_strong |
463 ObjCDeclSpec::DQ_PR_copy |
464 ObjCDeclSpec::DQ_PR_unsafe_unretained |
465 ObjCDeclSpec::DQ_PR_weak)))
466 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
467 }
468
Ted Kremenek959e8302010-03-12 02:31:10 +0000469 // Protocol is not in the primary class. Must build one for it.
470 ObjCDeclSpec ProtocolPropertyODS;
471 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
472 // and ObjCPropertyDecl::PropertyAttributeKind have identical
473 // values. Should consolidate both into one enum type.
474 ProtocolPropertyODS.
475 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
476 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000477 // Must re-establish the context from class extension to primary
478 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000479 ContextRAII SavedContext(*this, CCPrimary);
480
John McCall48871652010-08-21 09:40:31 +0000481 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000482 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000483 PIDecl->getGetterName(),
484 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000485 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000486 MethodImplKind,
487 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000488 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000489 }
490 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000494 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000495 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000496 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
497 PIDecl->setSetterName(SetterSel);
498 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000499 // Tailor the diagnostics for the common case where a readwrite
500 // property is declared both in the @interface and the continuation.
501 // This is a common error where the user often intended the original
502 // declaration to be readonly.
503 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000504 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000505 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
506 ? diag::err_use_continuation_class_redeclaration_readwrite
507 : diag::err_use_continuation_class;
508 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000509 << CCPrimary->getDeclName();
510 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000511 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000512 }
513 *isOverridingProperty = true;
514 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000515 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000516 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
517 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000518 if (ASTMutationListener *L = Context.getASTMutationListener())
519 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000520 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000521}
522
523ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
524 ObjCContainerDecl *CDecl,
525 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000526 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000527 FieldDeclarator &FD,
528 Selector GetterSel,
529 Selector SetterSel,
530 const bool isAssign,
531 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000532 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000533 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000534 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000535 tok::ObjCKeywordKind MethodImplKind,
536 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000537 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000538 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000539
540 // Issue a warning if property is 'assign' as default and its object, which is
541 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000542 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000543 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000544 if (const ObjCObjectPointerType *ObjPtrTy =
545 T->getAs<ObjCObjectPointerType>()) {
546 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
547 if (IDecl)
548 if (ObjCProtocolDecl* PNSCopying =
549 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
550 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
551 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000552 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000553
554 if (T->isObjCObjectType()) {
555 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
556 StarLoc = PP.getLocForEndOfToken(StarLoc);
557 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
558 << FixItHint::CreateInsertion(StarLoc, "*");
559 T = Context.getObjCObjectPointerType(T);
560 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
561 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
562 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000563
Ted Kremenek959e8302010-03-12 02:31:10 +0000564 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000565 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
566 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000567 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000568
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000569 if (ObjCPropertyDecl *prevDecl =
570 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000571 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000572 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000573 PDecl->setInvalidDecl();
574 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000575 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000576 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000577 if (lexicalDC)
578 PDecl->setLexicalDeclContext(lexicalDC);
579 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000580
581 if (T->isArrayType() || T->isFunctionType()) {
582 Diag(AtLoc, diag::err_property_type) << T;
583 PDecl->setInvalidDecl();
584 }
585
586 ProcessDeclAttributes(S, PDecl, FD.D);
587
588 // Regardless of setter/getter attribute, we save the default getter/setter
589 // selector names in anticipation of declaration of setter/getter methods.
590 PDecl->setGetterName(GetterSel);
591 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000592 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000593 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000594
Bill Wendling44426052012-12-20 19:22:21 +0000595 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
597
Bill Wendling44426052012-12-20 19:22:21 +0000598 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
600
Bill Wendling44426052012-12-20 19:22:21 +0000601 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
603
604 if (isReadWrite)
605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
606
Bill Wendling44426052012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
609
Bill Wendling44426052012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
612
Bill Wendling44426052012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
618
Bill Wendling44426052012-12-20 19:22:21 +0000619 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
621
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 if (isAssign)
623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
624
John McCall43192862011-09-13 18:31:23 +0000625 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000626 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000628 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000630
John McCall31168b02011-06-15 23:02:42 +0000631 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000632 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
634 if (isAssign)
635 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
636
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 if (MethodImplKind == tok::objc_required)
638 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
639 else if (MethodImplKind == tok::objc_optional)
640 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000641
Ted Kremenek959e8302010-03-12 02:31:10 +0000642 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000643}
644
John McCall31168b02011-06-15 23:02:42 +0000645static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
646 ObjCPropertyDecl *property,
647 ObjCIvarDecl *ivar) {
648 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
649
John McCall31168b02011-06-15 23:02:42 +0000650 QualType ivarType = ivar->getType();
651 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000652
John McCall43192862011-09-13 18:31:23 +0000653 // The lifetime implied by the property's attributes.
654 Qualifiers::ObjCLifetime propertyLifetime =
655 getImpliedARCOwnership(property->getPropertyAttributes(),
656 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000657
John McCall43192862011-09-13 18:31:23 +0000658 // We're fine if they match.
659 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000660
John McCall43192862011-09-13 18:31:23 +0000661 // These aren't valid lifetimes for object ivars; don't diagnose twice.
662 if (ivarLifetime == Qualifiers::OCL_None ||
663 ivarLifetime == Qualifiers::OCL_Autoreleasing)
664 return;
John McCall31168b02011-06-15 23:02:42 +0000665
John McCalld8561f02012-08-20 23:36:59 +0000666 // If the ivar is private, and it's implicitly __unsafe_unretained
667 // becaues of its type, then pretend it was actually implicitly
668 // __strong. This is only sound because we're processing the
669 // property implementation before parsing any method bodies.
670 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
671 propertyLifetime == Qualifiers::OCL_Strong &&
672 ivar->getAccessControl() == ObjCIvarDecl::Private) {
673 SplitQualType split = ivarType.split();
674 if (split.Quals.hasObjCLifetime()) {
675 assert(ivarType->isObjCARCImplicitlyUnretainedType());
676 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
677 ivarType = S.Context.getQualifiedType(split);
678 ivar->setType(ivarType);
679 return;
680 }
681 }
682
John McCall43192862011-09-13 18:31:23 +0000683 switch (propertyLifetime) {
684 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000685 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000686 << property->getDeclName()
687 << ivar->getDeclName()
688 << ivarLifetime;
689 break;
John McCall31168b02011-06-15 23:02:42 +0000690
John McCall43192862011-09-13 18:31:23 +0000691 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000692 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000693 << property->getDeclName()
694 << ivar->getDeclName();
695 break;
John McCall31168b02011-06-15 23:02:42 +0000696
John McCall43192862011-09-13 18:31:23 +0000697 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000698 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000699 << property->getDeclName()
700 << ivar->getDeclName()
701 << ((property->getPropertyAttributesAsWritten()
702 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
703 break;
John McCall31168b02011-06-15 23:02:42 +0000704
John McCall43192862011-09-13 18:31:23 +0000705 case Qualifiers::OCL_Autoreleasing:
706 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000707
John McCall43192862011-09-13 18:31:23 +0000708 case Qualifiers::OCL_None:
709 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000710 return;
711 }
712
713 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000714 if (propertyImplLoc.isValid())
715 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000716}
717
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000718/// setImpliedPropertyAttributeForReadOnlyProperty -
719/// This routine evaludates life-time attributes for a 'readonly'
720/// property with no known lifetime of its own, using backing
721/// 'ivar's attribute, if any. If no backing 'ivar', property's
722/// life-time is assumed 'strong'.
723static void setImpliedPropertyAttributeForReadOnlyProperty(
724 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
725 Qualifiers::ObjCLifetime propertyLifetime =
726 getImpliedARCOwnership(property->getPropertyAttributes(),
727 property->getType());
728 if (propertyLifetime != Qualifiers::OCL_None)
729 return;
730
731 if (!ivar) {
732 // if no backing ivar, make property 'strong'.
733 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
734 return;
735 }
736 // property assumes owenership of backing ivar.
737 QualType ivarType = ivar->getType();
738 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
739 if (ivarLifetime == Qualifiers::OCL_Strong)
740 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
741 else if (ivarLifetime == Qualifiers::OCL_Weak)
742 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
743 return;
744}
Ted Kremenekac597f32010-03-12 00:46:40 +0000745
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000746/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
747/// in inherited protocols with mismatched types. Since any of them can
748/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000749static void
750DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
751 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000752 ObjCPropertyDecl *Property) {
753 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000754 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
755 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000756 PDecl->collectInheritedProtocolProperties(Property, PropMap);
757 }
758 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
759 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000760 for (const auto *PI : SDecl->all_referenced_protocols()) {
761 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000762 PDecl->collectInheritedProtocolProperties(Property, PropMap);
763 }
764 SDecl = SDecl->getSuperClass();
765 }
766
767 if (PropMap.empty())
768 return;
769
770 QualType RHSType = S.Context.getCanonicalType(Property->getType());
771 bool FirsTime = true;
772 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
773 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
774 ObjCPropertyDecl *Prop = I->second;
775 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
776 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
777 bool IncompatibleObjC = false;
778 QualType ConvertedType;
779 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
780 || IncompatibleObjC) {
781 if (FirsTime) {
782 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
783 << Property->getType();
784 FirsTime = false;
785 }
786 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
787 << Prop->getType();
788 }
789 }
790 }
791 if (!FirsTime && AtLoc.isValid())
792 S.Diag(AtLoc, diag::note_property_synthesize);
793}
794
Ted Kremenekac597f32010-03-12 00:46:40 +0000795/// ActOnPropertyImplDecl - This routine performs semantic checks and
796/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000797/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000798///
John McCall48871652010-08-21 09:40:31 +0000799Decl *Sema::ActOnPropertyImplDecl(Scope *S,
800 SourceLocation AtLoc,
801 SourceLocation PropertyLoc,
802 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000803 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000804 IdentifierInfo *PropertyIvar,
805 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000806 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000807 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000808 // Make sure we have a context for the property implementation declaration.
809 if (!ClassImpDecl) {
810 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000811 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000812 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000813 if (PropertyIvarLoc.isInvalid())
814 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000815 SourceLocation PropertyDiagLoc = PropertyLoc;
816 if (PropertyDiagLoc.isInvalid())
817 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000818 ObjCPropertyDecl *property = 0;
819 ObjCInterfaceDecl* IDecl = 0;
820 // Find the class or category class where this property must have
821 // a declaration.
822 ObjCImplementationDecl *IC = 0;
823 ObjCCategoryImplDecl* CatImplClass = 0;
824 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
825 IDecl = IC->getClassInterface();
826 // We always synthesize an interface for an implementation
827 // without an interface decl. So, IDecl is always non-zero.
828 assert(IDecl &&
829 "ActOnPropertyImplDecl - @implementation without @interface");
830
831 // Look for this property declaration in the @implementation's @interface
832 property = IDecl->FindPropertyDeclaration(PropertyId);
833 if (!property) {
834 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000835 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000836 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000837 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000838 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
839 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000840 if (AtLoc.isValid())
841 Diag(AtLoc, diag::warn_implicit_atomic_property);
842 else
843 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
844 Diag(property->getLocation(), diag::note_property_declare);
845 }
846
Ted Kremenekac597f32010-03-12 00:46:40 +0000847 if (const ObjCCategoryDecl *CD =
848 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
849 if (!CD->IsClassExtension()) {
850 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
851 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000852 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000853 }
854 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000855 if (Synthesize&&
856 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
857 property->hasAttr<IBOutletAttr>() &&
858 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000859 bool ReadWriteProperty = false;
860 // Search into the class extensions and see if 'readonly property is
861 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000862 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000863 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
864 if (!R.empty())
865 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
866 PIkind = ExtProp->getPropertyAttributesAsWritten();
867 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
868 ReadWriteProperty = true;
869 break;
870 }
871 }
872 }
873
874 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000875 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000876 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000877 SourceLocation readonlyLoc;
878 if (LocPropertyAttribute(Context, "readonly",
879 property->getLParenLoc(), readonlyLoc)) {
880 SourceLocation endLoc =
881 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
882 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
883 Diag(property->getLocation(),
884 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
885 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
886 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000887 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000888 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000889 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
890 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000891
Ted Kremenekac597f32010-03-12 00:46:40 +0000892 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
893 if (Synthesize) {
894 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000895 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000896 }
897 IDecl = CatImplClass->getClassInterface();
898 if (!IDecl) {
899 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000900 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000901 }
902 ObjCCategoryDecl *Category =
903 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
904
905 // If category for this implementation not found, it is an error which
906 // has already been reported eralier.
907 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000908 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000909 // Look for this property declaration in @implementation's category
910 property = Category->FindPropertyDeclaration(PropertyId);
911 if (!property) {
912 Diag(PropertyLoc, diag::error_bad_category_property_decl)
913 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000914 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000915 }
916 } else {
917 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000918 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000919 }
920 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000921 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000922 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000923 // Check that we have a valid, previously declared ivar for @synthesize
924 if (Synthesize) {
925 // @synthesize
926 if (!PropertyIvar)
927 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000928 // Check that this is a previously declared 'ivar' in 'IDecl' interface
929 ObjCInterfaceDecl *ClassDeclared;
930 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
931 QualType PropType = property->getType();
932 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000933
934 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000935 diag::err_incomplete_synthesized_property,
936 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000937 Diag(property->getLocation(), diag::note_property_declare);
938 CompleteTypeErr = true;
939 }
940
David Blaikiebbafb8a2012-03-11 07:00:24 +0000941 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000942 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000943 ObjCPropertyDecl::OBJC_PR_readonly) &&
944 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000945 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
946 }
947
John McCall31168b02011-06-15 23:02:42 +0000948 ObjCPropertyDecl::PropertyAttributeKind kind
949 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000950
951 // Add GC __weak to the ivar type if the property is weak.
952 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000953 getLangOpts().getGC() != LangOptions::NonGC) {
954 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000955 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000956 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000957 Diag(property->getLocation(), diag::note_property_declare);
958 } else {
959 PropertyIvarType =
960 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000961 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000962 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000963 if (AtLoc.isInvalid()) {
964 // Check when default synthesizing a property that there is
965 // an ivar matching property name and issue warning; since this
966 // is the most common case of not using an ivar used for backing
967 // property in non-default synthesis case.
968 ObjCInterfaceDecl *ClassDeclared=0;
969 ObjCIvarDecl *originalIvar =
970 IDecl->lookupInstanceVariable(property->getIdentifier(),
971 ClassDeclared);
972 if (originalIvar) {
973 Diag(PropertyDiagLoc,
974 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000975 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000976 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000977 Diag(property->getLocation(), diag::note_property_declare);
978 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000979 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000980 }
981
982 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000983 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000984 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000985 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000986 !PropertyIvarType.getObjCLifetime() &&
987 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000988
John McCall43192862011-09-13 18:31:23 +0000989 // It's an error if we have to do this and the user didn't
990 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000991 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000992 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000993 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000994 diag::err_arc_objc_property_default_assign_on_object);
995 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +0000996 } else {
997 Qualifiers::ObjCLifetime lifetime =
998 getImpliedARCOwnership(kind, PropertyIvarType);
999 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001000 if (lifetime == Qualifiers::OCL_Weak) {
1001 bool err = false;
1002 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001003 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1004 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1005 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001006 Diag(property->getLocation(),
1007 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1008 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1009 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001010 err = true;
1011 }
Richard Smith802c4b72012-08-23 06:16:52 +00001012 }
John McCall3deb1ad2012-08-21 02:47:43 +00001013 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001014 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001015 Diag(property->getLocation(), diag::note_property_declare);
1016 }
John McCall31168b02011-06-15 23:02:42 +00001017 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001018
John McCall31168b02011-06-15 23:02:42 +00001019 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001020 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001021 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1022 }
John McCall31168b02011-06-15 23:02:42 +00001023 }
1024
1025 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001026 !getLangOpts().ObjCAutoRefCount &&
1027 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001028 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001029 Diag(property->getLocation(), diag::note_property_declare);
1030 }
1031
Abramo Bagnaradff19302011-03-08 08:55:46 +00001032 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001033 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001034 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001035 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001036 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001037 if (RequireNonAbstractType(PropertyIvarLoc,
1038 PropertyIvarType,
1039 diag::err_abstract_type_in_decl,
1040 AbstractSynthesizedIvarType)) {
1041 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001042 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001043 } else if (CompleteTypeErr)
1044 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001045 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001046 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001047
John McCall5fb5df92012-06-20 06:18:46 +00001048 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001049 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1050 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001051 // Note! I deliberately want it to fall thru so, we have a
1052 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001053 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001054 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001055 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001056 << property->getDeclName() << Ivar->getDeclName()
1057 << ClassDeclared->getDeclName();
1058 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001059 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001060 // Note! I deliberately want it to fall thru so more errors are caught.
1061 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001062 property->setPropertyIvarDecl(Ivar);
1063
Ted Kremenekac597f32010-03-12 00:46:40 +00001064 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1065
1066 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001067 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001068 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001069 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001070 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001071 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001072 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001073 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001074 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001075 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1076 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001077 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001078 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001079 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001080 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001081 << property->getDeclName() << PropType
1082 << Ivar->getDeclName() << IvarType;
1083 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001084 // Note! I deliberately want it to fall thru so, we have a
1085 // a property implementation and to avoid future warnings.
1086 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001087 else {
1088 // FIXME! Rules for properties are somewhat different that those
1089 // for assignments. Use a new routine to consolidate all cases;
1090 // specifically for property redeclarations as well as for ivars.
1091 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1092 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1093 if (lhsType != rhsType &&
1094 lhsType->isArithmeticType()) {
1095 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1096 << property->getDeclName() << PropType
1097 << Ivar->getDeclName() << IvarType;
1098 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1099 // Fall thru - see previous comment
1100 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001101 }
1102 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001103 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001104 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001105 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001106 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001107 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001108 // Fall thru - see previous comment
1109 }
John McCall31168b02011-06-15 23:02:42 +00001110 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 if ((property->getType()->isObjCObjectPointerType() ||
1112 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001113 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001114 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001115 << property->getDeclName() << Ivar->getDeclName();
1116 // Fall thru - see previous comment
1117 }
1118 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001119 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001120 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 } else if (PropertyIvar)
1122 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001123 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001124
Ted Kremenekac597f32010-03-12 00:46:40 +00001125 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1126 ObjCPropertyImplDecl *PIDecl =
1127 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1128 property,
1129 (Synthesize ?
1130 ObjCPropertyImplDecl::Synthesize
1131 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001132 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001133
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001134 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001135 PIDecl->setInvalidDecl();
1136
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001137 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1138 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001139 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001140 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001141 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1142 // returned by the getter as it must conform to C++'s copy-return rules.
1143 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001144 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001145 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1146 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001147 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001148 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001149 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001150 Expr *LoadSelfExpr =
1151 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1152 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001153 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001154 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001155 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001156 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001157 ExprResult Res = PerformCopyInitialization(
1158 InitializedEntity::InitializeResult(PropertyDiagLoc,
1159 getterMethod->getReturnType(),
1160 /*NRVO=*/false),
1161 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001162 if (!Res.isInvalid()) {
1163 Expr *ResExpr = Res.takeAs<Expr>();
1164 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001165 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001166 PIDecl->setGetterCXXConstructor(ResExpr);
1167 }
1168 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001169 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1170 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1171 Diag(getterMethod->getLocation(),
1172 diag::warn_property_getter_owning_mismatch);
1173 Diag(property->getLocation(), diag::note_property_declare);
1174 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001175 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1176 switch (getterMethod->getMethodFamily()) {
1177 case OMF_retain:
1178 case OMF_retainCount:
1179 case OMF_release:
1180 case OMF_autorelease:
1181 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1182 << 1 << getterMethod->getSelector();
1183 break;
1184 default:
1185 break;
1186 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001187 }
1188 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1189 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001190 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1191 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001192 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001193 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001194 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1195 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001196 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001197 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001198 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001199 Expr *LoadSelfExpr =
1200 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1201 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001202 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001203 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001204 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001205 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001206 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1207 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001208 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-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 McCalle3027922010-08-25 11:45:40 +00001213 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001214 if (property->getPropertyAttributes() &
1215 ObjCPropertyDecl::OBJC_PR_atomic) {
1216 Expr *callExpr = Res.takeAs<Expr>();
1217 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001218 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1219 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001220 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001221 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001222 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001223 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001224 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001225 Diag(FuncDecl->getLocStart(),
1226 diag::note_callee_decl) << FuncDecl;
1227 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001228 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001229 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1230 }
1231 }
1232
Ted Kremenekac597f32010-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);
John McCall48871652010-08-21 09:40:31 +00001247 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001248 }
1249 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001250 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001251 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001252 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-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 Jahanian76b35372010-08-24 18:48:05 +00001255 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001256 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001257 ObjCIvarDecl *Ivar = 0;
1258 if (!Synthesize)
1259 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1260 else {
1261 if (PropertyIvar && PropertyIvar != PropertyId)
1262 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1263 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001264 // Issue diagnostics only if Ivar belongs to current class.
1265 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001266 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001267 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1268 << PropertyId;
1269 Ivar->setInvalidDecl();
1270 }
1271 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001272 } else {
1273 if (Synthesize)
1274 if (ObjCPropertyImplDecl *PPIDecl =
1275 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001276 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-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 Friedman169ec352012-05-01 22:26:06 +00001284 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001285 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001286 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001287 }
1288 CatImplClass->addPropertyImplementation(PIDecl);
1289 }
1290
John McCall48871652010-08-21 09:40:31 +00001291 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001292}
1293
1294//===----------------------------------------------------------------------===//
1295// Helper methods.
1296//===----------------------------------------------------------------------===//
1297
Ted Kremenek7a7a0802010-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 Jahanianb809a0e2013-10-04 18:06:08 +00001304 const IdentifierInfo *inheritedName,
1305 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001306 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001307 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001308 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-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 McCall31168b02011-06-15 23:02:42 +00001324 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-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 McCall31168b02011-06-15 23:02:42 +00001339 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001340
1341 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001342 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001343 Diag(Property->getLocation(), diag::warn_property_attribute)
1344 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001345 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1346 }
1347 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001348 Diag(Property->getLocation(), diag::warn_property_attribute)
1349 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001350 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1351 }
1352 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001353 Diag(Property->getLocation(), diag::warn_property_attribute)
1354 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001355 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1356 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001357
1358 QualType LHSType =
1359 Context.getCanonicalType(SuperProperty->getType());
1360 QualType RHSType =
1361 Context.getCanonicalType(Property->getType());
1362
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001363 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-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 Jahanianfa643c82011-10-12 00:00:57 +00001370 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001371 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1372 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001373 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1374 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001375 }
1376}
1377
1378bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1379 ObjCMethodDecl *GetterMethod,
1380 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001381 if (!GetterMethod)
1382 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001383 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-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 Jahanianb5dd2cb2012-05-29 19:56:01 +00001391 GetterType->getAs<ObjCObjectPointerType>(),
1392 PropertyIvarType->getAs<ObjCObjectPointerType>());
1393 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-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 Kremenek7a7a0802010-03-12 00:38:38 +00001406 }
1407 }
Fariborz Jahanian0ebc0fa2012-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 Kremenek7a7a0802010-03-12 00:38:38 +00001417 return false;
1418}
1419
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001420/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001421/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001422static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1423 ObjCContainerDecl::PropertyMap &PropMap,
1424 ObjCContainerDecl::PropertyMap &SuperPropMap,
1425 bool IncludeProtocols = true) {
1426
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001427 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001428 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001429 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001430 if (IncludeProtocols) {
1431 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001432 for (auto *PI : IDecl->all_referenced_protocols())
1433 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001434 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001435 }
1436 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1437 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001438 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001439 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001440 if (IncludeProtocols) {
1441 // Scan through class's protocols.
1442 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1443 E = CATDecl->protocol_end(); PI != E; ++PI)
1444 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1445 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001446 }
1447 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001448 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001449 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1450 // Exclude property for protocols which conform to class's super-class,
1451 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001452 if (!PropertyFromSuper ||
1453 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001454 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1455 if (!PropEntry)
1456 PropEntry = Prop;
1457 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001458 }
1459 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001460 for (auto *PI : PDecl->protocols())
1461 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001462 }
1463}
1464
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001465/// CollectSuperClassPropertyImplementations - This routine collects list of
1466/// properties to be implemented in super class(s) and also coming from their
1467/// conforming protocols.
1468static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001469 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001470 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001471 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001472 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001473 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001474 SDecl = SDecl->getSuperClass();
1475 }
1476 }
1477}
1478
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001479/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1480/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1481/// declared in class 'IFace'.
1482bool
1483Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1484 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1485 if (!IV->getSynthesize())
1486 return false;
1487 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1488 Method->isInstanceMethod());
1489 if (!IMD || !IMD->isPropertyAccessor())
1490 return false;
1491
1492 // look up a property declaration whose one of its accessors is implemented
1493 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001494 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001495 if ((Property->getGetterName() == IMD->getSelector() ||
1496 Property->getSetterName() == IMD->getSelector()) &&
1497 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001498 return true;
1499 }
1500 return false;
1501}
1502
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001503static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1504 ObjCPropertyDecl *Prop) {
1505 bool SuperClassImplementsGetter = false;
1506 bool SuperClassImplementsSetter = false;
1507 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1508 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001509
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001510 while (IDecl->getSuperClass()) {
1511 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1512 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1513 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001514
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001515 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1516 SuperClassImplementsSetter = true;
1517 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1518 return true;
1519 IDecl = IDecl->getSuperClass();
1520 }
1521 return false;
1522}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001523
James Dennett2a4d13c2012-06-15 07:13:21 +00001524/// \brief Default synthesizes all properties which must be synthesized
1525/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001526void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1527 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001528
Anna Zaks673d76b2012-10-18 19:17:53 +00001529 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001530 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1531 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001532 if (PropMap.empty())
1533 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001534 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001535 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1536
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001537 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1538 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001539 // Is there a matching property synthesize/dynamic?
1540 if (Prop->isInvalidDecl() ||
1541 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1542 continue;
1543 // Property may have been synthesized by user.
1544 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1545 continue;
1546 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1547 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1548 continue;
1549 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1550 continue;
1551 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001552 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001553 if (SuperPropMap[Prop->getIdentifier()]) {
1554 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1555 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1556 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001557 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001558 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1559 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001560 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001561 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001562 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1563 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001564 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001565 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001566 if (ObjCPropertyImplDecl *PID =
1567 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1568 if (PID->getPropertyDecl() != Prop) {
1569 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001570 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001571 if (!PID->getLocation().isInvalid())
1572 Diag(PID->getLocation(), diag::note_property_synthesize);
1573 }
1574 continue;
1575 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001576 if (ObjCProtocolDecl *Proto =
1577 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001578 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001579 // Suppress the warning if class's superclass implements property's
1580 // getter and implements property's setter (if readwrite property).
1581 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1582 Diag(IMPDecl->getLocation(),
1583 diag::warn_auto_synthesizing_protocol_property)
1584 << Prop << Proto;
1585 Diag(Prop->getLocation(), diag::note_property_declare);
1586 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001587 continue;
1588 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001589
1590 // We use invalid SourceLocations for the synthesized ivars since they
1591 // aren't really synthesized at a particular location; they just exist.
1592 // Saying that they are located at the @implementation isn't really going
1593 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001594 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1595 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1596 true,
1597 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001598 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001599 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001600 if (PIDecl) {
1601 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001602 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001603 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001604 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001605}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001606
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001607void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001608 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001609 return;
1610 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1611 if (!IC)
1612 return;
1613 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001614 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001615 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001616}
1617
Ted Kremenek7e812952014-02-21 19:41:30 +00001618static void DiagnoseUnimplementedAccessor(Sema &S,
1619 ObjCInterfaceDecl *PrimaryClass,
1620 Selector Method,
1621 ObjCImplDecl* IMPDecl,
1622 ObjCContainerDecl *CDecl,
1623 ObjCCategoryDecl *C,
1624 ObjCPropertyDecl *Prop,
1625 Sema::SelectorSet &SMap) {
1626 // When reporting on missing property setter/getter implementation in
1627 // categories, do not report when they are declared in primary class,
1628 // class's protocol, or one of it super classes. This is because,
1629 // the class is going to implement them.
1630 if (!SMap.count(Method) &&
1631 (PrimaryClass == 0 ||
1632 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1633 S.Diag(IMPDecl->getLocation(),
1634 isa<ObjCCategoryDecl>(CDecl) ?
1635 diag::warn_setter_getter_impl_required_in_category :
1636 diag::warn_setter_getter_impl_required)
1637 << Prop->getDeclName() << Method;
1638 S.Diag(Prop->getLocation(),
1639 diag::note_property_declare);
1640 if (S.LangOpts.ObjCDefaultSynthProperties &&
1641 S.LangOpts.ObjCRuntime.isNonFragile())
1642 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1643 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1644 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1645 }
1646}
1647
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001648void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001649 ObjCContainerDecl *CDecl,
1650 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001651 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001652 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1653
Ted Kremenek348e88c2014-02-21 19:41:34 +00001654 if (!SynthesizeProperties) {
1655 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001656 // Gather properties which need not be implemented in this class
1657 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001658 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001659 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1660 // For categories, no need to implement properties declared in
1661 // its primary class (and its super classes) if property is
1662 // declared in one of those containers.
1663 if ((IDecl = C->getClassInterface())) {
1664 ObjCInterfaceDecl::PropertyDeclOrder PO;
1665 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1666 }
1667 }
1668 if (IDecl)
1669 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1670
1671 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1672 }
1673
Ted Kremenek38882022014-02-21 19:41:39 +00001674 // Scan the @interface to see if any of the protocols it adopts
1675 // require an explicit implementation, via attribute
1676 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001677 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001678 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001679
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001680 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001681 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1682 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001683 // Lazily construct a set of all the properties in the @interface
1684 // of the class, without looking at the superclass. We cannot
1685 // use the call to CollectImmediateProperties() above as that
1686 // utilizes information fromt he super class's properties as well
1687 // as scans the adopted protocols. This work only triggers for protocols
1688 // with the attribute, which is very rare, and only occurs when
1689 // analyzing the @implementation.
1690 if (!LazyMap) {
1691 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1692 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1693 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1694 /* IncludeProtocols */ false);
1695 }
Ted Kremenek38882022014-02-21 19:41:39 +00001696 // Add the properties of 'PDecl' to the list of properties that
1697 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001698 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001699 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001700 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001701 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001702 }
1703 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001704 }
Ted Kremenek38882022014-02-21 19:41:39 +00001705
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001706 if (PropMap.empty())
1707 return;
1708
1709 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1710 for (ObjCImplDecl::propimpl_iterator
1711 I = IMPDecl->propimpl_begin(),
1712 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001713 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001714
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001715 SelectorSet InsMap;
1716 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001717 for (const auto *I : IMPDecl->instance_methods())
1718 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001719
1720 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1721 ObjCInterfaceDecl *PrimaryClass = 0;
1722 if (C && !C->IsClassExtension())
1723 if ((PrimaryClass = C->getClassInterface()))
1724 // Report unimplemented properties in the category as well.
1725 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1726 // When reporting on missing setter/getters, do not report when
1727 // setter/getter is implemented in category's primary class
1728 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001729 for (const auto *I : IMP->instance_methods())
1730 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001731 }
1732
Anna Zaks673d76b2012-10-18 19:17:53 +00001733 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001734 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1735 ObjCPropertyDecl *Prop = P->second;
1736 // Is there a matching propery synthesize/dynamic?
1737 if (Prop->isInvalidDecl() ||
1738 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001739 PropImplMap.count(Prop) ||
1740 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001741 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001742
1743 // Diagnose unimplemented getters and setters.
1744 DiagnoseUnimplementedAccessor(*this,
1745 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1746 if (!Prop->isReadOnly())
1747 DiagnoseUnimplementedAccessor(*this,
1748 PrimaryClass, Prop->getSetterName(),
1749 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001750 }
1751}
1752
1753void
1754Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1755 ObjCContainerDecl* IDecl) {
1756 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001757 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001758 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001759 for (const auto *Property : IDecl->properties()) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001760 ObjCMethodDecl *GetterMethod = 0;
1761 ObjCMethodDecl *SetterMethod = 0;
1762 bool LookedUpGetterSetter = false;
1763
Bill Wendling44426052012-12-20 19:22:21 +00001764 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001765 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001766
John McCall43192862011-09-13 18:31:23 +00001767 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1768 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001769 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1770 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1771 LookedUpGetterSetter = true;
1772 if (GetterMethod) {
1773 Diag(GetterMethod->getLocation(),
1774 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001775 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001776 Diag(Property->getLocation(), diag::note_property_declare);
1777 }
1778 if (SetterMethod) {
1779 Diag(SetterMethod->getLocation(),
1780 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001781 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001782 Diag(Property->getLocation(), diag::note_property_declare);
1783 }
1784 }
1785
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001786 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001787 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1788 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001789 continue;
1790 if (const ObjCPropertyImplDecl *PIDecl
1791 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1792 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1793 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001794 if (!LookedUpGetterSetter) {
1795 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1796 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001797 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001798 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1799 SourceLocation MethodLoc =
1800 (GetterMethod ? GetterMethod->getLocation()
1801 : SetterMethod->getLocation());
1802 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001803 << Property->getIdentifier() << (GetterMethod != 0)
1804 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001805 // fixit stuff.
1806 if (!AttributesAsWritten) {
1807 if (Property->getLParenLoc().isValid()) {
1808 // @property () ... case.
1809 SourceRange PropSourceRange(Property->getAtLoc(),
1810 Property->getLParenLoc());
1811 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1812 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1813 }
1814 else {
1815 //@property id etc.
1816 SourceLocation endLoc =
1817 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1818 endLoc = endLoc.getLocWithOffset(-1);
1819 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1820 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1821 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1822 }
1823 }
1824 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1825 // @property () ... case.
1826 SourceLocation endLoc = Property->getLParenLoc();
1827 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1828 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1829 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1830 }
1831 else
1832 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001833 Diag(Property->getLocation(), diag::note_property_declare);
1834 }
1835 }
1836 }
1837}
1838
John McCall31168b02011-06-15 23:02:42 +00001839void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001840 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001841 return;
1842
1843 for (ObjCImplementationDecl::propimpl_iterator
1844 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001845 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001846 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001847 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1848 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001849 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1850 if (!method)
1851 continue;
1852 ObjCMethodFamily family = method->getMethodFamily();
1853 if (family == OMF_alloc || family == OMF_copy ||
1854 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001855 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001856 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001857 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001858 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001859 }
1860 }
1861 }
1862}
1863
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001864void Sema::DiagnoseMissingDesignatedInitOverrides(
1865 const ObjCImplementationDecl *ImplD,
1866 const ObjCInterfaceDecl *IFD) {
1867 assert(IFD->hasDesignatedInitializers());
1868 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1869 if (!SuperD)
1870 return;
1871
1872 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001873 for (const auto *I : ImplD->instance_methods())
1874 if (I->getMethodFamily() == OMF_init)
1875 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001876
1877 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1878 SuperD->getDesignatedInitializers(DesignatedInits);
1879 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1880 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1881 const ObjCMethodDecl *MD = *I;
1882 if (!InitSelSet.count(MD->getSelector())) {
1883 Diag(ImplD->getLocation(),
1884 diag::warn_objc_implementation_missing_designated_init_override)
1885 << MD->getSelector();
1886 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1887 }
1888 }
1889}
1890
John McCallad31b5f2010-11-10 07:01:40 +00001891/// AddPropertyAttrs - Propagates attributes from a property to the
1892/// implicitly-declared getter or setter for that property.
1893static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1894 ObjCPropertyDecl *Property) {
1895 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001896 for (const auto *A : Property->attrs()) {
1897 if (isa<DeprecatedAttr>(A) ||
1898 isa<UnavailableAttr>(A) ||
1899 isa<AvailabilityAttr>(A))
1900 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001901 }
John McCallad31b5f2010-11-10 07:01:40 +00001902}
1903
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1905/// have the property type and issue diagnostics if they don't.
1906/// Also synthesize a getter/setter method if none exist (and update the
1907/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1908/// methods is the "right" thing to do.
1909void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001910 ObjCContainerDecl *CD,
1911 ObjCPropertyDecl *redeclaredProperty,
1912 ObjCContainerDecl *lexicalDC) {
1913
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001914 ObjCMethodDecl *GetterMethod, *SetterMethod;
1915
1916 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1917 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1918 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1919 property->getLocation());
1920
1921 if (SetterMethod) {
1922 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1923 property->getPropertyAttributes();
1924 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001925 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1926 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001927 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1928 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001929 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001930 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1931 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001932 Diag(property->getLocation(),
1933 diag::warn_accessor_property_type_mismatch)
1934 << property->getDeclName()
1935 << SetterMethod->getSelector();
1936 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1937 }
1938 }
1939
1940 // Synthesize getter/setter methods if none exist.
1941 // Find the default getter and if one not found, add one.
1942 // FIXME: The synthesized property we set here is misleading. We almost always
1943 // synthesize these methods unless the user explicitly provided prototypes
1944 // (which is odd, but allowed). Sema should be typechecking that the
1945 // declarations jive in that situation (which it is not currently).
1946 if (!GetterMethod) {
1947 // No instance method of same name as property getter name was found.
1948 // Declare a getter method and add it to the list of methods
1949 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001950 SourceLocation Loc = redeclaredProperty ?
1951 redeclaredProperty->getLocation() :
1952 property->getLocation();
1953
1954 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1955 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001956 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001957 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001958 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001959 (property->getPropertyImplementation() ==
1960 ObjCPropertyDecl::Optional) ?
1961 ObjCMethodDecl::Optional :
1962 ObjCMethodDecl::Required);
1963 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001964
1965 AddPropertyAttrs(*this, GetterMethod, property);
1966
Ted Kremenek49be9e02010-05-18 21:09:07 +00001967 // FIXME: Eventually this shouldn't be needed, as the lexical context
1968 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001969 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001970 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001971 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001972 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1973 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001974
1975 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1976 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001977 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001978
1979 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001980 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1981 Loc));
John McCalle48f3892013-04-04 01:38:37 +00001982
1983 if (getLangOpts().ObjCAutoRefCount)
1984 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001985 } else
1986 // A user declared getter will be synthesize when @synthesize of
1987 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001988 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001989 property->setGetterMethodDecl(GetterMethod);
1990
1991 // Skip setter if property is read-only.
1992 if (!property->isReadOnly()) {
1993 // Find the default setter and if one not found, add one.
1994 if (!SetterMethod) {
1995 // No instance method of same name as property setter name was found.
1996 // Declare a setter method and add it to the list of methods
1997 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001998 SourceLocation Loc = redeclaredProperty ?
1999 redeclaredProperty->getLocation() :
2000 property->getLocation();
2001
2002 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002003 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002004 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002005 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002006 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002007 /*isImplicitlyDeclared=*/true,
2008 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002009 (property->getPropertyImplementation() ==
2010 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002011 ObjCMethodDecl::Optional :
2012 ObjCMethodDecl::Required);
2013
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002014 // Invent the arguments for the setter. We don't bother making a
2015 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002016 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2017 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002018 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002019 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002020 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00002021 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002022 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002023 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002024
2025 AddPropertyAttrs(*this, SetterMethod, property);
2026
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002027 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002028 // FIXME: Eventually this shouldn't be needed, as the lexical context
2029 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002030 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002031 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002032 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002033 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2034 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002035 // It's possible for the user to have set a very odd custom
2036 // setter selector that causes it to have a method family.
2037 if (getLangOpts().ObjCAutoRefCount)
2038 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002039 } else
2040 // A user declared setter will be synthesize when @synthesize of
2041 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002042 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002043 property->setSetterMethodDecl(SetterMethod);
2044 }
2045 // Add any synthesized methods to the global pool. This allows us to
2046 // handle the following, which is supported by GCC (and part of the design).
2047 //
2048 // @interface Foo
2049 // @property double bar;
2050 // @end
2051 //
2052 // void thisIsUnfortunate() {
2053 // id foo;
2054 // double bar = [foo bar];
2055 // }
2056 //
2057 if (GetterMethod)
2058 AddInstanceMethodToGlobalPool(GetterMethod);
2059 if (SetterMethod)
2060 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002061
2062 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2063 if (!CurrentClass) {
2064 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2065 CurrentClass = Cat->getClassInterface();
2066 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2067 CurrentClass = Impl->getClassInterface();
2068 }
2069 if (GetterMethod)
2070 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2071 if (SetterMethod)
2072 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002073}
2074
John McCall48871652010-08-21 09:40:31 +00002075void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002076 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002077 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002078 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002079 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002080 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002081 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002082
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002083 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2084 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2085 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2086 << "readonly" << "readwrite";
2087
2088 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2089 QualType PropertyTy = PropertyDecl->getType();
2090 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002091
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002092 // 'readonly' property with no obvious lifetime.
2093 // its life time will be determined by its backing ivar.
2094 if (getLangOpts().ObjCAutoRefCount &&
2095 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2096 PropertyTy->isObjCRetainableType() &&
2097 !PropertyOwnership)
2098 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002099
2100 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002101 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002102 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2103 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002104 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002105 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002106 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2107 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2108 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002109 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002110 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002111 }
2112
2113 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002114 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2115 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002116 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2117 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002118 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002119 }
Bill Wendling44426052012-12-20 19:22:21 +00002120 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002121 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2122 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002123 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002124 }
Bill Wendling44426052012-12-20 19:22:21 +00002125 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002126 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2127 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002128 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002129 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002130 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002131 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002132 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2133 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002134 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002135 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002136 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002137 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002138 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2139 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002140 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2141 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002142 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002143 }
Bill Wendling44426052012-12-20 19:22:21 +00002144 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002145 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2146 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002147 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002148 }
Bill Wendling44426052012-12-20 19:22:21 +00002149 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002150 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2151 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002152 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002153 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002154 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002155 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2157 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002158 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002159 }
Bill Wendling44426052012-12-20 19:22:21 +00002160 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2161 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002162 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2163 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002164 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002165 }
Bill Wendling44426052012-12-20 19:22:21 +00002166 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002167 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2168 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002169 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002170 }
Bill Wendling44426052012-12-20 19:22:21 +00002171 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002172 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2173 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002174 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002175 }
2176 }
Bill Wendling44426052012-12-20 19:22:21 +00002177 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2178 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002179 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2180 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002181 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002182 }
Bill Wendling44426052012-12-20 19:22:21 +00002183 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2184 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002185 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2186 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002187 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002188 }
2189
Bill Wendling44426052012-12-20 19:22:21 +00002190 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2191 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002192 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2193 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002194 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002195 }
2196
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002197 // Warn if user supplied no assignment attribute, property is
2198 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002199 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002200 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2201 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2202 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002203 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002204 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002205 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002206 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002207 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002208 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002209 bool isAnyClassTy =
2210 (PropertyTy->isObjCClassType() ||
2211 PropertyTy->isObjCQualifiedClassType());
2212 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2213 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002214 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002215 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002216 else if (propertyInPrimaryClass) {
2217 // Don't issue warning on property with no life time in class
2218 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002219 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002220 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002221 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002222
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002223 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002224 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002225 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002226 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002227 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002228
2229 // FIXME: Implement warning dependent on NSCopying being
2230 // implemented. See also:
2231 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2232 // (please trim this list while you are at it).
2233 }
2234
Bill Wendling44426052012-12-20 19:22:21 +00002235 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2236 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002237 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002238 && PropertyTy->isBlockPointerType())
2239 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002240 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2241 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2242 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002243 PropertyTy->isBlockPointerType())
2244 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002245
Bill Wendling44426052012-12-20 19:22:21 +00002246 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2247 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002248 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2249
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002250}