blob: 0beb336092665d7b1d86988f9a72835e4e0f20f3 [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
156static unsigned getOwnershipRule(unsigned attr) {
157 return attr & (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}
164
John McCall48871652010-08-21 09:40:31 +0000165Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000166 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000167 FieldDeclarator &FD,
168 ObjCDeclSpec &ODS,
169 Selector GetterSel,
170 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000171 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000172 tok::ObjCKeywordKind MethodImplKind,
173 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000174 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000175 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000176 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
177 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000178 if (!getOwnershipRule(Attributes)) {
179 Attributes |= deducePropertyOwnershipFromType(*this, T);
180 }
Bill Wendling44426052012-12-20 19:22:21 +0000181 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000182 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000183 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000184
185 // Property defaults to 'assign' if it is readwrite, unless this is ARC
186 // and the type is retainable.
187 bool isAssign;
188 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
189 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
190 isAssign = true;
191 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
192 isAssign = false;
193 } else {
194 isAssign = (!getLangOpts().ObjCAutoRefCount ||
195 !T->isObjCRetainableType());
196 }
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000197
Douglas Gregor90d34422013-01-21 19:05:22 +0000198 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000199 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000200 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000202 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000203 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000204 FD, GetterSel, SetterSel,
205 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000206 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000207 ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000208 isOverridingProperty, T, TSI,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000209 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000210 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000211 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000212 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000213 }
214
215 if (!Res) {
216 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
217 GetterSel, SetterSel, isAssign, isReadWrite,
218 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000219 T, TSI, 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;
308
309 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
310}
311
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000312static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000313 SourceLocation LParenLoc, SourceLocation &Loc) {
314 if (LParenLoc.isMacroID())
315 return false;
316
317 SourceManager &SM = Context.getSourceManager();
318 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
319 // Try to load the file buffer.
320 bool invalidTemp = false;
321 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
322 if (invalidTemp)
323 return false;
324 const char *tokenBegin = file.data() + locInfo.second;
325
326 // Lex from the start of the given location.
327 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
328 Context.getLangOpts(),
329 file.begin(), tokenBegin, file.end());
330 Token Tok;
331 do {
332 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000333 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000334 Loc = Tok.getLocation();
335 return true;
336 }
337 } while (Tok.isNot(tok::r_paren));
338 return false;
339
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000340}
341
Douglas Gregor429183e2015-12-09 22:57:32 +0000342/// Check for a mismatch in the atomicity of the given properties.
343static void checkAtomicPropertyMismatch(Sema &S,
344 ObjCPropertyDecl *OldProperty,
345 ObjCPropertyDecl *NewProperty) {
346 // If the atomicity of both matches, we're done.
347 bool OldIsAtomic =
348 (OldProperty->getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) == 0;
349 bool NewIsAtomic =
350 (NewProperty->getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nonatomic) == 0;
351 if (OldIsAtomic == NewIsAtomic) return;
352
353 // Determine whether the given property is readonly and implicitly
354 // atomic.
355 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
356 // Is it readonly?
357 auto Attrs = Property->getPropertyAttributes();
358 if ((Attrs & ObjCDeclSpec::DQ_PR_readonly) == 0) return false;
359
360 // Is it nonatomic?
361 if (Attrs & ObjCDeclSpec::DQ_PR_nonatomic) return false;
362
363 // Was 'atomic' specified directly?
364 if (Property->getPropertyAttributesAsWritten() & ObjCDeclSpec::DQ_PR_atomic)
365 return false;
366
367 return true;
368 };
369
370 // One of the properties is atomic; if it's a readonly property, and
371 // 'atomic' wasn't explicitly specified, we're okay.
372 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
373 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
374 return;
375
376 // Diagnose the conflict.
377 const IdentifierInfo *OldContextName;
378 auto *OldDC = OldProperty->getDeclContext();
379 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
380 OldContextName = Category->getClassInterface()->getIdentifier();
381 else
382 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
383
384 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
385 << NewProperty->getDeclName() << "atomic"
386 << OldContextName;
387 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
388}
389
Douglas Gregor90d34422013-01-21 19:05:22 +0000390ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000391Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000392 SourceLocation AtLoc,
393 SourceLocation LParenLoc,
394 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000395 Selector GetterSel, Selector SetterSel,
396 const bool isAssign,
397 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000398 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000399 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000400 bool *isOverridingProperty,
Douglas Gregor813a0662015-06-19 18:14:38 +0000401 QualType T,
402 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000403 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000404 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000405 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000406 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000407 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000408 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
409
Ted Kremenek959e8302010-03-12 02:31:10 +0000410 // We need to look in the @interface to see if the @property was
411 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000412 if (!CCPrimary) {
413 Diag(CDecl->getLocation(), diag::err_continuation_class);
414 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000415 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000416 }
417
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000418 // Find the property in the extended class's primary class or
419 // extensions.
Ted Kremenek959e8302010-03-12 02:31:10 +0000420 ObjCPropertyDecl *PIDecl =
421 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
422
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000423 // If we found a property in an extension, complain.
424 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
425 Diag(AtLoc, diag::err_duplicate_property);
426 Diag(PIDecl->getLocation(), diag::note_property_declare);
427 return nullptr;
428 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000429
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000430 // Create a new ObjCPropertyDecl with the DeclContext being
431 // the class extension.
432 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
433 FD, GetterSel, SetterSel,
434 isAssign, isReadWrite,
435 Attributes, AttributesAsWritten,
436 T, TSI, MethodImplKind, DC);
437
438 // If there was no declaration of a property with the same name in
439 // the primary class, we're done.
440 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000441 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000442 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000443 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000444
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000445 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
446 bool IncompatibleObjC = false;
447 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000448 // Relax the strict type matching for property type in continuation class.
449 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000450 // as it narrows the object type in its primary class property. Note that
451 // this conversion is safe only because the wider type is for a 'readonly'
452 // property in primary class and 'narrowed' type for a 'readwrite' property
453 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000454 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
455 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
456 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
457 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
458 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000459 ConvertedType, IncompatibleObjC))
460 || IncompatibleObjC) {
461 Diag(AtLoc,
462 diag::err_type_mismatch_continuation_class) << PDecl->getType();
463 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000464 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000465 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000466 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000467
468 // A readonly property declared in the primary class can be refined
Nico Weberbaf4b7d2015-12-03 02:25:26 +0000469 // by adding a readwrite property within an extension.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000470 // Anything else is an error.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000471 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000472 if (!(isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly))) {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000473 // Tailor the diagnostics for the common case where a readwrite
474 // property is declared both in the @interface and the continuation.
475 // This is a common error where the user often intended the original
476 // declaration to be readonly.
477 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000478 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000479 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
480 ? diag::err_use_continuation_class_redeclaration_readwrite
481 : diag::err_use_continuation_class;
482 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000483 << CCPrimary->getDeclName();
484 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000485 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000486 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000487
488 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
489 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
490 PIkind |= deducePropertyOwnershipFromType(*this, PIDecl->getType());
491 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
492 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
493 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
494 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
495 Diag(AtLoc, diag::warn_property_attr_mismatch);
496 Diag(PIDecl->getLocation(), diag::note_property_declare);
497 } else if (getLangOpts().ObjCAutoRefCount) {
498 QualType PrimaryPropertyQT =
499 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
500 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
501 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
502 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
503 PrimaryPropertyQT.getObjCLifetime();
504 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
505 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
506 !PropertyIsWeak) {
507 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
509 }
510 }
511 }
512
513 // Check that atomicity of property in class extension matches the previous
514 // declaration.
Douglas Gregor429183e2015-12-09 22:57:32 +0000515 checkAtomicPropertyMismatch(*this, PIDecl, PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000516
Ted Kremenek959e8302010-03-12 02:31:10 +0000517 *isOverridingProperty = true;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000518
Douglas Gregore17765e2015-11-03 17:02:34 +0000519 // Make sure getter/setter are appropriately synthesized.
520 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000521 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000522}
523
524ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
525 ObjCContainerDecl *CDecl,
526 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000527 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000528 FieldDeclarator &FD,
529 Selector GetterSel,
530 Selector SetterSel,
531 const bool isAssign,
532 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000533 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000534 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000535 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000536 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000537 tok::ObjCKeywordKind MethodImplKind,
538 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000539 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000540
541 // Issue a warning if property is 'assign' as default and its object, which is
542 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000543 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000544 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000545 if (const ObjCObjectPointerType *ObjPtrTy =
546 T->getAs<ObjCObjectPointerType>()) {
547 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
548 if (IDecl)
549 if (ObjCProtocolDecl* PNSCopying =
550 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
551 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
552 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000553 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000554
555 if (T->isObjCObjectType()) {
556 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000557 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000558 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
559 << FixItHint::CreateInsertion(StarLoc, "*");
560 T = Context.getObjCObjectPointerType(T);
561 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
562 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
563 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000564
Ted Kremenek959e8302010-03-12 02:31:10 +0000565 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000566 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
567 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000568 PropertyId, AtLoc,
569 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000570
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000571 if (ObjCPropertyDecl *prevDecl =
572 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000573 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000574 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000575 PDecl->setInvalidDecl();
576 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000577 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000578 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000579 if (lexicalDC)
580 PDecl->setLexicalDeclContext(lexicalDC);
581 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000582
583 if (T->isArrayType() || T->isFunctionType()) {
584 Diag(AtLoc, diag::err_property_type) << T;
585 PDecl->setInvalidDecl();
586 }
587
588 ProcessDeclAttributes(S, PDecl, FD.D);
589
590 // Regardless of setter/getter attribute, we save the default getter/setter
591 // selector names in anticipation of declaration of setter/getter methods.
592 PDecl->setGetterName(GetterSel);
593 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000594 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000595 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000596
Bill Wendling44426052012-12-20 19:22:21 +0000597 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
599
Bill Wendling44426052012-12-20 19:22:21 +0000600 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
602
Bill Wendling44426052012-12-20 19:22:21 +0000603 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
605
606 if (isReadWrite)
607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
608
Bill Wendling44426052012-12-20 19:22:21 +0000609 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
611
Bill Wendling44426052012-12-20 19:22:21 +0000612 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
614
Bill Wendling44426052012-12-20 19:22:21 +0000615 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
617
Bill Wendling44426052012-12-20 19:22:21 +0000618 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
620
Bill Wendling44426052012-12-20 19:22:21 +0000621 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
623
Ted Kremenekac597f32010-03-12 00:46:40 +0000624 if (isAssign)
625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
626
John McCall43192862011-09-13 18:31:23 +0000627 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000628 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000630 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000632
John McCall31168b02011-06-15 23:02:42 +0000633 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000634 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000635 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
636 if (isAssign)
637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
638
Ted Kremenekac597f32010-03-12 00:46:40 +0000639 if (MethodImplKind == tok::objc_required)
640 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
641 else if (MethodImplKind == tok::objc_optional)
642 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000643
Douglas Gregor813a0662015-06-19 18:14:38 +0000644 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
646
Douglas Gregor849ebc22015-06-19 18:14:46 +0000647 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
648 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
649
Ted Kremenek959e8302010-03-12 02:31:10 +0000650 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000651}
652
John McCall31168b02011-06-15 23:02:42 +0000653static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
654 ObjCPropertyDecl *property,
655 ObjCIvarDecl *ivar) {
656 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
657
John McCall31168b02011-06-15 23:02:42 +0000658 QualType ivarType = ivar->getType();
659 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000660
John McCall43192862011-09-13 18:31:23 +0000661 // The lifetime implied by the property's attributes.
662 Qualifiers::ObjCLifetime propertyLifetime =
663 getImpliedARCOwnership(property->getPropertyAttributes(),
664 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000665
John McCall43192862011-09-13 18:31:23 +0000666 // We're fine if they match.
667 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000668
John McCall460ce582015-10-22 18:38:17 +0000669 // None isn't a valid lifetime for an object ivar in ARC, and
670 // __autoreleasing is never valid; don't diagnose twice.
671 if ((ivarLifetime == Qualifiers::OCL_None &&
672 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000673 ivarLifetime == Qualifiers::OCL_Autoreleasing)
674 return;
John McCall31168b02011-06-15 23:02:42 +0000675
John McCalld8561f02012-08-20 23:36:59 +0000676 // If the ivar is private, and it's implicitly __unsafe_unretained
677 // becaues of its type, then pretend it was actually implicitly
678 // __strong. This is only sound because we're processing the
679 // property implementation before parsing any method bodies.
680 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
681 propertyLifetime == Qualifiers::OCL_Strong &&
682 ivar->getAccessControl() == ObjCIvarDecl::Private) {
683 SplitQualType split = ivarType.split();
684 if (split.Quals.hasObjCLifetime()) {
685 assert(ivarType->isObjCARCImplicitlyUnretainedType());
686 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
687 ivarType = S.Context.getQualifiedType(split);
688 ivar->setType(ivarType);
689 return;
690 }
691 }
692
John McCall43192862011-09-13 18:31:23 +0000693 switch (propertyLifetime) {
694 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000695 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000696 << property->getDeclName()
697 << ivar->getDeclName()
698 << ivarLifetime;
699 break;
John McCall31168b02011-06-15 23:02:42 +0000700
John McCall43192862011-09-13 18:31:23 +0000701 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000702 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000703 << property->getDeclName()
704 << ivar->getDeclName();
705 break;
John McCall31168b02011-06-15 23:02:42 +0000706
John McCall43192862011-09-13 18:31:23 +0000707 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000708 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000709 << property->getDeclName()
710 << ivar->getDeclName()
711 << ((property->getPropertyAttributesAsWritten()
712 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
713 break;
John McCall31168b02011-06-15 23:02:42 +0000714
John McCall43192862011-09-13 18:31:23 +0000715 case Qualifiers::OCL_Autoreleasing:
716 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000717
John McCall43192862011-09-13 18:31:23 +0000718 case Qualifiers::OCL_None:
719 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000720 return;
721 }
722
723 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000724 if (propertyImplLoc.isValid())
725 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000726}
727
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000728/// setImpliedPropertyAttributeForReadOnlyProperty -
729/// This routine evaludates life-time attributes for a 'readonly'
730/// property with no known lifetime of its own, using backing
731/// 'ivar's attribute, if any. If no backing 'ivar', property's
732/// life-time is assumed 'strong'.
733static void setImpliedPropertyAttributeForReadOnlyProperty(
734 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
735 Qualifiers::ObjCLifetime propertyLifetime =
736 getImpliedARCOwnership(property->getPropertyAttributes(),
737 property->getType());
738 if (propertyLifetime != Qualifiers::OCL_None)
739 return;
740
741 if (!ivar) {
742 // if no backing ivar, make property 'strong'.
743 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
744 return;
745 }
746 // property assumes owenership of backing ivar.
747 QualType ivarType = ivar->getType();
748 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
749 if (ivarLifetime == Qualifiers::OCL_Strong)
750 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
751 else if (ivarLifetime == Qualifiers::OCL_Weak)
752 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
753 return;
754}
Ted Kremenekac597f32010-03-12 00:46:40 +0000755
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000756/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
757/// in inherited protocols with mismatched types. Since any of them can
758/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000759static void
760DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
761 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000762 ObjCPropertyDecl *Property) {
763 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000764 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
765 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000766 PDecl->collectInheritedProtocolProperties(Property, PropMap);
767 }
768 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
769 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000770 for (const auto *PI : SDecl->all_referenced_protocols()) {
771 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000772 PDecl->collectInheritedProtocolProperties(Property, PropMap);
773 }
774 SDecl = SDecl->getSuperClass();
775 }
776
777 if (PropMap.empty())
778 return;
779
780 QualType RHSType = S.Context.getCanonicalType(Property->getType());
781 bool FirsTime = true;
782 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
783 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
784 ObjCPropertyDecl *Prop = I->second;
785 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
786 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
787 bool IncompatibleObjC = false;
788 QualType ConvertedType;
789 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
790 || IncompatibleObjC) {
791 if (FirsTime) {
792 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
793 << Property->getType();
794 FirsTime = false;
795 }
796 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
797 << Prop->getType();
798 }
799 }
800 }
801 if (!FirsTime && AtLoc.isValid())
802 S.Diag(AtLoc, diag::note_property_synthesize);
803}
804
Ted Kremenekac597f32010-03-12 00:46:40 +0000805/// ActOnPropertyImplDecl - This routine performs semantic checks and
806/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000807/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000808///
John McCall48871652010-08-21 09:40:31 +0000809Decl *Sema::ActOnPropertyImplDecl(Scope *S,
810 SourceLocation AtLoc,
811 SourceLocation PropertyLoc,
812 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000813 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000814 IdentifierInfo *PropertyIvar,
815 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000816 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000817 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000818 // Make sure we have a context for the property implementation declaration.
819 if (!ClassImpDecl) {
820 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000821 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000822 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000823 if (PropertyIvarLoc.isInvalid())
824 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000825 SourceLocation PropertyDiagLoc = PropertyLoc;
826 if (PropertyDiagLoc.isInvalid())
827 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000828 ObjCPropertyDecl *property = nullptr;
829 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000830 // Find the class or category class where this property must have
831 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000832 ObjCImplementationDecl *IC = nullptr;
833 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000834 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
835 IDecl = IC->getClassInterface();
836 // We always synthesize an interface for an implementation
837 // without an interface decl. So, IDecl is always non-zero.
838 assert(IDecl &&
839 "ActOnPropertyImplDecl - @implementation without @interface");
840
841 // Look for this property declaration in the @implementation's @interface
842 property = IDecl->FindPropertyDeclaration(PropertyId);
843 if (!property) {
844 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000845 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000846 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000847 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000848 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
849 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000850 if (AtLoc.isValid())
851 Diag(AtLoc, diag::warn_implicit_atomic_property);
852 else
853 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
854 Diag(property->getLocation(), diag::note_property_declare);
855 }
856
Ted Kremenekac597f32010-03-12 00:46:40 +0000857 if (const ObjCCategoryDecl *CD =
858 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
859 if (!CD->IsClassExtension()) {
860 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
861 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000862 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000863 }
864 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000865 if (Synthesize&&
866 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
867 property->hasAttr<IBOutletAttr>() &&
868 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000869 bool ReadWriteProperty = false;
870 // Search into the class extensions and see if 'readonly property is
871 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000872 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000873 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
874 if (!R.empty())
875 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
876 PIkind = ExtProp->getPropertyAttributesAsWritten();
877 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
878 ReadWriteProperty = true;
879 break;
880 }
881 }
882 }
883
884 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000885 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000886 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000887 SourceLocation readonlyLoc;
888 if (LocPropertyAttribute(Context, "readonly",
889 property->getLParenLoc(), readonlyLoc)) {
890 SourceLocation endLoc =
891 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
892 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
893 Diag(property->getLocation(),
894 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
895 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
896 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000897 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000898 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000899 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
900 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000901
Ted Kremenekac597f32010-03-12 00:46:40 +0000902 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
903 if (Synthesize) {
904 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000905 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000906 }
907 IDecl = CatImplClass->getClassInterface();
908 if (!IDecl) {
909 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000910 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000911 }
912 ObjCCategoryDecl *Category =
913 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
914
915 // If category for this implementation not found, it is an error which
916 // has already been reported eralier.
917 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000918 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000919 // Look for this property declaration in @implementation's category
920 property = Category->FindPropertyDeclaration(PropertyId);
921 if (!property) {
922 Diag(PropertyLoc, diag::error_bad_category_property_decl)
923 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000924 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000925 }
926 } else {
927 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000928 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000929 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000930 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000931 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000932 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000933 // Check that we have a valid, previously declared ivar for @synthesize
934 if (Synthesize) {
935 // @synthesize
936 if (!PropertyIvar)
937 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000938 // Check that this is a previously declared 'ivar' in 'IDecl' interface
939 ObjCInterfaceDecl *ClassDeclared;
940 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
941 QualType PropType = property->getType();
942 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000943
944 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000945 diag::err_incomplete_synthesized_property,
946 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000947 Diag(property->getLocation(), diag::note_property_declare);
948 CompleteTypeErr = true;
949 }
950
David Blaikiebbafb8a2012-03-11 07:00:24 +0000951 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000952 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000953 ObjCPropertyDecl::OBJC_PR_readonly) &&
954 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000955 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
956 }
957
John McCall31168b02011-06-15 23:02:42 +0000958 ObjCPropertyDecl::PropertyAttributeKind kind
959 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000960
John McCall460ce582015-10-22 18:38:17 +0000961 bool isARCWeak = false;
962 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
963 // Add GC __weak to the ivar type if the property is weak.
964 if (getLangOpts().getGC() != LangOptions::NonGC) {
965 assert(!getLangOpts().ObjCAutoRefCount);
966 if (PropertyIvarType.isObjCGCStrong()) {
967 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
968 Diag(property->getLocation(), diag::note_property_declare);
969 } else {
970 PropertyIvarType =
971 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
972 }
973
974 // Otherwise, check whether ARC __weak is enabled and works with
975 // the property type.
John McCall43192862011-09-13 18:31:23 +0000976 } else {
John McCall460ce582015-10-22 18:38:17 +0000977 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +0000978 // Only complain here when synthesizing an ivar.
979 if (!Ivar) {
980 Diag(PropertyDiagLoc,
981 getLangOpts().ObjCWeakRuntime
982 ? diag::err_synthesizing_arc_weak_property_disabled
983 : diag::err_synthesizing_arc_weak_property_no_runtime);
984 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +0000985 }
John McCallb61e14e2015-10-27 04:54:50 +0000986 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +0000987 } else {
988 isARCWeak = true;
989 if (const ObjCObjectPointerType *ObjT =
990 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
991 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
992 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
993 Diag(property->getLocation(),
994 diag::err_arc_weak_unavailable_property)
995 << PropertyIvarType;
996 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
997 << ClassImpDecl->getName();
998 }
999 }
1000 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001001 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001002 }
John McCall460ce582015-10-22 18:38:17 +00001003
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001004 if (AtLoc.isInvalid()) {
1005 // Check when default synthesizing a property that there is
1006 // an ivar matching property name and issue warning; since this
1007 // is the most common case of not using an ivar used for backing
1008 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001009 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001010 ObjCIvarDecl *originalIvar =
1011 IDecl->lookupInstanceVariable(property->getIdentifier(),
1012 ClassDeclared);
1013 if (originalIvar) {
1014 Diag(PropertyDiagLoc,
1015 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001016 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001017 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001018 Diag(property->getLocation(), diag::note_property_declare);
1019 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001020 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001021 }
1022
1023 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001024 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001025 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001026 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001027 !PropertyIvarType.getObjCLifetime() &&
1028 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001029
John McCall43192862011-09-13 18:31:23 +00001030 // It's an error if we have to do this and the user didn't
1031 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001032 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +00001033 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001034 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001035 diag::err_arc_objc_property_default_assign_on_object);
1036 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001037 } else {
1038 Qualifiers::ObjCLifetime lifetime =
1039 getImpliedARCOwnership(kind, PropertyIvarType);
1040 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001041
John McCall31168b02011-06-15 23:02:42 +00001042 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001043 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001044 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1045 }
John McCall31168b02011-06-15 23:02:42 +00001046 }
1047
Abramo Bagnaradff19302011-03-08 08:55:46 +00001048 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001049 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001050 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001051 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001052 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001053 if (RequireNonAbstractType(PropertyIvarLoc,
1054 PropertyIvarType,
1055 diag::err_abstract_type_in_decl,
1056 AbstractSynthesizedIvarType)) {
1057 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001058 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001059 } else if (CompleteTypeErr)
1060 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001061 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001062 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001063
John McCall5fb5df92012-06-20 06:18:46 +00001064 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001065 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1066 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001067 // Note! I deliberately want it to fall thru so, we have a
1068 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001069 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001070 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001071 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001072 << property->getDeclName() << Ivar->getDeclName()
1073 << ClassDeclared->getDeclName();
1074 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001075 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001076 // Note! I deliberately want it to fall thru so more errors are caught.
1077 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001078 property->setPropertyIvarDecl(Ivar);
1079
Ted Kremenekac597f32010-03-12 00:46:40 +00001080 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1081
1082 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001083 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001084 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001085 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001086 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001087 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001088 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001089 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001090 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001091 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1092 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001093 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001094 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001095 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001096 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001097 << property->getDeclName() << PropType
1098 << Ivar->getDeclName() << IvarType;
1099 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001100 // Note! I deliberately want it to fall thru so, we have a
1101 // a property implementation and to avoid future warnings.
1102 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001103 else {
1104 // FIXME! Rules for properties are somewhat different that those
1105 // for assignments. Use a new routine to consolidate all cases;
1106 // specifically for property redeclarations as well as for ivars.
1107 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1108 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1109 if (lhsType != rhsType &&
1110 lhsType->isArithmeticType()) {
1111 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1112 << property->getDeclName() << PropType
1113 << Ivar->getDeclName() << IvarType;
1114 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1115 // Fall thru - see previous comment
1116 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001117 }
1118 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001119 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001120 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001121 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001122 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001123 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001124 // Fall thru - see previous comment
1125 }
John McCall31168b02011-06-15 23:02:42 +00001126 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001127 if ((property->getType()->isObjCObjectPointerType() ||
1128 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001129 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001130 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001131 << property->getDeclName() << Ivar->getDeclName();
1132 // Fall thru - see previous comment
1133 }
1134 }
John McCall460ce582015-10-22 18:38:17 +00001135 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1136 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001137 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001138 } else if (PropertyIvar)
1139 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001140 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001141
Ted Kremenekac597f32010-03-12 00:46:40 +00001142 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1143 ObjCPropertyImplDecl *PIDecl =
1144 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1145 property,
1146 (Synthesize ?
1147 ObjCPropertyImplDecl::Synthesize
1148 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001149 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001150
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001151 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001152 PIDecl->setInvalidDecl();
1153
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001154 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1155 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001156 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001157 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001158 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1159 // returned by the getter as it must conform to C++'s copy-return rules.
1160 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001161 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001162 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1163 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001164 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001165 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001166 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001167 Expr *LoadSelfExpr =
1168 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001169 CK_LValueToRValue, SelfExpr, nullptr,
1170 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001171 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001172 new (Context) ObjCIvarRefExpr(Ivar,
1173 Ivar->getUsageType(SelfDecl->getType()),
1174 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001175 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001176 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001177 ExprResult Res = PerformCopyInitialization(
1178 InitializedEntity::InitializeResult(PropertyDiagLoc,
1179 getterMethod->getReturnType(),
1180 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001181 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001182 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001183 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001184 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001185 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001186 PIDecl->setGetterCXXConstructor(ResExpr);
1187 }
1188 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001189 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1190 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1191 Diag(getterMethod->getLocation(),
1192 diag::warn_property_getter_owning_mismatch);
1193 Diag(property->getLocation(), diag::note_property_declare);
1194 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001195 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1196 switch (getterMethod->getMethodFamily()) {
1197 case OMF_retain:
1198 case OMF_retainCount:
1199 case OMF_release:
1200 case OMF_autorelease:
1201 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1202 << 1 << getterMethod->getSelector();
1203 break;
1204 default:
1205 break;
1206 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001207 }
1208 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1209 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001210 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1211 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001212 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001213 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001214 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1215 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001216 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001217 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001218 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001219 Expr *LoadSelfExpr =
1220 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001221 CK_LValueToRValue, SelfExpr, nullptr,
1222 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001223 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001224 new (Context) ObjCIvarRefExpr(Ivar,
1225 Ivar->getUsageType(SelfDecl->getType()),
1226 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001227 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001228 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001229 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1230 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001231 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001232 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1233 VK_LValue, PropertyDiagLoc);
1234 MarkDeclRefReferenced(rhs);
1235 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001236 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001237 if (property->getPropertyAttributes() &
1238 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001239 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001240 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001241 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1242 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001243 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001244 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001245 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001246 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001247 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001248 Diag(FuncDecl->getLocStart(),
1249 diag::note_callee_decl) << FuncDecl;
1250 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001251 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001252 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001253 }
1254 }
1255
Ted Kremenekac597f32010-03-12 00:46:40 +00001256 if (IC) {
1257 if (Synthesize)
1258 if (ObjCPropertyImplDecl *PPIDecl =
1259 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1260 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1261 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1262 << PropertyIvar;
1263 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1264 }
1265
1266 if (ObjCPropertyImplDecl *PPIDecl
1267 = IC->FindPropertyImplDecl(PropertyId)) {
1268 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1269 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001270 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001271 }
1272 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001273 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001274 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001275 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001276 // Diagnose if an ivar was lazily synthesdized due to a previous
1277 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001278 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001279 ObjCInterfaceDecl *ClassDeclared=nullptr;
1280 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001281 if (!Synthesize)
1282 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1283 else {
1284 if (PropertyIvar && PropertyIvar != PropertyId)
1285 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1286 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001287 // Issue diagnostics only if Ivar belongs to current class.
1288 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001289 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001290 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1291 << PropertyId;
1292 Ivar->setInvalidDecl();
1293 }
1294 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001295 } else {
1296 if (Synthesize)
1297 if (ObjCPropertyImplDecl *PPIDecl =
1298 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001299 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001300 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1301 << PropertyIvar;
1302 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1303 }
1304
1305 if (ObjCPropertyImplDecl *PPIDecl =
1306 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001307 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001308 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001309 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001310 }
1311 CatImplClass->addPropertyImplementation(PIDecl);
1312 }
1313
John McCall48871652010-08-21 09:40:31 +00001314 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001315}
1316
1317//===----------------------------------------------------------------------===//
1318// Helper methods.
1319//===----------------------------------------------------------------------===//
1320
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001321/// DiagnosePropertyMismatch - Compares two properties for their
1322/// attributes and types and warns on a variety of inconsistencies.
1323///
1324void
1325Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1326 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001327 const IdentifierInfo *inheritedName,
1328 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001329 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001330 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001331 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001332 SuperProperty->getPropertyAttributes();
1333
1334 // We allow readonly properties without an explicit ownership
1335 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1336 // to be overridden by a property with any explicit ownership in the subclass.
1337 if (!OverridingProtocolProperty &&
1338 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1339 ;
1340 else {
1341 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1342 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1343 Diag(Property->getLocation(), diag::warn_readonly_property)
1344 << Property->getDeclName() << inheritedName;
1345 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1346 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001347 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001348 << Property->getDeclName() << "copy" << inheritedName;
1349 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1350 unsigned CAttrRetain =
1351 (CAttr &
1352 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1353 unsigned SAttrRetain =
1354 (SAttr &
1355 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1356 bool CStrong = (CAttrRetain != 0);
1357 bool SStrong = (SAttrRetain != 0);
1358 if (CStrong != SStrong)
1359 Diag(Property->getLocation(), diag::warn_property_attribute)
1360 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1361 }
John McCall31168b02011-06-15 23:02:42 +00001362 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001363
Douglas Gregor429183e2015-12-09 22:57:32 +00001364 // Check for nonatomic; note that nonatomic is effectively
1365 // meaningless for readonly properties, so don't diagnose if the
1366 // atomic property is 'readonly'.
1367 checkAtomicPropertyMismatch(*this, SuperProperty, Property);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001368 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001369 Diag(Property->getLocation(), diag::warn_property_attribute)
1370 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001371 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1372 }
1373 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001374 Diag(Property->getLocation(), diag::warn_property_attribute)
1375 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001376 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1377 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001378
1379 QualType LHSType =
1380 Context.getCanonicalType(SuperProperty->getType());
1381 QualType RHSType =
1382 Context.getCanonicalType(Property->getType());
1383
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001384 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001385 // Do cases not handled in above.
1386 // FIXME. For future support of covariant property types, revisit this.
1387 bool IncompatibleObjC = false;
1388 QualType ConvertedType;
1389 if (!isObjCPointerConversion(RHSType, LHSType,
1390 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001391 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001392 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1393 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001394 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1395 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001396 }
1397}
1398
1399bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1400 ObjCMethodDecl *GetterMethod,
1401 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001402 if (!GetterMethod)
1403 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001404 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001405 QualType PropertyIvarType = property->getType().getNonReferenceType();
1406 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1407 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001408 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1409 const ObjCObjectPointerType *getterObjCPtr = nullptr;
1410 if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) &&
1411 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1412 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001413 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001414 != Compatible) {
1415 Diag(Loc, diag::error_property_accessor_type)
1416 << property->getDeclName() << PropertyIvarType
1417 << GetterMethod->getSelector() << GetterType;
1418 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1419 return true;
1420 } else {
1421 compat = true;
1422 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1423 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1424 if (lhsType != rhsType && lhsType->isArithmeticType())
1425 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001426 }
1427 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001428
1429 if (!compat) {
1430 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1431 << property->getDeclName()
1432 << GetterMethod->getSelector();
1433 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1434 return true;
1435 }
1436
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001437 return false;
1438}
1439
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001440/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001441/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001442static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1443 ObjCContainerDecl::PropertyMap &PropMap,
1444 ObjCContainerDecl::PropertyMap &SuperPropMap,
1445 bool IncludeProtocols = true) {
1446
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001447 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001448 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001449 PropMap[Prop->getIdentifier()] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001450
1451 // Collect the properties from visible extensions.
1452 for (auto *Ext : IDecl->visible_extensions())
1453 CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
1454
Ted Kremenek204c3c52014-02-22 00:02:03 +00001455 if (IncludeProtocols) {
1456 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001457 for (auto *PI : IDecl->all_referenced_protocols())
1458 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001459 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001460 }
1461 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001462 for (auto *Prop : CATDecl->properties())
1463 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001464 if (IncludeProtocols) {
1465 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001466 for (auto *PI : CATDecl->protocols())
1467 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001468 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001469 }
1470 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001471 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001472 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1473 // Exclude property for protocols which conform to class's super-class,
1474 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001475 if (!PropertyFromSuper ||
1476 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001477 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1478 if (!PropEntry)
1479 PropEntry = Prop;
1480 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001481 }
1482 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001483 for (auto *PI : PDecl->protocols())
1484 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001485 }
1486}
1487
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001488/// CollectSuperClassPropertyImplementations - This routine collects list of
1489/// properties to be implemented in super class(s) and also coming from their
1490/// conforming protocols.
1491static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001492 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001493 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001494 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001495 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001496 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001497 SDecl = SDecl->getSuperClass();
1498 }
1499 }
1500}
1501
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001502/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1503/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1504/// declared in class 'IFace'.
1505bool
1506Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1507 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1508 if (!IV->getSynthesize())
1509 return false;
1510 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1511 Method->isInstanceMethod());
1512 if (!IMD || !IMD->isPropertyAccessor())
1513 return false;
1514
1515 // look up a property declaration whose one of its accessors is implemented
1516 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001517 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001518 if ((Property->getGetterName() == IMD->getSelector() ||
1519 Property->getSetterName() == IMD->getSelector()) &&
1520 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001521 return true;
1522 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001523 // Also look up property declaration in class extension whose one of its
1524 // accessors is implemented by this method.
1525 for (const auto *Ext : IFace->known_extensions())
1526 for (const auto *Property : Ext->properties())
1527 if ((Property->getGetterName() == IMD->getSelector() ||
1528 Property->getSetterName() == IMD->getSelector()) &&
1529 (Property->getPropertyIvarDecl() == IV))
1530 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001531 return false;
1532}
1533
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001534static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1535 ObjCPropertyDecl *Prop) {
1536 bool SuperClassImplementsGetter = false;
1537 bool SuperClassImplementsSetter = false;
1538 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1539 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001540
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001541 while (IDecl->getSuperClass()) {
1542 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1543 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1544 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001545
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001546 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1547 SuperClassImplementsSetter = true;
1548 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1549 return true;
1550 IDecl = IDecl->getSuperClass();
1551 }
1552 return false;
1553}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001554
James Dennett2a4d13c2012-06-15 07:13:21 +00001555/// \brief Default synthesizes all properties which must be synthesized
1556/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001557void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1558 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001559
Anna Zaks673d76b2012-10-18 19:17:53 +00001560 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001561 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1562 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001563 if (PropMap.empty())
1564 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001565 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001566 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1567
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001568 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1569 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001570 // Is there a matching property synthesize/dynamic?
1571 if (Prop->isInvalidDecl() ||
1572 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1573 continue;
1574 // Property may have been synthesized by user.
1575 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1576 continue;
1577 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1578 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1579 continue;
1580 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1581 continue;
1582 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001583 if (ObjCPropertyImplDecl *PID =
1584 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001585 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1586 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001587 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001588 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001589 continue;
1590 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001591 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001592 if (ObjCProtocolDecl *Proto =
1593 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001594 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001595 // Suppress the warning if class's superclass implements property's
1596 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001597 // Or, if property is going to be implemented in its super class.
1598 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001599 Diag(IMPDecl->getLocation(),
1600 diag::warn_auto_synthesizing_protocol_property)
1601 << Prop << Proto;
1602 Diag(Prop->getLocation(), diag::note_property_declare);
1603 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001604 continue;
1605 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001606 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001607 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001608 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1609 (PropInSuperClass->getPropertyAttributes() &
1610 ObjCPropertyDecl::OBJC_PR_readonly) &&
1611 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1612 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1613 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1614 << Prop->getIdentifier();
1615 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1616 }
1617 else {
1618 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1619 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001620 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001621 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1622 }
1623 continue;
1624 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001625 // We use invalid SourceLocations for the synthesized ivars since they
1626 // aren't really synthesized at a particular location; they just exist.
1627 // Saying that they are located at the @implementation isn't really going
1628 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001629 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1630 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1631 true,
1632 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001633 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001634 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001635 if (PIDecl) {
1636 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001637 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001638 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001639 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001640}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001641
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001642void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001643 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001644 return;
1645 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1646 if (!IC)
1647 return;
1648 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001649 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001650 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001651}
1652
Ted Kremenek7e812952014-02-21 19:41:30 +00001653static void DiagnoseUnimplementedAccessor(Sema &S,
1654 ObjCInterfaceDecl *PrimaryClass,
1655 Selector Method,
1656 ObjCImplDecl* IMPDecl,
1657 ObjCContainerDecl *CDecl,
1658 ObjCCategoryDecl *C,
1659 ObjCPropertyDecl *Prop,
1660 Sema::SelectorSet &SMap) {
1661 // When reporting on missing property setter/getter implementation in
1662 // categories, do not report when they are declared in primary class,
1663 // class's protocol, or one of it super classes. This is because,
1664 // the class is going to implement them.
1665 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001666 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001667 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1668 S.Diag(IMPDecl->getLocation(),
1669 isa<ObjCCategoryDecl>(CDecl) ?
1670 diag::warn_setter_getter_impl_required_in_category :
1671 diag::warn_setter_getter_impl_required)
1672 << Prop->getDeclName() << Method;
1673 S.Diag(Prop->getLocation(),
1674 diag::note_property_declare);
1675 if (S.LangOpts.ObjCDefaultSynthProperties &&
1676 S.LangOpts.ObjCRuntime.isNonFragile())
1677 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1678 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1679 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1680 }
1681}
1682
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001683void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001684 ObjCContainerDecl *CDecl,
1685 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001686 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001687 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1688
Ted Kremenek348e88c2014-02-21 19:41:34 +00001689 if (!SynthesizeProperties) {
1690 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001691 // Gather properties which need not be implemented in this class
1692 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001693 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001694 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1695 // For categories, no need to implement properties declared in
1696 // its primary class (and its super classes) if property is
1697 // declared in one of those containers.
1698 if ((IDecl = C->getClassInterface())) {
1699 ObjCInterfaceDecl::PropertyDeclOrder PO;
1700 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1701 }
1702 }
1703 if (IDecl)
1704 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1705
1706 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1707 }
1708
Ted Kremenek38882022014-02-21 19:41:39 +00001709 // Scan the @interface to see if any of the protocols it adopts
1710 // require an explicit implementation, via attribute
1711 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001712 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001713 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001714
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001715 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001716 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1717 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001718 // Lazily construct a set of all the properties in the @interface
1719 // of the class, without looking at the superclass. We cannot
1720 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001721 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001722 // as scans the adopted protocols. This work only triggers for protocols
1723 // with the attribute, which is very rare, and only occurs when
1724 // analyzing the @implementation.
1725 if (!LazyMap) {
1726 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1727 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1728 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1729 /* IncludeProtocols */ false);
1730 }
Ted Kremenek38882022014-02-21 19:41:39 +00001731 // Add the properties of 'PDecl' to the list of properties that
1732 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001733 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001734 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001735 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001736 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001737 }
1738 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001739 }
Ted Kremenek38882022014-02-21 19:41:39 +00001740
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001741 if (PropMap.empty())
1742 return;
1743
1744 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001745 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001746 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001747
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001748 SelectorSet InsMap;
1749 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001750 for (const auto *I : IMPDecl->instance_methods())
1751 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001752
1753 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001754 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001755 if (C && !C->IsClassExtension())
1756 if ((PrimaryClass = C->getClassInterface()))
1757 // Report unimplemented properties in the category as well.
1758 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1759 // When reporting on missing setter/getters, do not report when
1760 // setter/getter is implemented in category's primary class
1761 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001762 for (const auto *I : IMP->instance_methods())
1763 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001764 }
1765
Anna Zaks673d76b2012-10-18 19:17:53 +00001766 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001767 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1768 ObjCPropertyDecl *Prop = P->second;
1769 // Is there a matching propery synthesize/dynamic?
1770 if (Prop->isInvalidDecl() ||
1771 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001772 PropImplMap.count(Prop) ||
1773 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001774 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001775
1776 // Diagnose unimplemented getters and setters.
1777 DiagnoseUnimplementedAccessor(*this,
1778 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1779 if (!Prop->isReadOnly())
1780 DiagnoseUnimplementedAccessor(*this,
1781 PrimaryClass, Prop->getSetterName(),
1782 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001783 }
1784}
1785
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001786void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001787 for (const auto *propertyImpl : impDecl->property_impls()) {
1788 const auto *property = propertyImpl->getPropertyDecl();
1789
1790 // Warn about null_resettable properties with synthesized setters,
1791 // because the setter won't properly handle nil.
1792 if (propertyImpl->getPropertyImplementation()
1793 == ObjCPropertyImplDecl::Synthesize &&
1794 (property->getPropertyAttributes() &
1795 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1796 property->getGetterMethodDecl() &&
1797 property->getSetterMethodDecl()) {
1798 auto *getterMethod = property->getGetterMethodDecl();
1799 auto *setterMethod = property->getSetterMethodDecl();
1800 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1801 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1802 SourceLocation loc = propertyImpl->getLocation();
1803 if (loc.isInvalid())
1804 loc = impDecl->getLocStart();
1805
1806 Diag(loc, diag::warn_null_resettable_setter)
1807 << setterMethod->getSelector() << property->getDeclName();
1808 }
1809 }
1810 }
1811}
1812
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001813void
1814Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001815 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001816 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001817 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001818 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001819 ObjCContainerDecl::PropertyMap PM;
1820 for (auto *Prop : IDecl->properties())
1821 PM[Prop->getIdentifier()] = Prop;
1822 for (const auto *Ext : IDecl->known_extensions())
1823 for (auto *Prop : Ext->properties())
1824 PM[Prop->getIdentifier()] = Prop;
1825
1826 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1827 I != E; ++I) {
1828 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001829 ObjCMethodDecl *GetterMethod = nullptr;
1830 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001831 bool LookedUpGetterSetter = false;
1832
Bill Wendling44426052012-12-20 19:22:21 +00001833 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001834 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001835
John McCall43192862011-09-13 18:31:23 +00001836 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1837 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001838 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1839 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1840 LookedUpGetterSetter = true;
1841 if (GetterMethod) {
1842 Diag(GetterMethod->getLocation(),
1843 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001844 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001845 Diag(Property->getLocation(), diag::note_property_declare);
1846 }
1847 if (SetterMethod) {
1848 Diag(SetterMethod->getLocation(),
1849 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001850 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001851 Diag(Property->getLocation(), diag::note_property_declare);
1852 }
1853 }
1854
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001855 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001856 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1857 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001858 continue;
1859 if (const ObjCPropertyImplDecl *PIDecl
1860 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1861 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1862 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001863 if (!LookedUpGetterSetter) {
1864 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1865 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001866 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001867 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1868 SourceLocation MethodLoc =
1869 (GetterMethod ? GetterMethod->getLocation()
1870 : SetterMethod->getLocation());
1871 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001872 << Property->getIdentifier() << (GetterMethod != nullptr)
1873 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001874 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001875 if (Property->getLParenLoc().isValid() &&
1876 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001877 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001878 SourceLocation AfterLParen =
1879 getLocForEndOfToken(Property->getLParenLoc());
1880 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
1881 : "nonatomic";
1882 Diag(Property->getLocation(),
1883 diag::note_atomic_property_fixup_suggest)
1884 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
1885 } else if (Property->getLParenLoc().isInvalid()) {
1886 //@property id etc.
1887 SourceLocation startLoc =
1888 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1889 Diag(Property->getLocation(),
1890 diag::note_atomic_property_fixup_suggest)
1891 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001892 }
1893 else
1894 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001895 Diag(Property->getLocation(), diag::note_property_declare);
1896 }
1897 }
1898 }
1899}
1900
John McCall31168b02011-06-15 23:02:42 +00001901void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001902 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001903 return;
1904
Aaron Ballmand85eff42014-03-14 15:02:45 +00001905 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001906 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001907 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1908 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001909 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1910 if (!method)
1911 continue;
1912 ObjCMethodFamily family = method->getMethodFamily();
1913 if (family == OMF_alloc || family == OMF_copy ||
1914 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001915 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001916 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001917 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001918 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001919
1920 // Look for a getter explicitly declared alongside the property.
1921 // If we find one, use its location for the note.
1922 SourceLocation noteLoc = PD->getLocation();
1923 SourceLocation fixItLoc;
1924 for (auto *getterRedecl : method->redecls()) {
1925 if (getterRedecl->isImplicit())
1926 continue;
1927 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1928 continue;
1929 noteLoc = getterRedecl->getLocation();
1930 fixItLoc = getterRedecl->getLocEnd();
1931 }
1932
1933 Preprocessor &PP = getPreprocessor();
1934 TokenValue tokens[] = {
1935 tok::kw___attribute, tok::l_paren, tok::l_paren,
1936 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1937 PP.getIdentifierInfo("none"), tok::r_paren,
1938 tok::r_paren, tok::r_paren
1939 };
1940 StringRef spelling = "__attribute__((objc_method_family(none)))";
1941 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1942 if (!macroName.empty())
1943 spelling = macroName;
1944
1945 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1946 << method->getDeclName() << spelling;
1947 if (fixItLoc.isValid()) {
1948 SmallString<64> fixItText(" ");
1949 fixItText += spelling;
1950 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1951 }
John McCall31168b02011-06-15 23:02:42 +00001952 }
1953 }
1954 }
1955}
1956
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001957void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001958 const ObjCImplementationDecl *ImplD,
1959 const ObjCInterfaceDecl *IFD) {
1960 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001961 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1962 if (!SuperD)
1963 return;
1964
1965 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001966 for (const auto *I : ImplD->instance_methods())
1967 if (I->getMethodFamily() == OMF_init)
1968 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001969
1970 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1971 SuperD->getDesignatedInitializers(DesignatedInits);
1972 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1973 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1974 const ObjCMethodDecl *MD = *I;
1975 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00001976 bool Ignore = false;
1977 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
1978 Ignore = IMD->isUnavailable();
1979 }
1980 if (!Ignore) {
1981 Diag(ImplD->getLocation(),
1982 diag::warn_objc_implementation_missing_designated_init_override)
1983 << MD->getSelector();
1984 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1985 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001986 }
1987 }
1988}
1989
John McCallad31b5f2010-11-10 07:01:40 +00001990/// AddPropertyAttrs - Propagates attributes from a property to the
1991/// implicitly-declared getter or setter for that property.
1992static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1993 ObjCPropertyDecl *Property) {
1994 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001995 for (const auto *A : Property->attrs()) {
1996 if (isa<DeprecatedAttr>(A) ||
1997 isa<UnavailableAttr>(A) ||
1998 isa<AvailabilityAttr>(A))
1999 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002000 }
John McCallad31b5f2010-11-10 07:01:40 +00002001}
2002
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002003/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2004/// have the property type and issue diagnostics if they don't.
2005/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002006/// appropriate lookup tables.
2007void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002008 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002009 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002010 if (CD->isInvalidDecl())
2011 return;
2012
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002013 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002014 // if setter or getter is not found in class extension, it might be
2015 // in the primary class.
2016 if (!GetterMethod)
2017 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2018 if (CatDecl->IsClassExtension())
2019 GetterMethod = CatDecl->getClassInterface()->
2020 getInstanceMethod(property->getGetterName());
2021
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002022 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002023 if (!SetterMethod)
2024 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2025 if (CatDecl->IsClassExtension())
2026 SetterMethod = CatDecl->getClassInterface()->
2027 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002028 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2029 property->getLocation());
2030
2031 if (SetterMethod) {
2032 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2033 property->getPropertyAttributes();
2034 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002035 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2036 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002037 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2038 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002039 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002040 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2041 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002042 Diag(property->getLocation(),
2043 diag::warn_accessor_property_type_mismatch)
2044 << property->getDeclName()
2045 << SetterMethod->getSelector();
2046 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2047 }
2048 }
2049
2050 // Synthesize getter/setter methods if none exist.
2051 // Find the default getter and if one not found, add one.
2052 // FIXME: The synthesized property we set here is misleading. We almost always
2053 // synthesize these methods unless the user explicitly provided prototypes
2054 // (which is odd, but allowed). Sema should be typechecking that the
2055 // declarations jive in that situation (which it is not currently).
2056 if (!GetterMethod) {
2057 // No instance method of same name as property getter name was found.
2058 // Declare a getter method and add it to the list of methods
2059 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002060 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002061
Douglas Gregor849ebc22015-06-19 18:14:46 +00002062 // If the property is null_resettable, the getter returns nonnull.
2063 QualType resultTy = property->getType();
2064 if (property->getPropertyAttributes() &
2065 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2066 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002067 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002068 if (*nullability == NullabilityKind::Unspecified)
2069 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2070 modifiedTy, modifiedTy);
2071 }
2072 }
2073
Ted Kremenek2f075632010-09-21 20:52:59 +00002074 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2075 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002076 resultTy, nullptr, CD,
Craig Topperc3ec1492014-05-26 06:22:03 +00002077 /*isInstance=*/true, /*isVariadic=*/false,
2078 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002079 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002080 (property->getPropertyImplementation() ==
2081 ObjCPropertyDecl::Optional) ?
2082 ObjCMethodDecl::Optional :
2083 ObjCMethodDecl::Required);
2084 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002085
2086 AddPropertyAttrs(*this, GetterMethod, property);
2087
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002088 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002089 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2090 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002091
2092 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2093 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002094 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002095
2096 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002097 GetterMethod->addAttr(
2098 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2099 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002100
2101 if (getLangOpts().ObjCAutoRefCount)
2102 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002103 } else
2104 // A user declared getter will be synthesize when @synthesize of
2105 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002106 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002107 property->setGetterMethodDecl(GetterMethod);
2108
2109 // Skip setter if property is read-only.
2110 if (!property->isReadOnly()) {
2111 // Find the default setter and if one not found, add one.
2112 if (!SetterMethod) {
2113 // No instance method of same name as property setter name was found.
2114 // Declare a setter method and add it to the list of methods
2115 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002116 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002117
2118 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002119 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002120 property->getSetterName(), Context.VoidTy,
2121 nullptr, CD, /*isInstance=*/true,
2122 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002123 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002124 /*isImplicitlyDeclared=*/true,
2125 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002126 (property->getPropertyImplementation() ==
2127 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002128 ObjCMethodDecl::Optional :
2129 ObjCMethodDecl::Required);
2130
Douglas Gregor849ebc22015-06-19 18:14:46 +00002131 // If the property is null_resettable, the setter accepts a
2132 // nullable value.
2133 QualType paramTy = property->getType().getUnqualifiedType();
2134 if (property->getPropertyAttributes() &
2135 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2136 QualType modifiedTy = paramTy;
2137 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2138 if (*nullability == NullabilityKind::Unspecified)
2139 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2140 modifiedTy, modifiedTy);
2141 }
2142 }
2143
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002144 // Invent the arguments for the setter. We don't bother making a
2145 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002146 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2147 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002148 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002149 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002150 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002151 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002152 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002153 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002154
2155 AddPropertyAttrs(*this, SetterMethod, property);
2156
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002157 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002158 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002159 SetterMethod->addAttr(
2160 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2161 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002162 // It's possible for the user to have set a very odd custom
2163 // setter selector that causes it to have a method family.
2164 if (getLangOpts().ObjCAutoRefCount)
2165 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002166 } else
2167 // A user declared setter will be synthesize when @synthesize of
2168 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002169 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002170 property->setSetterMethodDecl(SetterMethod);
2171 }
2172 // Add any synthesized methods to the global pool. This allows us to
2173 // handle the following, which is supported by GCC (and part of the design).
2174 //
2175 // @interface Foo
2176 // @property double bar;
2177 // @end
2178 //
2179 // void thisIsUnfortunate() {
2180 // id foo;
2181 // double bar = [foo bar];
2182 // }
2183 //
2184 if (GetterMethod)
2185 AddInstanceMethodToGlobalPool(GetterMethod);
2186 if (SetterMethod)
2187 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002188
2189 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2190 if (!CurrentClass) {
2191 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2192 CurrentClass = Cat->getClassInterface();
2193 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2194 CurrentClass = Impl->getClassInterface();
2195 }
2196 if (GetterMethod)
2197 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2198 if (SetterMethod)
2199 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002200}
2201
John McCall48871652010-08-21 09:40:31 +00002202void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002203 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002204 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002205 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002206 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002207 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002208 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002209
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002210 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2211 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2212 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2213 << "readonly" << "readwrite";
2214
2215 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2216 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002217
2218 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002219 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002220 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2221 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002222 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002223 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002224 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2225 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2226 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002227 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002228 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002229 }
2230
2231 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002232 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2233 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002234 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2235 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002236 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002237 }
Bill Wendling44426052012-12-20 19:22:21 +00002238 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002239 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2240 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002241 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002242 }
Bill Wendling44426052012-12-20 19:22:21 +00002243 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002244 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2245 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002246 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002247 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002248 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002249 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002250 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2251 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002252 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002253 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002254 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002255 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002256 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2257 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002258 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2259 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002260 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002261 }
Bill Wendling44426052012-12-20 19:22:21 +00002262 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002263 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2264 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002265 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002266 }
Bill Wendling44426052012-12-20 19:22:21 +00002267 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002268 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2269 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002270 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002271 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002272 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002273 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002274 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2275 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002276 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002277 }
Bill Wendling44426052012-12-20 19:22:21 +00002278 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2279 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002280 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2281 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002282 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002283 }
Bill Wendling44426052012-12-20 19:22:21 +00002284 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002285 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2286 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002287 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002288 }
Bill Wendling44426052012-12-20 19:22:21 +00002289 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002290 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2291 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002292 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002293 }
2294 }
Bill Wendling44426052012-12-20 19:22:21 +00002295 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2296 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002297 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2298 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002299 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002300 }
Bill Wendling44426052012-12-20 19:22:21 +00002301 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2302 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002303 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2304 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002305 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002306 }
2307
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002308 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002309 // 'weak' and 'nonnull' are mutually exclusive.
2310 if (auto nullability = PropertyTy->getNullability(Context)) {
2311 if (*nullability == NullabilityKind::NonNull)
2312 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2313 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002314 }
2315 }
2316
Bill Wendling44426052012-12-20 19:22:21 +00002317 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2318 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002319 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2320 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002321 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002322 }
2323
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002324 // Warn if user supplied no assignment attribute, property is
2325 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002326 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2327 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2328 // do nothing
2329 } else if (getLangOpts().ObjCAutoRefCount) {
2330 // With arc, @property definitions should default to strong when
2331 // not specified.
2332 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2333 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002334 bool isAnyClassTy =
2335 (PropertyTy->isObjCClassType() ||
2336 PropertyTy->isObjCQualifiedClassType());
2337 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2338 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002339 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002340 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002341 else if (propertyInPrimaryClass) {
2342 // Don't issue warning on property with no life time in class
2343 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002344 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002345 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002346 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002347
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002348 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002349 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002350 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002351 }
John McCallb61e14e2015-10-27 04:54:50 +00002352 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002353
2354 // FIXME: Implement warning dependent on NSCopying being
2355 // implemented. See also:
2356 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2357 // (please trim this list while you are at it).
2358 }
2359
Bill Wendling44426052012-12-20 19:22:21 +00002360 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2361 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002362 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002363 && PropertyTy->isBlockPointerType())
2364 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002365 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2366 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2367 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002368 PropertyTy->isBlockPointerType())
2369 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002370
Bill Wendling44426052012-12-20 19:22:21 +00002371 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2372 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002373 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2374
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002375}