blob: f139c83c734bd0ca655bd56c97fc1d2c5db1ffab [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
Douglas Gregorb8982092013-01-21 19:42:21 +0000106/// \brief Check this Objective-C property against a property declared in the
107/// given protocol.
108static void
109CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
110 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000111 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000112 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000113 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000114 return;
115
116 // Look for a property with the same name.
117 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
118 for (unsigned I = 0, N = R.size(); I != N; ++I) {
119 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000120 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000121 return;
122 }
123 }
124
125 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000126 for (auto *P : Proto->protocols())
127 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000128}
129
John McCall48871652010-08-21 09:40:31 +0000130Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000131 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000132 FieldDeclarator &FD,
133 ObjCDeclSpec &ODS,
134 Selector GetterSel,
135 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000136 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000137 tok::ObjCKeywordKind MethodImplKind,
138 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000139 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000140 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000141 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
142 QualType T = TSI->getType();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000143 Attributes |= deduceWeakPropertyFromType(T);
Bill Wendling44426052012-12-20 19:22:21 +0000144 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000145 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000146 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000147 // property is defaulted to 'assign' if it is readwrite and is
148 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000149 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000150 (isReadWrite &&
Bill Wendling44426052012-12-20 19:22:21 +0000151 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
152 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
153 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
154 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
155 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000156
Douglas Gregor90d34422013-01-21 19:05:22 +0000157 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000158 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000159 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000160 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000161 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000162 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000163 FD, GetterSel, SetterSel,
164 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000165 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000166 ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000167 isOverridingProperty, T, TSI,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000168 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000169 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000170 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000171 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000172 }
173
174 if (!Res) {
175 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
176 GetterSel, SetterSel, isAssign, isReadWrite,
177 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000178 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000179 if (lexicalDC)
180 Res->setLexicalDeclContext(lexicalDC);
181 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000182
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000183 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000184 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000185 (isa<ObjCInterfaceDecl>(ClassDecl) ||
186 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000187
David Blaikiebbafb8a2012-03-11 07:00:24 +0000188 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000189 checkARCPropertyDecl(*this, Res);
190
Douglas Gregorb8982092013-01-21 19:42:21 +0000191 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000192 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000193 // For a class, compare the property against a property in our superclass.
194 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000195 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
196 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000198 for (unsigned I = 0, N = R.size(); I != N; ++I) {
199 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000200 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000201 FoundInSuper = true;
202 break;
203 }
204 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000205 if (FoundInSuper)
206 break;
207 else
208 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000209 }
210
211 if (FoundInSuper) {
212 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000213 for (auto *P : CurrentInterfaceDecl->protocols()) {
214 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000215 }
216 } else {
217 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000218 for (auto *P : IFace->all_referenced_protocols()) {
219 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000220 }
221 }
222 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Aaron Ballman19a41762014-03-14 12:55:57 +0000223 for (auto *P : Cat->protocols())
224 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000225 } else {
226 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000227 for (auto *P : Proto->protocols())
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000229 }
230
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000231 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000232 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000233}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000234
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000235static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000236makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000237 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000238 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000239 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000240 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000241 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000242 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000243 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000244 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000245 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000246 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000247 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000248 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000249 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
262
263 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
264}
265
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000266static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000267 SourceLocation LParenLoc, SourceLocation &Loc) {
268 if (LParenLoc.isMacroID())
269 return false;
270
271 SourceManager &SM = Context.getSourceManager();
272 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
273 // Try to load the file buffer.
274 bool invalidTemp = false;
275 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
276 if (invalidTemp)
277 return false;
278 const char *tokenBegin = file.data() + locInfo.second;
279
280 // Lex from the start of the given location.
281 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
282 Context.getLangOpts(),
283 file.begin(), tokenBegin, file.end());
284 Token Tok;
285 do {
286 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000287 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000288 Loc = Tok.getLocation();
289 return true;
290 }
291 } while (Tok.isNot(tok::r_paren));
292 return false;
293
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000294}
295
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000296static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000297 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
298 ObjCPropertyDecl::OBJC_PR_retain |
299 ObjCPropertyDecl::OBJC_PR_copy |
300 ObjCPropertyDecl::OBJC_PR_weak |
301 ObjCPropertyDecl::OBJC_PR_strong |
302 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
303}
304
Douglas Gregor90d34422013-01-21 19:05:22 +0000305ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000306Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000307 SourceLocation AtLoc,
308 SourceLocation LParenLoc,
309 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000310 Selector GetterSel, Selector SetterSel,
311 const bool isAssign,
312 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000313 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000314 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000315 bool *isOverridingProperty,
Douglas Gregor813a0662015-06-19 18:14:38 +0000316 QualType T,
317 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000318 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000319 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000320 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000321 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000322 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000323 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
324
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000325 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000326 // Check for duplicate declaration of this property in current and
327 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000328 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000329 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000330 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000331 Diag(AtLoc, diag::err_duplicate_property);
332 Diag(prevDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000333 return nullptr;
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000334 }
335 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000336 }
337
Ted Kremenek959e8302010-03-12 02:31:10 +0000338 // Create a new ObjCPropertyDecl with the DeclContext being
339 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000340 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000341 ObjCPropertyDecl *PDecl =
342 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000343 PropertyId, AtLoc, LParenLoc, T, TSI);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000344 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000345 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000346 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000347 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000348 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000349 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000350 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
351 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
352 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
353 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Douglas Gregor813a0662015-06-19 18:14:38 +0000354 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000356 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
358
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000359 // Set setter/getter selector name. Needed later.
360 PDecl->setGetterName(GetterSel);
361 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000362 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000363 DC->addDecl(PDecl);
364
365 // We need to look in the @interface to see if the @property was
366 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000367 if (!CCPrimary) {
368 Diag(CDecl->getLocation(), diag::err_continuation_class);
369 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000370 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000371 }
372
373 // Find the property in continuation class's primary class only.
374 ObjCPropertyDecl *PIDecl =
375 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
376
377 if (!PIDecl) {
378 // No matching property found in the primary class. Just fall thru
379 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000380 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000381 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000382 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Douglas Gregor813a0662015-06-19 18:14:38 +0000383 Attributes,AttributesAsWritten, T, TSI, MethodImplKind,
384 DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000385
386 // A case of continuation class adding a new property in the class. This
387 // is not what it was meant for. However, gcc supports it and so should we.
388 // Make sure setter/getters are declared here.
Craig Topperc3ec1492014-05-26 06:22:03 +0000389 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
390 /* redeclaredProperty = */ nullptr,
Ted Kremenek2f075632010-09-21 20:52:59 +0000391 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000392 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
393 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000394 if (ASTMutationListener *L = Context.getASTMutationListener())
Craig Topperc3ec1492014-05-26 06:22:03 +0000395 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
396 CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000397 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000398 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000399 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
400 bool IncompatibleObjC = false;
401 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000402 // Relax the strict type matching for property type in continuation class.
403 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000404 // as it narrows the object type in its primary class property. Note that
405 // this conversion is safe only because the wider type is for a 'readonly'
406 // property in primary class and 'narrowed' type for a 'readwrite' property
407 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000408 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
409 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
410 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
411 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
412 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000413 ConvertedType, IncompatibleObjC))
414 || IncompatibleObjC) {
415 Diag(AtLoc,
416 diag::err_type_mismatch_continuation_class) << PDecl->getType();
417 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000418 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000419 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000420 }
421
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 // The property 'PIDecl's readonly attribute will be over-ridden
423 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000424 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000425 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000426 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
427 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000428 PIkind |= deduceWeakPropertyFromType(PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000429 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000430 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000431 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
432 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 Diag(AtLoc, diag::warn_property_attr_mismatch);
434 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000435 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000436 else if (getLangOpts().ObjCAutoRefCount) {
437 QualType PrimaryPropertyQT =
438 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
439 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000440 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000441 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
442 PrimaryPropertyQT.getObjCLifetime();
443 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000444 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
445 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000446 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
447 Diag(PIDecl->getLocation(), diag::note_property_declare);
448 }
449 }
450 }
451
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000452 DeclContext *DC = cast<DeclContext>(CCPrimary);
453 if (!ObjCPropertyDecl::findPropertyDecl(DC,
454 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000455 // In mrr mode, 'readwrite' property must have an explicit
456 // memory attribute. If none specified, select the default (assign).
457 if (!getLangOpts().ObjCAutoRefCount) {
458 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
459 ObjCDeclSpec::DQ_PR_retain |
460 ObjCDeclSpec::DQ_PR_strong |
461 ObjCDeclSpec::DQ_PR_copy |
462 ObjCDeclSpec::DQ_PR_unsafe_unretained |
463 ObjCDeclSpec::DQ_PR_weak)))
464 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
465 }
466
Ted Kremenek959e8302010-03-12 02:31:10 +0000467 // Protocol is not in the primary class. Must build one for it.
468 ObjCDeclSpec ProtocolPropertyODS;
469 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
470 // and ObjCPropertyDecl::PropertyAttributeKind have identical
471 // values. Should consolidate both into one enum type.
472 ProtocolPropertyODS.
473 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
474 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000475 // Must re-establish the context from class extension to primary
476 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000477 ContextRAII SavedContext(*this, CCPrimary);
478
John McCall48871652010-08-21 09:40:31 +0000479 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000480 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000481 PIDecl->getGetterName(),
482 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000483 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000484 MethodImplKind,
485 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000486 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000487 }
488 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000489 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000490 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000494 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
495 PIDecl->setSetterName(SetterSel);
496 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000497 // Tailor the diagnostics for the common case where a readwrite
498 // property is declared both in the @interface and the continuation.
499 // This is a common error where the user often intended the original
500 // declaration to be readonly.
501 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000502 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000503 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
504 ? diag::err_use_continuation_class_redeclaration_readwrite
505 : diag::err_use_continuation_class;
506 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000507 << CCPrimary->getDeclName();
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000509 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000510 }
511 *isOverridingProperty = true;
512 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000513 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000514 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
515 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000516 if (ASTMutationListener *L = Context.getASTMutationListener())
517 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000518 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000519}
520
521ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
522 ObjCContainerDecl *CDecl,
523 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000524 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000525 FieldDeclarator &FD,
526 Selector GetterSel,
527 Selector SetterSel,
528 const bool isAssign,
529 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000530 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000531 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000532 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000533 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000534 tok::ObjCKeywordKind MethodImplKind,
535 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000536 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000537
538 // Issue a warning if property is 'assign' as default and its object, which is
539 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000540 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000541 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000542 if (const ObjCObjectPointerType *ObjPtrTy =
543 T->getAs<ObjCObjectPointerType>()) {
544 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
545 if (IDecl)
546 if (ObjCProtocolDecl* PNSCopying =
547 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
548 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
549 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000550 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000551
552 if (T->isObjCObjectType()) {
553 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000554 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000555 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
556 << FixItHint::CreateInsertion(StarLoc, "*");
557 T = Context.getObjCObjectPointerType(T);
558 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
559 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
560 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000561
Ted Kremenek959e8302010-03-12 02:31:10 +0000562 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000563 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
564 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000565 PropertyId, AtLoc,
566 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000567
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000568 if (ObjCPropertyDecl *prevDecl =
569 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000570 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000571 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000572 PDecl->setInvalidDecl();
573 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000574 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000575 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000576 if (lexicalDC)
577 PDecl->setLexicalDeclContext(lexicalDC);
578 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000579
580 if (T->isArrayType() || T->isFunctionType()) {
581 Diag(AtLoc, diag::err_property_type) << T;
582 PDecl->setInvalidDecl();
583 }
584
585 ProcessDeclAttributes(S, PDecl, FD.D);
586
587 // Regardless of setter/getter attribute, we save the default getter/setter
588 // selector names in anticipation of declaration of setter/getter methods.
589 PDecl->setGetterName(GetterSel);
590 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000591 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000592 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000593
Bill Wendling44426052012-12-20 19:22:21 +0000594 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
596
Bill Wendling44426052012-12-20 19:22:21 +0000597 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
599
Bill Wendling44426052012-12-20 19:22:21 +0000600 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
602
603 if (isReadWrite)
604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
605
Bill Wendling44426052012-12-20 19:22:21 +0000606 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
608
Bill Wendling44426052012-12-20 19:22:21 +0000609 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
611
Bill Wendling44426052012-12-20 19:22:21 +0000612 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
614
Bill Wendling44426052012-12-20 19:22:21 +0000615 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
617
Bill Wendling44426052012-12-20 19:22:21 +0000618 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
620
Ted Kremenekac597f32010-03-12 00:46:40 +0000621 if (isAssign)
622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
623
John McCall43192862011-09-13 18:31:23 +0000624 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000625 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000627 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000629
John McCall31168b02011-06-15 23:02:42 +0000630 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000631 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
633 if (isAssign)
634 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
635
Ted Kremenekac597f32010-03-12 00:46:40 +0000636 if (MethodImplKind == tok::objc_required)
637 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
638 else if (MethodImplKind == tok::objc_optional)
639 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000640
Douglas Gregor813a0662015-06-19 18:14:38 +0000641 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
643
Douglas Gregor849ebc22015-06-19 18:14:46 +0000644 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
646
Ted Kremenek959e8302010-03-12 02:31:10 +0000647 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000648}
649
John McCall31168b02011-06-15 23:02:42 +0000650static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
651 ObjCPropertyDecl *property,
652 ObjCIvarDecl *ivar) {
653 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
654
John McCall31168b02011-06-15 23:02:42 +0000655 QualType ivarType = ivar->getType();
656 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000657
John McCall43192862011-09-13 18:31:23 +0000658 // The lifetime implied by the property's attributes.
659 Qualifiers::ObjCLifetime propertyLifetime =
660 getImpliedARCOwnership(property->getPropertyAttributes(),
661 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000662
John McCall43192862011-09-13 18:31:23 +0000663 // We're fine if they match.
664 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000665
John McCall43192862011-09-13 18:31:23 +0000666 // These aren't valid lifetimes for object ivars; don't diagnose twice.
667 if (ivarLifetime == Qualifiers::OCL_None ||
668 ivarLifetime == Qualifiers::OCL_Autoreleasing)
669 return;
John McCall31168b02011-06-15 23:02:42 +0000670
John McCalld8561f02012-08-20 23:36:59 +0000671 // If the ivar is private, and it's implicitly __unsafe_unretained
672 // becaues of its type, then pretend it was actually implicitly
673 // __strong. This is only sound because we're processing the
674 // property implementation before parsing any method bodies.
675 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
676 propertyLifetime == Qualifiers::OCL_Strong &&
677 ivar->getAccessControl() == ObjCIvarDecl::Private) {
678 SplitQualType split = ivarType.split();
679 if (split.Quals.hasObjCLifetime()) {
680 assert(ivarType->isObjCARCImplicitlyUnretainedType());
681 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
682 ivarType = S.Context.getQualifiedType(split);
683 ivar->setType(ivarType);
684 return;
685 }
686 }
687
John McCall43192862011-09-13 18:31:23 +0000688 switch (propertyLifetime) {
689 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000690 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000691 << property->getDeclName()
692 << ivar->getDeclName()
693 << ivarLifetime;
694 break;
John McCall31168b02011-06-15 23:02:42 +0000695
John McCall43192862011-09-13 18:31:23 +0000696 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000697 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000698 << property->getDeclName()
699 << ivar->getDeclName();
700 break;
John McCall31168b02011-06-15 23:02:42 +0000701
John McCall43192862011-09-13 18:31:23 +0000702 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000703 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000704 << property->getDeclName()
705 << ivar->getDeclName()
706 << ((property->getPropertyAttributesAsWritten()
707 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
708 break;
John McCall31168b02011-06-15 23:02:42 +0000709
John McCall43192862011-09-13 18:31:23 +0000710 case Qualifiers::OCL_Autoreleasing:
711 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000712
John McCall43192862011-09-13 18:31:23 +0000713 case Qualifiers::OCL_None:
714 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000715 return;
716 }
717
718 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000719 if (propertyImplLoc.isValid())
720 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000721}
722
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000723/// setImpliedPropertyAttributeForReadOnlyProperty -
724/// This routine evaludates life-time attributes for a 'readonly'
725/// property with no known lifetime of its own, using backing
726/// 'ivar's attribute, if any. If no backing 'ivar', property's
727/// life-time is assumed 'strong'.
728static void setImpliedPropertyAttributeForReadOnlyProperty(
729 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
730 Qualifiers::ObjCLifetime propertyLifetime =
731 getImpliedARCOwnership(property->getPropertyAttributes(),
732 property->getType());
733 if (propertyLifetime != Qualifiers::OCL_None)
734 return;
735
736 if (!ivar) {
737 // if no backing ivar, make property 'strong'.
738 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
739 return;
740 }
741 // property assumes owenership of backing ivar.
742 QualType ivarType = ivar->getType();
743 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
744 if (ivarLifetime == Qualifiers::OCL_Strong)
745 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
746 else if (ivarLifetime == Qualifiers::OCL_Weak)
747 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
748 return;
749}
Ted Kremenekac597f32010-03-12 00:46:40 +0000750
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000751/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
752/// in inherited protocols with mismatched types. Since any of them can
753/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000754static void
755DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
756 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000757 ObjCPropertyDecl *Property) {
758 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000759 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
760 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000761 PDecl->collectInheritedProtocolProperties(Property, PropMap);
762 }
763 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
764 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000765 for (const auto *PI : SDecl->all_referenced_protocols()) {
766 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000767 PDecl->collectInheritedProtocolProperties(Property, PropMap);
768 }
769 SDecl = SDecl->getSuperClass();
770 }
771
772 if (PropMap.empty())
773 return;
774
775 QualType RHSType = S.Context.getCanonicalType(Property->getType());
776 bool FirsTime = true;
777 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
778 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
779 ObjCPropertyDecl *Prop = I->second;
780 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
781 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
782 bool IncompatibleObjC = false;
783 QualType ConvertedType;
784 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
785 || IncompatibleObjC) {
786 if (FirsTime) {
787 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
788 << Property->getType();
789 FirsTime = false;
790 }
791 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
792 << Prop->getType();
793 }
794 }
795 }
796 if (!FirsTime && AtLoc.isValid())
797 S.Diag(AtLoc, diag::note_property_synthesize);
798}
799
Ted Kremenekac597f32010-03-12 00:46:40 +0000800/// ActOnPropertyImplDecl - This routine performs semantic checks and
801/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000802/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000803///
John McCall48871652010-08-21 09:40:31 +0000804Decl *Sema::ActOnPropertyImplDecl(Scope *S,
805 SourceLocation AtLoc,
806 SourceLocation PropertyLoc,
807 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000808 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000809 IdentifierInfo *PropertyIvar,
810 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000811 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000812 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000813 // Make sure we have a context for the property implementation declaration.
814 if (!ClassImpDecl) {
815 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000816 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000817 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000818 if (PropertyIvarLoc.isInvalid())
819 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000820 SourceLocation PropertyDiagLoc = PropertyLoc;
821 if (PropertyDiagLoc.isInvalid())
822 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000823 ObjCPropertyDecl *property = nullptr;
824 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000825 // Find the class or category class where this property must have
826 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000827 ObjCImplementationDecl *IC = nullptr;
828 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000829 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
830 IDecl = IC->getClassInterface();
831 // We always synthesize an interface for an implementation
832 // without an interface decl. So, IDecl is always non-zero.
833 assert(IDecl &&
834 "ActOnPropertyImplDecl - @implementation without @interface");
835
836 // Look for this property declaration in the @implementation's @interface
837 property = IDecl->FindPropertyDeclaration(PropertyId);
838 if (!property) {
839 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000840 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000841 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000842 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000843 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
844 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000845 if (AtLoc.isValid())
846 Diag(AtLoc, diag::warn_implicit_atomic_property);
847 else
848 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
849 Diag(property->getLocation(), diag::note_property_declare);
850 }
851
Ted Kremenekac597f32010-03-12 00:46:40 +0000852 if (const ObjCCategoryDecl *CD =
853 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
854 if (!CD->IsClassExtension()) {
855 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
856 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000857 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000858 }
859 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000860 if (Synthesize&&
861 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
862 property->hasAttr<IBOutletAttr>() &&
863 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000864 bool ReadWriteProperty = false;
865 // Search into the class extensions and see if 'readonly property is
866 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000867 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000868 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
869 if (!R.empty())
870 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
871 PIkind = ExtProp->getPropertyAttributesAsWritten();
872 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
873 ReadWriteProperty = true;
874 break;
875 }
876 }
877 }
878
879 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000880 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000881 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000882 SourceLocation readonlyLoc;
883 if (LocPropertyAttribute(Context, "readonly",
884 property->getLParenLoc(), readonlyLoc)) {
885 SourceLocation endLoc =
886 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
887 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
888 Diag(property->getLocation(),
889 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
890 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
891 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000892 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000893 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000894 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
895 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000896
Ted Kremenekac597f32010-03-12 00:46:40 +0000897 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
898 if (Synthesize) {
899 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000900 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000901 }
902 IDecl = CatImplClass->getClassInterface();
903 if (!IDecl) {
904 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000905 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000906 }
907 ObjCCategoryDecl *Category =
908 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
909
910 // If category for this implementation not found, it is an error which
911 // has already been reported eralier.
912 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000913 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000914 // Look for this property declaration in @implementation's category
915 property = Category->FindPropertyDeclaration(PropertyId);
916 if (!property) {
917 Diag(PropertyLoc, diag::error_bad_category_property_decl)
918 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000919 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000920 }
921 } else {
922 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000923 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000924 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000925 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000926 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000927 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000928 // Check that we have a valid, previously declared ivar for @synthesize
929 if (Synthesize) {
930 // @synthesize
931 if (!PropertyIvar)
932 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000933 // Check that this is a previously declared 'ivar' in 'IDecl' interface
934 ObjCInterfaceDecl *ClassDeclared;
935 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
936 QualType PropType = property->getType();
937 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000938
939 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000940 diag::err_incomplete_synthesized_property,
941 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000942 Diag(property->getLocation(), diag::note_property_declare);
943 CompleteTypeErr = true;
944 }
945
David Blaikiebbafb8a2012-03-11 07:00:24 +0000946 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000947 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000948 ObjCPropertyDecl::OBJC_PR_readonly) &&
949 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000950 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
951 }
952
John McCall31168b02011-06-15 23:02:42 +0000953 ObjCPropertyDecl::PropertyAttributeKind kind
954 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000955
956 // Add GC __weak to the ivar type if the property is weak.
957 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000958 getLangOpts().getGC() != LangOptions::NonGC) {
959 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000960 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000961 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000962 Diag(property->getLocation(), diag::note_property_declare);
963 } else {
964 PropertyIvarType =
965 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000966 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000967 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000968 if (AtLoc.isInvalid()) {
969 // Check when default synthesizing a property that there is
970 // an ivar matching property name and issue warning; since this
971 // is the most common case of not using an ivar used for backing
972 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +0000973 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000974 ObjCIvarDecl *originalIvar =
975 IDecl->lookupInstanceVariable(property->getIdentifier(),
976 ClassDeclared);
977 if (originalIvar) {
978 Diag(PropertyDiagLoc,
979 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +0000980 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000981 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000982 Diag(property->getLocation(), diag::note_property_declare);
983 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000984 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000985 }
986
987 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000988 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000989 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000990 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000991 !PropertyIvarType.getObjCLifetime() &&
992 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000993
John McCall43192862011-09-13 18:31:23 +0000994 // It's an error if we have to do this and the user didn't
995 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000996 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000997 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000998 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000999 diag::err_arc_objc_property_default_assign_on_object);
1000 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001001 } else {
1002 Qualifiers::ObjCLifetime lifetime =
1003 getImpliedARCOwnership(kind, PropertyIvarType);
1004 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001005 if (lifetime == Qualifiers::OCL_Weak) {
1006 bool err = false;
1007 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001008 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1009 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1010 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001011 Diag(property->getLocation(),
1012 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1013 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1014 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001015 err = true;
1016 }
Richard Smith802c4b72012-08-23 06:16:52 +00001017 }
John McCall3deb1ad2012-08-21 02:47:43 +00001018 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001019 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001020 Diag(property->getLocation(), diag::note_property_declare);
1021 }
John McCall31168b02011-06-15 23:02:42 +00001022 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001023
John McCall31168b02011-06-15 23:02:42 +00001024 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001025 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001026 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1027 }
John McCall31168b02011-06-15 23:02:42 +00001028 }
1029
1030 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001031 !getLangOpts().ObjCAutoRefCount &&
1032 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001033 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001034 Diag(property->getLocation(), diag::note_property_declare);
1035 }
1036
Abramo Bagnaradff19302011-03-08 08:55:46 +00001037 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001038 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001039 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001040 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001041 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001042 if (RequireNonAbstractType(PropertyIvarLoc,
1043 PropertyIvarType,
1044 diag::err_abstract_type_in_decl,
1045 AbstractSynthesizedIvarType)) {
1046 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001047 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001048 } else if (CompleteTypeErr)
1049 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001050 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001051 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001052
John McCall5fb5df92012-06-20 06:18:46 +00001053 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001054 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1055 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001056 // Note! I deliberately want it to fall thru so, we have a
1057 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001058 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001059 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001060 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001061 << property->getDeclName() << Ivar->getDeclName()
1062 << ClassDeclared->getDeclName();
1063 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001064 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001065 // Note! I deliberately want it to fall thru so more errors are caught.
1066 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001067 property->setPropertyIvarDecl(Ivar);
1068
Ted Kremenekac597f32010-03-12 00:46:40 +00001069 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1070
1071 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001072 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001073 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001074 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001075 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001076 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001077 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001078 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001079 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001080 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1081 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001082 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001083 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001084 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001085 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001086 << property->getDeclName() << PropType
1087 << Ivar->getDeclName() << IvarType;
1088 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001089 // Note! I deliberately want it to fall thru so, we have a
1090 // a property implementation and to avoid future warnings.
1091 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001092 else {
1093 // FIXME! Rules for properties are somewhat different that those
1094 // for assignments. Use a new routine to consolidate all cases;
1095 // specifically for property redeclarations as well as for ivars.
1096 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1097 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1098 if (lhsType != rhsType &&
1099 lhsType->isArithmeticType()) {
1100 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1101 << property->getDeclName() << PropType
1102 << Ivar->getDeclName() << IvarType;
1103 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1104 // Fall thru - see previous comment
1105 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001106 }
1107 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001108 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001109 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001110 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001112 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001113 // Fall thru - see previous comment
1114 }
John McCall31168b02011-06-15 23:02:42 +00001115 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001116 if ((property->getType()->isObjCObjectPointerType() ||
1117 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001118 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001119 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001120 << property->getDeclName() << Ivar->getDeclName();
1121 // Fall thru - see previous comment
1122 }
1123 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001124 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001125 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001126 } else if (PropertyIvar)
1127 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001128 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001129
Ted Kremenekac597f32010-03-12 00:46:40 +00001130 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1131 ObjCPropertyImplDecl *PIDecl =
1132 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1133 property,
1134 (Synthesize ?
1135 ObjCPropertyImplDecl::Synthesize
1136 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001137 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001138
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001139 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001140 PIDecl->setInvalidDecl();
1141
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001142 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1143 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001144 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001145 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001146 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1147 // returned by the getter as it must conform to C++'s copy-return rules.
1148 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001149 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001150 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1151 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001152 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001153 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001154 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001155 Expr *LoadSelfExpr =
1156 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001157 CK_LValueToRValue, SelfExpr, nullptr,
1158 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001159 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001160 new (Context) ObjCIvarRefExpr(Ivar,
1161 Ivar->getUsageType(SelfDecl->getType()),
1162 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 =
Douglas Gregore83b9562015-07-07 03:57:53 +00001212 new (Context) ObjCIvarRefExpr(Ivar,
1213 Ivar->getUsageType(SelfDecl->getType()),
1214 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001215 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001216 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001217 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1218 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001219 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001220 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1221 VK_LValue, PropertyDiagLoc);
1222 MarkDeclRefReferenced(rhs);
1223 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001224 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001225 if (property->getPropertyAttributes() &
1226 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001227 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001228 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001229 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1230 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001231 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001232 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001233 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001234 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001235 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001236 Diag(FuncDecl->getLocStart(),
1237 diag::note_callee_decl) << FuncDecl;
1238 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001239 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001240 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001241 }
1242 }
1243
Ted Kremenekac597f32010-03-12 00:46:40 +00001244 if (IC) {
1245 if (Synthesize)
1246 if (ObjCPropertyImplDecl *PPIDecl =
1247 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1248 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1249 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1250 << PropertyIvar;
1251 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1252 }
1253
1254 if (ObjCPropertyImplDecl *PPIDecl
1255 = IC->FindPropertyImplDecl(PropertyId)) {
1256 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1257 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001258 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001259 }
1260 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001261 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001262 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001263 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001264 // Diagnose if an ivar was lazily synthesdized due to a previous
1265 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001266 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001267 ObjCInterfaceDecl *ClassDeclared=nullptr;
1268 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001269 if (!Synthesize)
1270 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1271 else {
1272 if (PropertyIvar && PropertyIvar != PropertyId)
1273 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1274 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001275 // Issue diagnostics only if Ivar belongs to current class.
1276 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001277 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001278 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1279 << PropertyId;
1280 Ivar->setInvalidDecl();
1281 }
1282 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001283 } else {
1284 if (Synthesize)
1285 if (ObjCPropertyImplDecl *PPIDecl =
1286 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001287 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001288 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1289 << PropertyIvar;
1290 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1291 }
1292
1293 if (ObjCPropertyImplDecl *PPIDecl =
1294 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001295 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001296 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001297 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001298 }
1299 CatImplClass->addPropertyImplementation(PIDecl);
1300 }
1301
John McCall48871652010-08-21 09:40:31 +00001302 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001303}
1304
1305//===----------------------------------------------------------------------===//
1306// Helper methods.
1307//===----------------------------------------------------------------------===//
1308
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001309/// DiagnosePropertyMismatch - Compares two properties for their
1310/// attributes and types and warns on a variety of inconsistencies.
1311///
1312void
1313Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1314 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001315 const IdentifierInfo *inheritedName,
1316 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001317 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001318 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001319 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001320 SuperProperty->getPropertyAttributes();
1321
1322 // We allow readonly properties without an explicit ownership
1323 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1324 // to be overridden by a property with any explicit ownership in the subclass.
1325 if (!OverridingProtocolProperty &&
1326 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1327 ;
1328 else {
1329 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1330 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1331 Diag(Property->getLocation(), diag::warn_readonly_property)
1332 << Property->getDeclName() << inheritedName;
1333 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1334 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001335 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001336 << Property->getDeclName() << "copy" << inheritedName;
1337 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1338 unsigned CAttrRetain =
1339 (CAttr &
1340 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1341 unsigned SAttrRetain =
1342 (SAttr &
1343 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1344 bool CStrong = (CAttrRetain != 0);
1345 bool SStrong = (SAttrRetain != 0);
1346 if (CStrong != SStrong)
1347 Diag(Property->getLocation(), diag::warn_property_attribute)
1348 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1349 }
John McCall31168b02011-06-15 23:02:42 +00001350 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001351
1352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001354 Diag(Property->getLocation(), diag::warn_property_attribute)
1355 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001356 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1357 }
1358 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001359 Diag(Property->getLocation(), diag::warn_property_attribute)
1360 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001361 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1362 }
1363 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001364 Diag(Property->getLocation(), diag::warn_property_attribute)
1365 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001366 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1367 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001368
1369 QualType LHSType =
1370 Context.getCanonicalType(SuperProperty->getType());
1371 QualType RHSType =
1372 Context.getCanonicalType(Property->getType());
1373
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001374 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001375 // Do cases not handled in above.
1376 // FIXME. For future support of covariant property types, revisit this.
1377 bool IncompatibleObjC = false;
1378 QualType ConvertedType;
1379 if (!isObjCPointerConversion(RHSType, LHSType,
1380 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001381 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001382 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1383 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001384 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1385 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001386 }
1387}
1388
1389bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1390 ObjCMethodDecl *GetterMethod,
1391 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001392 if (!GetterMethod)
1393 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001394 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001395 QualType PropertyIvarType = property->getType().getNonReferenceType();
1396 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1397 if (!compat) {
1398 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1399 isa<ObjCObjectPointerType>(GetterType))
1400 compat =
1401 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001402 GetterType->getAs<ObjCObjectPointerType>(),
1403 PropertyIvarType->getAs<ObjCObjectPointerType>());
1404 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001405 != Compatible) {
1406 Diag(Loc, diag::error_property_accessor_type)
1407 << property->getDeclName() << PropertyIvarType
1408 << GetterMethod->getSelector() << GetterType;
1409 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1410 return true;
1411 } else {
1412 compat = true;
1413 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1414 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1415 if (lhsType != rhsType && lhsType->isArithmeticType())
1416 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001417 }
1418 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001419
1420 if (!compat) {
1421 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1422 << property->getDeclName()
1423 << GetterMethod->getSelector();
1424 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1425 return true;
1426 }
1427
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001428 return false;
1429}
1430
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001431/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001432/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001433static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1434 ObjCContainerDecl::PropertyMap &PropMap,
1435 ObjCContainerDecl::PropertyMap &SuperPropMap,
1436 bool IncludeProtocols = true) {
1437
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001438 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001439 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001440 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001441 if (IncludeProtocols) {
1442 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001443 for (auto *PI : IDecl->all_referenced_protocols())
1444 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001445 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001446 }
1447 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1448 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001449 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001450 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001451 if (IncludeProtocols) {
1452 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001453 for (auto *PI : CATDecl->protocols())
1454 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001455 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001456 }
1457 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001458 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001459 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1460 // Exclude property for protocols which conform to class's super-class,
1461 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001462 if (!PropertyFromSuper ||
1463 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001464 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1465 if (!PropEntry)
1466 PropEntry = Prop;
1467 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001468 }
1469 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001470 for (auto *PI : PDecl->protocols())
1471 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001472 }
1473}
1474
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001475/// CollectSuperClassPropertyImplementations - This routine collects list of
1476/// properties to be implemented in super class(s) and also coming from their
1477/// conforming protocols.
1478static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001479 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001480 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001481 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001482 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001483 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001484 SDecl = SDecl->getSuperClass();
1485 }
1486 }
1487}
1488
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001489/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1490/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1491/// declared in class 'IFace'.
1492bool
1493Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1494 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1495 if (!IV->getSynthesize())
1496 return false;
1497 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1498 Method->isInstanceMethod());
1499 if (!IMD || !IMD->isPropertyAccessor())
1500 return false;
1501
1502 // look up a property declaration whose one of its accessors is implemented
1503 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001504 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001505 if ((Property->getGetterName() == IMD->getSelector() ||
1506 Property->getSetterName() == IMD->getSelector()) &&
1507 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001508 return true;
1509 }
1510 return false;
1511}
1512
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001513static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1514 ObjCPropertyDecl *Prop) {
1515 bool SuperClassImplementsGetter = false;
1516 bool SuperClassImplementsSetter = false;
1517 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1518 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001519
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001520 while (IDecl->getSuperClass()) {
1521 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1522 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1523 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001524
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001525 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1526 SuperClassImplementsSetter = true;
1527 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1528 return true;
1529 IDecl = IDecl->getSuperClass();
1530 }
1531 return false;
1532}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001533
James Dennett2a4d13c2012-06-15 07:13:21 +00001534/// \brief Default synthesizes all properties which must be synthesized
1535/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001536void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1537 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001538
Anna Zaks673d76b2012-10-18 19:17:53 +00001539 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001540 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1541 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001542 if (PropMap.empty())
1543 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001544 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001545 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1546
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001547 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1548 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001549 // Is there a matching property synthesize/dynamic?
1550 if (Prop->isInvalidDecl() ||
1551 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1552 continue;
1553 // Property may have been synthesized by user.
1554 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1555 continue;
1556 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1557 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1558 continue;
1559 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1560 continue;
1561 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001562 if (ObjCPropertyImplDecl *PID =
1563 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001564 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1565 << Prop->getIdentifier();
1566 if (!PID->getLocation().isInvalid())
1567 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001568 continue;
1569 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001570 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001571 if (ObjCProtocolDecl *Proto =
1572 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001573 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001574 // Suppress the warning if class's superclass implements property's
1575 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001576 // Or, if property is going to be implemented in its super class.
1577 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001578 Diag(IMPDecl->getLocation(),
1579 diag::warn_auto_synthesizing_protocol_property)
1580 << Prop << Proto;
1581 Diag(Prop->getLocation(), diag::note_property_declare);
1582 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001583 continue;
1584 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001585 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001586 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001587 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1588 (PropInSuperClass->getPropertyAttributes() &
1589 ObjCPropertyDecl::OBJC_PR_readonly) &&
1590 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1591 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1592 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1593 << Prop->getIdentifier();
1594 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1595 }
1596 else {
1597 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1598 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001599 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001600 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1601 }
1602 continue;
1603 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001604 // We use invalid SourceLocations for the synthesized ivars since they
1605 // aren't really synthesized at a particular location; they just exist.
1606 // Saying that they are located at the @implementation isn't really going
1607 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001608 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1609 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1610 true,
1611 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001612 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001613 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001614 if (PIDecl) {
1615 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001616 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001617 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001618 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001619}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001620
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001621void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001622 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001623 return;
1624 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1625 if (!IC)
1626 return;
1627 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001628 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001629 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001630}
1631
Ted Kremenek7e812952014-02-21 19:41:30 +00001632static void DiagnoseUnimplementedAccessor(Sema &S,
1633 ObjCInterfaceDecl *PrimaryClass,
1634 Selector Method,
1635 ObjCImplDecl* IMPDecl,
1636 ObjCContainerDecl *CDecl,
1637 ObjCCategoryDecl *C,
1638 ObjCPropertyDecl *Prop,
1639 Sema::SelectorSet &SMap) {
1640 // When reporting on missing property setter/getter implementation in
1641 // categories, do not report when they are declared in primary class,
1642 // class's protocol, or one of it super classes. This is because,
1643 // the class is going to implement them.
1644 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001645 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001646 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1647 S.Diag(IMPDecl->getLocation(),
1648 isa<ObjCCategoryDecl>(CDecl) ?
1649 diag::warn_setter_getter_impl_required_in_category :
1650 diag::warn_setter_getter_impl_required)
1651 << Prop->getDeclName() << Method;
1652 S.Diag(Prop->getLocation(),
1653 diag::note_property_declare);
1654 if (S.LangOpts.ObjCDefaultSynthProperties &&
1655 S.LangOpts.ObjCRuntime.isNonFragile())
1656 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1657 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1658 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1659 }
1660}
1661
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001662void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001663 ObjCContainerDecl *CDecl,
1664 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001665 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001666 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1667
Ted Kremenek348e88c2014-02-21 19:41:34 +00001668 if (!SynthesizeProperties) {
1669 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001670 // Gather properties which need not be implemented in this class
1671 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001672 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001673 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1674 // For categories, no need to implement properties declared in
1675 // its primary class (and its super classes) if property is
1676 // declared in one of those containers.
1677 if ((IDecl = C->getClassInterface())) {
1678 ObjCInterfaceDecl::PropertyDeclOrder PO;
1679 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1680 }
1681 }
1682 if (IDecl)
1683 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1684
1685 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1686 }
1687
Ted Kremenek38882022014-02-21 19:41:39 +00001688 // Scan the @interface to see if any of the protocols it adopts
1689 // require an explicit implementation, via attribute
1690 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001691 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001692 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001693
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001694 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001695 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1696 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001697 // Lazily construct a set of all the properties in the @interface
1698 // of the class, without looking at the superclass. We cannot
1699 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001700 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001701 // as scans the adopted protocols. This work only triggers for protocols
1702 // with the attribute, which is very rare, and only occurs when
1703 // analyzing the @implementation.
1704 if (!LazyMap) {
1705 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1706 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1707 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1708 /* IncludeProtocols */ false);
1709 }
Ted Kremenek38882022014-02-21 19:41:39 +00001710 // Add the properties of 'PDecl' to the list of properties that
1711 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001712 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001713 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001714 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001715 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001716 }
1717 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001718 }
Ted Kremenek38882022014-02-21 19:41:39 +00001719
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001720 if (PropMap.empty())
1721 return;
1722
1723 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001724 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001725 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001726
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001727 SelectorSet InsMap;
1728 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001729 for (const auto *I : IMPDecl->instance_methods())
1730 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001731
1732 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001733 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001734 if (C && !C->IsClassExtension())
1735 if ((PrimaryClass = C->getClassInterface()))
1736 // Report unimplemented properties in the category as well.
1737 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1738 // When reporting on missing setter/getters, do not report when
1739 // setter/getter is implemented in category's primary class
1740 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001741 for (const auto *I : IMP->instance_methods())
1742 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001743 }
1744
Anna Zaks673d76b2012-10-18 19:17:53 +00001745 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001746 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1747 ObjCPropertyDecl *Prop = P->second;
1748 // Is there a matching propery synthesize/dynamic?
1749 if (Prop->isInvalidDecl() ||
1750 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001751 PropImplMap.count(Prop) ||
1752 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001753 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001754
1755 // Diagnose unimplemented getters and setters.
1756 DiagnoseUnimplementedAccessor(*this,
1757 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1758 if (!Prop->isReadOnly())
1759 DiagnoseUnimplementedAccessor(*this,
1760 PrimaryClass, Prop->getSetterName(),
1761 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001762 }
1763}
1764
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001765void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001766 for (const auto *propertyImpl : impDecl->property_impls()) {
1767 const auto *property = propertyImpl->getPropertyDecl();
1768
1769 // Warn about null_resettable properties with synthesized setters,
1770 // because the setter won't properly handle nil.
1771 if (propertyImpl->getPropertyImplementation()
1772 == ObjCPropertyImplDecl::Synthesize &&
1773 (property->getPropertyAttributes() &
1774 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1775 property->getGetterMethodDecl() &&
1776 property->getSetterMethodDecl()) {
1777 auto *getterMethod = property->getGetterMethodDecl();
1778 auto *setterMethod = property->getSetterMethodDecl();
1779 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1780 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1781 SourceLocation loc = propertyImpl->getLocation();
1782 if (loc.isInvalid())
1783 loc = impDecl->getLocStart();
1784
1785 Diag(loc, diag::warn_null_resettable_setter)
1786 << setterMethod->getSelector() << property->getDeclName();
1787 }
1788 }
1789 }
1790}
1791
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001792void
1793Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1794 ObjCContainerDecl* IDecl) {
1795 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001796 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001797 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001798 for (const auto *Property : IDecl->properties()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001799 ObjCMethodDecl *GetterMethod = nullptr;
1800 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001801 bool LookedUpGetterSetter = false;
1802
Bill Wendling44426052012-12-20 19:22:21 +00001803 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001804 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001805
John McCall43192862011-09-13 18:31:23 +00001806 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1807 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001808 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1809 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1810 LookedUpGetterSetter = true;
1811 if (GetterMethod) {
1812 Diag(GetterMethod->getLocation(),
1813 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001814 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001815 Diag(Property->getLocation(), diag::note_property_declare);
1816 }
1817 if (SetterMethod) {
1818 Diag(SetterMethod->getLocation(),
1819 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001820 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001821 Diag(Property->getLocation(), diag::note_property_declare);
1822 }
1823 }
1824
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001825 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001826 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1827 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001828 continue;
1829 if (const ObjCPropertyImplDecl *PIDecl
1830 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1831 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1832 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001833 if (!LookedUpGetterSetter) {
1834 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1835 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001836 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001837 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1838 SourceLocation MethodLoc =
1839 (GetterMethod ? GetterMethod->getLocation()
1840 : SetterMethod->getLocation());
1841 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001842 << Property->getIdentifier() << (GetterMethod != nullptr)
1843 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001844 // fixit stuff.
1845 if (!AttributesAsWritten) {
1846 if (Property->getLParenLoc().isValid()) {
1847 // @property () ... case.
1848 SourceRange PropSourceRange(Property->getAtLoc(),
1849 Property->getLParenLoc());
1850 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1851 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1852 }
1853 else {
1854 //@property id etc.
1855 SourceLocation endLoc =
1856 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1857 endLoc = endLoc.getLocWithOffset(-1);
1858 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1859 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1860 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1861 }
1862 }
1863 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1864 // @property () ... case.
1865 SourceLocation endLoc = Property->getLParenLoc();
1866 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1867 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1868 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1869 }
1870 else
1871 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001872 Diag(Property->getLocation(), diag::note_property_declare);
1873 }
1874 }
1875 }
1876}
1877
John McCall31168b02011-06-15 23:02:42 +00001878void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001879 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001880 return;
1881
Aaron Ballmand85eff42014-03-14 15:02:45 +00001882 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001883 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001884 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1885 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001886 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1887 if (!method)
1888 continue;
1889 ObjCMethodFamily family = method->getMethodFamily();
1890 if (family == OMF_alloc || family == OMF_copy ||
1891 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001892 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001893 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001894 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001895 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001896
1897 // Look for a getter explicitly declared alongside the property.
1898 // If we find one, use its location for the note.
1899 SourceLocation noteLoc = PD->getLocation();
1900 SourceLocation fixItLoc;
1901 for (auto *getterRedecl : method->redecls()) {
1902 if (getterRedecl->isImplicit())
1903 continue;
1904 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1905 continue;
1906 noteLoc = getterRedecl->getLocation();
1907 fixItLoc = getterRedecl->getLocEnd();
1908 }
1909
1910 Preprocessor &PP = getPreprocessor();
1911 TokenValue tokens[] = {
1912 tok::kw___attribute, tok::l_paren, tok::l_paren,
1913 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1914 PP.getIdentifierInfo("none"), tok::r_paren,
1915 tok::r_paren, tok::r_paren
1916 };
1917 StringRef spelling = "__attribute__((objc_method_family(none)))";
1918 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1919 if (!macroName.empty())
1920 spelling = macroName;
1921
1922 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1923 << method->getDeclName() << spelling;
1924 if (fixItLoc.isValid()) {
1925 SmallString<64> fixItText(" ");
1926 fixItText += spelling;
1927 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1928 }
John McCall31168b02011-06-15 23:02:42 +00001929 }
1930 }
1931 }
1932}
1933
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001934void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001935 const ObjCImplementationDecl *ImplD,
1936 const ObjCInterfaceDecl *IFD) {
1937 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001938 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1939 if (!SuperD)
1940 return;
1941
1942 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001943 for (const auto *I : ImplD->instance_methods())
1944 if (I->getMethodFamily() == OMF_init)
1945 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001946
1947 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1948 SuperD->getDesignatedInitializers(DesignatedInits);
1949 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1950 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1951 const ObjCMethodDecl *MD = *I;
1952 if (!InitSelSet.count(MD->getSelector())) {
1953 Diag(ImplD->getLocation(),
1954 diag::warn_objc_implementation_missing_designated_init_override)
1955 << MD->getSelector();
1956 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1957 }
1958 }
1959}
1960
John McCallad31b5f2010-11-10 07:01:40 +00001961/// AddPropertyAttrs - Propagates attributes from a property to the
1962/// implicitly-declared getter or setter for that property.
1963static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1964 ObjCPropertyDecl *Property) {
1965 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001966 for (const auto *A : Property->attrs()) {
1967 if (isa<DeprecatedAttr>(A) ||
1968 isa<UnavailableAttr>(A) ||
1969 isa<AvailabilityAttr>(A))
1970 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001971 }
John McCallad31b5f2010-11-10 07:01:40 +00001972}
1973
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001974/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1975/// have the property type and issue diagnostics if they don't.
1976/// Also synthesize a getter/setter method if none exist (and update the
1977/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1978/// methods is the "right" thing to do.
1979void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001980 ObjCContainerDecl *CD,
1981 ObjCPropertyDecl *redeclaredProperty,
1982 ObjCContainerDecl *lexicalDC) {
1983
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001984 ObjCMethodDecl *GetterMethod, *SetterMethod;
1985
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00001986 if (CD->isInvalidDecl())
1987 return;
1988
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001989 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1990 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1991 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1992 property->getLocation());
1993
1994 if (SetterMethod) {
1995 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1996 property->getPropertyAttributes();
1997 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001998 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1999 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002000 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2001 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002002 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002003 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2004 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002005 Diag(property->getLocation(),
2006 diag::warn_accessor_property_type_mismatch)
2007 << property->getDeclName()
2008 << SetterMethod->getSelector();
2009 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2010 }
2011 }
2012
2013 // Synthesize getter/setter methods if none exist.
2014 // Find the default getter and if one not found, add one.
2015 // FIXME: The synthesized property we set here is misleading. We almost always
2016 // synthesize these methods unless the user explicitly provided prototypes
2017 // (which is odd, but allowed). Sema should be typechecking that the
2018 // declarations jive in that situation (which it is not currently).
2019 if (!GetterMethod) {
2020 // No instance method of same name as property getter name was found.
2021 // Declare a getter method and add it to the list of methods
2022 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00002023 SourceLocation Loc = redeclaredProperty ?
2024 redeclaredProperty->getLocation() :
2025 property->getLocation();
2026
Douglas Gregor849ebc22015-06-19 18:14:46 +00002027 // If the property is null_resettable, the getter returns nonnull.
2028 QualType resultTy = property->getType();
2029 if (property->getPropertyAttributes() &
2030 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2031 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002032 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002033 if (*nullability == NullabilityKind::Unspecified)
2034 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2035 modifiedTy, modifiedTy);
2036 }
2037 }
2038
Ted Kremenek2f075632010-09-21 20:52:59 +00002039 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2040 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002041 resultTy, nullptr, CD,
Craig Topperc3ec1492014-05-26 06:22:03 +00002042 /*isInstance=*/true, /*isVariadic=*/false,
2043 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002044 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002045 (property->getPropertyImplementation() ==
2046 ObjCPropertyDecl::Optional) ?
2047 ObjCMethodDecl::Optional :
2048 ObjCMethodDecl::Required);
2049 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002050
2051 AddPropertyAttrs(*this, GetterMethod, property);
2052
Ted Kremenek49be9e02010-05-18 21:09:07 +00002053 // FIXME: Eventually this shouldn't be needed, as the lexical context
2054 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00002055 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002056 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002057 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002058 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2059 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002060
2061 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2062 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002063 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002064
2065 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002066 GetterMethod->addAttr(
2067 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2068 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002069
2070 if (getLangOpts().ObjCAutoRefCount)
2071 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002072 } else
2073 // A user declared getter will be synthesize when @synthesize of
2074 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002075 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002076 property->setGetterMethodDecl(GetterMethod);
2077
2078 // Skip setter if property is read-only.
2079 if (!property->isReadOnly()) {
2080 // Find the default setter and if one not found, add one.
2081 if (!SetterMethod) {
2082 // No instance method of same name as property setter name was found.
2083 // Declare a setter method and add it to the list of methods
2084 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002085 SourceLocation Loc = redeclaredProperty ?
2086 redeclaredProperty->getLocation() :
2087 property->getLocation();
2088
2089 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002090 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002091 property->getSetterName(), Context.VoidTy,
2092 nullptr, CD, /*isInstance=*/true,
2093 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002094 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002095 /*isImplicitlyDeclared=*/true,
2096 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002097 (property->getPropertyImplementation() ==
2098 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002099 ObjCMethodDecl::Optional :
2100 ObjCMethodDecl::Required);
2101
Douglas Gregor849ebc22015-06-19 18:14:46 +00002102 // If the property is null_resettable, the setter accepts a
2103 // nullable value.
2104 QualType paramTy = property->getType().getUnqualifiedType();
2105 if (property->getPropertyAttributes() &
2106 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2107 QualType modifiedTy = paramTy;
2108 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2109 if (*nullability == NullabilityKind::Unspecified)
2110 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2111 modifiedTy, modifiedTy);
2112 }
2113 }
2114
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002115 // Invent the arguments for the setter. We don't bother making a
2116 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002117 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2118 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002119 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002120 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002121 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002122 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002123 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002124 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002125
2126 AddPropertyAttrs(*this, SetterMethod, property);
2127
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002128 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002129 // FIXME: Eventually this shouldn't be needed, as the lexical context
2130 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002131 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002132 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002133 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002134 SetterMethod->addAttr(
2135 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2136 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002137 // It's possible for the user to have set a very odd custom
2138 // setter selector that causes it to have a method family.
2139 if (getLangOpts().ObjCAutoRefCount)
2140 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002141 } else
2142 // A user declared setter will be synthesize when @synthesize of
2143 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002144 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002145 property->setSetterMethodDecl(SetterMethod);
2146 }
2147 // Add any synthesized methods to the global pool. This allows us to
2148 // handle the following, which is supported by GCC (and part of the design).
2149 //
2150 // @interface Foo
2151 // @property double bar;
2152 // @end
2153 //
2154 // void thisIsUnfortunate() {
2155 // id foo;
2156 // double bar = [foo bar];
2157 // }
2158 //
2159 if (GetterMethod)
2160 AddInstanceMethodToGlobalPool(GetterMethod);
2161 if (SetterMethod)
2162 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002163
2164 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2165 if (!CurrentClass) {
2166 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2167 CurrentClass = Cat->getClassInterface();
2168 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2169 CurrentClass = Impl->getClassInterface();
2170 }
2171 if (GetterMethod)
2172 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2173 if (SetterMethod)
2174 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002175}
2176
John McCall48871652010-08-21 09:40:31 +00002177void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002178 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002179 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002180 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002181 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002182 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002183 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002184
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002185 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2186 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2187 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2188 << "readonly" << "readwrite";
2189
2190 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2191 QualType PropertyTy = PropertyDecl->getType();
2192 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002193
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002194 // 'readonly' property with no obvious lifetime.
2195 // its life time will be determined by its backing ivar.
2196 if (getLangOpts().ObjCAutoRefCount &&
2197 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2198 PropertyTy->isObjCRetainableType() &&
2199 !PropertyOwnership)
2200 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002201
2202 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002203 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002204 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2205 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002206 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002207 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002208 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2209 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2210 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002211 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002212 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002213 }
2214
2215 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002216 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2217 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002218 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2219 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002220 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002221 }
Bill Wendling44426052012-12-20 19:22:21 +00002222 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002223 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2224 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002225 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002226 }
Bill Wendling44426052012-12-20 19:22:21 +00002227 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002228 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2229 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002230 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002231 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002232 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002233 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002234 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2235 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002236 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002237 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002238 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002239 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002240 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2241 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002242 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2243 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002244 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002245 }
Bill Wendling44426052012-12-20 19:22:21 +00002246 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002247 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2248 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002249 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002250 }
Bill Wendling44426052012-12-20 19:22:21 +00002251 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002252 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2253 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002254 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002255 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002256 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002257 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002258 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2259 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002260 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002261 }
Bill Wendling44426052012-12-20 19:22:21 +00002262 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2263 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002264 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2265 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002266 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002267 }
Bill Wendling44426052012-12-20 19:22:21 +00002268 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002269 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2270 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002271 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002272 }
Bill Wendling44426052012-12-20 19:22:21 +00002273 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002274 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2275 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002276 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002277 }
2278 }
Bill Wendling44426052012-12-20 19:22:21 +00002279 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2280 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002281 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2282 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002283 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002284 }
Bill Wendling44426052012-12-20 19:22:21 +00002285 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2286 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002287 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2288 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002289 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002290 }
2291
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002292 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002293 // 'weak' and 'nonnull' are mutually exclusive.
2294 if (auto nullability = PropertyTy->getNullability(Context)) {
2295 if (*nullability == NullabilityKind::NonNull)
2296 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2297 << "nonnull" << "weak";
2298 } else {
2299 PropertyTy =
2300 Context.getAttributedType(
2301 AttributedType::getNullabilityAttrKind(NullabilityKind::Nullable),
2302 PropertyTy, PropertyTy);
2303 TypeSourceInfo *TSInfo = PropertyDecl->getTypeSourceInfo();
2304 PropertyDecl->setType(PropertyTy, TSInfo);
2305 }
2306 }
2307
Bill Wendling44426052012-12-20 19:22:21 +00002308 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2309 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002310 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2311 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002312 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002313 }
2314
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002315 // Warn if user supplied no assignment attribute, property is
2316 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002317 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002318 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2319 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2320 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002321 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002322 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002323 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002324 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002325 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002326 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002327 bool isAnyClassTy =
2328 (PropertyTy->isObjCClassType() ||
2329 PropertyTy->isObjCQualifiedClassType());
2330 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2331 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002332 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002333 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002334 else if (propertyInPrimaryClass) {
2335 // Don't issue warning on property with no life time in class
2336 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002337 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002338 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002339 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002340
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002341 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002342 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002343 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002344 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002345 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002346
2347 // FIXME: Implement warning dependent on NSCopying being
2348 // implemented. See also:
2349 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2350 // (please trim this list while you are at it).
2351 }
2352
Bill Wendling44426052012-12-20 19:22:21 +00002353 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2354 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002355 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002356 && PropertyTy->isBlockPointerType())
2357 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002358 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2359 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2360 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002361 PropertyTy->isBlockPointerType())
2362 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002363
Bill Wendling44426052012-12-20 19:22:21 +00002364 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2365 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002366 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2367
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002368}