blob: f62af8eb81d544e3b1c5eaae3f0f6e56dcaed8b0 [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCall31168b02011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb8982092013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000120 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000121 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000122 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000135 for (auto *P : Proto->protocols())
136 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000137}
138
John McCall48871652010-08-21 09:40:31 +0000139Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000140 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000141 FieldDeclarator &FD,
142 ObjCDeclSpec &ODS,
143 Selector GetterSel,
144 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000145 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000146 tok::ObjCKeywordKind MethodImplKind,
147 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000148 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000149 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
150 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000151 Attributes |= deduceWeakPropertyFromType(*this, T);
Bill Wendling44426052012-12-20 19:22:21 +0000152 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000153 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000154 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000155 // property is defaulted to 'assign' if it is readwrite and is
156 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000157 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000158 (isReadWrite &&
Bill Wendling44426052012-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 Jahanianb24b5682011-03-28 23:47:18 +0000164
Douglas Gregor90d34422013-01-21 19:05:22 +0000165 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000166 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000167 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000168 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000169 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000170 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000171 FD, GetterSel, SetterSel,
172 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000173 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000174 ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000175 isOverridingProperty, T, TSI,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000176 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000177 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000178 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000179 }
Douglas Gregor90d34422013-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(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000186 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000187 if (lexicalDC)
188 Res->setLexicalDeclContext(lexicalDC);
189 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000190
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000191 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000192 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000193 (isa<ObjCInterfaceDecl>(ClassDecl) ||
194 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000195
David Blaikiebbafb8a2012-03-11 07:00:24 +0000196 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000197 checkARCPropertyDecl(*this, Res);
198
Douglas Gregorb8982092013-01-21 19:42:21 +0000199 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000201 // For a class, compare the property against a property in our superclass.
202 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000203 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
204 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000205 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-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 Jahanianb809a0e2013-10-04 18:06:08 +0000208 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000209 FoundInSuper = true;
210 break;
211 }
212 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000213 if (FoundInSuper)
214 break;
215 else
216 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000217 }
218
219 if (FoundInSuper) {
220 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000221 for (auto *P : CurrentInterfaceDecl->protocols()) {
222 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000223 }
224 } else {
225 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000226 for (auto *P : IFace->all_referenced_protocols()) {
227 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000228 }
229 }
230 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (auto *P : Cat->protocols())
232 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 } else {
234 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000235 for (auto *P : Proto->protocols())
236 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000237 }
238
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000239 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000240 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000241}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000242
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000243static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000244makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000245 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000246 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000247 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000248 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000249 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
270
271 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
272}
273
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000274static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-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);
Alp Toker2d57cea2014-05-17 04:53:25 +0000295 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-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 Jahanian199a9b52012-05-19 18:17:17 +0000302}
303
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000304static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-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 Gregor90d34422013-01-21 19:05:22 +0000313ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000314Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000315 SourceLocation AtLoc,
316 SourceLocation LParenLoc,
317 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000318 Selector GetterSel, Selector SetterSel,
319 const bool isAssign,
320 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000321 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000322 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000323 bool *isOverridingProperty,
Douglas Gregor813a0662015-06-19 18:14:38 +0000324 QualType T,
325 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000326 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000327 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000328 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000329 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000330 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000331 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
332
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000333 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000334 // Check for duplicate declaration of this property in current and
335 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000336 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000337 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000338 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000339 Diag(AtLoc, diag::err_duplicate_property);
340 Diag(prevDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000341 return nullptr;
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000342 }
343 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000344 }
345
Ted Kremenek959e8302010-03-12 02:31:10 +0000346 // Create a new ObjCPropertyDecl with the DeclContext being
347 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000348 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000349 ObjCPropertyDecl *PDecl =
350 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000351 PropertyId, AtLoc, LParenLoc, T, TSI);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000352 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000353 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000354 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000356 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000358 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
359 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
360 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Douglas Gregor813a0662015-06-19 18:14:38 +0000362 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
363 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000364 // Set setter/getter selector name. Needed later.
365 PDecl->setGetterName(GetterSel);
366 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000367 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000368 DC->addDecl(PDecl);
369
370 // We need to look in the @interface to see if the @property was
371 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000372 if (!CCPrimary) {
373 Diag(CDecl->getLocation(), diag::err_continuation_class);
374 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000375 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000376 }
377
378 // Find the property in continuation class's primary class only.
379 ObjCPropertyDecl *PIDecl =
380 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
381
382 if (!PIDecl) {
383 // No matching property found in the primary class. Just fall thru
384 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000385 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000386 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000387 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Douglas Gregor813a0662015-06-19 18:14:38 +0000388 Attributes,AttributesAsWritten, T, TSI, MethodImplKind,
389 DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000390
391 // A case of continuation class adding a new property in the class. This
392 // is not what it was meant for. However, gcc supports it and so should we.
393 // Make sure setter/getters are declared here.
Craig Topperc3ec1492014-05-26 06:22:03 +0000394 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
395 /* redeclaredProperty = */ nullptr,
Ted Kremenek2f075632010-09-21 20:52:59 +0000396 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000397 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
398 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000399 if (ASTMutationListener *L = Context.getASTMutationListener())
Craig Topperc3ec1492014-05-26 06:22:03 +0000400 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
401 CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000402 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000403 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000404 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
405 bool IncompatibleObjC = false;
406 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000407 // Relax the strict type matching for property type in continuation class.
408 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000409 // as it narrows the object type in its primary class property. Note that
410 // this conversion is safe only because the wider type is for a 'readonly'
411 // property in primary class and 'narrowed' type for a 'readwrite' property
412 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000413 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
414 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
415 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
416 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
417 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000418 ConvertedType, IncompatibleObjC))
419 || IncompatibleObjC) {
420 Diag(AtLoc,
421 diag::err_type_mismatch_continuation_class) << PDecl->getType();
422 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000423 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000424 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000425 }
426
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 // The property 'PIDecl's readonly attribute will be over-ridden
428 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000429 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000431 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
432 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000433 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000434 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000435 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000436 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
437 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000438 Diag(AtLoc, diag::warn_property_attr_mismatch);
439 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000440 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000441 else if (getLangOpts().ObjCAutoRefCount) {
442 QualType PrimaryPropertyQT =
443 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
444 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000445 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000446 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
447 PrimaryPropertyQT.getObjCLifetime();
448 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000449 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
450 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000451 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
452 Diag(PIDecl->getLocation(), diag::note_property_declare);
453 }
454 }
455 }
456
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000457 DeclContext *DC = cast<DeclContext>(CCPrimary);
458 if (!ObjCPropertyDecl::findPropertyDecl(DC,
459 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000460 // In mrr mode, 'readwrite' property must have an explicit
461 // memory attribute. If none specified, select the default (assign).
462 if (!getLangOpts().ObjCAutoRefCount) {
463 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
464 ObjCDeclSpec::DQ_PR_retain |
465 ObjCDeclSpec::DQ_PR_strong |
466 ObjCDeclSpec::DQ_PR_copy |
467 ObjCDeclSpec::DQ_PR_unsafe_unretained |
468 ObjCDeclSpec::DQ_PR_weak)))
469 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
470 }
471
Ted Kremenek959e8302010-03-12 02:31:10 +0000472 // Protocol is not in the primary class. Must build one for it.
473 ObjCDeclSpec ProtocolPropertyODS;
474 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
475 // and ObjCPropertyDecl::PropertyAttributeKind have identical
476 // values. Should consolidate both into one enum type.
477 ProtocolPropertyODS.
478 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
479 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000480 // Must re-establish the context from class extension to primary
481 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000482 ContextRAII SavedContext(*this, CCPrimary);
483
John McCall48871652010-08-21 09:40:31 +0000484 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000485 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000486 PIDecl->getGetterName(),
487 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000488 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000489 MethodImplKind,
490 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000491 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000492 }
493 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000494 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000495 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000496 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000497 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000498 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000499 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
500 PIDecl->setSetterName(SetterSel);
501 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000502 // Tailor the diagnostics for the common case where a readwrite
503 // property is declared both in the @interface and the continuation.
504 // This is a common error where the user often intended the original
505 // declaration to be readonly.
506 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000507 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000508 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
509 ? diag::err_use_continuation_class_redeclaration_readwrite
510 : diag::err_use_continuation_class;
511 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000512 << CCPrimary->getDeclName();
513 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000514 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000515 }
516 *isOverridingProperty = true;
517 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000518 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000519 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
520 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000521 if (ASTMutationListener *L = Context.getASTMutationListener())
522 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000523 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000524}
525
526ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
527 ObjCContainerDecl *CDecl,
528 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000529 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000530 FieldDeclarator &FD,
531 Selector GetterSel,
532 Selector SetterSel,
533 const bool isAssign,
534 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000535 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000536 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000537 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000538 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000539 tok::ObjCKeywordKind MethodImplKind,
540 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000541 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000542
543 // Issue a warning if property is 'assign' as default and its object, which is
544 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000545 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000546 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000547 if (const ObjCObjectPointerType *ObjPtrTy =
548 T->getAs<ObjCObjectPointerType>()) {
549 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
550 if (IDecl)
551 if (ObjCProtocolDecl* PNSCopying =
552 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
553 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
554 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000555 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000556
557 if (T->isObjCObjectType()) {
558 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000559 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000560 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
561 << FixItHint::CreateInsertion(StarLoc, "*");
562 T = Context.getObjCObjectPointerType(T);
563 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
564 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
565 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000566
Ted Kremenek959e8302010-03-12 02:31:10 +0000567 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000568 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
569 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000570 PropertyId, AtLoc,
571 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000572
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000573 if (ObjCPropertyDecl *prevDecl =
574 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000575 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000576 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000577 PDecl->setInvalidDecl();
578 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000579 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000580 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000581 if (lexicalDC)
582 PDecl->setLexicalDeclContext(lexicalDC);
583 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000584
585 if (T->isArrayType() || T->isFunctionType()) {
586 Diag(AtLoc, diag::err_property_type) << T;
587 PDecl->setInvalidDecl();
588 }
589
590 ProcessDeclAttributes(S, PDecl, FD.D);
591
592 // Regardless of setter/getter attribute, we save the default getter/setter
593 // selector names in anticipation of declaration of setter/getter methods.
594 PDecl->setGetterName(GetterSel);
595 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000596 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000597 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000598
Bill Wendling44426052012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
601
Bill Wendling44426052012-12-20 19:22:21 +0000602 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
604
Bill Wendling44426052012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
607
608 if (isReadWrite)
609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
610
Bill Wendling44426052012-12-20 19:22:21 +0000611 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
613
Bill Wendling44426052012-12-20 19:22:21 +0000614 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
616
Bill Wendling44426052012-12-20 19:22:21 +0000617 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
619
Bill Wendling44426052012-12-20 19:22:21 +0000620 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000621 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
622
Bill Wendling44426052012-12-20 19:22:21 +0000623 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
625
Ted Kremenekac597f32010-03-12 00:46:40 +0000626 if (isAssign)
627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
628
John McCall43192862011-09-13 18:31:23 +0000629 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000630 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000632 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000634
John McCall31168b02011-06-15 23:02:42 +0000635 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000636 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
638 if (isAssign)
639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
640
Ted Kremenekac597f32010-03-12 00:46:40 +0000641 if (MethodImplKind == tok::objc_required)
642 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
643 else if (MethodImplKind == tok::objc_optional)
644 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000645
Douglas Gregor813a0662015-06-19 18:14:38 +0000646 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
647 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
648
Ted Kremenek959e8302010-03-12 02:31:10 +0000649 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000650}
651
John McCall31168b02011-06-15 23:02:42 +0000652static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
653 ObjCPropertyDecl *property,
654 ObjCIvarDecl *ivar) {
655 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
656
John McCall31168b02011-06-15 23:02:42 +0000657 QualType ivarType = ivar->getType();
658 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000659
John McCall43192862011-09-13 18:31:23 +0000660 // The lifetime implied by the property's attributes.
661 Qualifiers::ObjCLifetime propertyLifetime =
662 getImpliedARCOwnership(property->getPropertyAttributes(),
663 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000664
John McCall43192862011-09-13 18:31:23 +0000665 // We're fine if they match.
666 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000667
John McCall43192862011-09-13 18:31:23 +0000668 // These aren't valid lifetimes for object ivars; don't diagnose twice.
669 if (ivarLifetime == Qualifiers::OCL_None ||
670 ivarLifetime == Qualifiers::OCL_Autoreleasing)
671 return;
John McCall31168b02011-06-15 23:02:42 +0000672
John McCalld8561f02012-08-20 23:36:59 +0000673 // If the ivar is private, and it's implicitly __unsafe_unretained
674 // becaues of its type, then pretend it was actually implicitly
675 // __strong. This is only sound because we're processing the
676 // property implementation before parsing any method bodies.
677 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
678 propertyLifetime == Qualifiers::OCL_Strong &&
679 ivar->getAccessControl() == ObjCIvarDecl::Private) {
680 SplitQualType split = ivarType.split();
681 if (split.Quals.hasObjCLifetime()) {
682 assert(ivarType->isObjCARCImplicitlyUnretainedType());
683 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
684 ivarType = S.Context.getQualifiedType(split);
685 ivar->setType(ivarType);
686 return;
687 }
688 }
689
John McCall43192862011-09-13 18:31:23 +0000690 switch (propertyLifetime) {
691 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000692 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000693 << property->getDeclName()
694 << ivar->getDeclName()
695 << ivarLifetime;
696 break;
John McCall31168b02011-06-15 23:02:42 +0000697
John McCall43192862011-09-13 18:31:23 +0000698 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000699 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000700 << property->getDeclName()
701 << ivar->getDeclName();
702 break;
John McCall31168b02011-06-15 23:02:42 +0000703
John McCall43192862011-09-13 18:31:23 +0000704 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000705 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000706 << property->getDeclName()
707 << ivar->getDeclName()
708 << ((property->getPropertyAttributesAsWritten()
709 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
710 break;
John McCall31168b02011-06-15 23:02:42 +0000711
John McCall43192862011-09-13 18:31:23 +0000712 case Qualifiers::OCL_Autoreleasing:
713 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000714
John McCall43192862011-09-13 18:31:23 +0000715 case Qualifiers::OCL_None:
716 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000717 return;
718 }
719
720 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000721 if (propertyImplLoc.isValid())
722 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000723}
724
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000725/// setImpliedPropertyAttributeForReadOnlyProperty -
726/// This routine evaludates life-time attributes for a 'readonly'
727/// property with no known lifetime of its own, using backing
728/// 'ivar's attribute, if any. If no backing 'ivar', property's
729/// life-time is assumed 'strong'.
730static void setImpliedPropertyAttributeForReadOnlyProperty(
731 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
732 Qualifiers::ObjCLifetime propertyLifetime =
733 getImpliedARCOwnership(property->getPropertyAttributes(),
734 property->getType());
735 if (propertyLifetime != Qualifiers::OCL_None)
736 return;
737
738 if (!ivar) {
739 // if no backing ivar, make property 'strong'.
740 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
741 return;
742 }
743 // property assumes owenership of backing ivar.
744 QualType ivarType = ivar->getType();
745 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
746 if (ivarLifetime == Qualifiers::OCL_Strong)
747 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
748 else if (ivarLifetime == Qualifiers::OCL_Weak)
749 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
750 return;
751}
Ted Kremenekac597f32010-03-12 00:46:40 +0000752
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000753/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
754/// in inherited protocols with mismatched types. Since any of them can
755/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000756static void
757DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
758 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000759 ObjCPropertyDecl *Property) {
760 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000761 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
762 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000763 PDecl->collectInheritedProtocolProperties(Property, PropMap);
764 }
765 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
766 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000767 for (const auto *PI : SDecl->all_referenced_protocols()) {
768 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000769 PDecl->collectInheritedProtocolProperties(Property, PropMap);
770 }
771 SDecl = SDecl->getSuperClass();
772 }
773
774 if (PropMap.empty())
775 return;
776
777 QualType RHSType = S.Context.getCanonicalType(Property->getType());
778 bool FirsTime = true;
779 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
780 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
781 ObjCPropertyDecl *Prop = I->second;
782 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
783 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
784 bool IncompatibleObjC = false;
785 QualType ConvertedType;
786 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
787 || IncompatibleObjC) {
788 if (FirsTime) {
789 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
790 << Property->getType();
791 FirsTime = false;
792 }
793 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
794 << Prop->getType();
795 }
796 }
797 }
798 if (!FirsTime && AtLoc.isValid())
799 S.Diag(AtLoc, diag::note_property_synthesize);
800}
801
Ted Kremenekac597f32010-03-12 00:46:40 +0000802/// ActOnPropertyImplDecl - This routine performs semantic checks and
803/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000804/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000805///
John McCall48871652010-08-21 09:40:31 +0000806Decl *Sema::ActOnPropertyImplDecl(Scope *S,
807 SourceLocation AtLoc,
808 SourceLocation PropertyLoc,
809 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000810 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000811 IdentifierInfo *PropertyIvar,
812 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000813 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000814 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000815 // Make sure we have a context for the property implementation declaration.
816 if (!ClassImpDecl) {
817 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000818 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000819 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000820 if (PropertyIvarLoc.isInvalid())
821 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000822 SourceLocation PropertyDiagLoc = PropertyLoc;
823 if (PropertyDiagLoc.isInvalid())
824 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000825 ObjCPropertyDecl *property = nullptr;
826 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000827 // Find the class or category class where this property must have
828 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000829 ObjCImplementationDecl *IC = nullptr;
830 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000831 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
832 IDecl = IC->getClassInterface();
833 // We always synthesize an interface for an implementation
834 // without an interface decl. So, IDecl is always non-zero.
835 assert(IDecl &&
836 "ActOnPropertyImplDecl - @implementation without @interface");
837
838 // Look for this property declaration in the @implementation's @interface
839 property = IDecl->FindPropertyDeclaration(PropertyId);
840 if (!property) {
841 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000842 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000843 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000844 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000845 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
846 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000847 if (AtLoc.isValid())
848 Diag(AtLoc, diag::warn_implicit_atomic_property);
849 else
850 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
851 Diag(property->getLocation(), diag::note_property_declare);
852 }
853
Ted Kremenekac597f32010-03-12 00:46:40 +0000854 if (const ObjCCategoryDecl *CD =
855 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
856 if (!CD->IsClassExtension()) {
857 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
858 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000859 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000860 }
861 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000862 if (Synthesize&&
863 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
864 property->hasAttr<IBOutletAttr>() &&
865 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000866 bool ReadWriteProperty = false;
867 // Search into the class extensions and see if 'readonly property is
868 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000869 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000870 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
871 if (!R.empty())
872 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
873 PIkind = ExtProp->getPropertyAttributesAsWritten();
874 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
875 ReadWriteProperty = true;
876 break;
877 }
878 }
879 }
880
881 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000882 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000883 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000884 SourceLocation readonlyLoc;
885 if (LocPropertyAttribute(Context, "readonly",
886 property->getLParenLoc(), readonlyLoc)) {
887 SourceLocation endLoc =
888 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
889 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
890 Diag(property->getLocation(),
891 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
892 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
893 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000894 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000895 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000896 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
897 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000898
Ted Kremenekac597f32010-03-12 00:46:40 +0000899 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
900 if (Synthesize) {
901 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000902 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000903 }
904 IDecl = CatImplClass->getClassInterface();
905 if (!IDecl) {
906 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000907 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000908 }
909 ObjCCategoryDecl *Category =
910 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
911
912 // If category for this implementation not found, it is an error which
913 // has already been reported eralier.
914 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000915 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000916 // Look for this property declaration in @implementation's category
917 property = Category->FindPropertyDeclaration(PropertyId);
918 if (!property) {
919 Diag(PropertyLoc, diag::error_bad_category_property_decl)
920 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000921 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000922 }
923 } else {
924 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000925 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000926 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000927 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000928 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000929 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000930 // Check that we have a valid, previously declared ivar for @synthesize
931 if (Synthesize) {
932 // @synthesize
933 if (!PropertyIvar)
934 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000935 // Check that this is a previously declared 'ivar' in 'IDecl' interface
936 ObjCInterfaceDecl *ClassDeclared;
937 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
938 QualType PropType = property->getType();
939 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000940
941 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000942 diag::err_incomplete_synthesized_property,
943 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000944 Diag(property->getLocation(), diag::note_property_declare);
945 CompleteTypeErr = true;
946 }
947
David Blaikiebbafb8a2012-03-11 07:00:24 +0000948 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000949 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000950 ObjCPropertyDecl::OBJC_PR_readonly) &&
951 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000952 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
953 }
954
John McCall31168b02011-06-15 23:02:42 +0000955 ObjCPropertyDecl::PropertyAttributeKind kind
956 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000957
958 // Add GC __weak to the ivar type if the property is weak.
959 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000960 getLangOpts().getGC() != LangOptions::NonGC) {
961 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000962 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000963 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000964 Diag(property->getLocation(), diag::note_property_declare);
965 } else {
966 PropertyIvarType =
967 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000968 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000969 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000970 if (AtLoc.isInvalid()) {
971 // Check when default synthesizing a property that there is
972 // an ivar matching property name and issue warning; since this
973 // is the most common case of not using an ivar used for backing
974 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +0000975 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000976 ObjCIvarDecl *originalIvar =
977 IDecl->lookupInstanceVariable(property->getIdentifier(),
978 ClassDeclared);
979 if (originalIvar) {
980 Diag(PropertyDiagLoc,
981 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +0000982 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000983 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000984 Diag(property->getLocation(), diag::note_property_declare);
985 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000986 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000987 }
988
989 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000990 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000991 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000992 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000993 !PropertyIvarType.getObjCLifetime() &&
994 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000995
John McCall43192862011-09-13 18:31:23 +0000996 // It's an error if we have to do this and the user didn't
997 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000998 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000999 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001000 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001001 diag::err_arc_objc_property_default_assign_on_object);
1002 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001003 } else {
1004 Qualifiers::ObjCLifetime lifetime =
1005 getImpliedARCOwnership(kind, PropertyIvarType);
1006 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001007 if (lifetime == Qualifiers::OCL_Weak) {
1008 bool err = false;
1009 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001010 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1011 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1012 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001013 Diag(property->getLocation(),
1014 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1015 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1016 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001017 err = true;
1018 }
Richard Smith802c4b72012-08-23 06:16:52 +00001019 }
John McCall3deb1ad2012-08-21 02:47:43 +00001020 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001021 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001022 Diag(property->getLocation(), diag::note_property_declare);
1023 }
John McCall31168b02011-06-15 23:02:42 +00001024 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001025
John McCall31168b02011-06-15 23:02:42 +00001026 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001027 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001028 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1029 }
John McCall31168b02011-06-15 23:02:42 +00001030 }
1031
1032 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001033 !getLangOpts().ObjCAutoRefCount &&
1034 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001035 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001036 Diag(property->getLocation(), diag::note_property_declare);
1037 }
1038
Abramo Bagnaradff19302011-03-08 08:55:46 +00001039 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001040 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001041 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001042 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001043 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001044 if (RequireNonAbstractType(PropertyIvarLoc,
1045 PropertyIvarType,
1046 diag::err_abstract_type_in_decl,
1047 AbstractSynthesizedIvarType)) {
1048 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001049 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001050 } else if (CompleteTypeErr)
1051 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001052 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001053 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001054
John McCall5fb5df92012-06-20 06:18:46 +00001055 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001056 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1057 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001058 // Note! I deliberately want it to fall thru so, we have a
1059 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001060 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001061 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001062 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001063 << property->getDeclName() << Ivar->getDeclName()
1064 << ClassDeclared->getDeclName();
1065 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001066 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001067 // Note! I deliberately want it to fall thru so more errors are caught.
1068 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001069 property->setPropertyIvarDecl(Ivar);
1070
Ted Kremenekac597f32010-03-12 00:46:40 +00001071 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1072
1073 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001074 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001075 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001076 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001077 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001078 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001079 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001080 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001081 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001082 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1083 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001084 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001085 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001086 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001087 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001088 << property->getDeclName() << PropType
1089 << Ivar->getDeclName() << IvarType;
1090 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001091 // Note! I deliberately want it to fall thru so, we have a
1092 // a property implementation and to avoid future warnings.
1093 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001094 else {
1095 // FIXME! Rules for properties are somewhat different that those
1096 // for assignments. Use a new routine to consolidate all cases;
1097 // specifically for property redeclarations as well as for ivars.
1098 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1099 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1100 if (lhsType != rhsType &&
1101 lhsType->isArithmeticType()) {
1102 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1103 << property->getDeclName() << PropType
1104 << Ivar->getDeclName() << IvarType;
1105 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1106 // Fall thru - see previous comment
1107 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001108 }
1109 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001110 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001111 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001112 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001113 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001114 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001115 // Fall thru - see previous comment
1116 }
John McCall31168b02011-06-15 23:02:42 +00001117 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001118 if ((property->getType()->isObjCObjectPointerType() ||
1119 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001120 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001121 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001122 << property->getDeclName() << Ivar->getDeclName();
1123 // Fall thru - see previous comment
1124 }
1125 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001126 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001127 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001128 } else if (PropertyIvar)
1129 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001130 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001131
Ted Kremenekac597f32010-03-12 00:46:40 +00001132 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1133 ObjCPropertyImplDecl *PIDecl =
1134 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1135 property,
1136 (Synthesize ?
1137 ObjCPropertyImplDecl::Synthesize
1138 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001139 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001140
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001141 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001142 PIDecl->setInvalidDecl();
1143
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001144 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1145 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001146 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001147 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001148 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1149 // returned by the getter as it must conform to C++'s copy-return rules.
1150 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001151 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001152 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1153 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001154 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001155 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001156 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001157 Expr *LoadSelfExpr =
1158 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001159 CK_LValueToRValue, SelfExpr, nullptr,
1160 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001161 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001162 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001163 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001164 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001165 ExprResult Res = PerformCopyInitialization(
1166 InitializedEntity::InitializeResult(PropertyDiagLoc,
1167 getterMethod->getReturnType(),
1168 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001169 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001170 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001171 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001172 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001173 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001174 PIDecl->setGetterCXXConstructor(ResExpr);
1175 }
1176 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001177 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1178 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1179 Diag(getterMethod->getLocation(),
1180 diag::warn_property_getter_owning_mismatch);
1181 Diag(property->getLocation(), diag::note_property_declare);
1182 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001183 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1184 switch (getterMethod->getMethodFamily()) {
1185 case OMF_retain:
1186 case OMF_retainCount:
1187 case OMF_release:
1188 case OMF_autorelease:
1189 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1190 << 1 << getterMethod->getSelector();
1191 break;
1192 default:
1193 break;
1194 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001195 }
1196 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1197 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001198 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1199 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001200 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001201 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001202 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1203 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001204 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001205 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001206 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001207 Expr *LoadSelfExpr =
1208 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001209 CK_LValueToRValue, SelfExpr, nullptr,
1210 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001211 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001212 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001213 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001214 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001215 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1216 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001217 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001218 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1219 VK_LValue, PropertyDiagLoc);
1220 MarkDeclRefReferenced(rhs);
1221 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001222 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001223 if (property->getPropertyAttributes() &
1224 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001225 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001226 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001227 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1228 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001229 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001230 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001231 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001232 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001233 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001234 Diag(FuncDecl->getLocStart(),
1235 diag::note_callee_decl) << FuncDecl;
1236 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001237 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001238 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001239 }
1240 }
1241
Ted Kremenekac597f32010-03-12 00:46:40 +00001242 if (IC) {
1243 if (Synthesize)
1244 if (ObjCPropertyImplDecl *PPIDecl =
1245 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1246 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1247 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1248 << PropertyIvar;
1249 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1250 }
1251
1252 if (ObjCPropertyImplDecl *PPIDecl
1253 = IC->FindPropertyImplDecl(PropertyId)) {
1254 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1255 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001256 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001257 }
1258 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001259 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001260 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001261 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001262 // Diagnose if an ivar was lazily synthesdized due to a previous
1263 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001264 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001265 ObjCInterfaceDecl *ClassDeclared=nullptr;
1266 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001267 if (!Synthesize)
1268 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1269 else {
1270 if (PropertyIvar && PropertyIvar != PropertyId)
1271 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1272 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001273 // Issue diagnostics only if Ivar belongs to current class.
1274 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001275 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001276 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1277 << PropertyId;
1278 Ivar->setInvalidDecl();
1279 }
1280 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001281 } else {
1282 if (Synthesize)
1283 if (ObjCPropertyImplDecl *PPIDecl =
1284 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001285 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001286 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1287 << PropertyIvar;
1288 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1289 }
1290
1291 if (ObjCPropertyImplDecl *PPIDecl =
1292 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001293 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001294 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001295 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001296 }
1297 CatImplClass->addPropertyImplementation(PIDecl);
1298 }
1299
John McCall48871652010-08-21 09:40:31 +00001300 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001301}
1302
1303//===----------------------------------------------------------------------===//
1304// Helper methods.
1305//===----------------------------------------------------------------------===//
1306
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001307/// DiagnosePropertyMismatch - Compares two properties for their
1308/// attributes and types and warns on a variety of inconsistencies.
1309///
1310void
1311Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1312 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001313 const IdentifierInfo *inheritedName,
1314 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001315 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001316 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001317 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001318 SuperProperty->getPropertyAttributes();
1319
1320 // We allow readonly properties without an explicit ownership
1321 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1322 // to be overridden by a property with any explicit ownership in the subclass.
1323 if (!OverridingProtocolProperty &&
1324 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1325 ;
1326 else {
1327 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1328 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1329 Diag(Property->getLocation(), diag::warn_readonly_property)
1330 << Property->getDeclName() << inheritedName;
1331 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1332 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001333 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001334 << Property->getDeclName() << "copy" << inheritedName;
1335 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1336 unsigned CAttrRetain =
1337 (CAttr &
1338 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1339 unsigned SAttrRetain =
1340 (SAttr &
1341 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1342 bool CStrong = (CAttrRetain != 0);
1343 bool SStrong = (SAttrRetain != 0);
1344 if (CStrong != SStrong)
1345 Diag(Property->getLocation(), diag::warn_property_attribute)
1346 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1347 }
John McCall31168b02011-06-15 23:02:42 +00001348 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001349
1350 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001351 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001352 Diag(Property->getLocation(), diag::warn_property_attribute)
1353 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001354 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1355 }
1356 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001357 Diag(Property->getLocation(), diag::warn_property_attribute)
1358 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001359 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1360 }
1361 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001362 Diag(Property->getLocation(), diag::warn_property_attribute)
1363 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001364 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1365 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001366
1367 QualType LHSType =
1368 Context.getCanonicalType(SuperProperty->getType());
1369 QualType RHSType =
1370 Context.getCanonicalType(Property->getType());
1371
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001372 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001373 // Do cases not handled in above.
1374 // FIXME. For future support of covariant property types, revisit this.
1375 bool IncompatibleObjC = false;
1376 QualType ConvertedType;
1377 if (!isObjCPointerConversion(RHSType, LHSType,
1378 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001379 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001380 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1381 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001382 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1383 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001384 }
1385}
1386
1387bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1388 ObjCMethodDecl *GetterMethod,
1389 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001390 if (!GetterMethod)
1391 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001392 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001393 QualType PropertyIvarType = property->getType().getNonReferenceType();
1394 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1395 if (!compat) {
1396 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1397 isa<ObjCObjectPointerType>(GetterType))
1398 compat =
1399 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001400 GetterType->getAs<ObjCObjectPointerType>(),
1401 PropertyIvarType->getAs<ObjCObjectPointerType>());
1402 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001403 != Compatible) {
1404 Diag(Loc, diag::error_property_accessor_type)
1405 << property->getDeclName() << PropertyIvarType
1406 << GetterMethod->getSelector() << GetterType;
1407 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1408 return true;
1409 } else {
1410 compat = true;
1411 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1412 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1413 if (lhsType != rhsType && lhsType->isArithmeticType())
1414 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001415 }
1416 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001417
1418 if (!compat) {
1419 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1420 << property->getDeclName()
1421 << GetterMethod->getSelector();
1422 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1423 return true;
1424 }
1425
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001426 return false;
1427}
1428
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001429/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001430/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001431static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1432 ObjCContainerDecl::PropertyMap &PropMap,
1433 ObjCContainerDecl::PropertyMap &SuperPropMap,
1434 bool IncludeProtocols = true) {
1435
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001436 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001437 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001438 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001439 if (IncludeProtocols) {
1440 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001441 for (auto *PI : IDecl->all_referenced_protocols())
1442 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001443 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001444 }
1445 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1446 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001447 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001448 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001449 if (IncludeProtocols) {
1450 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001451 for (auto *PI : CATDecl->protocols())
1452 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001453 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001454 }
1455 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001456 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001457 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1458 // Exclude property for protocols which conform to class's super-class,
1459 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001460 if (!PropertyFromSuper ||
1461 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001462 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1463 if (!PropEntry)
1464 PropEntry = Prop;
1465 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001466 }
1467 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001468 for (auto *PI : PDecl->protocols())
1469 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001470 }
1471}
1472
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001473/// CollectSuperClassPropertyImplementations - This routine collects list of
1474/// properties to be implemented in super class(s) and also coming from their
1475/// conforming protocols.
1476static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001477 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001478 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001479 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001480 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001481 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001482 SDecl = SDecl->getSuperClass();
1483 }
1484 }
1485}
1486
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001487/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1488/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1489/// declared in class 'IFace'.
1490bool
1491Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1492 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1493 if (!IV->getSynthesize())
1494 return false;
1495 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1496 Method->isInstanceMethod());
1497 if (!IMD || !IMD->isPropertyAccessor())
1498 return false;
1499
1500 // look up a property declaration whose one of its accessors is implemented
1501 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001502 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001503 if ((Property->getGetterName() == IMD->getSelector() ||
1504 Property->getSetterName() == IMD->getSelector()) &&
1505 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001506 return true;
1507 }
1508 return false;
1509}
1510
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001511static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1512 ObjCPropertyDecl *Prop) {
1513 bool SuperClassImplementsGetter = false;
1514 bool SuperClassImplementsSetter = false;
1515 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1516 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001517
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001518 while (IDecl->getSuperClass()) {
1519 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1520 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1521 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001522
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001523 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1524 SuperClassImplementsSetter = true;
1525 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1526 return true;
1527 IDecl = IDecl->getSuperClass();
1528 }
1529 return false;
1530}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001531
James Dennett2a4d13c2012-06-15 07:13:21 +00001532/// \brief Default synthesizes all properties which must be synthesized
1533/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001534void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1535 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001536
Anna Zaks673d76b2012-10-18 19:17:53 +00001537 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001538 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1539 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001540 if (PropMap.empty())
1541 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001542 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001543 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1544
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001545 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1546 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001547 // Is there a matching property synthesize/dynamic?
1548 if (Prop->isInvalidDecl() ||
1549 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1550 continue;
1551 // Property may have been synthesized by user.
1552 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1553 continue;
1554 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1555 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1556 continue;
1557 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1558 continue;
1559 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001560 if (ObjCPropertyImplDecl *PID =
1561 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001562 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1563 << Prop->getIdentifier();
1564 if (!PID->getLocation().isInvalid())
1565 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001566 continue;
1567 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001568 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001569 if (ObjCProtocolDecl *Proto =
1570 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001571 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001572 // Suppress the warning if class's superclass implements property's
1573 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001574 // Or, if property is going to be implemented in its super class.
1575 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001576 Diag(IMPDecl->getLocation(),
1577 diag::warn_auto_synthesizing_protocol_property)
1578 << Prop << Proto;
1579 Diag(Prop->getLocation(), diag::note_property_declare);
1580 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001581 continue;
1582 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001583 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001584 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001585 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1586 (PropInSuperClass->getPropertyAttributes() &
1587 ObjCPropertyDecl::OBJC_PR_readonly) &&
1588 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1589 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1590 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1591 << Prop->getIdentifier();
1592 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1593 }
1594 else {
1595 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1596 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001597 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001598 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1599 }
1600 continue;
1601 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001602 // We use invalid SourceLocations for the synthesized ivars since they
1603 // aren't really synthesized at a particular location; they just exist.
1604 // Saying that they are located at the @implementation isn't really going
1605 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001606 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1607 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1608 true,
1609 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001610 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001611 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001612 if (PIDecl) {
1613 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001614 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001615 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001616 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001617}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001618
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001619void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001620 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001621 return;
1622 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1623 if (!IC)
1624 return;
1625 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001626 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001627 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001628}
1629
Ted Kremenek7e812952014-02-21 19:41:30 +00001630static void DiagnoseUnimplementedAccessor(Sema &S,
1631 ObjCInterfaceDecl *PrimaryClass,
1632 Selector Method,
1633 ObjCImplDecl* IMPDecl,
1634 ObjCContainerDecl *CDecl,
1635 ObjCCategoryDecl *C,
1636 ObjCPropertyDecl *Prop,
1637 Sema::SelectorSet &SMap) {
1638 // When reporting on missing property setter/getter implementation in
1639 // categories, do not report when they are declared in primary class,
1640 // class's protocol, or one of it super classes. This is because,
1641 // the class is going to implement them.
1642 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001643 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001644 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1645 S.Diag(IMPDecl->getLocation(),
1646 isa<ObjCCategoryDecl>(CDecl) ?
1647 diag::warn_setter_getter_impl_required_in_category :
1648 diag::warn_setter_getter_impl_required)
1649 << Prop->getDeclName() << Method;
1650 S.Diag(Prop->getLocation(),
1651 diag::note_property_declare);
1652 if (S.LangOpts.ObjCDefaultSynthProperties &&
1653 S.LangOpts.ObjCRuntime.isNonFragile())
1654 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1655 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1656 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1657 }
1658}
1659
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001660void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001661 ObjCContainerDecl *CDecl,
1662 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001663 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001664 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1665
Ted Kremenek348e88c2014-02-21 19:41:34 +00001666 if (!SynthesizeProperties) {
1667 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001668 // Gather properties which need not be implemented in this class
1669 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001670 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001671 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1672 // For categories, no need to implement properties declared in
1673 // its primary class (and its super classes) if property is
1674 // declared in one of those containers.
1675 if ((IDecl = C->getClassInterface())) {
1676 ObjCInterfaceDecl::PropertyDeclOrder PO;
1677 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1678 }
1679 }
1680 if (IDecl)
1681 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1682
1683 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1684 }
1685
Ted Kremenek38882022014-02-21 19:41:39 +00001686 // Scan the @interface to see if any of the protocols it adopts
1687 // require an explicit implementation, via attribute
1688 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001689 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001690 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001691
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001692 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001693 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1694 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001695 // Lazily construct a set of all the properties in the @interface
1696 // of the class, without looking at the superclass. We cannot
1697 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001698 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001699 // as scans the adopted protocols. This work only triggers for protocols
1700 // with the attribute, which is very rare, and only occurs when
1701 // analyzing the @implementation.
1702 if (!LazyMap) {
1703 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1704 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1705 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1706 /* IncludeProtocols */ false);
1707 }
Ted Kremenek38882022014-02-21 19:41:39 +00001708 // Add the properties of 'PDecl' to the list of properties that
1709 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001710 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001711 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001712 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001713 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001714 }
1715 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001716 }
Ted Kremenek38882022014-02-21 19:41:39 +00001717
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001718 if (PropMap.empty())
1719 return;
1720
1721 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001722 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001723 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001724
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001725 SelectorSet InsMap;
1726 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001727 for (const auto *I : IMPDecl->instance_methods())
1728 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001729
1730 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001731 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001732 if (C && !C->IsClassExtension())
1733 if ((PrimaryClass = C->getClassInterface()))
1734 // Report unimplemented properties in the category as well.
1735 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1736 // When reporting on missing setter/getters, do not report when
1737 // setter/getter is implemented in category's primary class
1738 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001739 for (const auto *I : IMP->instance_methods())
1740 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001741 }
1742
Anna Zaks673d76b2012-10-18 19:17:53 +00001743 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001744 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1745 ObjCPropertyDecl *Prop = P->second;
1746 // Is there a matching propery synthesize/dynamic?
1747 if (Prop->isInvalidDecl() ||
1748 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001749 PropImplMap.count(Prop) ||
1750 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001751 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001752
1753 // Diagnose unimplemented getters and setters.
1754 DiagnoseUnimplementedAccessor(*this,
1755 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1756 if (!Prop->isReadOnly())
1757 DiagnoseUnimplementedAccessor(*this,
1758 PrimaryClass, Prop->getSetterName(),
1759 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001760 }
1761}
1762
1763void
1764Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1765 ObjCContainerDecl* IDecl) {
1766 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001767 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001768 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001769 for (const auto *Property : IDecl->properties()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001770 ObjCMethodDecl *GetterMethod = nullptr;
1771 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001772 bool LookedUpGetterSetter = false;
1773
Bill Wendling44426052012-12-20 19:22:21 +00001774 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001775 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001776
John McCall43192862011-09-13 18:31:23 +00001777 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1778 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001779 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1780 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1781 LookedUpGetterSetter = true;
1782 if (GetterMethod) {
1783 Diag(GetterMethod->getLocation(),
1784 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001785 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001786 Diag(Property->getLocation(), diag::note_property_declare);
1787 }
1788 if (SetterMethod) {
1789 Diag(SetterMethod->getLocation(),
1790 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001791 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001792 Diag(Property->getLocation(), diag::note_property_declare);
1793 }
1794 }
1795
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001796 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001797 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1798 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001799 continue;
1800 if (const ObjCPropertyImplDecl *PIDecl
1801 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1802 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1803 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001804 if (!LookedUpGetterSetter) {
1805 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1806 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001807 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001808 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1809 SourceLocation MethodLoc =
1810 (GetterMethod ? GetterMethod->getLocation()
1811 : SetterMethod->getLocation());
1812 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001813 << Property->getIdentifier() << (GetterMethod != nullptr)
1814 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001815 // fixit stuff.
1816 if (!AttributesAsWritten) {
1817 if (Property->getLParenLoc().isValid()) {
1818 // @property () ... case.
1819 SourceRange PropSourceRange(Property->getAtLoc(),
1820 Property->getLParenLoc());
1821 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1822 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1823 }
1824 else {
1825 //@property id etc.
1826 SourceLocation endLoc =
1827 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1828 endLoc = endLoc.getLocWithOffset(-1);
1829 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1830 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1831 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1832 }
1833 }
1834 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1835 // @property () ... case.
1836 SourceLocation endLoc = Property->getLParenLoc();
1837 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1838 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1839 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1840 }
1841 else
1842 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001843 Diag(Property->getLocation(), diag::note_property_declare);
1844 }
1845 }
1846 }
1847}
1848
John McCall31168b02011-06-15 23:02:42 +00001849void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001850 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001851 return;
1852
Aaron Ballmand85eff42014-03-14 15:02:45 +00001853 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001854 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001855 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1856 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001857 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1858 if (!method)
1859 continue;
1860 ObjCMethodFamily family = method->getMethodFamily();
1861 if (family == OMF_alloc || family == OMF_copy ||
1862 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001863 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001864 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001865 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001866 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001867
1868 // Look for a getter explicitly declared alongside the property.
1869 // If we find one, use its location for the note.
1870 SourceLocation noteLoc = PD->getLocation();
1871 SourceLocation fixItLoc;
1872 for (auto *getterRedecl : method->redecls()) {
1873 if (getterRedecl->isImplicit())
1874 continue;
1875 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1876 continue;
1877 noteLoc = getterRedecl->getLocation();
1878 fixItLoc = getterRedecl->getLocEnd();
1879 }
1880
1881 Preprocessor &PP = getPreprocessor();
1882 TokenValue tokens[] = {
1883 tok::kw___attribute, tok::l_paren, tok::l_paren,
1884 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1885 PP.getIdentifierInfo("none"), tok::r_paren,
1886 tok::r_paren, tok::r_paren
1887 };
1888 StringRef spelling = "__attribute__((objc_method_family(none)))";
1889 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1890 if (!macroName.empty())
1891 spelling = macroName;
1892
1893 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1894 << method->getDeclName() << spelling;
1895 if (fixItLoc.isValid()) {
1896 SmallString<64> fixItText(" ");
1897 fixItText += spelling;
1898 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1899 }
John McCall31168b02011-06-15 23:02:42 +00001900 }
1901 }
1902 }
1903}
1904
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001905void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001906 const ObjCImplementationDecl *ImplD,
1907 const ObjCInterfaceDecl *IFD) {
1908 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001909 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1910 if (!SuperD)
1911 return;
1912
1913 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001914 for (const auto *I : ImplD->instance_methods())
1915 if (I->getMethodFamily() == OMF_init)
1916 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001917
1918 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1919 SuperD->getDesignatedInitializers(DesignatedInits);
1920 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1921 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1922 const ObjCMethodDecl *MD = *I;
1923 if (!InitSelSet.count(MD->getSelector())) {
1924 Diag(ImplD->getLocation(),
1925 diag::warn_objc_implementation_missing_designated_init_override)
1926 << MD->getSelector();
1927 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1928 }
1929 }
1930}
1931
John McCallad31b5f2010-11-10 07:01:40 +00001932/// AddPropertyAttrs - Propagates attributes from a property to the
1933/// implicitly-declared getter or setter for that property.
1934static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1935 ObjCPropertyDecl *Property) {
1936 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001937 for (const auto *A : Property->attrs()) {
1938 if (isa<DeprecatedAttr>(A) ||
1939 isa<UnavailableAttr>(A) ||
1940 isa<AvailabilityAttr>(A))
1941 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001942 }
John McCallad31b5f2010-11-10 07:01:40 +00001943}
1944
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001945/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1946/// have the property type and issue diagnostics if they don't.
1947/// Also synthesize a getter/setter method if none exist (and update the
1948/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1949/// methods is the "right" thing to do.
1950void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001951 ObjCContainerDecl *CD,
1952 ObjCPropertyDecl *redeclaredProperty,
1953 ObjCContainerDecl *lexicalDC) {
1954
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001955 ObjCMethodDecl *GetterMethod, *SetterMethod;
1956
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00001957 if (CD->isInvalidDecl())
1958 return;
1959
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001960 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1961 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1962 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1963 property->getLocation());
1964
1965 if (SetterMethod) {
1966 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1967 property->getPropertyAttributes();
1968 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001969 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1970 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001971 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1972 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001973 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001974 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1975 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001976 Diag(property->getLocation(),
1977 diag::warn_accessor_property_type_mismatch)
1978 << property->getDeclName()
1979 << SetterMethod->getSelector();
1980 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1981 }
1982 }
1983
1984 // Synthesize getter/setter methods if none exist.
1985 // Find the default getter and if one not found, add one.
1986 // FIXME: The synthesized property we set here is misleading. We almost always
1987 // synthesize these methods unless the user explicitly provided prototypes
1988 // (which is odd, but allowed). Sema should be typechecking that the
1989 // declarations jive in that situation (which it is not currently).
1990 if (!GetterMethod) {
1991 // No instance method of same name as property getter name was found.
1992 // Declare a getter method and add it to the list of methods
1993 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001994 SourceLocation Loc = redeclaredProperty ?
1995 redeclaredProperty->getLocation() :
1996 property->getLocation();
1997
1998 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1999 property->getGetterName(),
Craig Topperc3ec1492014-05-26 06:22:03 +00002000 property->getType(), nullptr, CD,
2001 /*isInstance=*/true, /*isVariadic=*/false,
2002 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002003 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002004 (property->getPropertyImplementation() ==
2005 ObjCPropertyDecl::Optional) ?
2006 ObjCMethodDecl::Optional :
2007 ObjCMethodDecl::Required);
2008 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002009
2010 AddPropertyAttrs(*this, GetterMethod, property);
2011
Ted Kremenek49be9e02010-05-18 21:09:07 +00002012 // FIXME: Eventually this shouldn't be needed, as the lexical context
2013 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00002014 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002015 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002016 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002017 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2018 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002019
2020 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2021 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002022 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002023
2024 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002025 GetterMethod->addAttr(
2026 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2027 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002028
2029 if (getLangOpts().ObjCAutoRefCount)
2030 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002031 } else
2032 // A user declared getter will be synthesize when @synthesize of
2033 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002034 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002035 property->setGetterMethodDecl(GetterMethod);
2036
2037 // Skip setter if property is read-only.
2038 if (!property->isReadOnly()) {
2039 // Find the default setter and if one not found, add one.
2040 if (!SetterMethod) {
2041 // No instance method of same name as property setter name was found.
2042 // Declare a setter method and add it to the list of methods
2043 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002044 SourceLocation Loc = redeclaredProperty ?
2045 redeclaredProperty->getLocation() :
2046 property->getLocation();
2047
2048 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002049 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002050 property->getSetterName(), Context.VoidTy,
2051 nullptr, CD, /*isInstance=*/true,
2052 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002053 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002054 /*isImplicitlyDeclared=*/true,
2055 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002056 (property->getPropertyImplementation() ==
2057 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002058 ObjCMethodDecl::Optional :
2059 ObjCMethodDecl::Required);
2060
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002061 // Invent the arguments for the setter. We don't bother making a
2062 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002063 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2064 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002065 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002066 property->getType().getUnqualifiedType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00002067 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002068 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002069 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002070 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002071
2072 AddPropertyAttrs(*this, SetterMethod, property);
2073
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002074 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002075 // FIXME: Eventually this shouldn't be needed, as the lexical context
2076 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002077 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002078 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002079 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002080 SetterMethod->addAttr(
2081 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2082 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002083 // It's possible for the user to have set a very odd custom
2084 // setter selector that causes it to have a method family.
2085 if (getLangOpts().ObjCAutoRefCount)
2086 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002087 } else
2088 // A user declared setter will be synthesize when @synthesize of
2089 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002090 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002091 property->setSetterMethodDecl(SetterMethod);
2092 }
2093 // Add any synthesized methods to the global pool. This allows us to
2094 // handle the following, which is supported by GCC (and part of the design).
2095 //
2096 // @interface Foo
2097 // @property double bar;
2098 // @end
2099 //
2100 // void thisIsUnfortunate() {
2101 // id foo;
2102 // double bar = [foo bar];
2103 // }
2104 //
2105 if (GetterMethod)
2106 AddInstanceMethodToGlobalPool(GetterMethod);
2107 if (SetterMethod)
2108 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002109
2110 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2111 if (!CurrentClass) {
2112 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2113 CurrentClass = Cat->getClassInterface();
2114 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2115 CurrentClass = Impl->getClassInterface();
2116 }
2117 if (GetterMethod)
2118 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2119 if (SetterMethod)
2120 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002121}
2122
John McCall48871652010-08-21 09:40:31 +00002123void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002124 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002125 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002126 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002127 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002128 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002129 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002130
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002131 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2132 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "readonly" << "readwrite";
2135
2136 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2137 QualType PropertyTy = PropertyDecl->getType();
2138 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002139
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002140 // 'readonly' property with no obvious lifetime.
2141 // its life time will be determined by its backing ivar.
2142 if (getLangOpts().ObjCAutoRefCount &&
2143 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2144 PropertyTy->isObjCRetainableType() &&
2145 !PropertyOwnership)
2146 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147
2148 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002149 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002150 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2151 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002152 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002153 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002154 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2155 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2156 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002157 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002158 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002159 }
2160
2161 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002162 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2163 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002164 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2165 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002166 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002167 }
Bill Wendling44426052012-12-20 19:22:21 +00002168 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002169 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2170 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002171 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002172 }
Bill Wendling44426052012-12-20 19:22:21 +00002173 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002174 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2175 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002176 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002177 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002178 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002179 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002180 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2181 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002182 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002183 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002184 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002185 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002186 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2187 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002188 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2189 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002190 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002191 }
Bill Wendling44426052012-12-20 19:22:21 +00002192 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002193 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2194 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002195 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002196 }
Bill Wendling44426052012-12-20 19:22:21 +00002197 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002198 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2199 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002200 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002201 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002202 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002203 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002204 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2205 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002206 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002207 }
Bill Wendling44426052012-12-20 19:22:21 +00002208 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2209 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002210 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2211 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002212 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002213 }
Bill Wendling44426052012-12-20 19:22:21 +00002214 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002215 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2216 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002217 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002218 }
Bill Wendling44426052012-12-20 19:22:21 +00002219 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002220 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2221 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002222 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002223 }
2224 }
Bill Wendling44426052012-12-20 19:22:21 +00002225 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2226 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002227 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2228 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002229 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002230 }
Bill Wendling44426052012-12-20 19:22:21 +00002231 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2232 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002233 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2234 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002235 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002236 }
2237
Douglas Gregor813a0662015-06-19 18:14:38 +00002238 if ((Attributes & ObjCDeclSpec::DQ_PR_weak) &&
2239 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
2240 // 'weak' and 'nonnull' are mutually exclusive.
2241 if (auto nullability = PropertyTy->getNullability(Context)) {
2242 if (*nullability == NullabilityKind::NonNull)
2243 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2244 << "nonnull" << "weak";
2245 } else {
2246 PropertyTy =
2247 Context.getAttributedType(
2248 AttributedType::getNullabilityAttrKind(NullabilityKind::Nullable),
2249 PropertyTy, PropertyTy);
2250 TypeSourceInfo *TSInfo = PropertyDecl->getTypeSourceInfo();
2251 PropertyDecl->setType(PropertyTy, TSInfo);
2252 }
2253 }
2254
Bill Wendling44426052012-12-20 19:22:21 +00002255 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2256 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002257 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2258 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002259 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002260 }
2261
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002262 // Warn if user supplied no assignment attribute, property is
2263 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002264 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002265 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2266 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2267 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002268 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002269 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002270 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002271 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002272 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002273 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002274 bool isAnyClassTy =
2275 (PropertyTy->isObjCClassType() ||
2276 PropertyTy->isObjCQualifiedClassType());
2277 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2278 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002279 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002280 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002281 else if (propertyInPrimaryClass) {
2282 // Don't issue warning on property with no life time in class
2283 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002284 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002285 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002286 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002287
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002288 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002289 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002290 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002291 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002292 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002293
2294 // FIXME: Implement warning dependent on NSCopying being
2295 // implemented. See also:
2296 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2297 // (please trim this list while you are at it).
2298 }
2299
Bill Wendling44426052012-12-20 19:22:21 +00002300 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2301 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002302 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002303 && PropertyTy->isBlockPointerType())
2304 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002305 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2306 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2307 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002308 PropertyTy->isBlockPointerType())
2309 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002310
Bill Wendling44426052012-12-20 19:22:21 +00002311 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2312 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002313 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2314
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002315}