blob: d0b59eb7895141ad2983b55f2551c238d139ce54 [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 McCallb61e14e2015-10-27 04:54:50 +000064/// Check the internal consistency of a property declaration with
65/// an explicit ownership qualifier.
66static void checkPropertyDeclWithOwnership(Sema &S,
67 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000068 if (property->isInvalidDecl()) return;
69
70 ObjCPropertyDecl::PropertyAttributeKind propertyKind
71 = property->getPropertyAttributes();
72 Qualifiers::ObjCLifetime propertyLifetime
73 = property->getType().getObjCLifetime();
74
John McCallb61e14e2015-10-27 04:54:50 +000075 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000076
John McCall43192862011-09-13 18:31:23 +000077 Qualifiers::ObjCLifetime expectedLifetime
78 = getImpliedARCOwnership(propertyKind, property->getType());
79 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000080 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000081 // attribute. That's okay, but restore reasonable invariants by
82 // setting the property attribute according to the lifetime
83 // qualifier.
84 ObjCPropertyDecl::PropertyAttributeKind attr;
85 if (propertyLifetime == Qualifiers::OCL_Strong) {
86 attr = ObjCPropertyDecl::OBJC_PR_strong;
87 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
88 attr = ObjCPropertyDecl::OBJC_PR_weak;
89 } else {
90 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
91 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
92 }
93 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000094 return;
95 }
96
97 if (propertyLifetime == expectedLifetime) return;
98
99 property->setInvalidDecl();
100 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000101 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000102 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000103 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000104 << propertyLifetime;
105}
106
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// \brief Check this Objective-C property against a property declared in the
108/// given protocol.
109static void
110CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
111 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000113 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000114 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000115 return;
116
117 // Look for a property with the same name.
118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
119 for (unsigned I = 0, N = R.size(); I != N; ++I) {
120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000122 return;
123 }
124 }
125
126 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000127 for (auto *P : Proto->protocols())
128 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129}
130
John McCallb61e14e2015-10-27 04:54:50 +0000131static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
132 // In GC mode, just look for the __weak qualifier.
133 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
135
136 // In ARC/MRC, look for an explicit ownership qualifier.
137 // For some reason, this only applies to __weak.
138 } else if (auto ownership = T.getObjCLifetime()) {
139 switch (ownership) {
140 case Qualifiers::OCL_Weak:
141 return ObjCDeclSpec::DQ_PR_weak;
142 case Qualifiers::OCL_Strong:
143 return ObjCDeclSpec::DQ_PR_strong;
144 case Qualifiers::OCL_ExplicitNone:
145 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
146 case Qualifiers::OCL_Autoreleasing:
147 case Qualifiers::OCL_None:
148 return 0;
149 }
150 llvm_unreachable("bad qualifier");
151 }
152
153 return 0;
154}
155
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000156static const unsigned OwnershipMask =
157 (ObjCPropertyDecl::OBJC_PR_assign |
158 ObjCPropertyDecl::OBJC_PR_retain |
159 ObjCPropertyDecl::OBJC_PR_copy |
160 ObjCPropertyDecl::OBJC_PR_weak |
161 ObjCPropertyDecl::OBJC_PR_strong |
162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
163
John McCallb61e14e2015-10-27 04:54:50 +0000164static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000165 unsigned result = attr & OwnershipMask;
166
167 // From an ownership perspective, assign and unsafe_unretained are
168 // identical; make sure one also implies the other.
169 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
171 result |= ObjCPropertyDecl::OBJC_PR_assign |
172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
173 }
174
175 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000176}
177
John McCall48871652010-08-21 09:40:31 +0000178Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000179 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000180 FieldDeclarator &FD,
181 ObjCDeclSpec &ODS,
182 Selector GetterSel,
183 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000184 tok::ObjCKeywordKind MethodImplKind,
185 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000186 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
189 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000190 if (!getOwnershipRule(Attributes)) {
191 Attributes |= deducePropertyOwnershipFromType(*this, T);
192 }
Bill Wendling44426052012-12-20 19:22:21 +0000193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000194 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000196
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000199 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000201 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000203 FD,
204 GetterSel, ODS.getGetterNameLoc(),
205 SetterSel, ODS.getSetterNameLoc(),
206 isReadWrite, Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000207 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000208 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000209 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000210 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000211 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000212 }
213
214 if (!Res) {
215 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000216 GetterSel, ODS.getGetterNameLoc(), SetterSel,
217 ODS.getSetterNameLoc(), isReadWrite, Attributes,
218 ODS.getPropertyAttributes(), T, TSI,
219 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000220 if (lexicalDC)
221 Res->setLexicalDeclContext(lexicalDC);
222 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000223
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000224 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000225 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000226 (isa<ObjCInterfaceDecl>(ClassDecl) ||
227 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000228
John McCallb61e14e2015-10-27 04:54:50 +0000229 // Check consistency if the type has explicit ownership qualification.
230 if (Res->getType().getObjCLifetime())
231 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000232
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000234 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000235 // For a class, compare the property against a property in our superclass.
236 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000237 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
238 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000239 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000240 for (unsigned I = 0, N = R.size(); I != N; ++I) {
241 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000242 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000243 FoundInSuper = true;
244 break;
245 }
246 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000247 if (FoundInSuper)
248 break;
249 else
250 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000251 }
252
253 if (FoundInSuper) {
254 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000255 for (auto *P : CurrentInterfaceDecl->protocols()) {
256 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000257 }
258 } else {
259 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (auto *P : IFace->all_referenced_protocols()) {
261 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000262 }
263 }
264 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000265 // We don't check if class extension. Because properties in class extension
266 // are meant to override some of the attributes and checking has already done
267 // when property in class extension is constructed.
268 if (!Cat->IsClassExtension())
269 for (auto *P : Cat->protocols())
270 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000271 } else {
272 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000273 for (auto *P : Proto->protocols())
274 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000275 }
276
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000277 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000278 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000279}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000280
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000282makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000308 if (Attributes & ObjCDeclSpec::DQ_PR_class)
309 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000310
311 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
312}
313
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000314static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000315 SourceLocation LParenLoc, SourceLocation &Loc) {
316 if (LParenLoc.isMacroID())
317 return false;
318
319 SourceManager &SM = Context.getSourceManager();
320 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
321 // Try to load the file buffer.
322 bool invalidTemp = false;
323 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
324 if (invalidTemp)
325 return false;
326 const char *tokenBegin = file.data() + locInfo.second;
327
328 // Lex from the start of the given location.
329 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
330 Context.getLangOpts(),
331 file.begin(), tokenBegin, file.end());
332 Token Tok;
333 do {
334 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000335 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000336 Loc = Tok.getLocation();
337 return true;
338 }
339 } while (Tok.isNot(tok::r_paren));
340 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000341}
342
Douglas Gregor429183e2015-12-09 22:57:32 +0000343/// Check for a mismatch in the atomicity of the given properties.
344static void checkAtomicPropertyMismatch(Sema &S,
345 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000346 ObjCPropertyDecl *NewProperty,
347 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000348 // If the atomicity of both matches, we're done.
349 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000350 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
351 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000352 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000353 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
354 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000355 if (OldIsAtomic == NewIsAtomic) return;
356
357 // Determine whether the given property is readonly and implicitly
358 // atomic.
359 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
360 // Is it readonly?
361 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000362 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000363
364 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000365 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000366
367 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000368 if (Property->getPropertyAttributesAsWritten() &
369 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000370 return false;
371
372 return true;
373 };
374
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000375 // If we're allowed to propagate atomicity, and the new property did
376 // not specify atomicity at all, propagate.
377 const unsigned AtomicityMask =
378 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
379 if (PropagateAtomicity &&
380 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
381 unsigned Attrs = NewProperty->getPropertyAttributes();
382 Attrs = Attrs & ~AtomicityMask;
383 if (OldIsAtomic)
384 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
385 else
386 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
387
388 NewProperty->overwritePropertyAttributes(Attrs);
389 return;
390 }
391
Douglas Gregor429183e2015-12-09 22:57:32 +0000392 // One of the properties is atomic; if it's a readonly property, and
393 // 'atomic' wasn't explicitly specified, we're okay.
394 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
395 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
396 return;
397
398 // Diagnose the conflict.
399 const IdentifierInfo *OldContextName;
400 auto *OldDC = OldProperty->getDeclContext();
401 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
402 OldContextName = Category->getClassInterface()->getIdentifier();
403 else
404 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
405
406 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
407 << NewProperty->getDeclName() << "atomic"
408 << OldContextName;
409 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
410}
411
Douglas Gregor90d34422013-01-21 19:05:22 +0000412ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000413Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000414 SourceLocation AtLoc,
415 SourceLocation LParenLoc,
416 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000417 Selector GetterSel,
418 SourceLocation GetterNameLoc,
419 Selector SetterSel,
420 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000421 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000422 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000423 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000424 QualType T,
425 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000426 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000427 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000428 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000429 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000431 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
432
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 // We need to look in the @interface to see if the @property was
434 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 if (!CCPrimary) {
436 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000437 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000438 }
439
Manman Ren5b786402016-01-28 18:49:28 +0000440 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
441 (Attributes & ObjCDeclSpec::DQ_PR_class);
442
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000443 // Find the property in the extended class's primary class or
444 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000445 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
446 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000447
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000448 // If we found a property in an extension, complain.
449 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
450 Diag(AtLoc, diag::err_duplicate_property);
451 Diag(PIDecl->getLocation(), diag::note_property_declare);
452 return nullptr;
453 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000454
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000455 // Check for consistency with the previous declaration, if there is one.
456 if (PIDecl) {
457 // A readonly property declared in the primary class can be refined
458 // by adding a readwrite property within an extension.
459 // Anything else is an error.
460 if (!(PIDecl->isReadOnly() && isReadWrite)) {
461 // Tailor the diagnostics for the common case where a readwrite
462 // property is declared both in the @interface and the continuation.
463 // This is a common error where the user often intended the original
464 // declaration to be readonly.
465 unsigned diag =
466 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
467 (PIDecl->getPropertyAttributesAsWritten() &
468 ObjCPropertyDecl::OBJC_PR_readwrite)
469 ? diag::err_use_continuation_class_redeclaration_readwrite
470 : diag::err_use_continuation_class;
471 Diag(AtLoc, diag)
472 << CCPrimary->getDeclName();
473 Diag(PIDecl->getLocation(), diag::note_property_declare);
474 return nullptr;
475 }
476
477 // Check for consistency of getters.
478 if (PIDecl->getGetterName() != GetterSel) {
479 // If the getter was written explicitly, complain.
480 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
481 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
482 << PIDecl->getGetterName() << GetterSel;
483 Diag(PIDecl->getLocation(), diag::note_property_declare);
484 }
485
486 // Always adopt the getter from the original declaration.
487 GetterSel = PIDecl->getGetterName();
488 Attributes |= ObjCDeclSpec::DQ_PR_getter;
489 }
490
491 // Check consistency of ownership.
492 unsigned ExistingOwnership
493 = getOwnershipRule(PIDecl->getPropertyAttributes());
494 unsigned NewOwnership = getOwnershipRule(Attributes);
495 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
496 // If the ownership was written explicitly, complain.
497 if (getOwnershipRule(AttributesAsWritten)) {
498 Diag(AtLoc, diag::warn_property_attr_mismatch);
499 Diag(PIDecl->getLocation(), diag::note_property_declare);
500 }
501
502 // Take the ownership from the original property.
503 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
504 }
505
506 // If the redeclaration is 'weak' but the original property is not,
507 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
508 !(PIDecl->getPropertyAttributesAsWritten()
509 & ObjCPropertyDecl::OBJC_PR_weak) &&
510 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
511 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
512 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
513 Diag(PIDecl->getLocation(), diag::note_property_declare);
514 }
515 }
516
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000517 // Create a new ObjCPropertyDecl with the DeclContext being
518 // the class extension.
519 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000520 FD, GetterSel, GetterNameLoc,
521 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000522 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000523 Attributes, AttributesAsWritten,
524 T, TSI, MethodImplKind, DC);
525
526 // If there was no declaration of a property with the same name in
527 // the primary class, we're done.
528 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000529 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000530 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000531 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000532
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000533 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
534 bool IncompatibleObjC = false;
535 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000536 // Relax the strict type matching for property type in continuation class.
537 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000538 // as it narrows the object type in its primary class property. Note that
539 // this conversion is safe only because the wider type is for a 'readonly'
540 // property in primary class and 'narrowed' type for a 'readwrite' property
541 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000542 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
543 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
544 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
545 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
546 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000547 ConvertedType, IncompatibleObjC))
548 || IncompatibleObjC) {
549 Diag(AtLoc,
550 diag::err_type_mismatch_continuation_class) << PDecl->getType();
551 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000552 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000553 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000554 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000555
556 // Check that atomicity of property in class extension matches the previous
557 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000558 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000559
Douglas Gregore17765e2015-11-03 17:02:34 +0000560 // Make sure getter/setter are appropriately synthesized.
561 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000562 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000563}
564
565ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
566 ObjCContainerDecl *CDecl,
567 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000568 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000569 FieldDeclarator &FD,
570 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000571 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000572 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000573 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000574 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000575 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000576 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000577 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000578 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000579 tok::ObjCKeywordKind MethodImplKind,
580 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000581 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000582
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000583 // Property defaults to 'assign' if it is readwrite, unless this is ARC
584 // and the type is retainable.
585 bool isAssign;
586 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
587 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
588 isAssign = true;
589 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
590 isAssign = false;
591 } else {
592 isAssign = (!getLangOpts().ObjCAutoRefCount ||
593 !T->isObjCRetainableType());
594 }
595
596 // Issue a warning if property is 'assign' as default and its
597 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000598 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000599 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000600 if (const ObjCObjectPointerType *ObjPtrTy =
601 T->getAs<ObjCObjectPointerType>()) {
602 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
603 if (IDecl)
604 if (ObjCProtocolDecl* PNSCopying =
605 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
606 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
607 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000609 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000610
611 if (T->isObjCObjectType()) {
612 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000613 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000614 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
615 << FixItHint::CreateInsertion(StarLoc, "*");
616 T = Context.getObjCObjectPointerType(T);
617 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
618 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
619 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000620
Ted Kremenek959e8302010-03-12 02:31:10 +0000621 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
623 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000624 PropertyId, AtLoc,
625 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000626
Manman Ren5b786402016-01-28 18:49:28 +0000627 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
628 (Attributes & ObjCDeclSpec::DQ_PR_class);
629 // Class property and instance property can have the same name.
630 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
631 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000632 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000633 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000634 PDecl->setInvalidDecl();
635 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000636 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000638 if (lexicalDC)
639 PDecl->setLexicalDeclContext(lexicalDC);
640 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000641
642 if (T->isArrayType() || T->isFunctionType()) {
643 Diag(AtLoc, diag::err_property_type) << T;
644 PDecl->setInvalidDecl();
645 }
646
647 ProcessDeclAttributes(S, PDecl, FD.D);
648
649 // Regardless of setter/getter attribute, we save the default getter/setter
650 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000651 PDecl->setGetterName(GetterSel, GetterNameLoc);
652 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000653 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000654 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000655
Bill Wendling44426052012-12-20 19:22:21 +0000656 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000657 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
658
Bill Wendling44426052012-12-20 19:22:21 +0000659 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000660 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
661
Bill Wendling44426052012-12-20 19:22:21 +0000662 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000663 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
664
665 if (isReadWrite)
666 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
667
Bill Wendling44426052012-12-20 19:22:21 +0000668 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000669 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
670
Bill Wendling44426052012-12-20 19:22:21 +0000671 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000672 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
673
Bill Wendling44426052012-12-20 19:22:21 +0000674 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
676
Bill Wendling44426052012-12-20 19:22:21 +0000677 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000678 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
679
Bill Wendling44426052012-12-20 19:22:21 +0000680 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
682
Ted Kremenekac597f32010-03-12 00:46:40 +0000683 if (isAssign)
684 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
685
John McCall43192862011-09-13 18:31:23 +0000686 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000687 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000689 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000690 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000691
John McCall31168b02011-06-15 23:02:42 +0000692 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000693 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000694 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
695 if (isAssign)
696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
697
Ted Kremenekac597f32010-03-12 00:46:40 +0000698 if (MethodImplKind == tok::objc_required)
699 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
700 else if (MethodImplKind == tok::objc_optional)
701 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000702
Douglas Gregor813a0662015-06-19 18:14:38 +0000703 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
704 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
705
Douglas Gregor849ebc22015-06-19 18:14:46 +0000706 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
707 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
708
Manman Ren387ff7f2016-01-26 18:52:43 +0000709 if (Attributes & ObjCDeclSpec::DQ_PR_class)
710 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
711
Ted Kremenek959e8302010-03-12 02:31:10 +0000712 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000713}
714
John McCall31168b02011-06-15 23:02:42 +0000715static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
716 ObjCPropertyDecl *property,
717 ObjCIvarDecl *ivar) {
718 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
719
John McCall31168b02011-06-15 23:02:42 +0000720 QualType ivarType = ivar->getType();
721 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall43192862011-09-13 18:31:23 +0000723 // The lifetime implied by the property's attributes.
724 Qualifiers::ObjCLifetime propertyLifetime =
725 getImpliedARCOwnership(property->getPropertyAttributes(),
726 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000727
John McCall43192862011-09-13 18:31:23 +0000728 // We're fine if they match.
729 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000730
John McCall460ce582015-10-22 18:38:17 +0000731 // None isn't a valid lifetime for an object ivar in ARC, and
732 // __autoreleasing is never valid; don't diagnose twice.
733 if ((ivarLifetime == Qualifiers::OCL_None &&
734 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000735 ivarLifetime == Qualifiers::OCL_Autoreleasing)
736 return;
John McCall31168b02011-06-15 23:02:42 +0000737
John McCalld8561f02012-08-20 23:36:59 +0000738 // If the ivar is private, and it's implicitly __unsafe_unretained
739 // becaues of its type, then pretend it was actually implicitly
740 // __strong. This is only sound because we're processing the
741 // property implementation before parsing any method bodies.
742 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
743 propertyLifetime == Qualifiers::OCL_Strong &&
744 ivar->getAccessControl() == ObjCIvarDecl::Private) {
745 SplitQualType split = ivarType.split();
746 if (split.Quals.hasObjCLifetime()) {
747 assert(ivarType->isObjCARCImplicitlyUnretainedType());
748 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
749 ivarType = S.Context.getQualifiedType(split);
750 ivar->setType(ivarType);
751 return;
752 }
753 }
754
John McCall43192862011-09-13 18:31:23 +0000755 switch (propertyLifetime) {
756 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000757 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000758 << property->getDeclName()
759 << ivar->getDeclName()
760 << ivarLifetime;
761 break;
John McCall31168b02011-06-15 23:02:42 +0000762
John McCall43192862011-09-13 18:31:23 +0000763 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000764 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000765 << property->getDeclName()
766 << ivar->getDeclName();
767 break;
John McCall31168b02011-06-15 23:02:42 +0000768
John McCall43192862011-09-13 18:31:23 +0000769 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000770 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000771 << property->getDeclName()
772 << ivar->getDeclName()
773 << ((property->getPropertyAttributesAsWritten()
774 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
775 break;
John McCall31168b02011-06-15 23:02:42 +0000776
John McCall43192862011-09-13 18:31:23 +0000777 case Qualifiers::OCL_Autoreleasing:
778 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000779
John McCall43192862011-09-13 18:31:23 +0000780 case Qualifiers::OCL_None:
781 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000782 return;
783 }
784
785 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000786 if (propertyImplLoc.isValid())
787 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000788}
789
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000790/// setImpliedPropertyAttributeForReadOnlyProperty -
791/// This routine evaludates life-time attributes for a 'readonly'
792/// property with no known lifetime of its own, using backing
793/// 'ivar's attribute, if any. If no backing 'ivar', property's
794/// life-time is assumed 'strong'.
795static void setImpliedPropertyAttributeForReadOnlyProperty(
796 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
797 Qualifiers::ObjCLifetime propertyLifetime =
798 getImpliedARCOwnership(property->getPropertyAttributes(),
799 property->getType());
800 if (propertyLifetime != Qualifiers::OCL_None)
801 return;
802
803 if (!ivar) {
804 // if no backing ivar, make property 'strong'.
805 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
806 return;
807 }
808 // property assumes owenership of backing ivar.
809 QualType ivarType = ivar->getType();
810 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
811 if (ivarLifetime == Qualifiers::OCL_Strong)
812 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
813 else if (ivarLifetime == Qualifiers::OCL_Weak)
814 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000815}
Ted Kremenekac597f32010-03-12 00:46:40 +0000816
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000817static bool
818isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
819 ObjCPropertyDecl::PropertyAttributeKind Kind) {
820 return (Attr1 & Kind) != (Attr2 & Kind);
821}
822
823static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
824 unsigned Kinds) {
825 return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
826}
827
828/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
829/// property declaration that should be synthesised in all of the inherited
830/// protocols. It also diagnoses properties declared in inherited protocols with
831/// mismatched types or attributes, since any of them can be candidate for
832/// synthesis.
833static ObjCPropertyDecl *
834SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000835 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000836 ObjCPropertyDecl *Property) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000837 assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
838 "Expected a property from a protocol");
839 ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
840 ObjCInterfaceDecl::PropertyDeclOrder Properties;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000841 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
842 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000843 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
844 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000845 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000846 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000847 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000848 for (const auto *PI : SDecl->all_referenced_protocols()) {
849 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000850 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
851 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000852 }
853 SDecl = SDecl->getSuperClass();
854 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000855 }
856
857 if (Properties.empty())
858 return Property;
859
860 ObjCPropertyDecl *OriginalProperty = Property;
861 size_t SelectedIndex = 0;
862 for (const auto &Prop : llvm::enumerate(Properties)) {
863 // Select the 'readwrite' property if such property exists.
864 if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
865 Property = Prop.value();
866 SelectedIndex = Prop.index();
867 }
868 }
869 if (Property != OriginalProperty) {
870 // Check that the old property is compatible with the new one.
871 Properties[SelectedIndex] = OriginalProperty;
872 }
873
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000874 QualType RHSType = S.Context.getCanonicalType(Property->getType());
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000875 unsigned OriginalAttributes = Property->getPropertyAttributes();
876 enum MismatchKind {
877 IncompatibleType = 0,
878 HasNoExpectedAttribute,
879 HasUnexpectedAttribute,
880 DifferentGetter,
881 DifferentSetter
882 };
883 // Represents a property from another protocol that conflicts with the
884 // selected declaration.
885 struct MismatchingProperty {
886 const ObjCPropertyDecl *Prop;
887 MismatchKind Kind;
888 StringRef AttributeName;
889 };
890 SmallVector<MismatchingProperty, 4> Mismatches;
891 for (ObjCPropertyDecl *Prop : Properties) {
892 // Verify the property attributes.
893 unsigned Attr = Prop->getPropertyAttributes();
894 if (Attr != OriginalAttributes) {
895 auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
896 MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
897 : HasUnexpectedAttribute;
898 Mismatches.push_back({Prop, Kind, AttributeName});
899 };
900 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
901 ObjCPropertyDecl::OBJC_PR_copy)) {
902 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
903 continue;
904 }
905 if (areIncompatiblePropertyAttributes(
906 OriginalAttributes, Attr, ObjCPropertyDecl::OBJC_PR_retain |
907 ObjCPropertyDecl::OBJC_PR_strong)) {
908 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
909 ObjCPropertyDecl::OBJC_PR_strong),
910 "retain (or strong)");
911 continue;
912 }
913 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
914 ObjCPropertyDecl::OBJC_PR_atomic)) {
915 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
916 continue;
917 }
918 }
919 if (Property->getGetterName() != Prop->getGetterName()) {
920 Mismatches.push_back({Prop, DifferentGetter, ""});
921 continue;
922 }
923 if (!Property->isReadOnly() && !Prop->isReadOnly() &&
924 Property->getSetterName() != Prop->getSetterName()) {
925 Mismatches.push_back({Prop, DifferentSetter, ""});
926 continue;
927 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000928 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
929 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
930 bool IncompatibleObjC = false;
931 QualType ConvertedType;
932 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
933 || IncompatibleObjC) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000934 Mismatches.push_back({Prop, IncompatibleType, ""});
935 continue;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000936 }
937 }
938 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000939
940 if (Mismatches.empty())
941 return Property;
942
943 // Diagnose incompability.
944 {
945 bool HasIncompatibleAttributes = false;
946 for (const auto &Note : Mismatches)
947 HasIncompatibleAttributes =
948 Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
949 // Promote the warning to an error if there are incompatible attributes or
950 // incompatible types together with readwrite/readonly incompatibility.
951 auto Diag = S.Diag(Property->getLocation(),
952 Property != OriginalProperty || HasIncompatibleAttributes
953 ? diag::err_protocol_property_mismatch
954 : diag::warn_protocol_property_mismatch);
955 Diag << Mismatches[0].Kind;
956 switch (Mismatches[0].Kind) {
957 case IncompatibleType:
958 Diag << Property->getType();
959 break;
960 case HasNoExpectedAttribute:
961 case HasUnexpectedAttribute:
962 Diag << Mismatches[0].AttributeName;
963 break;
964 case DifferentGetter:
965 Diag << Property->getGetterName();
966 break;
967 case DifferentSetter:
968 Diag << Property->getSetterName();
969 break;
970 }
971 }
972 for (const auto &Note : Mismatches) {
973 auto Diag =
974 S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
975 << Note.Kind;
976 switch (Note.Kind) {
977 case IncompatibleType:
978 Diag << Note.Prop->getType();
979 break;
980 case HasNoExpectedAttribute:
981 case HasUnexpectedAttribute:
982 Diag << Note.AttributeName;
983 break;
984 case DifferentGetter:
985 Diag << Note.Prop->getGetterName();
986 break;
987 case DifferentSetter:
988 Diag << Note.Prop->getSetterName();
989 break;
990 }
991 }
992 if (AtLoc.isValid())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000993 S.Diag(AtLoc, diag::note_property_synthesize);
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000994
995 return Property;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000996}
997
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000998/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +0000999static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1000 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001001 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
1002
1003 // If this is a readwrite property in a class extension that refines
1004 // a readonly property in the original class definition, check it as
1005 // well.
1006
1007 // If it's a readonly property, we're not interested.
1008 if (Prop->isReadOnly()) return false;
1009
1010 // Is it declared in an extension?
1011 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1012 if (!Category || !Category->IsClassExtension()) return false;
1013
1014 // Find the corresponding property in the primary class definition.
1015 auto OrigClass = Category->getClassInterface();
1016 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1017 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1018 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1019 }
1020
Douglas Gregor02535432015-12-18 00:52:31 +00001021 // Look through all of the protocols.
1022 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +00001023 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1024 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +00001025 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1026 }
1027
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001028 return false;
1029}
1030
Ted Kremenekac597f32010-03-12 00:46:40 +00001031/// ActOnPropertyImplDecl - This routine performs semantic checks and
1032/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +00001033/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +00001034///
John McCall48871652010-08-21 09:40:31 +00001035Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1036 SourceLocation AtLoc,
1037 SourceLocation PropertyLoc,
1038 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +00001039 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001040 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +00001041 SourceLocation PropertyIvarLoc,
1042 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +00001043 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +00001044 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +00001045 // Make sure we have a context for the property implementation declaration.
1046 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001047 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001048 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001049 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001050 if (PropertyIvarLoc.isInvalid())
1051 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +00001052 SourceLocation PropertyDiagLoc = PropertyLoc;
1053 if (PropertyDiagLoc.isInvalid())
1054 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +00001055 ObjCPropertyDecl *property = nullptr;
1056 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001057 // Find the class or category class where this property must have
1058 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00001059 ObjCImplementationDecl *IC = nullptr;
1060 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001061 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1062 IDecl = IC->getClassInterface();
1063 // We always synthesize an interface for an implementation
1064 // without an interface decl. So, IDecl is always non-zero.
1065 assert(IDecl &&
1066 "ActOnPropertyImplDecl - @implementation without @interface");
1067
1068 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +00001069 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001070 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001071 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001072 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001073 }
Manman Rendfef4062016-01-29 19:16:39 +00001074 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001075 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +00001076 return nullptr;
1077 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001078 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +00001079 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
1080 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001081 if (AtLoc.isValid())
1082 Diag(AtLoc, diag::warn_implicit_atomic_property);
1083 else
1084 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1085 Diag(property->getLocation(), diag::note_property_declare);
1086 }
1087
Ted Kremenekac597f32010-03-12 00:46:40 +00001088 if (const ObjCCategoryDecl *CD =
1089 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1090 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +00001091 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001092 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +00001093 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001094 }
1095 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001096 if (Synthesize&&
1097 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
1098 property->hasAttr<IBOutletAttr>() &&
1099 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001100 bool ReadWriteProperty = false;
1101 // Search into the class extensions and see if 'readonly property is
1102 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001103 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001104 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1105 if (!R.empty())
1106 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1107 PIkind = ExtProp->getPropertyAttributesAsWritten();
1108 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
1109 ReadWriteProperty = true;
1110 break;
1111 }
1112 }
1113 }
1114
1115 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +00001116 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +00001117 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001118 SourceLocation readonlyLoc;
1119 if (LocPropertyAttribute(Context, "readonly",
1120 property->getLParenLoc(), readonlyLoc)) {
1121 SourceLocation endLoc =
1122 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1123 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
1124 Diag(property->getLocation(),
1125 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1126 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1127 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +00001128 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001129 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001130 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001131 property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1132 property);
1133
Ted Kremenekac597f32010-03-12 00:46:40 +00001134 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1135 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001136 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001137 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001138 }
1139 IDecl = CatImplClass->getClassInterface();
1140 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001141 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001142 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001143 }
1144 ObjCCategoryDecl *Category =
1145 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1146
1147 // If category for this implementation not found, it is an error which
1148 // has already been reported eralier.
1149 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001150 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001151 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001152 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001153 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001154 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001155 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001156 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001157 }
1158 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001159 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001160 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001161 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001162 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001163 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001164 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001165 // Check that we have a valid, previously declared ivar for @synthesize
1166 if (Synthesize) {
1167 // @synthesize
1168 if (!PropertyIvar)
1169 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001170 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1171 ObjCInterfaceDecl *ClassDeclared;
1172 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1173 QualType PropType = property->getType();
1174 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001175
1176 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001177 diag::err_incomplete_synthesized_property,
1178 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001179 Diag(property->getLocation(), diag::note_property_declare);
1180 CompleteTypeErr = true;
1181 }
1182
David Blaikiebbafb8a2012-03-11 07:00:24 +00001183 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001184 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001185 ObjCPropertyDecl::OBJC_PR_readonly) &&
1186 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001187 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1188 }
1189
John McCall31168b02011-06-15 23:02:42 +00001190 ObjCPropertyDecl::PropertyAttributeKind kind
1191 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001192
John McCall460ce582015-10-22 18:38:17 +00001193 bool isARCWeak = false;
1194 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1195 // Add GC __weak to the ivar type if the property is weak.
1196 if (getLangOpts().getGC() != LangOptions::NonGC) {
1197 assert(!getLangOpts().ObjCAutoRefCount);
1198 if (PropertyIvarType.isObjCGCStrong()) {
1199 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1200 Diag(property->getLocation(), diag::note_property_declare);
1201 } else {
1202 PropertyIvarType =
1203 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1204 }
1205
1206 // Otherwise, check whether ARC __weak is enabled and works with
1207 // the property type.
John McCall43192862011-09-13 18:31:23 +00001208 } else {
John McCall460ce582015-10-22 18:38:17 +00001209 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001210 // Only complain here when synthesizing an ivar.
1211 if (!Ivar) {
1212 Diag(PropertyDiagLoc,
1213 getLangOpts().ObjCWeakRuntime
1214 ? diag::err_synthesizing_arc_weak_property_disabled
1215 : diag::err_synthesizing_arc_weak_property_no_runtime);
1216 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001217 }
John McCallb61e14e2015-10-27 04:54:50 +00001218 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001219 } else {
1220 isARCWeak = true;
1221 if (const ObjCObjectPointerType *ObjT =
1222 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1223 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1224 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1225 Diag(property->getLocation(),
1226 diag::err_arc_weak_unavailable_property)
1227 << PropertyIvarType;
1228 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1229 << ClassImpDecl->getName();
1230 }
1231 }
1232 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001233 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001234 }
John McCall460ce582015-10-22 18:38:17 +00001235
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001236 if (AtLoc.isInvalid()) {
1237 // Check when default synthesizing a property that there is
1238 // an ivar matching property name and issue warning; since this
1239 // is the most common case of not using an ivar used for backing
1240 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001241 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001242 ObjCIvarDecl *originalIvar =
1243 IDecl->lookupInstanceVariable(property->getIdentifier(),
1244 ClassDeclared);
1245 if (originalIvar) {
1246 Diag(PropertyDiagLoc,
1247 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001248 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001249 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001250 Diag(property->getLocation(), diag::note_property_declare);
1251 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001252 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001253 }
1254
1255 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001256 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001257 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001258 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001259 !PropertyIvarType.getObjCLifetime() &&
1260 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001261
John McCall43192862011-09-13 18:31:23 +00001262 // It's an error if we have to do this and the user didn't
1263 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001264 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001265 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001266 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001267 diag::err_arc_objc_property_default_assign_on_object);
1268 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001269 } else {
1270 Qualifiers::ObjCLifetime lifetime =
1271 getImpliedARCOwnership(kind, PropertyIvarType);
1272 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001273
John McCall31168b02011-06-15 23:02:42 +00001274 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001275 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001276 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1277 }
John McCall31168b02011-06-15 23:02:42 +00001278 }
1279
Abramo Bagnaradff19302011-03-08 08:55:46 +00001280 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001281 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001282 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001283 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001284 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001285 if (RequireNonAbstractType(PropertyIvarLoc,
1286 PropertyIvarType,
1287 diag::err_abstract_type_in_decl,
1288 AbstractSynthesizedIvarType)) {
1289 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001290 // An abstract type is as bad as an incomplete type.
1291 CompleteTypeErr = true;
1292 }
1293 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001294 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001295 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001296 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001297
John McCall5fb5df92012-06-20 06:18:46 +00001298 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001299 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001300 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001301 // Note! I deliberately want it to fall thru so, we have a
1302 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001303 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001304 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001305 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001306 << property->getDeclName() << Ivar->getDeclName()
1307 << ClassDeclared->getDeclName();
1308 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001309 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001310 // Note! I deliberately want it to fall thru so more errors are caught.
1311 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001312 property->setPropertyIvarDecl(Ivar);
1313
Ted Kremenekac597f32010-03-12 00:46:40 +00001314 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1315
1316 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001317 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001318 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001319 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001320 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001321 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001322 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001323 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001324 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001325 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1326 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001327 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001328 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001329 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001330 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001331 << property->getDeclName() << PropType
1332 << Ivar->getDeclName() << IvarType;
1333 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001334 // Note! I deliberately want it to fall thru so, we have a
1335 // a property implementation and to avoid future warnings.
1336 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001337 else {
1338 // FIXME! Rules for properties are somewhat different that those
1339 // for assignments. Use a new routine to consolidate all cases;
1340 // specifically for property redeclarations as well as for ivars.
1341 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1342 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1343 if (lhsType != rhsType &&
1344 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001345 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001346 << property->getDeclName() << PropType
1347 << Ivar->getDeclName() << IvarType;
1348 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1349 // Fall thru - see previous comment
1350 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001351 }
1352 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001353 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001354 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001355 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001356 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001357 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001358 // Fall thru - see previous comment
1359 }
John McCall31168b02011-06-15 23:02:42 +00001360 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001361 if ((property->getType()->isObjCObjectPointerType() ||
1362 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001363 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001364 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001365 << property->getDeclName() << Ivar->getDeclName();
1366 // Fall thru - see previous comment
1367 }
1368 }
John McCall460ce582015-10-22 18:38:17 +00001369 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1370 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001371 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001372 } else if (PropertyIvar)
1373 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001374 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001375
Ted Kremenekac597f32010-03-12 00:46:40 +00001376 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1377 ObjCPropertyImplDecl *PIDecl =
1378 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1379 property,
1380 (Synthesize ?
1381 ObjCPropertyImplDecl::Synthesize
1382 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001383 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001384
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001385 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001386 PIDecl->setInvalidDecl();
1387
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001388 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1389 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001390 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001391 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001392 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1393 // returned by the getter as it must conform to C++'s copy-return rules.
1394 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001395 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001396 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1397 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001398 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001399 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001400 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001401 Expr *LoadSelfExpr =
1402 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001403 CK_LValueToRValue, SelfExpr, nullptr,
1404 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001405 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001406 new (Context) ObjCIvarRefExpr(Ivar,
1407 Ivar->getUsageType(SelfDecl->getType()),
1408 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001409 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001410 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001411 ExprResult Res = PerformCopyInitialization(
1412 InitializedEntity::InitializeResult(PropertyDiagLoc,
1413 getterMethod->getReturnType(),
1414 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001415 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001416 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001417 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001418 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001419 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001420 PIDecl->setGetterCXXConstructor(ResExpr);
1421 }
1422 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001423 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1424 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1425 Diag(getterMethod->getLocation(),
1426 diag::warn_property_getter_owning_mismatch);
1427 Diag(property->getLocation(), diag::note_property_declare);
1428 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001429 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1430 switch (getterMethod->getMethodFamily()) {
1431 case OMF_retain:
1432 case OMF_retainCount:
1433 case OMF_release:
1434 case OMF_autorelease:
1435 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1436 << 1 << getterMethod->getSelector();
1437 break;
1438 default:
1439 break;
1440 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001441 }
1442 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1443 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001444 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1445 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001446 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001447 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001448 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1449 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001450 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001451 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001452 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001453 Expr *LoadSelfExpr =
1454 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001455 CK_LValueToRValue, SelfExpr, nullptr,
1456 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001457 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001458 new (Context) ObjCIvarRefExpr(Ivar,
1459 Ivar->getUsageType(SelfDecl->getType()),
1460 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001461 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001462 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001463 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1464 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001465 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001466 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1467 VK_LValue, PropertyDiagLoc);
1468 MarkDeclRefReferenced(rhs);
1469 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001470 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001471 if (property->getPropertyAttributes() &
1472 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001473 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001474 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001475 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1476 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001477 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001478 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001479 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001480 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001481 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001482 Diag(FuncDecl->getLocStart(),
1483 diag::note_callee_decl) << FuncDecl;
1484 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001485 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001486 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001487 }
1488 }
1489
Ted Kremenekac597f32010-03-12 00:46:40 +00001490 if (IC) {
1491 if (Synthesize)
1492 if (ObjCPropertyImplDecl *PPIDecl =
1493 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001494 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001495 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1496 << PropertyIvar;
1497 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1498 }
1499
1500 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001501 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001502 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001503 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001504 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001505 }
1506 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001507 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001508 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001509 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001510 // Diagnose if an ivar was lazily synthesdized due to a previous
1511 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001512 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001513 ObjCInterfaceDecl *ClassDeclared=nullptr;
1514 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001515 if (!Synthesize)
1516 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1517 else {
1518 if (PropertyIvar && PropertyIvar != PropertyId)
1519 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1520 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001521 // Issue diagnostics only if Ivar belongs to current class.
1522 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001523 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001524 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1525 << PropertyId;
1526 Ivar->setInvalidDecl();
1527 }
1528 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001529 } else {
1530 if (Synthesize)
1531 if (ObjCPropertyImplDecl *PPIDecl =
1532 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001533 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001534 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1535 << PropertyIvar;
1536 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1537 }
1538
1539 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001540 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001541 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001542 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001543 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001544 }
1545 CatImplClass->addPropertyImplementation(PIDecl);
1546 }
1547
John McCall48871652010-08-21 09:40:31 +00001548 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001549}
1550
1551//===----------------------------------------------------------------------===//
1552// Helper methods.
1553//===----------------------------------------------------------------------===//
1554
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001555/// DiagnosePropertyMismatch - Compares two properties for their
1556/// attributes and types and warns on a variety of inconsistencies.
1557///
1558void
1559Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1560 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001561 const IdentifierInfo *inheritedName,
1562 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001563 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001564 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001565 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001566 SuperProperty->getPropertyAttributes();
1567
1568 // We allow readonly properties without an explicit ownership
1569 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1570 // to be overridden by a property with any explicit ownership in the subclass.
1571 if (!OverridingProtocolProperty &&
1572 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1573 ;
1574 else {
1575 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1576 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1577 Diag(Property->getLocation(), diag::warn_readonly_property)
1578 << Property->getDeclName() << inheritedName;
1579 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1580 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001581 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001582 << Property->getDeclName() << "copy" << inheritedName;
1583 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1584 unsigned CAttrRetain =
1585 (CAttr &
1586 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1587 unsigned SAttrRetain =
1588 (SAttr &
1589 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1590 bool CStrong = (CAttrRetain != 0);
1591 bool SStrong = (SAttrRetain != 0);
1592 if (CStrong != SStrong)
1593 Diag(Property->getLocation(), diag::warn_property_attribute)
1594 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1595 }
John McCall31168b02011-06-15 23:02:42 +00001596 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001597
Douglas Gregor429183e2015-12-09 22:57:32 +00001598 // Check for nonatomic; note that nonatomic is effectively
1599 // meaningless for readonly properties, so don't diagnose if the
1600 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001601 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001602 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001603 Diag(Property->getLocation(), diag::warn_property_attribute)
1604 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001605 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1606 }
1607 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001608 Diag(Property->getLocation(), diag::warn_property_attribute)
1609 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001610 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1611 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001612
1613 QualType LHSType =
1614 Context.getCanonicalType(SuperProperty->getType());
1615 QualType RHSType =
1616 Context.getCanonicalType(Property->getType());
1617
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001618 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001619 // Do cases not handled in above.
1620 // FIXME. For future support of covariant property types, revisit this.
1621 bool IncompatibleObjC = false;
1622 QualType ConvertedType;
1623 if (!isObjCPointerConversion(RHSType, LHSType,
1624 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001625 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001626 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1627 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001628 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1629 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001630 }
1631}
1632
1633bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1634 ObjCMethodDecl *GetterMethod,
1635 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001636 if (!GetterMethod)
1637 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001638 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001639 QualType PropertyRValueType =
1640 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1641 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001642 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001643 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1644 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001645 if ((propertyObjCPtr =
1646 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001647 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1648 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001649 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001650 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001651 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001652 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001653 << GetterMethod->getSelector() << GetterType;
1654 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1655 return true;
1656 } else {
1657 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001658 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001659 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1660 if (lhsType != rhsType && lhsType->isArithmeticType())
1661 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001662 }
1663 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001664
1665 if (!compat) {
1666 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1667 << property->getDeclName()
1668 << GetterMethod->getSelector();
1669 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1670 return true;
1671 }
1672
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001673 return false;
1674}
1675
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001676/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001677/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001678static void
1679CollectImmediateProperties(ObjCContainerDecl *CDecl,
1680 ObjCContainerDecl::PropertyMap &PropMap,
1681 ObjCContainerDecl::PropertyMap &SuperPropMap,
1682 bool CollectClassPropsOnly = false,
1683 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001684 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001685 for (auto *Prop : IDecl->properties()) {
1686 if (CollectClassPropsOnly && !Prop->isClassProperty())
1687 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001688 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1689 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001690 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001691
1692 // Collect the properties from visible extensions.
1693 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001694 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1695 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001696
Ted Kremenek204c3c52014-02-22 00:02:03 +00001697 if (IncludeProtocols) {
1698 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001699 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001700 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1701 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001702 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001703 }
1704 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001705 for (auto *Prop : CATDecl->properties()) {
1706 if (CollectClassPropsOnly && !Prop->isClassProperty())
1707 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001708 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1709 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001710 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001711 if (IncludeProtocols) {
1712 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001713 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001714 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1715 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001716 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001717 }
1718 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001719 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001720 if (CollectClassPropsOnly && !Prop->isClassProperty())
1721 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001722 ObjCPropertyDecl *PropertyFromSuper =
1723 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1724 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001725 // Exclude property for protocols which conform to class's super-class,
1726 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001727 if (!PropertyFromSuper ||
1728 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001729 ObjCPropertyDecl *&PropEntry =
1730 PropMap[std::make_pair(Prop->getIdentifier(),
1731 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001732 if (!PropEntry)
1733 PropEntry = Prop;
1734 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001735 }
Manman Ren16a7d632016-04-12 23:01:55 +00001736 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001737 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001738 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1739 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001740 }
1741}
1742
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001743/// CollectSuperClassPropertyImplementations - This routine collects list of
1744/// properties to be implemented in super class(s) and also coming from their
1745/// conforming protocols.
1746static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001747 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001748 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001749 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001750 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001751 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001752 SDecl = SDecl->getSuperClass();
1753 }
1754 }
1755}
1756
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001757/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1758/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1759/// declared in class 'IFace'.
1760bool
1761Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1762 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1763 if (!IV->getSynthesize())
1764 return false;
1765 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1766 Method->isInstanceMethod());
1767 if (!IMD || !IMD->isPropertyAccessor())
1768 return false;
1769
1770 // look up a property declaration whose one of its accessors is implemented
1771 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001772 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001773 if ((Property->getGetterName() == IMD->getSelector() ||
1774 Property->getSetterName() == IMD->getSelector()) &&
1775 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001776 return true;
1777 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001778 // Also look up property declaration in class extension whose one of its
1779 // accessors is implemented by this method.
1780 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001781 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001782 if ((Property->getGetterName() == IMD->getSelector() ||
1783 Property->getSetterName() == IMD->getSelector()) &&
1784 (Property->getPropertyIvarDecl() == IV))
1785 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001786 return false;
1787}
1788
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001789static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1790 ObjCPropertyDecl *Prop) {
1791 bool SuperClassImplementsGetter = false;
1792 bool SuperClassImplementsSetter = false;
1793 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1794 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001795
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001796 while (IDecl->getSuperClass()) {
1797 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1798 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1799 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001800
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001801 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1802 SuperClassImplementsSetter = true;
1803 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1804 return true;
1805 IDecl = IDecl->getSuperClass();
1806 }
1807 return false;
1808}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001809
James Dennett2a4d13c2012-06-15 07:13:21 +00001810/// \brief Default synthesizes all properties which must be synthesized
1811/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001812void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1813 ObjCInterfaceDecl *IDecl,
1814 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001815 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001816 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1817 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001818 if (PropMap.empty())
1819 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001820 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001821 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1822
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001823 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1824 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001825 // Is there a matching property synthesize/dynamic?
1826 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001827 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001828 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1829 continue;
1830 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001831 if (IMPDecl->FindPropertyImplDecl(
1832 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001833 continue;
1834 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1835 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1836 continue;
1837 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1838 continue;
1839 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001840 if (ObjCPropertyImplDecl *PID =
1841 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001842 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1843 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001844 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001845 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001846 continue;
1847 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001848 ObjCPropertyDecl *PropInSuperClass =
1849 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1850 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001851 if (ObjCProtocolDecl *Proto =
1852 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001853 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001854 // Suppress the warning if class's superclass implements property's
1855 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001856 // Or, if property is going to be implemented in its super class.
1857 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001858 Diag(IMPDecl->getLocation(),
1859 diag::warn_auto_synthesizing_protocol_property)
1860 << Prop << Proto;
1861 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001862 std::string FixIt =
1863 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1864 Diag(AtEnd, diag::note_add_synthesize_directive)
1865 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001866 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001867 continue;
1868 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001869 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001870 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001871 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1872 (PropInSuperClass->getPropertyAttributes() &
1873 ObjCPropertyDecl::OBJC_PR_readonly) &&
1874 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1875 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1876 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1877 << Prop->getIdentifier();
1878 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1879 }
1880 else {
1881 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1882 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001883 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001884 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1885 }
1886 continue;
1887 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001888 // We use invalid SourceLocations for the synthesized ivars since they
1889 // aren't really synthesized at a particular location; they just exist.
1890 // Saying that they are located at the @implementation isn't really going
1891 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001892 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1893 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1894 true,
1895 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001896 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001897 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001898 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001899 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001900 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001901 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001902 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001903}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904
Alex Lorenz6c9af502017-07-03 10:12:24 +00001905void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1906 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00001907 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001908 return;
1909 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1910 if (!IC)
1911 return;
1912 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001913 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00001914 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001915}
1916
Manman Ren08ce7342016-05-18 18:12:34 +00001917static void DiagnoseUnimplementedAccessor(
1918 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
1919 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
1920 ObjCPropertyDecl *Prop,
1921 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
1922 // Check to see if we have a corresponding selector in SMap and with the
1923 // right method type.
1924 auto I = std::find_if(SMap.begin(), SMap.end(),
1925 [&](const ObjCMethodDecl *x) {
1926 return x->getSelector() == Method &&
1927 x->isClassMethod() == Prop->isClassProperty();
1928 });
Ted Kremenek7e812952014-02-21 19:41:30 +00001929 // When reporting on missing property setter/getter implementation in
1930 // categories, do not report when they are declared in primary class,
1931 // class's protocol, or one of it super classes. This is because,
1932 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00001933 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001934 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001935 !PrimaryClass->lookupPropertyAccessor(Method, C,
1936 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00001937 unsigned diag =
1938 isa<ObjCCategoryDecl>(CDecl)
1939 ? (Prop->isClassProperty()
1940 ? diag::warn_impl_required_in_category_for_class_property
1941 : diag::warn_setter_getter_impl_required_in_category)
1942 : (Prop->isClassProperty()
1943 ? diag::warn_impl_required_for_class_property
1944 : diag::warn_setter_getter_impl_required);
1945 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
1946 S.Diag(Prop->getLocation(), diag::note_property_declare);
1947 if (S.LangOpts.ObjCDefaultSynthProperties &&
1948 S.LangOpts.ObjCRuntime.isNonFragile())
1949 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1950 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1951 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1952 }
Ted Kremenek7e812952014-02-21 19:41:30 +00001953}
1954
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001955void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001956 ObjCContainerDecl *CDecl,
1957 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001958 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001959 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1960
Manman Ren16a7d632016-04-12 23:01:55 +00001961 // Since we don't synthesize class properties, we should emit diagnose even
1962 // if SynthesizeProperties is true.
1963 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1964 // Gather properties which need not be implemented in this class
1965 // or category.
1966 if (!IDecl)
1967 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1968 // For categories, no need to implement properties declared in
1969 // its primary class (and its super classes) if property is
1970 // declared in one of those containers.
1971 if ((IDecl = C->getClassInterface())) {
1972 ObjCInterfaceDecl::PropertyDeclOrder PO;
1973 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001974 }
Manman Ren16a7d632016-04-12 23:01:55 +00001975 }
1976 if (IDecl)
1977 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001978
Manman Ren16a7d632016-04-12 23:01:55 +00001979 // When SynthesizeProperties is true, we only check class properties.
1980 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
1981 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001982
Ted Kremenek38882022014-02-21 19:41:39 +00001983 // Scan the @interface to see if any of the protocols it adopts
1984 // require an explicit implementation, via attribute
1985 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001986 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001987 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001988
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001989 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001990 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1991 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001992 // Lazily construct a set of all the properties in the @interface
1993 // of the class, without looking at the superclass. We cannot
1994 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001995 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001996 // as scans the adopted protocols. This work only triggers for protocols
1997 // with the attribute, which is very rare, and only occurs when
1998 // analyzing the @implementation.
1999 if (!LazyMap) {
2000 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2001 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2002 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002003 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002004 /* IncludeProtocols */ false);
2005 }
Ted Kremenek38882022014-02-21 19:41:39 +00002006 // Add the properties of 'PDecl' to the list of properties that
2007 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002008 for (auto *PropDecl : PDecl->properties()) {
2009 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2010 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002011 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002012 PropMap[std::make_pair(PropDecl->getIdentifier(),
2013 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002014 }
2015 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002016 }
Ted Kremenek38882022014-02-21 19:41:39 +00002017
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002018 if (PropMap.empty())
2019 return;
2020
2021 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002022 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002023 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002024
Manman Ren08ce7342016-05-18 18:12:34 +00002025 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002026 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002027 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002028 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002029
2030 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002031 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002032 if (C && !C->IsClassExtension())
2033 if ((PrimaryClass = C->getClassInterface()))
2034 // Report unimplemented properties in the category as well.
2035 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2036 // When reporting on missing setter/getters, do not report when
2037 // setter/getter is implemented in category's primary class
2038 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002039 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002040 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002041 }
2042
Anna Zaks673d76b2012-10-18 19:17:53 +00002043 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002044 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2045 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002046 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002047 if (Prop->isInvalidDecl() ||
2048 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002049 PropImplMap.count(Prop) ||
2050 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002051 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002052
2053 // Diagnose unimplemented getters and setters.
2054 DiagnoseUnimplementedAccessor(*this,
2055 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2056 if (!Prop->isReadOnly())
2057 DiagnoseUnimplementedAccessor(*this,
2058 PrimaryClass, Prop->getSetterName(),
2059 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002060 }
2061}
2062
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002063void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002064 for (const auto *propertyImpl : impDecl->property_impls()) {
2065 const auto *property = propertyImpl->getPropertyDecl();
2066
2067 // Warn about null_resettable properties with synthesized setters,
2068 // because the setter won't properly handle nil.
2069 if (propertyImpl->getPropertyImplementation()
2070 == ObjCPropertyImplDecl::Synthesize &&
2071 (property->getPropertyAttributes() &
2072 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2073 property->getGetterMethodDecl() &&
2074 property->getSetterMethodDecl()) {
2075 auto *getterMethod = property->getGetterMethodDecl();
2076 auto *setterMethod = property->getSetterMethodDecl();
2077 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
2078 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
2079 SourceLocation loc = propertyImpl->getLocation();
2080 if (loc.isInvalid())
2081 loc = impDecl->getLocStart();
2082
2083 Diag(loc, diag::warn_null_resettable_setter)
2084 << setterMethod->getSelector() << property->getDeclName();
2085 }
2086 }
2087 }
2088}
2089
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002090void
2091Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002092 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002093 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002094 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002095 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002096 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002097 for (auto *Prop : IDecl->properties())
2098 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002099 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002100 for (auto *Prop : Ext->properties())
2101 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002102
Manman Renefe1bac2016-01-27 20:00:32 +00002103 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2104 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002105 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002106 ObjCMethodDecl *GetterMethod = nullptr;
2107 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002108 bool LookedUpGetterSetter = false;
2109
Bill Wendling44426052012-12-20 19:22:21 +00002110 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002111 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002112
John McCall43192862011-09-13 18:31:23 +00002113 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2114 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002115 GetterMethod = Property->isClassProperty() ?
2116 IMPDecl->getClassMethod(Property->getGetterName()) :
2117 IMPDecl->getInstanceMethod(Property->getGetterName());
2118 SetterMethod = Property->isClassProperty() ?
2119 IMPDecl->getClassMethod(Property->getSetterName()) :
2120 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002121 LookedUpGetterSetter = true;
2122 if (GetterMethod) {
2123 Diag(GetterMethod->getLocation(),
2124 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002125 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002126 Diag(Property->getLocation(), diag::note_property_declare);
2127 }
2128 if (SetterMethod) {
2129 Diag(SetterMethod->getLocation(),
2130 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002131 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002132 Diag(Property->getLocation(), diag::note_property_declare);
2133 }
2134 }
2135
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002136 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00002137 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2138 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002139 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002140 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2141 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002142 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2143 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002144 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00002145 GetterMethod = Property->isClassProperty() ?
2146 IMPDecl->getClassMethod(Property->getGetterName()) :
2147 IMPDecl->getInstanceMethod(Property->getGetterName());
2148 SetterMethod = Property->isClassProperty() ?
2149 IMPDecl->getClassMethod(Property->getSetterName()) :
2150 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002151 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002152 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
2153 SourceLocation MethodLoc =
2154 (GetterMethod ? GetterMethod->getLocation()
2155 : SetterMethod->getLocation());
2156 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002157 << Property->getIdentifier() << (GetterMethod != nullptr)
2158 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002159 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002160 if (Property->getLParenLoc().isValid() &&
2161 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002162 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002163 SourceLocation AfterLParen =
2164 getLocForEndOfToken(Property->getLParenLoc());
2165 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2166 : "nonatomic";
2167 Diag(Property->getLocation(),
2168 diag::note_atomic_property_fixup_suggest)
2169 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2170 } else if (Property->getLParenLoc().isInvalid()) {
2171 //@property id etc.
2172 SourceLocation startLoc =
2173 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2174 Diag(Property->getLocation(),
2175 diag::note_atomic_property_fixup_suggest)
2176 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002177 }
2178 else
2179 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002180 Diag(Property->getLocation(), diag::note_property_declare);
2181 }
2182 }
2183 }
2184}
2185
John McCall31168b02011-06-15 23:02:42 +00002186void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002187 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002188 return;
2189
Aaron Ballmand85eff42014-03-14 15:02:45 +00002190 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002191 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002192 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002193 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002194 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002195 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2196 if (!method)
2197 continue;
2198 ObjCMethodFamily family = method->getMethodFamily();
2199 if (family == OMF_alloc || family == OMF_copy ||
2200 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002201 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002202 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002203 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002204 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002205
2206 // Look for a getter explicitly declared alongside the property.
2207 // If we find one, use its location for the note.
2208 SourceLocation noteLoc = PD->getLocation();
2209 SourceLocation fixItLoc;
2210 for (auto *getterRedecl : method->redecls()) {
2211 if (getterRedecl->isImplicit())
2212 continue;
2213 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2214 continue;
2215 noteLoc = getterRedecl->getLocation();
2216 fixItLoc = getterRedecl->getLocEnd();
2217 }
2218
2219 Preprocessor &PP = getPreprocessor();
2220 TokenValue tokens[] = {
2221 tok::kw___attribute, tok::l_paren, tok::l_paren,
2222 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2223 PP.getIdentifierInfo("none"), tok::r_paren,
2224 tok::r_paren, tok::r_paren
2225 };
2226 StringRef spelling = "__attribute__((objc_method_family(none)))";
2227 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2228 if (!macroName.empty())
2229 spelling = macroName;
2230
2231 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2232 << method->getDeclName() << spelling;
2233 if (fixItLoc.isValid()) {
2234 SmallString<64> fixItText(" ");
2235 fixItText += spelling;
2236 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2237 }
John McCall31168b02011-06-15 23:02:42 +00002238 }
2239 }
2240 }
2241}
2242
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002243void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002244 const ObjCImplementationDecl *ImplD,
2245 const ObjCInterfaceDecl *IFD) {
2246 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002247 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2248 if (!SuperD)
2249 return;
2250
2251 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002252 for (const auto *I : ImplD->instance_methods())
2253 if (I->getMethodFamily() == OMF_init)
2254 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002255
2256 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2257 SuperD->getDesignatedInitializers(DesignatedInits);
2258 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2259 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2260 const ObjCMethodDecl *MD = *I;
2261 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002262 bool Ignore = false;
2263 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2264 Ignore = IMD->isUnavailable();
2265 }
2266 if (!Ignore) {
2267 Diag(ImplD->getLocation(),
2268 diag::warn_objc_implementation_missing_designated_init_override)
2269 << MD->getSelector();
2270 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2271 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002272 }
2273 }
2274}
2275
John McCallad31b5f2010-11-10 07:01:40 +00002276/// AddPropertyAttrs - Propagates attributes from a property to the
2277/// implicitly-declared getter or setter for that property.
2278static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2279 ObjCPropertyDecl *Property) {
2280 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002281 for (const auto *A : Property->attrs()) {
2282 if (isa<DeprecatedAttr>(A) ||
2283 isa<UnavailableAttr>(A) ||
2284 isa<AvailabilityAttr>(A))
2285 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002286 }
John McCallad31b5f2010-11-10 07:01:40 +00002287}
2288
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002289/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2290/// have the property type and issue diagnostics if they don't.
2291/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002292/// appropriate lookup tables.
2293void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002294 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002295 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002296 if (CD->isInvalidDecl())
2297 return;
2298
Manman Rend36f7d52016-01-27 20:10:32 +00002299 bool IsClassProperty = property->isClassProperty();
2300 GetterMethod = IsClassProperty ?
2301 CD->getClassMethod(property->getGetterName()) :
2302 CD->getInstanceMethod(property->getGetterName());
2303
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002304 // if setter or getter is not found in class extension, it might be
2305 // in the primary class.
2306 if (!GetterMethod)
2307 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2308 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002309 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2310 getClassMethod(property->getGetterName()) :
2311 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002312 getInstanceMethod(property->getGetterName());
2313
Manman Rend36f7d52016-01-27 20:10:32 +00002314 SetterMethod = IsClassProperty ?
2315 CD->getClassMethod(property->getSetterName()) :
2316 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002317 if (!SetterMethod)
2318 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2319 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002320 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2321 getClassMethod(property->getSetterName()) :
2322 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002323 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002324 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2325 property->getLocation());
2326
Alex Lorenz535571a2017-03-30 13:33:51 +00002327 if (!property->isReadOnly() && SetterMethod) {
2328 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2329 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002330 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2331 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002332 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002333 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2334 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002335 Diag(property->getLocation(),
2336 diag::warn_accessor_property_type_mismatch)
2337 << property->getDeclName()
2338 << SetterMethod->getSelector();
2339 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2340 }
2341 }
2342
2343 // Synthesize getter/setter methods if none exist.
2344 // Find the default getter and if one not found, add one.
2345 // FIXME: The synthesized property we set here is misleading. We almost always
2346 // synthesize these methods unless the user explicitly provided prototypes
2347 // (which is odd, but allowed). Sema should be typechecking that the
2348 // declarations jive in that situation (which it is not currently).
2349 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002350 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002351 // Declare a getter method and add it to the list of methods
2352 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002353 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002354
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002355 // The getter returns the declared property type with all qualifiers
2356 // removed.
2357 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2358
Douglas Gregor849ebc22015-06-19 18:14:46 +00002359 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002360 if (property->getPropertyAttributes() &
2361 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2362 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002363 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002364 if (*nullability == NullabilityKind::Unspecified)
2365 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2366 modifiedTy, modifiedTy);
2367 }
2368 }
2369
Ted Kremenek2f075632010-09-21 20:52:59 +00002370 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2371 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002372 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002373 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002374 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002375 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002376 (property->getPropertyImplementation() ==
2377 ObjCPropertyDecl::Optional) ?
2378 ObjCMethodDecl::Optional :
2379 ObjCMethodDecl::Required);
2380 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002381
2382 AddPropertyAttrs(*this, GetterMethod, property);
2383
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002384 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002385 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2386 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002387
2388 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2389 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002390 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002391
2392 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002393 GetterMethod->addAttr(
2394 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2395 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002396
2397 if (getLangOpts().ObjCAutoRefCount)
2398 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002399 } else
2400 // A user declared getter will be synthesize when @synthesize of
2401 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002402 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002403 property->setGetterMethodDecl(GetterMethod);
2404
2405 // Skip setter if property is read-only.
2406 if (!property->isReadOnly()) {
2407 // Find the default setter and if one not found, add one.
2408 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002409 // No instance/class method of same name as property setter name was
2410 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002411 // Declare a setter method and add it to the list of methods
2412 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002413 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002414
2415 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002416 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002417 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002418 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002419 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002420 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002421 /*isImplicitlyDeclared=*/true,
2422 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002423 (property->getPropertyImplementation() ==
2424 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002425 ObjCMethodDecl::Optional :
2426 ObjCMethodDecl::Required);
2427
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002428 // Remove all qualifiers from the setter's parameter type.
2429 QualType paramTy =
2430 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2431
Douglas Gregor849ebc22015-06-19 18:14:46 +00002432 // If the property is null_resettable, the setter accepts a
2433 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002434 if (property->getPropertyAttributes() &
2435 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2436 QualType modifiedTy = paramTy;
2437 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2438 if (*nullability == NullabilityKind::Unspecified)
2439 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2440 modifiedTy, modifiedTy);
2441 }
2442 }
2443
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002444 // Invent the arguments for the setter. We don't bother making a
2445 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002446 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2447 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002448 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002449 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002450 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002451 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002452 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002453 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002454
2455 AddPropertyAttrs(*this, SetterMethod, property);
2456
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002457 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002458 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002459 SetterMethod->addAttr(
2460 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2461 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002462 // It's possible for the user to have set a very odd custom
2463 // setter selector that causes it to have a method family.
2464 if (getLangOpts().ObjCAutoRefCount)
2465 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002466 } else
2467 // A user declared setter will be synthesize when @synthesize of
2468 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002469 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002470 property->setSetterMethodDecl(SetterMethod);
2471 }
2472 // Add any synthesized methods to the global pool. This allows us to
2473 // handle the following, which is supported by GCC (and part of the design).
2474 //
2475 // @interface Foo
2476 // @property double bar;
2477 // @end
2478 //
2479 // void thisIsUnfortunate() {
2480 // id foo;
2481 // double bar = [foo bar];
2482 // }
2483 //
Manman Rend36f7d52016-01-27 20:10:32 +00002484 if (!IsClassProperty) {
2485 if (GetterMethod)
2486 AddInstanceMethodToGlobalPool(GetterMethod);
2487 if (SetterMethod)
2488 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002489 } else {
2490 if (GetterMethod)
2491 AddFactoryMethodToGlobalPool(GetterMethod);
2492 if (SetterMethod)
2493 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002494 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002495
2496 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2497 if (!CurrentClass) {
2498 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2499 CurrentClass = Cat->getClassInterface();
2500 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2501 CurrentClass = Impl->getClassInterface();
2502 }
2503 if (GetterMethod)
2504 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2505 if (SetterMethod)
2506 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002507}
2508
John McCall48871652010-08-21 09:40:31 +00002509void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002510 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002511 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002512 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002513 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002514 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002515 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002516
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002517 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2518 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2519 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2520 << "readonly" << "readwrite";
2521
2522 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2523 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002524
2525 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002526 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002527 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2528 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002529 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002530 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002531 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2532 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2533 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002534 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002535 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002536 }
2537
2538 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002539 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2540 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002541 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2542 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002543 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002544 }
Bill Wendling44426052012-12-20 19:22:21 +00002545 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002546 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2547 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002548 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002549 }
Bill Wendling44426052012-12-20 19:22:21 +00002550 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002551 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2552 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002553 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002554 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002555 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002556 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002557 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2558 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002559 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002560 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002561 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002562 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002563 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2564 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002565 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2566 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002567 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002568 }
Bill Wendling44426052012-12-20 19:22:21 +00002569 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002570 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2571 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002572 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002573 }
Bill Wendling44426052012-12-20 19:22:21 +00002574 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002575 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2576 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002577 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002578 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002579 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002580 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002581 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2582 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002583 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002584 }
Bill Wendling44426052012-12-20 19:22:21 +00002585 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2586 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002587 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2588 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002589 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002590 }
Bill Wendling44426052012-12-20 19:22:21 +00002591 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002592 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2593 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002594 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002595 }
Bill Wendling44426052012-12-20 19:22:21 +00002596 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002597 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2598 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002599 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002600 }
2601 }
Bill Wendling44426052012-12-20 19:22:21 +00002602 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2603 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002604 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2605 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002606 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002607 }
Bill Wendling44426052012-12-20 19:22:21 +00002608 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2609 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002610 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2611 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002612 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002613 }
2614
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002615 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002616 // 'weak' and 'nonnull' are mutually exclusive.
2617 if (auto nullability = PropertyTy->getNullability(Context)) {
2618 if (*nullability == NullabilityKind::NonNull)
2619 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2620 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002621 }
2622 }
2623
Bill Wendling44426052012-12-20 19:22:21 +00002624 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2625 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002626 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2627 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002628 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002629 }
2630
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002631 // Warn if user supplied no assignment attribute, property is
2632 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002633 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2634 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2635 // do nothing
2636 } else if (getLangOpts().ObjCAutoRefCount) {
2637 // With arc, @property definitions should default to strong when
2638 // not specified.
2639 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2640 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002641 bool isAnyClassTy =
2642 (PropertyTy->isObjCClassType() ||
2643 PropertyTy->isObjCQualifiedClassType());
2644 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2645 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002646 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002647 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002648 else if (propertyInPrimaryClass) {
2649 // Don't issue warning on property with no life time in class
2650 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002651 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002652 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002653 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002654
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002655 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002656 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002657 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002658 }
John McCallb61e14e2015-10-27 04:54:50 +00002659 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002660
2661 // FIXME: Implement warning dependent on NSCopying being
2662 // implemented. See also:
2663 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2664 // (please trim this list while you are at it).
2665 }
2666
Bill Wendling44426052012-12-20 19:22:21 +00002667 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2668 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002669 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002670 && PropertyTy->isBlockPointerType())
2671 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002672 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2673 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2674 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002675 PropertyTy->isBlockPointerType())
2676 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002677
Bill Wendling44426052012-12-20 19:22:21 +00002678 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2679 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002680 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002681}