blob: 8eb806ba301a17c6e912f93595bd6e2c58bdbb8f [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000025
26using namespace clang;
27
Ted Kremenek28685ab2010-03-12 00:46:40 +000028//===----------------------------------------------------------------------===//
29// Grammar actions.
30//===----------------------------------------------------------------------===//
31
John McCall265941b2011-09-13 18:31:23 +000032/// getImpliedARCOwnership - Given a set of property attributes and a
33/// type, infer an expected lifetime. The type's ownership qualification
34/// is not considered.
35///
36/// Returns OCL_None if the attributes as stated do not imply an ownership.
37/// Never returns OCL_Autoreleasing.
38static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
41 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
43 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld64c2eb2012-08-20 23:36:59 +000046 return Qualifiers::OCL_Strong;
John McCall265941b2011-09-13 18:31:23 +000047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
48 return Qualifiers::OCL_Weak;
49 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
50 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
55 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
56 type->isObjCRetainableType()) {
57 return Qualifiers::OCL_ExplicitNone;
58 }
59
60 return Qualifiers::OCL_None;
61}
62
John McCallf85e1932011-06-15 23:02:42 +000063/// Check the internal consistency of a property declaration.
64static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
65 if (property->isInvalidDecl()) return;
66
67 ObjCPropertyDecl::PropertyAttributeKind propertyKind
68 = property->getPropertyAttributes();
69 Qualifiers::ObjCLifetime propertyLifetime
70 = property->getType().getObjCLifetime();
71
72 // Nothing to do if we don't have a lifetime.
73 if (propertyLifetime == Qualifiers::OCL_None) return;
74
John McCall265941b2011-09-13 18:31:23 +000075 Qualifiers::ObjCLifetime expectedLifetime
76 = getImpliedARCOwnership(propertyKind, property->getType());
77 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000078 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000079 // attribute. That's okay, but restore reasonable invariants by
80 // setting the property attribute according to the lifetime
81 // qualifier.
82 ObjCPropertyDecl::PropertyAttributeKind attr;
83 if (propertyLifetime == Qualifiers::OCL_Strong) {
84 attr = ObjCPropertyDecl::OBJC_PR_strong;
85 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
86 attr = ObjCPropertyDecl::OBJC_PR_weak;
87 } else {
88 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
89 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
90 }
91 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000092 return;
93 }
94
95 if (propertyLifetime == expectedLifetime) return;
96
97 property->setInvalidDecl();
98 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000099 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000100 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000101 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000102 << propertyLifetime;
103}
104
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000105static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
106 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
107 T.isObjCGCWeak()) ||
108 (S.getLangOpts().ObjCAutoRefCount &&
109 T.getObjCLifetime() == Qualifiers::OCL_Weak))
110 return ObjCDeclSpec::DQ_PR_weak;
111 return 0;
112}
113
Douglas Gregorb892d702013-01-21 19:42:21 +0000114/// \brief Check this Objective-C property against a property declared in the
115/// given protocol.
116static void
117CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
118 ObjCProtocolDecl *Proto,
119 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
120 // Have we seen this protocol before?
121 if (!Known.insert(Proto))
122 return;
123
124 // Look for a property with the same name.
125 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
126 for (unsigned I = 0, N = R.size(); I != N; ++I) {
127 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000128 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb892d702013-01-21 19:42:21 +0000129 return;
130 }
131 }
132
133 // Check this property against any protocols we inherit.
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 for (auto *P : Proto->protocols())
135 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb892d702013-01-21 19:42:21 +0000136}
137
John McCalld226f652010-08-21 09:40:31 +0000138Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000139 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000140 FieldDeclarator &FD,
141 ObjCDeclSpec &ODS,
142 Selector GetterSel,
143 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000144 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000145 tok::ObjCKeywordKind MethodImplKind,
146 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000147 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000148 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
149 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000150 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000151
Bill Wendlingad017fa2012-12-20 19:22:21 +0000152 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000153 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000154 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000155 // property is defaulted to 'assign' if it is readwrite and is
156 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000157 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000158 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000159 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
160 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
161 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
162 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
163 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000164
Douglas Gregoraabd0942013-01-21 19:05:22 +0000165 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000166 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700167 ObjCPropertyDecl *Res = nullptr;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000168 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000169 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000170 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000171 FD, GetterSel, SetterSel,
172 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000173 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000174 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000175 isOverridingProperty, TSI,
176 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000177 if (!Res)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700178 return nullptr;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000179 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000180 }
181
182 if (!Res) {
183 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
184 GetterSel, SetterSel, isAssign, isReadWrite,
185 Attributes, ODS.getPropertyAttributes(),
186 TSI, MethodImplKind);
187 if (lexicalDC)
188 Res->setLexicalDeclContext(lexicalDC);
189 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000190
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000191 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000192 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000193 (isa<ObjCInterfaceDecl>(ClassDecl) ||
194 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000195
David Blaikie4e4d0842012-03-11 07:00:24 +0000196 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000197 checkARCPropertyDecl(*this, Res);
198
Douglas Gregorb892d702013-01-21 19:42:21 +0000199 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000200 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000201 // For a class, compare the property against a property in our superclass.
202 bool FoundInSuper = false;
Stephen Hines651f13c2014-04-23 16:59:28 -0700203 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
204 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000205 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000206 for (unsigned I = 0, N = R.size(); I != N; ++I) {
207 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000208 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb892d702013-01-21 19:42:21 +0000209 FoundInSuper = true;
210 break;
211 }
212 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700213 if (FoundInSuper)
214 break;
215 else
216 CurrentInterfaceDecl = Super;
Douglas Gregorb892d702013-01-21 19:42:21 +0000217 }
218
219 if (FoundInSuper) {
220 // Also compare the property against a property in our protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -0700221 for (auto *P : CurrentInterfaceDecl->protocols()) {
222 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000223 }
224 } else {
225 // Slower path: look in all protocols we referenced.
Stephen Hines651f13c2014-04-23 16:59:28 -0700226 for (auto *P : IFace->all_referenced_protocols()) {
227 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000228 }
229 }
230 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 for (auto *P : Cat->protocols())
232 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb892d702013-01-21 19:42:21 +0000233 } else {
234 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Stephen Hines651f13c2014-04-23 16:59:28 -0700235 for (auto *P : Proto->protocols())
236 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000237 }
238
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000239 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000240 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000241}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000242
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000243static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000244makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000245 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000246 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000247 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000248 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000249 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
270
271 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
272}
273
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000274static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000275 SourceLocation LParenLoc, SourceLocation &Loc) {
276 if (LParenLoc.isMacroID())
277 return false;
278
279 SourceManager &SM = Context.getSourceManager();
280 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
281 // Try to load the file buffer.
282 bool invalidTemp = false;
283 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
284 if (invalidTemp)
285 return false;
286 const char *tokenBegin = file.data() + locInfo.second;
287
288 // Lex from the start of the given location.
289 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
290 Context.getLangOpts(),
291 file.begin(), tokenBegin, file.end());
292 Token Tok;
293 do {
294 lexer.LexFromRawLexer(Tok);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700295 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000296 Loc = Tok.getLocation();
297 return true;
298 }
299 } while (Tok.isNot(tok::r_paren));
300 return false;
301
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000302}
303
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000304static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000305 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
306 ObjCPropertyDecl::OBJC_PR_retain |
307 ObjCPropertyDecl::OBJC_PR_copy |
308 ObjCPropertyDecl::OBJC_PR_weak |
309 ObjCPropertyDecl::OBJC_PR_strong |
310 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
311}
312
Douglas Gregoraabd0942013-01-21 19:05:22 +0000313ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000314Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000315 SourceLocation AtLoc,
316 SourceLocation LParenLoc,
317 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000318 Selector GetterSel, Selector SetterSel,
319 const bool isAssign,
320 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000321 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000322 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000323 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000324 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000325 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000326 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000327 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000328 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000330 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
331
Douglas Gregord3297242013-01-16 23:00:23 +0000332 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000333 // Check for duplicate declaration of this property in current and
334 // other class extensions.
Stephen Hines651f13c2014-04-23 16:59:28 -0700335 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregord3297242013-01-16 23:00:23 +0000336 if (ObjCPropertyDecl *prevDecl
Stephen Hines651f13c2014-04-23 16:59:28 -0700337 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000338 Diag(AtLoc, diag::err_duplicate_property);
339 Diag(prevDecl->getLocation(), diag::note_property_declare);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700340 return nullptr;
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000341 }
342 }
Douglas Gregord3297242013-01-16 23:00:23 +0000343 }
344
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000345 // Create a new ObjCPropertyDecl with the DeclContext being
346 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000347 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000348 ObjCPropertyDecl *PDecl =
349 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000350 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000351 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000352 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000353 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000354 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000355 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000356 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000357 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
358 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
359 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
360 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000361 // Set setter/getter selector name. Needed later.
362 PDecl->setGetterName(GetterSel);
363 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000364 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000365 DC->addDecl(PDecl);
366
367 // We need to look in the @interface to see if the @property was
368 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000369 if (!CCPrimary) {
370 Diag(CDecl->getLocation(), diag::err_continuation_class);
371 *isOverridingProperty = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700372 return nullptr;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000373 }
374
375 // Find the property in continuation class's primary class only.
376 ObjCPropertyDecl *PIDecl =
377 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
378
379 if (!PIDecl) {
380 // No matching property found in the primary class. Just fall thru
381 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000382 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000383 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000384 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000385 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000386
387 // A case of continuation class adding a new property in the class. This
388 // is not what it was meant for. However, gcc supports it and so should we.
389 // Make sure setter/getters are declared here.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700390 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
391 /* redeclaredProperty = */ nullptr,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000392 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000393 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
394 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000395 if (ASTMutationListener *L = Context.getASTMutationListener())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700396 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
397 CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000398 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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700417 return nullptr;
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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700508 return nullptr;
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();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700553 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman27d46442013-07-09 01:38:07 +0000554 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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700808 return nullptr;
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();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700815 ObjCPropertyDecl *property = nullptr;
816 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 // Find the class or category class where this property must have
818 // a declaration.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700819 ObjCImplementationDecl *IC = nullptr;
820 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000821 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();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700832 return nullptr;
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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700849 return nullptr;
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);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700892 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000893 }
894 IDecl = CatImplClass->getClassInterface();
895 if (!IDecl) {
896 Diag(AtLoc, diag::error_missing_property_interface);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700897 return nullptr;
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)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700905 return nullptr;
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();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700911 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000912 }
913 } else {
914 Diag(AtLoc, diag::error_bad_property_context);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700915 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000916 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700917 ObjCIvarDecl *Ivar = nullptr;
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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700965 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000966 ObjCIvarDecl *originalIvar =
967 IDecl->lookupInstanceVariable(property->getIdentifier(),
968 ClassDeclared);
969 if (originalIvar) {
970 Diag(PropertyDiagLoc,
971 diag::warn_autosynthesis_property_ivar_match)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700972 << PropertyId << (Ivar == nullptr) << 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,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001031 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001032 ObjCIvarDecl::Private,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001033 (Expr *)nullptr, 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(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001149 CK_LValueToRValue, SelfExpr, nullptr,
1150 VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001151 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001152 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001153 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001154 LoadSelfExpr, true, true);
1155 ExprResult Res = PerformCopyInitialization(
1156 InitializedEntity::InitializeResult(PropertyDiagLoc,
1157 getterMethod->getReturnType(),
1158 /*NRVO=*/false),
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001159 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001160 if (!Res.isInvalid()) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001161 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001162 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001163 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001164 PIDecl->setGetterCXXConstructor(ResExpr);
1165 }
1166 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001167 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1168 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1169 Diag(getterMethod->getLocation(),
1170 diag::warn_property_getter_owning_mismatch);
1171 Diag(property->getLocation(), diag::note_property_declare);
1172 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001173 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1174 switch (getterMethod->getMethodFamily()) {
1175 case OMF_retain:
1176 case OMF_retainCount:
1177 case OMF_release:
1178 case OMF_autorelease:
1179 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1180 << 1 << getterMethod->getSelector();
1181 break;
1182 default:
1183 break;
1184 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001185 }
1186 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1187 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001188 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1189 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001190 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001191 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001192 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1193 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001194 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001195 VK_LValue, PropertyDiagLoc);
Eli Friedman9a14db32012-10-18 20:14:08 +00001196 MarkDeclRefReferenced(SelfExpr);
Stephen Hines651f13c2014-04-23 16:59:28 -07001197 Expr *LoadSelfExpr =
1198 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001199 CK_LValueToRValue, SelfExpr, nullptr,
1200 VK_RValue);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001201 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001202 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001203 Ivar->getLocation(),
Stephen Hines651f13c2014-04-23 16:59:28 -07001204 LoadSelfExpr, true, true);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001205 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1206 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001207 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001208 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1209 VK_LValue, PropertyDiagLoc);
1210 MarkDeclRefReferenced(rhs);
1211 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001212 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001213 if (property->getPropertyAttributes() &
1214 ObjCPropertyDecl::OBJC_PR_atomic) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001215 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001216 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001217 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1218 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001219 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001220 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001221 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001222 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001223 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001224 Diag(FuncDecl->getLocStart(),
1225 diag::note_callee_decl) << FuncDecl;
1226 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001227 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001228 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001229 }
1230 }
1231
Ted Kremenek28685ab2010-03-12 00:46:40 +00001232 if (IC) {
1233 if (Synthesize)
1234 if (ObjCPropertyImplDecl *PPIDecl =
1235 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1236 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1237 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1238 << PropertyIvar;
1239 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1240 }
1241
1242 if (ObjCPropertyImplDecl *PPIDecl
1243 = IC->FindPropertyImplDecl(PropertyId)) {
1244 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1245 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001246 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001247 }
1248 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001249 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001250 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001251 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001252 // Diagnose if an ivar was lazily synthesdized due to a previous
1253 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001254 // but it requires an ivar of different name.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001255 ObjCInterfaceDecl *ClassDeclared=nullptr;
1256 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001257 if (!Synthesize)
1258 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1259 else {
1260 if (PropertyIvar && PropertyIvar != PropertyId)
1261 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1262 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001263 // Issue diagnostics only if Ivar belongs to current class.
1264 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001265 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001266 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1267 << PropertyId;
1268 Ivar->setInvalidDecl();
1269 }
1270 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001271 } else {
1272 if (Synthesize)
1273 if (ObjCPropertyImplDecl *PPIDecl =
1274 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001275 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001276 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1277 << PropertyIvar;
1278 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1279 }
1280
1281 if (ObjCPropertyImplDecl *PPIDecl =
1282 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001283 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001284 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001285 return nullptr;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001286 }
1287 CatImplClass->addPropertyImplementation(PIDecl);
1288 }
1289
John McCalld226f652010-08-21 09:40:31 +00001290 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001291}
1292
1293//===----------------------------------------------------------------------===//
1294// Helper methods.
1295//===----------------------------------------------------------------------===//
1296
Ted Kremenek9d64c152010-03-12 00:38:38 +00001297/// DiagnosePropertyMismatch - Compares two properties for their
1298/// attributes and types and warns on a variety of inconsistencies.
1299///
1300void
1301Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1302 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001303 const IdentifierInfo *inheritedName,
1304 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001305 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001306 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001307 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001308 SuperProperty->getPropertyAttributes();
1309
1310 // We allow readonly properties without an explicit ownership
1311 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1312 // to be overridden by a property with any explicit ownership in the subclass.
1313 if (!OverridingProtocolProperty &&
1314 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1315 ;
1316 else {
1317 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1318 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1319 Diag(Property->getLocation(), diag::warn_readonly_property)
1320 << Property->getDeclName() << inheritedName;
1321 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1322 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001323 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001324 << Property->getDeclName() << "copy" << inheritedName;
1325 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1326 unsigned CAttrRetain =
1327 (CAttr &
1328 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1329 unsigned SAttrRetain =
1330 (SAttr &
1331 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1332 bool CStrong = (CAttrRetain != 0);
1333 bool SStrong = (SAttrRetain != 0);
1334 if (CStrong != SStrong)
1335 Diag(Property->getLocation(), diag::warn_property_attribute)
1336 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1337 }
John McCallf85e1932011-06-15 23:02:42 +00001338 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001339
1340 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001341 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001342 Diag(Property->getLocation(), diag::warn_property_attribute)
1343 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001344 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1345 }
1346 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001347 Diag(Property->getLocation(), diag::warn_property_attribute)
1348 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001349 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1350 }
1351 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352 Diag(Property->getLocation(), diag::warn_property_attribute)
1353 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001354 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1355 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001356
1357 QualType LHSType =
1358 Context.getCanonicalType(SuperProperty->getType());
1359 QualType RHSType =
1360 Context.getCanonicalType(Property->getType());
1361
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001362 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001363 // Do cases not handled in above.
1364 // FIXME. For future support of covariant property types, revisit this.
1365 bool IncompatibleObjC = false;
1366 QualType ConvertedType;
1367 if (!isObjCPointerConversion(RHSType, LHSType,
1368 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001369 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001370 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1371 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001372 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1373 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001374 }
1375}
1376
1377bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1378 ObjCMethodDecl *GetterMethod,
1379 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001380 if (!GetterMethod)
1381 return false;
Stephen Hines651f13c2014-04-23 16:59:28 -07001382 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001383 QualType PropertyIvarType = property->getType().getNonReferenceType();
1384 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1385 if (!compat) {
1386 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1387 isa<ObjCObjectPointerType>(GetterType))
1388 compat =
1389 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001390 GetterType->getAs<ObjCObjectPointerType>(),
1391 PropertyIvarType->getAs<ObjCObjectPointerType>());
1392 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001393 != Compatible) {
1394 Diag(Loc, diag::error_property_accessor_type)
1395 << property->getDeclName() << PropertyIvarType
1396 << GetterMethod->getSelector() << GetterType;
1397 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1398 return true;
1399 } else {
1400 compat = true;
1401 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1402 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1403 if (lhsType != rhsType && lhsType->isArithmeticType())
1404 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001405 }
1406 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001407
1408 if (!compat) {
1409 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1410 << property->getDeclName()
1411 << GetterMethod->getSelector();
1412 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1413 return true;
1414 }
1415
Ted Kremenek9d64c152010-03-12 00:38:38 +00001416 return false;
1417}
1418
Ted Kremenek9d64c152010-03-12 00:38:38 +00001419/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001420/// the class and its conforming protocols; but not those in its super class.
Stephen Hines651f13c2014-04-23 16:59:28 -07001421static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1422 ObjCContainerDecl::PropertyMap &PropMap,
1423 ObjCContainerDecl::PropertyMap &SuperPropMap,
1424 bool IncludeProtocols = true) {
1425
Ted Kremenek9d64c152010-03-12 00:38:38 +00001426 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001427 for (auto *Prop : IDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001428 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001429 if (IncludeProtocols) {
1430 // Scan through class's protocols.
1431 for (auto *PI : IDecl->all_referenced_protocols())
1432 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 }
1435 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1436 if (!CATDecl->IsClassExtension())
Stephen Hines651f13c2014-04-23 16:59:28 -07001437 for (auto *Prop : CATDecl->properties())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001438 PropMap[Prop->getIdentifier()] = Prop;
Stephen Hines651f13c2014-04-23 16:59:28 -07001439 if (IncludeProtocols) {
1440 // Scan through class's protocols.
1441 for (auto *PI : CATDecl->protocols())
1442 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1443 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001444 }
1445 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001446 for (auto *Prop : PDecl->properties()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001447 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1448 // Exclude property for protocols which conform to class's super-class,
1449 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001450 if (!PropertyFromSuper ||
1451 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001452 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1453 if (!PropEntry)
1454 PropEntry = Prop;
1455 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 }
1457 // scan through protocol's protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001458 for (auto *PI : PDecl->protocols())
1459 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 }
1461}
1462
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001463/// CollectSuperClassPropertyImplementations - This routine collects list of
1464/// properties to be implemented in super class(s) and also coming from their
1465/// conforming protocols.
1466static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001467 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001468 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001469 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001470 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001471 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001472 SDecl = SDecl->getSuperClass();
1473 }
1474 }
1475}
1476
Fariborz Jahanian26202292013-02-14 19:07:19 +00001477/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1478/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1479/// declared in class 'IFace'.
1480bool
1481Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1482 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1483 if (!IV->getSynthesize())
1484 return false;
1485 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1486 Method->isInstanceMethod());
1487 if (!IMD || !IMD->isPropertyAccessor())
1488 return false;
1489
1490 // look up a property declaration whose one of its accessors is implemented
1491 // by this method.
Stephen Hines651f13c2014-04-23 16:59:28 -07001492 for (const auto *Property : IFace->properties()) {
1493 if ((Property->getGetterName() == IMD->getSelector() ||
1494 Property->getSetterName() == IMD->getSelector()) &&
1495 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahanian26202292013-02-14 19:07:19 +00001496 return true;
1497 }
1498 return false;
1499}
1500
Stephen Hines651f13c2014-04-23 16:59:28 -07001501static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1502 ObjCPropertyDecl *Prop) {
1503 bool SuperClassImplementsGetter = false;
1504 bool SuperClassImplementsSetter = false;
1505 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1506 SuperClassImplementsSetter = true;
1507
1508 while (IDecl->getSuperClass()) {
1509 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1510 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1511 SuperClassImplementsGetter = true;
1512
1513 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1514 SuperClassImplementsSetter = true;
1515 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1516 return true;
1517 IDecl = IDecl->getSuperClass();
1518 }
1519 return false;
1520}
Fariborz Jahanian26202292013-02-14 19:07:19 +00001521
James Dennett699c9042012-06-15 07:13:21 +00001522/// \brief Default synthesizes all properties which must be synthesized
1523/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001524void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1525 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001526
Anna Zaksb36ea372012-10-18 19:17:53 +00001527 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001528 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1529 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001530 if (PropMap.empty())
1531 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001532 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001533 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1534
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001535 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1536 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001537 // Is there a matching property synthesize/dynamic?
1538 if (Prop->isInvalidDecl() ||
1539 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1540 continue;
1541 // Property may have been synthesized by user.
1542 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1543 continue;
1544 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1545 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1546 continue;
1547 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1548 continue;
1549 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001550 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001551 if (SuperPropMap[Prop->getIdentifier()]) {
1552 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1553 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1554 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001555 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001556 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1557 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001558 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Stephen Hines651f13c2014-04-23 16:59:28 -07001559 << Prop->getIdentifier();
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001560 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1561 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001562 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001563 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001564 if (ObjCPropertyImplDecl *PID =
1565 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1566 if (PID->getPropertyDecl() != Prop) {
1567 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Stephen Hines651f13c2014-04-23 16:59:28 -07001568 << Prop->getIdentifier();
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001569 if (!PID->getLocation().isInvalid())
1570 Diag(PID->getLocation(), diag::note_property_synthesize);
1571 }
1572 continue;
1573 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001574 if (ObjCProtocolDecl *Proto =
1575 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001576 // We won't auto-synthesize properties declared in protocols.
Stephen Hines651f13c2014-04-23 16:59:28 -07001577 // Suppress the warning if class's superclass implements property's
1578 // getter and implements property's setter (if readwrite property).
1579 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1580 Diag(IMPDecl->getLocation(),
1581 diag::warn_auto_synthesizing_protocol_property)
1582 << Prop << Proto;
1583 Diag(Prop->getLocation(), diag::note_property_declare);
1584 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001585 continue;
1586 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001587
1588 // We use invalid SourceLocations for the synthesized ivars since they
1589 // aren't really synthesized at a particular location; they just exist.
1590 // Saying that they are located at the @implementation isn't really going
1591 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001592 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1593 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1594 true,
1595 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001596 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001597 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001598 if (PIDecl) {
1599 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001600 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001601 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001602 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001603}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001605void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001606 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001607 return;
1608 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1609 if (!IC)
1610 return;
1611 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001612 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001613 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001614}
1615
Stephen Hines651f13c2014-04-23 16:59:28 -07001616static void DiagnoseUnimplementedAccessor(Sema &S,
1617 ObjCInterfaceDecl *PrimaryClass,
1618 Selector Method,
1619 ObjCImplDecl* IMPDecl,
1620 ObjCContainerDecl *CDecl,
1621 ObjCCategoryDecl *C,
1622 ObjCPropertyDecl *Prop,
1623 Sema::SelectorSet &SMap) {
1624 // When reporting on missing property setter/getter implementation in
1625 // categories, do not report when they are declared in primary class,
1626 // class's protocol, or one of it super classes. This is because,
1627 // the class is going to implement them.
1628 if (!SMap.count(Method) &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001629 (PrimaryClass == nullptr ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001630 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1631 S.Diag(IMPDecl->getLocation(),
1632 isa<ObjCCategoryDecl>(CDecl) ?
1633 diag::warn_setter_getter_impl_required_in_category :
1634 diag::warn_setter_getter_impl_required)
1635 << Prop->getDeclName() << Method;
1636 S.Diag(Prop->getLocation(),
1637 diag::note_property_declare);
1638 if (S.LangOpts.ObjCDefaultSynthProperties &&
1639 S.LangOpts.ObjCRuntime.isNonFragile())
1640 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1641 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1642 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1643 }
1644}
1645
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001646void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Stephen Hines651f13c2014-04-23 16:59:28 -07001647 ObjCContainerDecl *CDecl,
1648 bool SynthesizeProperties) {
1649 ObjCContainerDecl::PropertyMap PropMap;
1650 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1651
1652 if (!SynthesizeProperties) {
1653 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1654 // Gather properties which need not be implemented in this class
1655 // or category.
1656 if (!IDecl)
1657 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1658 // For categories, no need to implement properties declared in
1659 // its primary class (and its super classes) if property is
1660 // declared in one of those containers.
1661 if ((IDecl = C->getClassInterface())) {
1662 ObjCInterfaceDecl::PropertyDeclOrder PO;
1663 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1664 }
1665 }
1666 if (IDecl)
1667 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1668
1669 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1670 }
1671
1672 // Scan the @interface to see if any of the protocols it adopts
1673 // require an explicit implementation, via attribute
1674 // 'objc_protocol_requires_explicit_implementation'.
1675 if (IDecl) {
1676 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
1677
1678 for (auto *PDecl : IDecl->all_referenced_protocols()) {
1679 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1680 continue;
1681 // Lazily construct a set of all the properties in the @interface
1682 // of the class, without looking at the superclass. We cannot
1683 // use the call to CollectImmediateProperties() above as that
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001684 // utilizes information from the super class's properties as well
Stephen Hines651f13c2014-04-23 16:59:28 -07001685 // as scans the adopted protocols. This work only triggers for protocols
1686 // with the attribute, which is very rare, and only occurs when
1687 // analyzing the @implementation.
1688 if (!LazyMap) {
1689 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1690 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1691 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1692 /* IncludeProtocols */ false);
1693 }
1694 // Add the properties of 'PDecl' to the list of properties that
1695 // need to be implemented.
1696 for (auto *PropDecl : PDecl->properties()) {
1697 if ((*LazyMap)[PropDecl->getIdentifier()])
1698 continue;
1699 PropMap[PropDecl->getIdentifier()] = PropDecl;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001700 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001701 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001702 }
1703
Ted Kremenek9d64c152010-03-12 00:38:38 +00001704 if (PropMap.empty())
1705 return;
1706
1707 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Stephen Hines651f13c2014-04-23 16:59:28 -07001708 for (const auto *I : IMPDecl->property_impls())
David Blaikie262bc182012-04-30 02:36:29 +00001709 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001710
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001711 SelectorSet InsMap;
1712 // Collect property accessors implemented in current implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001713 for (const auto *I : IMPDecl->instance_methods())
1714 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001715
1716 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001717 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001718 if (C && !C->IsClassExtension())
1719 if ((PrimaryClass = C->getClassInterface()))
1720 // Report unimplemented properties in the category as well.
1721 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1722 // When reporting on missing setter/getters, do not report when
1723 // setter/getter is implemented in category's primary class
1724 // implementation.
Stephen Hines651f13c2014-04-23 16:59:28 -07001725 for (const auto *I : IMP->instance_methods())
1726 InsMap.insert(I->getSelector());
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001727 }
1728
Anna Zaksb36ea372012-10-18 19:17:53 +00001729 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001730 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1731 ObjCPropertyDecl *Prop = P->second;
1732 // Is there a matching propery synthesize/dynamic?
1733 if (Prop->isInvalidDecl() ||
1734 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001735 PropImplMap.count(Prop) ||
1736 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001737 continue;
Stephen Hines651f13c2014-04-23 16:59:28 -07001738
1739 // Diagnose unimplemented getters and setters.
1740 DiagnoseUnimplementedAccessor(*this,
1741 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1742 if (!Prop->isReadOnly())
1743 DiagnoseUnimplementedAccessor(*this,
1744 PrimaryClass, Prop->getSetterName(),
1745 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001746 }
1747}
1748
1749void
1750Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1751 ObjCContainerDecl* IDecl) {
1752 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001753 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001754 return;
Stephen Hines651f13c2014-04-23 16:59:28 -07001755 for (const auto *Property : IDecl->properties()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001756 ObjCMethodDecl *GetterMethod = nullptr;
1757 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001758 bool LookedUpGetterSetter = false;
1759
Bill Wendlingad017fa2012-12-20 19:22:21 +00001760 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001761 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001762
John McCall265941b2011-09-13 18:31:23 +00001763 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1764 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001765 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1766 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1767 LookedUpGetterSetter = true;
1768 if (GetterMethod) {
1769 Diag(GetterMethod->getLocation(),
1770 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001771 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001772 Diag(Property->getLocation(), diag::note_property_declare);
1773 }
1774 if (SetterMethod) {
1775 Diag(SetterMethod->getLocation(),
1776 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001777 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001778 Diag(Property->getLocation(), diag::note_property_declare);
1779 }
1780 }
1781
Ted Kremenek9d64c152010-03-12 00:38:38 +00001782 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001783 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1784 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001785 continue;
1786 if (const ObjCPropertyImplDecl *PIDecl
1787 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1788 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1789 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001790 if (!LookedUpGetterSetter) {
1791 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1792 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001793 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001794 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1795 SourceLocation MethodLoc =
1796 (GetterMethod ? GetterMethod->getLocation()
1797 : SetterMethod->getLocation());
1798 Diag(MethodLoc, diag::warn_atomic_property_rule)
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001799 << Property->getIdentifier() << (GetterMethod != nullptr)
1800 << (SetterMethod != nullptr);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001801 // fixit stuff.
1802 if (!AttributesAsWritten) {
1803 if (Property->getLParenLoc().isValid()) {
1804 // @property () ... case.
1805 SourceRange PropSourceRange(Property->getAtLoc(),
1806 Property->getLParenLoc());
1807 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1808 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1809 }
1810 else {
1811 //@property id etc.
1812 SourceLocation endLoc =
1813 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1814 endLoc = endLoc.getLocWithOffset(-1);
1815 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1816 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1817 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1818 }
1819 }
1820 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1821 // @property () ... case.
1822 SourceLocation endLoc = Property->getLParenLoc();
1823 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1824 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1825 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1826 }
1827 else
1828 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001829 Diag(Property->getLocation(), diag::note_property_declare);
1830 }
1831 }
1832 }
1833}
1834
John McCallf85e1932011-06-15 23:02:42 +00001835void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001836 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001837 return;
1838
Stephen Hines651f13c2014-04-23 16:59:28 -07001839 for (const auto *PID : D->property_impls()) {
John McCallf85e1932011-06-15 23:02:42 +00001840 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001841 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1842 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001843 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1844 if (!method)
1845 continue;
1846 ObjCMethodFamily family = method->getMethodFamily();
1847 if (family == OMF_alloc || family == OMF_copy ||
1848 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001849 if (getLangOpts().ObjCAutoRefCount)
Stephen Hines651f13c2014-04-23 16:59:28 -07001850 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCallf85e1932011-06-15 23:02:42 +00001851 else
Stephen Hines651f13c2014-04-23 16:59:28 -07001852 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCallf85e1932011-06-15 23:02:42 +00001853 }
1854 }
1855 }
1856}
1857
Stephen Hines651f13c2014-04-23 16:59:28 -07001858void Sema::DiagnoseMissingDesignatedInitOverrides(
1859 const ObjCImplementationDecl *ImplD,
1860 const ObjCInterfaceDecl *IFD) {
1861 assert(IFD->hasDesignatedInitializers());
1862 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1863 if (!SuperD)
1864 return;
1865
1866 SelectorSet InitSelSet;
1867 for (const auto *I : ImplD->instance_methods())
1868 if (I->getMethodFamily() == OMF_init)
1869 InitSelSet.insert(I->getSelector());
1870
1871 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1872 SuperD->getDesignatedInitializers(DesignatedInits);
1873 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1874 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1875 const ObjCMethodDecl *MD = *I;
1876 if (!InitSelSet.count(MD->getSelector())) {
1877 Diag(ImplD->getLocation(),
1878 diag::warn_objc_implementation_missing_designated_init_override)
1879 << MD->getSelector();
1880 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1881 }
1882 }
1883}
1884
John McCall5de74d12010-11-10 07:01:40 +00001885/// AddPropertyAttrs - Propagates attributes from a property to the
1886/// implicitly-declared getter or setter for that property.
1887static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1888 ObjCPropertyDecl *Property) {
1889 // Should we just clone all attributes over?
Stephen Hines651f13c2014-04-23 16:59:28 -07001890 for (const auto *A : Property->attrs()) {
1891 if (isa<DeprecatedAttr>(A) ||
1892 isa<UnavailableAttr>(A) ||
1893 isa<AvailabilityAttr>(A))
1894 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001895 }
John McCall5de74d12010-11-10 07:01:40 +00001896}
1897
Ted Kremenek9d64c152010-03-12 00:38:38 +00001898/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1899/// have the property type and issue diagnostics if they don't.
1900/// Also synthesize a getter/setter method if none exist (and update the
1901/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1902/// methods is the "right" thing to do.
1903void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001904 ObjCContainerDecl *CD,
1905 ObjCPropertyDecl *redeclaredProperty,
1906 ObjCContainerDecl *lexicalDC) {
1907
Ted Kremenek9d64c152010-03-12 00:38:38 +00001908 ObjCMethodDecl *GetterMethod, *SetterMethod;
1909
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001910 if (CD->isInvalidDecl())
1911 return;
1912
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1914 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1915 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1916 property->getLocation());
1917
1918 if (SetterMethod) {
1919 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1920 property->getPropertyAttributes();
1921 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Stephen Hines651f13c2014-04-23 16:59:28 -07001922 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1923 Context.VoidTy)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001924 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1925 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001926 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001927 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1928 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001929 Diag(property->getLocation(),
1930 diag::warn_accessor_property_type_mismatch)
1931 << property->getDeclName()
1932 << SetterMethod->getSelector();
1933 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1934 }
1935 }
1936
1937 // Synthesize getter/setter methods if none exist.
1938 // Find the default getter and if one not found, add one.
1939 // FIXME: The synthesized property we set here is misleading. We almost always
1940 // synthesize these methods unless the user explicitly provided prototypes
1941 // (which is odd, but allowed). Sema should be typechecking that the
1942 // declarations jive in that situation (which it is not currently).
1943 if (!GetterMethod) {
1944 // No instance method of same name as property getter name was found.
1945 // Declare a getter method and add it to the list of methods
1946 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001947 SourceLocation Loc = redeclaredProperty ?
1948 redeclaredProperty->getLocation() :
1949 property->getLocation();
1950
1951 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1952 property->getGetterName(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001953 property->getType(), nullptr, CD,
1954 /*isInstance=*/true, /*isVariadic=*/false,
1955 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001956 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001957 (property->getPropertyImplementation() ==
1958 ObjCPropertyDecl::Optional) ?
1959 ObjCMethodDecl::Optional :
1960 ObjCMethodDecl::Required);
1961 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001962
1963 AddPropertyAttrs(*this, GetterMethod, property);
1964
Ted Kremenek23173d72010-05-18 21:09:07 +00001965 // FIXME: Eventually this shouldn't be needed, as the lexical context
1966 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001967 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001968 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001969 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Stephen Hines651f13c2014-04-23 16:59:28 -07001970 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1971 Loc));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001972
1973 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1974 GetterMethod->addAttr(
Stephen Hines651f13c2014-04-23 16:59:28 -07001975 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
1976
1977 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001978 GetterMethod->addAttr(
1979 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
1980 SA->getName(), Loc));
John McCallb8463812013-04-04 01:38:37 +00001981
1982 if (getLangOpts().ObjCAutoRefCount)
1983 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001984 } else
1985 // A user declared getter will be synthesize when @synthesize of
1986 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001987 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001988 property->setGetterMethodDecl(GetterMethod);
1989
1990 // Skip setter if property is read-only.
1991 if (!property->isReadOnly()) {
1992 // Find the default setter and if one not found, add one.
1993 if (!SetterMethod) {
1994 // No instance method of same name as property setter name was found.
1995 // Declare a setter method and add it to the list of methods
1996 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001997 SourceLocation Loc = redeclaredProperty ?
1998 redeclaredProperty->getLocation() :
1999 property->getLocation();
2000
2001 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002002 ObjCMethodDecl::Create(Context, Loc, Loc,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002003 property->getSetterName(), Context.VoidTy,
2004 nullptr, CD, /*isInstance=*/true,
2005 /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002006 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002007 /*isImplicitlyDeclared=*/true,
2008 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002009 (property->getPropertyImplementation() ==
2010 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00002011 ObjCMethodDecl::Optional :
2012 ObjCMethodDecl::Required);
2013
Ted Kremenek9d64c152010-03-12 00:38:38 +00002014 // Invent the arguments for the setter. We don't bother making a
2015 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002016 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2017 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002018 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00002019 property->getType().getUnqualifiedType(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002020 /*TInfo=*/nullptr,
John McCalld931b082010-08-26 03:08:43 +00002021 SC_None,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002022 nullptr);
Dmitri Gribenko55431692013-05-05 00:41:58 +00002023 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00002024
2025 AddPropertyAttrs(*this, SetterMethod, property);
2026
Ted Kremenek9d64c152010-03-12 00:38:38 +00002027 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002028 // FIXME: Eventually this shouldn't be needed, as the lexical context
2029 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002030 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002031 SetterMethod->setLexicalDeclContext(lexicalDC);
Stephen Hines651f13c2014-04-23 16:59:28 -07002032 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002033 SetterMethod->addAttr(
2034 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2035 SA->getName(), Loc));
John McCallb8463812013-04-04 01:38:37 +00002036 // It's possible for the user to have set a very odd custom
2037 // setter selector that causes it to have a method family.
2038 if (getLangOpts().ObjCAutoRefCount)
2039 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002040 } else
2041 // A user declared setter will be synthesize when @synthesize of
2042 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002043 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002044 property->setSetterMethodDecl(SetterMethod);
2045 }
2046 // Add any synthesized methods to the global pool. This allows us to
2047 // handle the following, which is supported by GCC (and part of the design).
2048 //
2049 // @interface Foo
2050 // @property double bar;
2051 // @end
2052 //
2053 // void thisIsUnfortunate() {
2054 // id foo;
2055 // double bar = [foo bar];
2056 // }
2057 //
2058 if (GetterMethod)
2059 AddInstanceMethodToGlobalPool(GetterMethod);
2060 if (SetterMethod)
2061 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002062
2063 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2064 if (!CurrentClass) {
2065 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2066 CurrentClass = Cat->getClassInterface();
2067 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2068 CurrentClass = Impl->getClassInterface();
2069 }
2070 if (GetterMethod)
2071 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2072 if (SetterMethod)
2073 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002074}
2075
John McCalld226f652010-08-21 09:40:31 +00002076void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002077 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002078 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002079 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002080 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002081 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002082 return;
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002083
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002084 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2085 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2086 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2087 << "readonly" << "readwrite";
2088
2089 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2090 QualType PropertyTy = PropertyDecl->getType();
2091 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002092
Stephen Hines651f13c2014-04-23 16:59:28 -07002093 // 'readonly' property with no obvious lifetime.
2094 // its life time will be determined by its backing ivar.
2095 if (getLangOpts().ObjCAutoRefCount &&
2096 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2097 PropertyTy->isObjCRetainableType() &&
2098 !PropertyOwnership)
2099 return;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002100
2101 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002102 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002103 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2104 !PropertyTy->isObjCRetainableType() &&
Stephen Hines651f13c2014-04-23 16:59:28 -07002105 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002106 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002107 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2108 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2109 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002110 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002111 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002112 }
2113
2114 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2116 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002119 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002120 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002121 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002124 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002125 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002126 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002127 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2128 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002129 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002130 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002131 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002132 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002135 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002136 }
Stephen Hines651f13c2014-04-23 16:59:28 -07002137 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002138 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002139 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2140 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002141 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2142 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002143 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002144 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002145 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002146 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2147 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002148 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002149 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002150 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2152 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002153 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002154 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002155 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002157 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2158 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002159 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002160 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002161 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2162 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002163 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2164 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002165 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002166 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002167 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002168 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2169 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002170 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002171 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002172 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002173 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2174 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002175 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002176 }
2177 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002178 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2179 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002180 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2181 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002182 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002183 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002184 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2185 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002186 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2187 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002188 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002189 }
2190
Bill Wendlingad017fa2012-12-20 19:22:21 +00002191 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2192 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002193 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2194 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002195 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002196 }
2197
Ted Kremenek9d64c152010-03-12 00:38:38 +00002198 // Warn if user supplied no assignment attribute, property is
2199 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002200 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002201 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2202 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2203 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002204 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002205 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002206 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002207 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002208 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002209 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002210 bool isAnyClassTy =
2211 (PropertyTy->isObjCClassType() ||
2212 PropertyTy->isObjCQualifiedClassType());
2213 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2214 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002215 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002216 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002217 else if (propertyInPrimaryClass) {
2218 // Don't issue warning on property with no life time in class
2219 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002220 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002221 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002222 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002223
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002224 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002225 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002226 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002227 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002228 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002229
2230 // FIXME: Implement warning dependent on NSCopying being
2231 // implemented. See also:
2232 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2233 // (please trim this list while you are at it).
2234 }
2235
Bill Wendlingad017fa2012-12-20 19:22:21 +00002236 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2237 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002238 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002239 && PropertyTy->isBlockPointerType())
2240 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002241 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2242 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2243 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002244 PropertyTy->isBlockPointerType())
2245 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002246
Bill Wendlingad017fa2012-12-20 19:22:21 +00002247 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2248 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002249 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2250
Ted Kremenek9d64c152010-03-12 00:38:38 +00002251}