blob: 3f37efa7c172cc8d76964ad095288077f95c451f [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenek28685ab2010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall265941b2011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld64c2eb2012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall265941b2011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallf85e1932011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall265941b2011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000102 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb892d702013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
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 Jahanian1cd6fab2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb892d702013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
Stephen Hines651f13c2014-04-23 16:59:28 -0700135 for (auto *P : Proto->protocols())
136 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb892d702013-01-21 19:42:21 +0000137}
138
John McCalld226f652010-08-21 09:40:31 +0000139Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000140 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000141 FieldDeclarator &FD,
142 ObjCDeclSpec &ODS,
143 Selector GetterSel,
144 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000145 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000146 tok::ObjCKeywordKind MethodImplKind,
147 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000148 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000149 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
150 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000151 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000152
Bill Wendlingad017fa2012-12-20 19:22:21 +0000153 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000154 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000155 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000156 // property is defaulted to 'assign' if it is readwrite and is
157 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000158 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000159 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000160 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
161 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
162 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
163 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000165
Douglas Gregoraabd0942013-01-21 19:05:22 +0000166 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000167 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000168 ObjCPropertyDecl *Res = 0;
169 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000170 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000171 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000172 FD, GetterSel, SetterSel,
173 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000174 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000175 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000176 isOverridingProperty, TSI,
177 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000178 if (!Res)
179 return 0;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000180 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000181 }
182
183 if (!Res) {
184 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
185 GetterSel, SetterSel, isAssign, isReadWrite,
186 Attributes, ODS.getPropertyAttributes(),
187 TSI, MethodImplKind);
188 if (lexicalDC)
189 Res->setLexicalDeclContext(lexicalDC);
190 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000191
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000192 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000193 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000194 (isa<ObjCInterfaceDecl>(ClassDecl) ||
195 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000196
David Blaikie4e4d0842012-03-11 07:00:24 +0000197 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000198 checkARCPropertyDecl(*this, Res);
199
Douglas Gregorb892d702013-01-21 19:42:21 +0000200 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000201 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000202 // For a class, compare the property against a property in our superclass.
203 bool FoundInSuper = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700204 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
205 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000206 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000207 for (unsigned I = 0, N = R.size(); I != N; ++I) {
208 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000209 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb892d702013-01-21 19:42:21 +0000210 FoundInSuper = true;
211 break;
212 }
213 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 if (FoundInSuper)
215 break;
216 else
217 CurrentInterfaceDecl = Super;
Douglas Gregorb892d702013-01-21 19:42:21 +0000218 }
219
220 if (FoundInSuper) {
221 // Also compare the property against a property in our protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700222 for (auto *P : CurrentInterfaceDecl->protocols()) {
223 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
Stephen Hines651f13c2014-04-23 16:59:28 -0700227 for (auto *P : IFace->all_referenced_protocols()) {
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000229 }
230 }
231 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700232 for (auto *P : Cat->protocols())
233 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000234 } else {
235 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Stephen Hines651f13c2014-04-23 16:59:28 -0700236 for (auto *P : Proto->protocols())
237 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000238 }
239
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000240 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000241 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000242}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000243
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000244static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000245makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000246 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000247 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000248 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000249 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000250 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000251 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000252 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000253 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000254 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000255 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000256 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000257 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000258 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000259 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000260 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000261 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000262 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000263 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000264 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000265 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000266 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000267 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000268 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000269 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000270 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
271
272 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
273}
274
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000275static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000276 SourceLocation LParenLoc, SourceLocation &Loc) {
277 if (LParenLoc.isMacroID())
278 return false;
279
280 SourceManager &SM = Context.getSourceManager();
281 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
282 // Try to load the file buffer.
283 bool invalidTemp = false;
284 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
285 if (invalidTemp)
286 return false;
287 const char *tokenBegin = file.data() + locInfo.second;
288
289 // Lex from the start of the given location.
290 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
291 Context.getLangOpts(),
292 file.begin(), tokenBegin, file.end());
293 Token Tok;
294 do {
295 lexer.LexFromRawLexer(Tok);
296 if (Tok.is(tok::raw_identifier) &&
297 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
298 Loc = Tok.getLocation();
299 return true;
300 }
301 } while (Tok.isNot(tok::r_paren));
302 return false;
303
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000304}
305
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000306static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000307 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
308 ObjCPropertyDecl::OBJC_PR_retain |
309 ObjCPropertyDecl::OBJC_PR_copy |
310 ObjCPropertyDecl::OBJC_PR_weak |
311 ObjCPropertyDecl::OBJC_PR_strong |
312 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
313}
314
Douglas Gregoraabd0942013-01-21 19:05:22 +0000315ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000316Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000317 SourceLocation AtLoc,
318 SourceLocation LParenLoc,
319 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000320 Selector GetterSel, Selector SetterSel,
321 const bool isAssign,
322 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000323 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000324 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000325 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000326 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000327 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000328 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000330 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000331 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000332 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
333
Douglas Gregord3297242013-01-16 23:00:23 +0000334 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000335 // Check for duplicate declaration of this property in current and
336 // other class extensions.
Stephen Hines651f13c2014-04-23 16:59:28 -0700337 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +0000338 if (ObjCPropertyDecl *prevDecl
Stephen Hines651f13c2014-04-23 16:59:28 -0700339 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000340 Diag(AtLoc, diag::err_duplicate_property);
341 Diag(prevDecl->getLocation(), diag::note_property_declare);
342 return 0;
343 }
344 }
Douglas Gregord3297242013-01-16 23:00:23 +0000345 }
346
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000347 // Create a new ObjCPropertyDecl with the DeclContext being
348 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000349 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000350 ObjCPropertyDecl *PDecl =
351 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000352 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000353 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000354 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000355 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000356 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000357 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000358 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000359 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
360 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
361 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
362 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000363 // Set setter/getter selector name. Needed later.
364 PDecl->setGetterName(GetterSel);
365 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000366 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000367 DC->addDecl(PDecl);
368
369 // We need to look in the @interface to see if the @property was
370 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000371 if (!CCPrimary) {
372 Diag(CDecl->getLocation(), diag::err_continuation_class);
373 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000374 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000375 }
376
377 // Find the property in continuation class's primary class only.
378 ObjCPropertyDecl *PIDecl =
379 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
380
381 if (!PIDecl) {
382 // No matching property found in the primary class. Just fall thru
383 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000384 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000385 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000386 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000387 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000388
389 // A case of continuation class adding a new property in the class. This
390 // is not what it was meant for. However, gcc supports it and so should we.
391 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000392 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000393 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000394 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
395 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000396 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000397 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
398 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000399 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000400 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
401 bool IncompatibleObjC = false;
402 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000403 // Relax the strict type matching for property type in continuation class.
404 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000405 // as it narrows the object type in its primary class property. Note that
406 // this conversion is safe only because the wider type is for a 'readonly'
407 // property in primary class and 'narrowed' type for a 'readwrite' property
408 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000409 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
410 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
411 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
412 ConvertedType, IncompatibleObjC))
413 || IncompatibleObjC) {
414 Diag(AtLoc,
415 diag::err_type_mismatch_continuation_class) << PDecl->getType();
416 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000417 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000418 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000419 }
420
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000421 // The property 'PIDecl's readonly attribute will be over-ridden
422 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000423 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000424 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +0000425 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
426 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000427 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000428 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000429 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000430 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
431 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000432 Diag(AtLoc, diag::warn_property_attr_mismatch);
433 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000434 }
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000435 else if (getLangOpts().ObjCAutoRefCount) {
436 QualType PrimaryPropertyQT =
437 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
438 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Bill Wendlingd75c6a72013-11-20 06:43:12 +0000439 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000440 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
441 PrimaryPropertyQT.getObjCLifetime();
442 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Bill Wendlingd75c6a72013-11-20 06:43:12 +0000443 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
444 !PropertyIsWeak) {
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000445 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
446 Diag(PIDecl->getLocation(), diag::note_property_declare);
447 }
448 }
449 }
450
Ted Kremenek9944c762010-03-18 01:22:36 +0000451 DeclContext *DC = cast<DeclContext>(CCPrimary);
452 if (!ObjCPropertyDecl::findPropertyDecl(DC,
453 PIDecl->getDeclName().getAsIdentifierInfo())) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700454 // In mrr mode, 'readwrite' property must have an explicit
455 // memory attribute. If none specified, select the default (assign).
456 if (!getLangOpts().ObjCAutoRefCount) {
457 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
458 ObjCDeclSpec::DQ_PR_retain |
459 ObjCDeclSpec::DQ_PR_strong |
460 ObjCDeclSpec::DQ_PR_copy |
461 ObjCDeclSpec::DQ_PR_unsafe_unretained |
462 ObjCDeclSpec::DQ_PR_weak)))
463 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
464 }
465
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000466 // Protocol is not in the primary class. Must build one for it.
467 ObjCDeclSpec ProtocolPropertyODS;
468 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
469 // and ObjCPropertyDecl::PropertyAttributeKind have identical
470 // values. Should consolidate both into one enum type.
471 ProtocolPropertyODS.
472 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
473 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000474 // Must re-establish the context from class extension to primary
475 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000476 ContextRAII SavedContext(*this, CCPrimary);
477
John McCalld226f652010-08-21 09:40:31 +0000478 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000479 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000480 PIDecl->getGetterName(),
481 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000482 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000483 MethodImplKind,
484 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000485 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000486 }
487 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000488 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000489 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000491 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000492 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000493 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
494 PIDecl->setSetterName(SetterSel);
495 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000496 // Tailor the diagnostics for the common case where a readwrite
497 // property is declared both in the @interface and the continuation.
498 // This is a common error where the user often intended the original
499 // declaration to be readonly.
500 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000501 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000502 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
503 ? diag::err_use_continuation_class_redeclaration_readwrite
504 : diag::err_use_continuation_class;
505 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000506 << CCPrimary->getDeclName();
507 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000508 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000509 }
510 *isOverridingProperty = true;
511 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000512 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000513 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
514 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000515 if (ASTMutationListener *L = Context.getASTMutationListener())
516 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000517 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000518}
519
520ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
521 ObjCContainerDecl *CDecl,
522 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000523 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000524 FieldDeclarator &FD,
525 Selector GetterSel,
526 Selector SetterSel,
527 const bool isAssign,
528 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000529 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000530 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000531 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000532 tok::ObjCKeywordKind MethodImplKind,
533 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000534 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000535 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000536
537 // Issue a warning if property is 'assign' as default and its object, which is
538 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000539 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000540 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000541 if (const ObjCObjectPointerType *ObjPtrTy =
542 T->getAs<ObjCObjectPointerType>()) {
543 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
544 if (IDecl)
545 if (ObjCProtocolDecl* PNSCopying =
546 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
547 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
548 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000549 }
Eli Friedman27d46442013-07-09 01:38:07 +0000550
551 if (T->isObjCObjectType()) {
552 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
553 StarLoc = PP.getLocForEndOfToken(StarLoc);
554 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
555 << FixItHint::CreateInsertion(StarLoc, "*");
556 T = Context.getObjCObjectPointerType(T);
557 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
558 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
559 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000560
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000561 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000562 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
563 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000564 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000565
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000566 if (ObjCPropertyDecl *prevDecl =
567 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000568 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000569 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000570 PDecl->setInvalidDecl();
571 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000572 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000574 if (lexicalDC)
575 PDecl->setLexicalDeclContext(lexicalDC);
576 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000577
578 if (T->isArrayType() || T->isFunctionType()) {
579 Diag(AtLoc, diag::err_property_type) << T;
580 PDecl->setInvalidDecl();
581 }
582
583 ProcessDeclAttributes(S, PDecl, FD.D);
584
585 // Regardless of setter/getter attribute, we save the default getter/setter
586 // selector names in anticipation of declaration of setter/getter methods.
587 PDecl->setGetterName(GetterSel);
588 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000589 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000590 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000591
Bill Wendlingad017fa2012-12-20 19:22:21 +0000592 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000593 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
594
Bill Wendlingad017fa2012-12-20 19:22:21 +0000595 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
597
Bill Wendlingad017fa2012-12-20 19:22:21 +0000598 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
600
601 if (isReadWrite)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
603
Bill Wendlingad017fa2012-12-20 19:22:21 +0000604 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
606
Bill Wendlingad017fa2012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
609
Bill Wendlingad017fa2012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
612
Bill Wendlingad017fa2012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
615
Bill Wendlingad017fa2012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
618
Ted Kremenek28685ab2010-03-12 00:46:40 +0000619 if (isAssign)
620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
621
John McCall265941b2011-09-13 18:31:23 +0000622 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000623 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000625 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000627
John McCallf85e1932011-06-15 23:02:42 +0000628 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000629 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
631 if (isAssign)
632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
633
Ted Kremenek28685ab2010-03-12 00:46:40 +0000634 if (MethodImplKind == tok::objc_required)
635 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
636 else if (MethodImplKind == tok::objc_optional)
637 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000638
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000639 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640}
641
John McCallf85e1932011-06-15 23:02:42 +0000642static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
643 ObjCPropertyDecl *property,
644 ObjCIvarDecl *ivar) {
645 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
646
John McCallf85e1932011-06-15 23:02:42 +0000647 QualType ivarType = ivar->getType();
648 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000649
John McCall265941b2011-09-13 18:31:23 +0000650 // The lifetime implied by the property's attributes.
651 Qualifiers::ObjCLifetime propertyLifetime =
652 getImpliedARCOwnership(property->getPropertyAttributes(),
653 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000654
John McCall265941b2011-09-13 18:31:23 +0000655 // We're fine if they match.
656 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000657
John McCall265941b2011-09-13 18:31:23 +0000658 // These aren't valid lifetimes for object ivars; don't diagnose twice.
659 if (ivarLifetime == Qualifiers::OCL_None ||
660 ivarLifetime == Qualifiers::OCL_Autoreleasing)
661 return;
John McCallf85e1932011-06-15 23:02:42 +0000662
John McCalld64c2eb2012-08-20 23:36:59 +0000663 // If the ivar is private, and it's implicitly __unsafe_unretained
664 // becaues of its type, then pretend it was actually implicitly
665 // __strong. This is only sound because we're processing the
666 // property implementation before parsing any method bodies.
667 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
668 propertyLifetime == Qualifiers::OCL_Strong &&
669 ivar->getAccessControl() == ObjCIvarDecl::Private) {
670 SplitQualType split = ivarType.split();
671 if (split.Quals.hasObjCLifetime()) {
672 assert(ivarType->isObjCARCImplicitlyUnretainedType());
673 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
674 ivarType = S.Context.getQualifiedType(split);
675 ivar->setType(ivarType);
676 return;
677 }
678 }
679
John McCall265941b2011-09-13 18:31:23 +0000680 switch (propertyLifetime) {
681 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000682 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000683 << property->getDeclName()
684 << ivar->getDeclName()
685 << ivarLifetime;
686 break;
John McCallf85e1932011-06-15 23:02:42 +0000687
John McCall265941b2011-09-13 18:31:23 +0000688 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000689 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000690 << property->getDeclName()
691 << ivar->getDeclName();
692 break;
John McCallf85e1932011-06-15 23:02:42 +0000693
John McCall265941b2011-09-13 18:31:23 +0000694 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000695 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000696 << property->getDeclName()
697 << ivar->getDeclName()
698 << ((property->getPropertyAttributesAsWritten()
699 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
700 break;
John McCallf85e1932011-06-15 23:02:42 +0000701
John McCall265941b2011-09-13 18:31:23 +0000702 case Qualifiers::OCL_Autoreleasing:
703 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000704
John McCall265941b2011-09-13 18:31:23 +0000705 case Qualifiers::OCL_None:
706 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000707 return;
708 }
709
710 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000711 if (propertyImplLoc.isValid())
712 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000713}
714
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000715/// setImpliedPropertyAttributeForReadOnlyProperty -
716/// This routine evaludates life-time attributes for a 'readonly'
717/// property with no known lifetime of its own, using backing
718/// 'ivar's attribute, if any. If no backing 'ivar', property's
719/// life-time is assumed 'strong'.
720static void setImpliedPropertyAttributeForReadOnlyProperty(
721 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
722 Qualifiers::ObjCLifetime propertyLifetime =
723 getImpliedARCOwnership(property->getPropertyAttributes(),
724 property->getType());
725 if (propertyLifetime != Qualifiers::OCL_None)
726 return;
727
728 if (!ivar) {
729 // if no backing ivar, make property 'strong'.
730 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
731 return;
732 }
733 // property assumes owenership of backing ivar.
734 QualType ivarType = ivar->getType();
735 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
736 if (ivarLifetime == Qualifiers::OCL_Strong)
737 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
738 else if (ivarLifetime == Qualifiers::OCL_Weak)
739 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
740 return;
741}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000742
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000743/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
744/// in inherited protocols with mismatched types. Since any of them can
745/// be candidate for synthesis.
Benjamin Kramerb1a4d372013-05-23 15:53:44 +0000746static void
747DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
748 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000749 ObjCPropertyDecl *Property) {
750 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Stephen Hines651f13c2014-04-23 16:59:28 -0700751 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
752 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000753 PDecl->collectInheritedProtocolProperties(Property, PropMap);
754 }
755 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
756 while (SDecl) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700757 for (const auto *PI : SDecl->all_referenced_protocols()) {
758 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000759 PDecl->collectInheritedProtocolProperties(Property, PropMap);
760 }
761 SDecl = SDecl->getSuperClass();
762 }
763
764 if (PropMap.empty())
765 return;
766
767 QualType RHSType = S.Context.getCanonicalType(Property->getType());
768 bool FirsTime = true;
769 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
770 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
771 ObjCPropertyDecl *Prop = I->second;
772 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
773 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
774 bool IncompatibleObjC = false;
775 QualType ConvertedType;
776 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
777 || IncompatibleObjC) {
778 if (FirsTime) {
779 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
780 << Property->getType();
781 FirsTime = false;
782 }
783 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
784 << Prop->getType();
785 }
786 }
787 }
788 if (!FirsTime && AtLoc.isValid())
789 S.Diag(AtLoc, diag::note_property_synthesize);
790}
791
Ted Kremenek28685ab2010-03-12 00:46:40 +0000792/// ActOnPropertyImplDecl - This routine performs semantic checks and
793/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000794/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795///
John McCalld226f652010-08-21 09:40:31 +0000796Decl *Sema::ActOnPropertyImplDecl(Scope *S,
797 SourceLocation AtLoc,
798 SourceLocation PropertyLoc,
799 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000800 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000801 IdentifierInfo *PropertyIvar,
802 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000803 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000804 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 // Make sure we have a context for the property implementation declaration.
806 if (!ClassImpDecl) {
807 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000808 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000810 if (PropertyIvarLoc.isInvalid())
811 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000812 SourceLocation PropertyDiagLoc = PropertyLoc;
813 if (PropertyDiagLoc.isInvalid())
814 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000815 ObjCPropertyDecl *property = 0;
816 ObjCInterfaceDecl* IDecl = 0;
817 // Find the class or category class where this property must have
818 // a declaration.
819 ObjCImplementationDecl *IC = 0;
820 ObjCCategoryImplDecl* CatImplClass = 0;
821 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
822 IDecl = IC->getClassInterface();
823 // We always synthesize an interface for an implementation
824 // without an interface decl. So, IDecl is always non-zero.
825 assert(IDecl &&
826 "ActOnPropertyImplDecl - @implementation without @interface");
827
828 // Look for this property declaration in the @implementation's @interface
829 property = IDecl->FindPropertyDeclaration(PropertyId);
830 if (!property) {
831 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000832 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000833 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000834 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000835 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
836 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000837 if (AtLoc.isValid())
838 Diag(AtLoc, diag::warn_implicit_atomic_property);
839 else
840 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
841 Diag(property->getLocation(), diag::note_property_declare);
842 }
843
Ted Kremenek28685ab2010-03-12 00:46:40 +0000844 if (const ObjCCategoryDecl *CD =
845 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
846 if (!CD->IsClassExtension()) {
847 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
848 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000849 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000850 }
851 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000852 if (Synthesize&&
853 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
854 property->hasAttr<IBOutletAttr>() &&
855 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000856 bool ReadWriteProperty = false;
857 // Search into the class extensions and see if 'readonly property is
858 // redeclared 'readwrite', then no warning is to be issued.
Stephen Hines651f13c2014-04-23 16:59:28 -0700859 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000860 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
861 if (!R.empty())
862 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
863 PIkind = ExtProp->getPropertyAttributesAsWritten();
864 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
865 ReadWriteProperty = true;
866 break;
867 }
868 }
869 }
870
871 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000872 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Stephen Hines651f13c2014-04-23 16:59:28 -0700873 << property;
Fariborz Jahanian12564342013-02-08 23:32:30 +0000874 SourceLocation readonlyLoc;
875 if (LocPropertyAttribute(Context, "readonly",
876 property->getLParenLoc(), readonlyLoc)) {
877 SourceLocation endLoc =
878 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
879 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
880 Diag(property->getLocation(),
881 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
882 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
883 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000884 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000885 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000886 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
887 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000888
Ted Kremenek28685ab2010-03-12 00:46:40 +0000889 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
890 if (Synthesize) {
891 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000892 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000893 }
894 IDecl = CatImplClass->getClassInterface();
895 if (!IDecl) {
896 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000897 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000898 }
899 ObjCCategoryDecl *Category =
900 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
901
902 // If category for this implementation not found, it is an error which
903 // has already been reported eralier.
904 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000905 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000906 // Look for this property declaration in @implementation's category
907 property = Category->FindPropertyDeclaration(PropertyId);
908 if (!property) {
909 Diag(PropertyLoc, diag::error_bad_category_property_decl)
910 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000911 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000912 }
913 } else {
914 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000915 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000916 }
917 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000918 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000919 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000920 // Check that we have a valid, previously declared ivar for @synthesize
921 if (Synthesize) {
922 // @synthesize
923 if (!PropertyIvar)
924 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000925 // Check that this is a previously declared 'ivar' in 'IDecl' interface
926 ObjCInterfaceDecl *ClassDeclared;
927 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
928 QualType PropType = property->getType();
929 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000930
931 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000932 diag::err_incomplete_synthesized_property,
933 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000934 Diag(property->getLocation(), diag::note_property_declare);
935 CompleteTypeErr = true;
936 }
937
David Blaikie4e4d0842012-03-11 07:00:24 +0000938 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000939 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000940 ObjCPropertyDecl::OBJC_PR_readonly) &&
941 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000942 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
943 }
944
John McCallf85e1932011-06-15 23:02:42 +0000945 ObjCPropertyDecl::PropertyAttributeKind kind
946 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000947
948 // Add GC __weak to the ivar type if the property is weak.
949 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000950 getLangOpts().getGC() != LangOptions::NonGC) {
951 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000952 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000953 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000954 Diag(property->getLocation(), diag::note_property_declare);
955 } else {
956 PropertyIvarType =
957 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000958 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000959 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000960 if (AtLoc.isInvalid()) {
961 // Check when default synthesizing a property that there is
962 // an ivar matching property name and issue warning; since this
963 // is the most common case of not using an ivar used for backing
964 // property in non-default synthesis case.
965 ObjCInterfaceDecl *ClassDeclared=0;
966 ObjCIvarDecl *originalIvar =
967 IDecl->lookupInstanceVariable(property->getIdentifier(),
968 ClassDeclared);
969 if (originalIvar) {
970 Diag(PropertyDiagLoc,
971 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000972 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000973 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000974 Diag(property->getLocation(), diag::note_property_declare);
975 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000976 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000977 }
978
979 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000980 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000981 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000982 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000983 !PropertyIvarType.getObjCLifetime() &&
984 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000985
John McCall265941b2011-09-13 18:31:23 +0000986 // It's an error if we have to do this and the user didn't
987 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000988 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000989 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000990 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000991 diag::err_arc_objc_property_default_assign_on_object);
992 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000993 } else {
994 Qualifiers::ObjCLifetime lifetime =
995 getImpliedARCOwnership(kind, PropertyIvarType);
996 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000997 if (lifetime == Qualifiers::OCL_Weak) {
998 bool err = false;
999 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001000 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1001 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1002 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001003 Diag(property->getLocation(),
1004 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1005 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1006 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001007 err = true;
1008 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001009 }
John McCall0a7dd782012-08-21 02:47:43 +00001010 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001011 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001012 Diag(property->getLocation(), diag::note_property_declare);
1013 }
John McCallf85e1932011-06-15 23:02:42 +00001014 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001015
John McCallf85e1932011-06-15 23:02:42 +00001016 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001017 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001018 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1019 }
John McCallf85e1932011-06-15 23:02:42 +00001020 }
1021
1022 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001023 !getLangOpts().ObjCAutoRefCount &&
1024 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001025 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001026 Diag(property->getLocation(), diag::note_property_declare);
1027 }
1028
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001029 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001030 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001031 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001032 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001033 (Expr *)0, true);
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001034 if (RequireNonAbstractType(PropertyIvarLoc,
1035 PropertyIvarType,
1036 diag::err_abstract_type_in_decl,
1037 AbstractSynthesizedIvarType)) {
1038 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001039 Ivar->setInvalidDecl();
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001040 } else if (CompleteTypeErr)
1041 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001042 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001043 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001044
John McCall260611a2012-06-20 06:18:46 +00001045 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001046 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1047 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001048 // Note! I deliberately want it to fall thru so, we have a
1049 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001050 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001051 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001052 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001053 << property->getDeclName() << Ivar->getDeclName()
1054 << ClassDeclared->getDeclName();
1055 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001056 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001057 // Note! I deliberately want it to fall thru so more errors are caught.
1058 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001059 property->setPropertyIvarDecl(Ivar);
1060
Ted Kremenek28685ab2010-03-12 00:46:40 +00001061 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1062
1063 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001064 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001065 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001066 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001067 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001068 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001069 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001070 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001071 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001072 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1073 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001074 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001075 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001076 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001077 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001078 << property->getDeclName() << PropType
1079 << Ivar->getDeclName() << IvarType;
1080 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001081 // Note! I deliberately want it to fall thru so, we have a
1082 // a property implementation and to avoid future warnings.
1083 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001084 else {
1085 // FIXME! Rules for properties are somewhat different that those
1086 // for assignments. Use a new routine to consolidate all cases;
1087 // specifically for property redeclarations as well as for ivars.
1088 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1089 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1090 if (lhsType != rhsType &&
1091 lhsType->isArithmeticType()) {
1092 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1093 << property->getDeclName() << PropType
1094 << Ivar->getDeclName() << IvarType;
1095 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1096 // Fall thru - see previous comment
1097 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001098 }
1099 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001100 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001101 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001102 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001103 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001104 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001105 // Fall thru - see previous comment
1106 }
John McCallf85e1932011-06-15 23:02:42 +00001107 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001108 if ((property->getType()->isObjCObjectPointerType() ||
1109 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001110 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001111 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001112 << property->getDeclName() << Ivar->getDeclName();
1113 // Fall thru - see previous comment
1114 }
1115 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001116 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001117 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001118 } else if (PropertyIvar)
1119 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001120 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001121
Ted Kremenek28685ab2010-03-12 00:46:40 +00001122 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1123 ObjCPropertyImplDecl *PIDecl =
1124 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1125 property,
1126 (Synthesize ?
1127 ObjCPropertyImplDecl::Synthesize
1128 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001129 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001130
Fariborz Jahanian74414712012-05-15 18:12:51 +00001131 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001132 PIDecl->setInvalidDecl();
1133
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001134 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1135 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001136 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001137 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001138 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1139 // returned by the getter as it must conform to C++'s copy-return rules.
1140 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001141 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001142 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1143 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001144 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001145 VK_LValue, PropertyDiagLoc);
Eli Friedman9a14db32012-10-18 20:14:08 +00001146 MarkDeclRefReferenced(SelfExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001147 Expr *LoadSelfExpr =
1148 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1149 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001150 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001151 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001152 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001153 LoadSelfExpr, true, true);
1154 ExprResult Res = PerformCopyInitialization(
1155 InitializedEntity::InitializeResult(PropertyDiagLoc,
1156 getterMethod->getReturnType(),
1157 /*NRVO=*/false),
1158 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001159 if (!Res.isInvalid()) {
1160 Expr *ResExpr = Res.takeAs<Expr>();
1161 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001162 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001163 PIDecl->setGetterCXXConstructor(ResExpr);
1164 }
1165 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001166 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1167 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1168 Diag(getterMethod->getLocation(),
1169 diag::warn_property_getter_owning_mismatch);
1170 Diag(property->getLocation(), diag::note_property_declare);
1171 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001172 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1173 switch (getterMethod->getMethodFamily()) {
1174 case OMF_retain:
1175 case OMF_retainCount:
1176 case OMF_release:
1177 case OMF_autorelease:
1178 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1179 << 1 << getterMethod->getSelector();
1180 break;
1181 default:
1182 break;
1183 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001184 }
1185 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1186 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001187 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1188 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001189 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001190 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001191 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1192 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001193 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001194 VK_LValue, PropertyDiagLoc);
Eli Friedman9a14db32012-10-18 20:14:08 +00001195 MarkDeclRefReferenced(SelfExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001196 Expr *LoadSelfExpr =
1197 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1198 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001199 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001200 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001201 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001202 LoadSelfExpr, true, true);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001203 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1204 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001205 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001206 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1207 VK_LValue, PropertyDiagLoc);
1208 MarkDeclRefReferenced(rhs);
1209 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001210 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001211 if (property->getPropertyAttributes() &
1212 ObjCPropertyDecl::OBJC_PR_atomic) {
1213 Expr *callExpr = Res.takeAs<Expr>();
1214 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001215 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1216 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001217 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001218 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001219 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001220 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001221 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001222 Diag(FuncDecl->getLocStart(),
1223 diag::note_callee_decl) << FuncDecl;
1224 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001225 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001226 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1227 }
1228 }
1229
Ted Kremenek28685ab2010-03-12 00:46:40 +00001230 if (IC) {
1231 if (Synthesize)
1232 if (ObjCPropertyImplDecl *PPIDecl =
1233 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1234 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1235 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1236 << PropertyIvar;
1237 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1238 }
1239
1240 if (ObjCPropertyImplDecl *PPIDecl
1241 = IC->FindPropertyImplDecl(PropertyId)) {
1242 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1243 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001244 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001245 }
1246 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001247 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001248 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001249 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001250 // Diagnose if an ivar was lazily synthesdized due to a previous
1251 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001252 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001253 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001254 ObjCIvarDecl *Ivar = 0;
1255 if (!Synthesize)
1256 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1257 else {
1258 if (PropertyIvar && PropertyIvar != PropertyId)
1259 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1260 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001261 // Issue diagnostics only if Ivar belongs to current class.
1262 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001263 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001264 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1265 << PropertyId;
1266 Ivar->setInvalidDecl();
1267 }
1268 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001269 } else {
1270 if (Synthesize)
1271 if (ObjCPropertyImplDecl *PPIDecl =
1272 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001273 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001274 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1275 << PropertyIvar;
1276 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1277 }
1278
1279 if (ObjCPropertyImplDecl *PPIDecl =
1280 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001281 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001282 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001283 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001284 }
1285 CatImplClass->addPropertyImplementation(PIDecl);
1286 }
1287
John McCalld226f652010-08-21 09:40:31 +00001288 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001289}
1290
1291//===----------------------------------------------------------------------===//
1292// Helper methods.
1293//===----------------------------------------------------------------------===//
1294
Ted Kremenek9d64c152010-03-12 00:38:38 +00001295/// DiagnosePropertyMismatch - Compares two properties for their
1296/// attributes and types and warns on a variety of inconsistencies.
1297///
1298void
1299Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1300 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001301 const IdentifierInfo *inheritedName,
1302 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001303 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001304 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001305 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001306 SuperProperty->getPropertyAttributes();
1307
1308 // We allow readonly properties without an explicit ownership
1309 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1310 // to be overridden by a property with any explicit ownership in the subclass.
1311 if (!OverridingProtocolProperty &&
1312 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1313 ;
1314 else {
1315 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1316 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1317 Diag(Property->getLocation(), diag::warn_readonly_property)
1318 << Property->getDeclName() << inheritedName;
1319 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1320 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001321 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001322 << Property->getDeclName() << "copy" << inheritedName;
1323 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1324 unsigned CAttrRetain =
1325 (CAttr &
1326 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1327 unsigned SAttrRetain =
1328 (SAttr &
1329 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1330 bool CStrong = (CAttrRetain != 0);
1331 bool SStrong = (SAttrRetain != 0);
1332 if (CStrong != SStrong)
1333 Diag(Property->getLocation(), diag::warn_property_attribute)
1334 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1335 }
John McCallf85e1932011-06-15 23:02:42 +00001336 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001337
1338 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001339 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 Diag(Property->getLocation(), diag::warn_property_attribute)
1341 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001342 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1343 }
1344 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001345 Diag(Property->getLocation(), diag::warn_property_attribute)
1346 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001347 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1348 }
1349 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001350 Diag(Property->getLocation(), diag::warn_property_attribute)
1351 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001352 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1353 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001354
1355 QualType LHSType =
1356 Context.getCanonicalType(SuperProperty->getType());
1357 QualType RHSType =
1358 Context.getCanonicalType(Property->getType());
1359
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001360 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001361 // Do cases not handled in above.
1362 // FIXME. For future support of covariant property types, revisit this.
1363 bool IncompatibleObjC = false;
1364 QualType ConvertedType;
1365 if (!isObjCPointerConversion(RHSType, LHSType,
1366 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001367 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001368 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1369 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001370 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1371 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001372 }
1373}
1374
1375bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1376 ObjCMethodDecl *GetterMethod,
1377 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001378 if (!GetterMethod)
1379 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001380 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001381 QualType PropertyIvarType = property->getType().getNonReferenceType();
1382 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1383 if (!compat) {
1384 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1385 isa<ObjCObjectPointerType>(GetterType))
1386 compat =
1387 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001388 GetterType->getAs<ObjCObjectPointerType>(),
1389 PropertyIvarType->getAs<ObjCObjectPointerType>());
1390 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001391 != Compatible) {
1392 Diag(Loc, diag::error_property_accessor_type)
1393 << property->getDeclName() << PropertyIvarType
1394 << GetterMethod->getSelector() << GetterType;
1395 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1396 return true;
1397 } else {
1398 compat = true;
1399 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1400 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1401 if (lhsType != rhsType && lhsType->isArithmeticType())
1402 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001403 }
1404 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001405
1406 if (!compat) {
1407 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1408 << property->getDeclName()
1409 << GetterMethod->getSelector();
1410 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1411 return true;
1412 }
1413
Ted Kremenek9d64c152010-03-12 00:38:38 +00001414 return false;
1415}
1416
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001418/// the class and its conforming protocols; but not those in its super class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001419static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1420 ObjCContainerDecl::PropertyMap &PropMap,
1421 ObjCContainerDecl::PropertyMap &SuperPropMap,
1422 bool IncludeProtocols = true) {
1423
Ted Kremenek9d64c152010-03-12 00:38:38 +00001424 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001425 for (auto *Prop : IDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001426 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001427 if (IncludeProtocols) {
1428 // Scan through class's protocols.
1429 for (auto *PI : IDecl->all_referenced_protocols())
1430 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001431 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001432 }
1433 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1434 if (!CATDecl->IsClassExtension())
Stephen Hines651f13c2014-04-23 16:59:28 -07001435 for (auto *Prop : CATDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001436 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001437 if (IncludeProtocols) {
1438 // Scan through class's protocols.
1439 for (auto *PI : CATDecl->protocols())
1440 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1441 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442 }
1443 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001444 for (auto *Prop : PDecl->properties()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001445 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1446 // Exclude property for protocols which conform to class's super-class,
1447 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001448 if (!PropertyFromSuper ||
1449 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001450 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1451 if (!PropEntry)
1452 PropEntry = Prop;
1453 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 }
1455 // scan through protocol's protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001456 for (auto *PI : PDecl->protocols())
1457 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 }
1459}
1460
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001461/// CollectSuperClassPropertyImplementations - This routine collects list of
1462/// properties to be implemented in super class(s) and also coming from their
1463/// conforming protocols.
1464static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001465 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001466 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001467 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001468 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001469 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001470 SDecl = SDecl->getSuperClass();
1471 }
1472 }
1473}
1474
Fariborz Jahanian26202292013-02-14 19:07:19 +00001475/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1476/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1477/// declared in class 'IFace'.
1478bool
1479Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1480 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1481 if (!IV->getSynthesize())
1482 return false;
1483 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1484 Method->isInstanceMethod());
1485 if (!IMD || !IMD->isPropertyAccessor())
1486 return false;
1487
1488 // look up a property declaration whose one of its accessors is implemented
1489 // by this method.
Stephen Hines651f13c2014-04-23 16:59:28 -07001490 for (const auto *Property : IFace->properties()) {
1491 if ((Property->getGetterName() == IMD->getSelector() ||
1492 Property->getSetterName() == IMD->getSelector()) &&
1493 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahanian26202292013-02-14 19:07:19 +00001494 return true;
1495 }
1496 return false;
1497}
1498
Stephen Hines651f13c2014-04-23 16:59:28 -07001499static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1500 ObjCPropertyDecl *Prop) {
1501 bool SuperClassImplementsGetter = false;
1502 bool SuperClassImplementsSetter = false;
1503 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1504 SuperClassImplementsSetter = true;
1505
1506 while (IDecl->getSuperClass()) {
1507 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1508 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1509 SuperClassImplementsGetter = true;
1510
1511 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1512 SuperClassImplementsSetter = true;
1513 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1514 return true;
1515 IDecl = IDecl->getSuperClass();
1516 }
1517 return false;
1518}
Fariborz Jahanian26202292013-02-14 19:07:19 +00001519
James Dennett699c9042012-06-15 07:13:21 +00001520/// \brief Default synthesizes all properties which must be synthesized
1521/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001522void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1523 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001524
Anna Zaksb36ea372012-10-18 19:17:53 +00001525 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001526 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1527 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001528 if (PropMap.empty())
1529 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001530 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001531 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1532
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001533 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1534 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001535 // Is there a matching property synthesize/dynamic?
1536 if (Prop->isInvalidDecl() ||
1537 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1538 continue;
1539 // Property may have been synthesized by user.
1540 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1541 continue;
1542 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1543 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1544 continue;
1545 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1546 continue;
1547 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001548 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001549 if (SuperPropMap[Prop->getIdentifier()]) {
1550 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1551 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1552 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001553 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001554 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1555 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001556 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Stephen Hines651f13c2014-04-23 16:59:28 -07001557 << Prop->getIdentifier();
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001558 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1559 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001560 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001561 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001562 if (ObjCPropertyImplDecl *PID =
1563 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1564 if (PID->getPropertyDecl() != Prop) {
1565 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Stephen Hines651f13c2014-04-23 16:59:28 -07001566 << Prop->getIdentifier();
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001567 if (!PID->getLocation().isInvalid())
1568 Diag(PID->getLocation(), diag::note_property_synthesize);
1569 }
1570 continue;
1571 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001572 if (ObjCProtocolDecl *Proto =
1573 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001574 // We won't auto-synthesize properties declared in protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001575 // Suppress the warning if class's superclass implements property's
1576 // getter and implements property's setter (if readwrite property).
1577 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1578 Diag(IMPDecl->getLocation(),
1579 diag::warn_auto_synthesizing_protocol_property)
1580 << Prop << Proto;
1581 Diag(Prop->getLocation(), diag::note_property_declare);
1582 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001583 continue;
1584 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001585
1586 // We use invalid SourceLocations for the synthesized ivars since they
1587 // aren't really synthesized at a particular location; they just exist.
1588 // Saying that they are located at the @implementation isn't really going
1589 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001590 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1591 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1592 true,
1593 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001594 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001595 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001596 if (PIDecl) {
1597 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001598 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001599 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001600 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001601}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001602
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001603void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001604 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001605 return;
1606 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1607 if (!IC)
1608 return;
1609 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001610 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001611 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001612}
1613
Stephen Hines651f13c2014-04-23 16:59:28 -07001614static void DiagnoseUnimplementedAccessor(Sema &S,
1615 ObjCInterfaceDecl *PrimaryClass,
1616 Selector Method,
1617 ObjCImplDecl* IMPDecl,
1618 ObjCContainerDecl *CDecl,
1619 ObjCCategoryDecl *C,
1620 ObjCPropertyDecl *Prop,
1621 Sema::SelectorSet &SMap) {
1622 // When reporting on missing property setter/getter implementation in
1623 // categories, do not report when they are declared in primary class,
1624 // class's protocol, or one of it super classes. This is because,
1625 // the class is going to implement them.
1626 if (!SMap.count(Method) &&
1627 (PrimaryClass == 0 ||
1628 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1629 S.Diag(IMPDecl->getLocation(),
1630 isa<ObjCCategoryDecl>(CDecl) ?
1631 diag::warn_setter_getter_impl_required_in_category :
1632 diag::warn_setter_getter_impl_required)
1633 << Prop->getDeclName() << Method;
1634 S.Diag(Prop->getLocation(),
1635 diag::note_property_declare);
1636 if (S.LangOpts.ObjCDefaultSynthProperties &&
1637 S.LangOpts.ObjCRuntime.isNonFragile())
1638 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1639 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1640 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1641 }
1642}
1643
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001644void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Stephen Hines651f13c2014-04-23 16:59:28 -07001645 ObjCContainerDecl *CDecl,
1646 bool SynthesizeProperties) {
1647 ObjCContainerDecl::PropertyMap PropMap;
1648 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1649
1650 if (!SynthesizeProperties) {
1651 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1652 // Gather properties which need not be implemented in this class
1653 // or category.
1654 if (!IDecl)
1655 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1656 // For categories, no need to implement properties declared in
1657 // its primary class (and its super classes) if property is
1658 // declared in one of those containers.
1659 if ((IDecl = C->getClassInterface())) {
1660 ObjCInterfaceDecl::PropertyDeclOrder PO;
1661 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1662 }
1663 }
1664 if (IDecl)
1665 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1666
1667 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1668 }
1669
1670 // Scan the @interface to see if any of the protocols it adopts
1671 // require an explicit implementation, via attribute
1672 // 'objc_protocol_requires_explicit_implementation'.
1673 if (IDecl) {
1674 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
1675
1676 for (auto *PDecl : IDecl->all_referenced_protocols()) {
1677 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1678 continue;
1679 // Lazily construct a set of all the properties in the @interface
1680 // of the class, without looking at the superclass. We cannot
1681 // use the call to CollectImmediateProperties() above as that
1682 // utilizes information fromt he super class's properties as well
1683 // as scans the adopted protocols. This work only triggers for protocols
1684 // with the attribute, which is very rare, and only occurs when
1685 // analyzing the @implementation.
1686 if (!LazyMap) {
1687 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1688 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1689 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1690 /* IncludeProtocols */ false);
1691 }
1692 // Add the properties of 'PDecl' to the list of properties that
1693 // need to be implemented.
1694 for (auto *PropDecl : PDecl->properties()) {
1695 if ((*LazyMap)[PropDecl->getIdentifier()])
1696 continue;
1697 PropMap[PropDecl->getIdentifier()] = PropDecl;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001698 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001699 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001700 }
1701
Ted Kremenek9d64c152010-03-12 00:38:38 +00001702 if (PropMap.empty())
1703 return;
1704
1705 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Stephen Hines651f13c2014-04-23 16:59:28 -07001706 for (const auto *I : IMPDecl->property_impls())
David Blaikie262bc182012-04-30 02:36:29 +00001707 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001708
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001709 SelectorSet InsMap;
1710 // Collect property accessors implemented in current implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001711 for (const auto *I : IMPDecl->instance_methods())
1712 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001713
1714 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1715 ObjCInterfaceDecl *PrimaryClass = 0;
1716 if (C && !C->IsClassExtension())
1717 if ((PrimaryClass = C->getClassInterface()))
1718 // Report unimplemented properties in the category as well.
1719 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1720 // When reporting on missing setter/getters, do not report when
1721 // setter/getter is implemented in category's primary class
1722 // implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001723 for (const auto *I : IMP->instance_methods())
1724 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001725 }
1726
Anna Zaksb36ea372012-10-18 19:17:53 +00001727 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001728 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1729 ObjCPropertyDecl *Prop = P->second;
1730 // Is there a matching propery synthesize/dynamic?
1731 if (Prop->isInvalidDecl() ||
1732 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001733 PropImplMap.count(Prop) ||
1734 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001735 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001736
1737 // Diagnose unimplemented getters and setters.
1738 DiagnoseUnimplementedAccessor(*this,
1739 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1740 if (!Prop->isReadOnly())
1741 DiagnoseUnimplementedAccessor(*this,
1742 PrimaryClass, Prop->getSetterName(),
1743 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001744 }
1745}
1746
1747void
1748Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1749 ObjCContainerDecl* IDecl) {
1750 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001751 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001752 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07001753 for (const auto *Property : IDecl->properties()) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001754 ObjCMethodDecl *GetterMethod = 0;
1755 ObjCMethodDecl *SetterMethod = 0;
1756 bool LookedUpGetterSetter = false;
1757
Bill Wendlingad017fa2012-12-20 19:22:21 +00001758 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001759 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001760
John McCall265941b2011-09-13 18:31:23 +00001761 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1762 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001763 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1764 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1765 LookedUpGetterSetter = true;
1766 if (GetterMethod) {
1767 Diag(GetterMethod->getLocation(),
1768 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001769 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001770 Diag(Property->getLocation(), diag::note_property_declare);
1771 }
1772 if (SetterMethod) {
1773 Diag(SetterMethod->getLocation(),
1774 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001775 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001776 Diag(Property->getLocation(), diag::note_property_declare);
1777 }
1778 }
1779
Ted Kremenek9d64c152010-03-12 00:38:38 +00001780 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001781 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1782 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001783 continue;
1784 if (const ObjCPropertyImplDecl *PIDecl
1785 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1786 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1787 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001788 if (!LookedUpGetterSetter) {
1789 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1790 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001791 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001792 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1793 SourceLocation MethodLoc =
1794 (GetterMethod ? GetterMethod->getLocation()
1795 : SetterMethod->getLocation());
1796 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001797 << Property->getIdentifier() << (GetterMethod != 0)
1798 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001799 // fixit stuff.
1800 if (!AttributesAsWritten) {
1801 if (Property->getLParenLoc().isValid()) {
1802 // @property () ... case.
1803 SourceRange PropSourceRange(Property->getAtLoc(),
1804 Property->getLParenLoc());
1805 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1806 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1807 }
1808 else {
1809 //@property id etc.
1810 SourceLocation endLoc =
1811 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1812 endLoc = endLoc.getLocWithOffset(-1);
1813 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1814 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1815 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1816 }
1817 }
1818 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1819 // @property () ... case.
1820 SourceLocation endLoc = Property->getLParenLoc();
1821 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1822 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1823 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1824 }
1825 else
1826 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001827 Diag(Property->getLocation(), diag::note_property_declare);
1828 }
1829 }
1830 }
1831}
1832
John McCallf85e1932011-06-15 23:02:42 +00001833void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001834 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001835 return;
1836
Stephen Hines651f13c2014-04-23 16:59:28 -07001837 for (const auto *PID : D->property_impls()) {
John McCallf85e1932011-06-15 23:02:42 +00001838 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001839 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1840 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001841 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1842 if (!method)
1843 continue;
1844 ObjCMethodFamily family = method->getMethodFamily();
1845 if (family == OMF_alloc || family == OMF_copy ||
1846 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001847 if (getLangOpts().ObjCAutoRefCount)
Stephen Hines651f13c2014-04-23 16:59:28 -07001848 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCallf85e1932011-06-15 23:02:42 +00001849 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001850 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCallf85e1932011-06-15 23:02:42 +00001851 }
1852 }
1853 }
1854}
1855
Stephen Hines651f13c2014-04-23 16:59:28 -07001856void Sema::DiagnoseMissingDesignatedInitOverrides(
1857 const ObjCImplementationDecl *ImplD,
1858 const ObjCInterfaceDecl *IFD) {
1859 assert(IFD->hasDesignatedInitializers());
1860 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1861 if (!SuperD)
1862 return;
1863
1864 SelectorSet InitSelSet;
1865 for (const auto *I : ImplD->instance_methods())
1866 if (I->getMethodFamily() == OMF_init)
1867 InitSelSet.insert(I->getSelector());
1868
1869 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1870 SuperD->getDesignatedInitializers(DesignatedInits);
1871 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1872 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1873 const ObjCMethodDecl *MD = *I;
1874 if (!InitSelSet.count(MD->getSelector())) {
1875 Diag(ImplD->getLocation(),
1876 diag::warn_objc_implementation_missing_designated_init_override)
1877 << MD->getSelector();
1878 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1879 }
1880 }
1881}
1882
John McCall5de74d12010-11-10 07:01:40 +00001883/// AddPropertyAttrs - Propagates attributes from a property to the
1884/// implicitly-declared getter or setter for that property.
1885static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1886 ObjCPropertyDecl *Property) {
1887 // Should we just clone all attributes over?
Stephen Hines651f13c2014-04-23 16:59:28 -07001888 for (const auto *A : Property->attrs()) {
1889 if (isa<DeprecatedAttr>(A) ||
1890 isa<UnavailableAttr>(A) ||
1891 isa<AvailabilityAttr>(A))
1892 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001893 }
John McCall5de74d12010-11-10 07:01:40 +00001894}
1895
Ted Kremenek9d64c152010-03-12 00:38:38 +00001896/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1897/// have the property type and issue diagnostics if they don't.
1898/// Also synthesize a getter/setter method if none exist (and update the
1899/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1900/// methods is the "right" thing to do.
1901void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001902 ObjCContainerDecl *CD,
1903 ObjCPropertyDecl *redeclaredProperty,
1904 ObjCContainerDecl *lexicalDC) {
1905
Ted Kremenek9d64c152010-03-12 00:38:38 +00001906 ObjCMethodDecl *GetterMethod, *SetterMethod;
1907
1908 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1909 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1910 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1911 property->getLocation());
1912
1913 if (SetterMethod) {
1914 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1915 property->getPropertyAttributes();
1916 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001917 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1918 Context.VoidTy)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001919 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1920 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001921 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001922 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1923 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001924 Diag(property->getLocation(),
1925 diag::warn_accessor_property_type_mismatch)
1926 << property->getDeclName()
1927 << SetterMethod->getSelector();
1928 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1929 }
1930 }
1931
1932 // Synthesize getter/setter methods if none exist.
1933 // Find the default getter and if one not found, add one.
1934 // FIXME: The synthesized property we set here is misleading. We almost always
1935 // synthesize these methods unless the user explicitly provided prototypes
1936 // (which is odd, but allowed). Sema should be typechecking that the
1937 // declarations jive in that situation (which it is not currently).
1938 if (!GetterMethod) {
1939 // No instance method of same name as property getter name was found.
1940 // Declare a getter method and add it to the list of methods
1941 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001942 SourceLocation Loc = redeclaredProperty ?
1943 redeclaredProperty->getLocation() :
1944 property->getLocation();
1945
1946 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1947 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001948 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001949 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001950 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001951 (property->getPropertyImplementation() ==
1952 ObjCPropertyDecl::Optional) ?
1953 ObjCMethodDecl::Optional :
1954 ObjCMethodDecl::Required);
1955 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001956
1957 AddPropertyAttrs(*this, GetterMethod, property);
1958
Ted Kremenek23173d72010-05-18 21:09:07 +00001959 // FIXME: Eventually this shouldn't be needed, as the lexical context
1960 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001961 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001962 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001963 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Stephen Hines651f13c2014-04-23 16:59:28 -07001964 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1965 Loc));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001966
1967 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1968 GetterMethod->addAttr(
Stephen Hines651f13c2014-04-23 16:59:28 -07001969 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
1970
1971 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
1972 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1973 Loc));
John McCallb8463812013-04-04 01:38:37 +00001974
1975 if (getLangOpts().ObjCAutoRefCount)
1976 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001977 } else
1978 // A user declared getter will be synthesize when @synthesize of
1979 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001980 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001981 property->setGetterMethodDecl(GetterMethod);
1982
1983 // Skip setter if property is read-only.
1984 if (!property->isReadOnly()) {
1985 // Find the default setter and if one not found, add one.
1986 if (!SetterMethod) {
1987 // No instance method of same name as property setter name was found.
1988 // Declare a setter method and add it to the list of methods
1989 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001990 SourceLocation Loc = redeclaredProperty ?
1991 redeclaredProperty->getLocation() :
1992 property->getLocation();
1993
1994 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001995 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001996 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001997 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001998 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001999 /*isImplicitlyDeclared=*/true,
2000 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002001 (property->getPropertyImplementation() ==
2002 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00002003 ObjCMethodDecl::Optional :
2004 ObjCMethodDecl::Required);
2005
Ted Kremenek9d64c152010-03-12 00:38:38 +00002006 // Invent the arguments for the setter. We don't bother making a
2007 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002008 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2009 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002010 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00002011 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00002012 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00002013 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002014 0);
Dmitri Gribenko55431692013-05-05 00:41:58 +00002015 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00002016
2017 AddPropertyAttrs(*this, SetterMethod, property);
2018
Ted Kremenek9d64c152010-03-12 00:38:38 +00002019 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002020 // FIXME: Eventually this shouldn't be needed, as the lexical context
2021 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002022 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002023 SetterMethod->setLexicalDeclContext(lexicalDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07002024 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2025 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2026 SA->getName(), Loc));
John McCallb8463812013-04-04 01:38:37 +00002027 // It's possible for the user to have set a very odd custom
2028 // setter selector that causes it to have a method family.
2029 if (getLangOpts().ObjCAutoRefCount)
2030 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002031 } else
2032 // A user declared setter will be synthesize when @synthesize of
2033 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002034 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002035 property->setSetterMethodDecl(SetterMethod);
2036 }
2037 // Add any synthesized methods to the global pool. This allows us to
2038 // handle the following, which is supported by GCC (and part of the design).
2039 //
2040 // @interface Foo
2041 // @property double bar;
2042 // @end
2043 //
2044 // void thisIsUnfortunate() {
2045 // id foo;
2046 // double bar = [foo bar];
2047 // }
2048 //
2049 if (GetterMethod)
2050 AddInstanceMethodToGlobalPool(GetterMethod);
2051 if (SetterMethod)
2052 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002053
2054 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2055 if (!CurrentClass) {
2056 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2057 CurrentClass = Cat->getClassInterface();
2058 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2059 CurrentClass = Impl->getClassInterface();
2060 }
2061 if (GetterMethod)
2062 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2063 if (SetterMethod)
2064 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002065}
2066
John McCalld226f652010-08-21 09:40:31 +00002067void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002068 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002070 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002071 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002072 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002073 return;
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002074
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002075 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2076 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "readonly" << "readwrite";
2079
2080 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2081 QualType PropertyTy = PropertyDecl->getType();
2082 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002083
Stephen Hines651f13c2014-04-23 16:59:28 -07002084 // 'readonly' property with no obvious lifetime.
2085 // its life time will be determined by its backing ivar.
2086 if (getLangOpts().ObjCAutoRefCount &&
2087 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2088 PropertyTy->isObjCRetainableType() &&
2089 !PropertyOwnership)
2090 return;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002091
2092 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002093 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002094 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2095 !PropertyTy->isObjCRetainableType() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002096 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002097 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002098 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2099 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2100 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002101 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002102 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002103 }
2104
2105 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002106 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2107 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002108 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2109 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002110 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002111 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002112 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002113 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2114 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002116 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002117 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002118 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2119 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002120 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002121 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002122 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002123 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002126 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002127 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002128 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002129 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002130 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2131 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002132 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2133 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002134 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002135 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002136 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002137 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2138 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002139 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002140 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002141 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002142 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2143 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002144 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002145 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002146 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002147 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002148 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2149 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002150 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002151 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002152 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2153 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002154 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2155 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002157 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002158 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002159 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2160 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002161 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002162 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002163 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002164 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2165 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002166 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002167 }
2168 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002169 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2170 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002173 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002174 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002175 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2176 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002177 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2178 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002179 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002180 }
2181
Bill Wendlingad017fa2012-12-20 19:22:21 +00002182 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2183 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002184 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2185 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002186 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002187 }
2188
Ted Kremenek9d64c152010-03-12 00:38:38 +00002189 // Warn if user supplied no assignment attribute, property is
2190 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002191 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002192 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2193 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2194 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002195 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002196 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002197 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002198 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002199 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002200 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002201 bool isAnyClassTy =
2202 (PropertyTy->isObjCClassType() ||
2203 PropertyTy->isObjCQualifiedClassType());
2204 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2205 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002206 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002207 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002208 else if (propertyInPrimaryClass) {
2209 // Don't issue warning on property with no life time in class
2210 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002211 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002212 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002213 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002214
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002215 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002216 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002217 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002218 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002219 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002220
2221 // FIXME: Implement warning dependent on NSCopying being
2222 // implemented. See also:
2223 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2224 // (please trim this list while you are at it).
2225 }
2226
Bill Wendlingad017fa2012-12-20 19:22:21 +00002227 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2228 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002229 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002230 && PropertyTy->isBlockPointerType())
2231 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002232 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2233 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2234 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002235 PropertyTy->isBlockPointerType())
2236 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002237
Bill Wendlingad017fa2012-12-20 19:22:21 +00002238 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2239 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002240 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2241
Ted Kremenek9d64c152010-03-12 00:38:38 +00002242}