blob: 35f79b2a3e3657cc76c6689eca5fc8433764d70e [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallb61e14e2015-10-27 04:54:50 +000064/// Check the internal consistency of a property declaration with
65/// an explicit ownership qualifier.
66static void checkPropertyDeclWithOwnership(Sema &S,
67 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000068 if (property->isInvalidDecl()) return;
69
70 ObjCPropertyDecl::PropertyAttributeKind propertyKind
71 = property->getPropertyAttributes();
72 Qualifiers::ObjCLifetime propertyLifetime
73 = property->getType().getObjCLifetime();
74
John McCallb61e14e2015-10-27 04:54:50 +000075 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000076
John McCall43192862011-09-13 18:31:23 +000077 Qualifiers::ObjCLifetime expectedLifetime
78 = getImpliedARCOwnership(propertyKind, property->getType());
79 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000080 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000081 // attribute. That's okay, but restore reasonable invariants by
82 // setting the property attribute according to the lifetime
83 // qualifier.
84 ObjCPropertyDecl::PropertyAttributeKind attr;
85 if (propertyLifetime == Qualifiers::OCL_Strong) {
86 attr = ObjCPropertyDecl::OBJC_PR_strong;
87 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
88 attr = ObjCPropertyDecl::OBJC_PR_weak;
89 } else {
90 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
91 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
92 }
93 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000094 return;
95 }
96
97 if (propertyLifetime == expectedLifetime) return;
98
99 property->setInvalidDecl();
100 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000101 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000102 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000103 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000104 << propertyLifetime;
105}
106
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// \brief Check this Objective-C property against a property declared in the
108/// given protocol.
109static void
110CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
111 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000113 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000114 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000115 return;
116
117 // Look for a property with the same name.
118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
119 for (unsigned I = 0, N = R.size(); I != N; ++I) {
120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000122 return;
123 }
124 }
125
126 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000127 for (auto *P : Proto->protocols())
128 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129}
130
John McCallb61e14e2015-10-27 04:54:50 +0000131static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
132 // In GC mode, just look for the __weak qualifier.
133 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
135
136 // In ARC/MRC, look for an explicit ownership qualifier.
137 // For some reason, this only applies to __weak.
138 } else if (auto ownership = T.getObjCLifetime()) {
139 switch (ownership) {
140 case Qualifiers::OCL_Weak:
141 return ObjCDeclSpec::DQ_PR_weak;
142 case Qualifiers::OCL_Strong:
143 return ObjCDeclSpec::DQ_PR_strong;
144 case Qualifiers::OCL_ExplicitNone:
145 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
146 case Qualifiers::OCL_Autoreleasing:
147 case Qualifiers::OCL_None:
148 return 0;
149 }
150 llvm_unreachable("bad qualifier");
151 }
152
153 return 0;
154}
155
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000156static const unsigned OwnershipMask =
157 (ObjCPropertyDecl::OBJC_PR_assign |
158 ObjCPropertyDecl::OBJC_PR_retain |
159 ObjCPropertyDecl::OBJC_PR_copy |
160 ObjCPropertyDecl::OBJC_PR_weak |
161 ObjCPropertyDecl::OBJC_PR_strong |
162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
163
John McCallb61e14e2015-10-27 04:54:50 +0000164static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000165 unsigned result = attr & OwnershipMask;
166
167 // From an ownership perspective, assign and unsafe_unretained are
168 // identical; make sure one also implies the other.
169 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
171 result |= ObjCPropertyDecl::OBJC_PR_assign |
172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
173 }
174
175 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000176}
177
John McCall48871652010-08-21 09:40:31 +0000178Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000179 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000180 FieldDeclarator &FD,
181 ObjCDeclSpec &ODS,
182 Selector GetterSel,
183 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000184 tok::ObjCKeywordKind MethodImplKind,
185 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000186 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
189 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000190 if (!getOwnershipRule(Attributes)) {
191 Attributes |= deducePropertyOwnershipFromType(*this, T);
192 }
Bill Wendling44426052012-12-20 19:22:21 +0000193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000194 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000196
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000199 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000201 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000203 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000204 isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000205 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000206 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000207 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000208 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000209 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000210 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000211 }
212
213 if (!Res) {
214 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000215 GetterSel, SetterSel, isReadWrite,
Douglas Gregor90d34422013-01-21 19:05:22 +0000216 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000217 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000218 if (lexicalDC)
219 Res->setLexicalDeclContext(lexicalDC);
220 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000221
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000222 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000223 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000224 (isa<ObjCInterfaceDecl>(ClassDecl) ||
225 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000226
John McCallb61e14e2015-10-27 04:54:50 +0000227 // Check consistency if the type has explicit ownership qualification.
228 if (Res->getType().getObjCLifetime())
229 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000230
Douglas Gregorb8982092013-01-21 19:42:21 +0000231 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000232 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 // For a class, compare the property against a property in our superclass.
234 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000235 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
236 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000237 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000238 for (unsigned I = 0, N = R.size(); I != N; ++I) {
239 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000240 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000241 FoundInSuper = true;
242 break;
243 }
244 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000245 if (FoundInSuper)
246 break;
247 else
248 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000249 }
250
251 if (FoundInSuper) {
252 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000253 for (auto *P : CurrentInterfaceDecl->protocols()) {
254 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000255 }
256 } else {
257 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000258 for (auto *P : IFace->all_referenced_protocols()) {
259 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000260 }
261 }
262 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000263 // We don't check if class extension. Because properties in class extension
264 // are meant to override some of the attributes and checking has already done
265 // when property in class extension is constructed.
266 if (!Cat->IsClassExtension())
267 for (auto *P : Cat->protocols())
268 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000269 } else {
270 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000271 for (auto *P : Proto->protocols())
272 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000273 }
274
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000275 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000276 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000277}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000278
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000280makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_class)
307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000308
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,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000345 ObjCPropertyDecl *NewProperty,
346 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000347 // If the atomicity of both matches, we're done.
348 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000349 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
350 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000351 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000352 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000354 if (OldIsAtomic == NewIsAtomic) return;
355
356 // Determine whether the given property is readonly and implicitly
357 // atomic.
358 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
359 // Is it readonly?
360 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000361 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000362
363 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000364 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000365
366 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000367 if (Property->getPropertyAttributesAsWritten() &
368 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000369 return false;
370
371 return true;
372 };
373
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000374 // If we're allowed to propagate atomicity, and the new property did
375 // not specify atomicity at all, propagate.
376 const unsigned AtomicityMask =
377 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
378 if (PropagateAtomicity &&
379 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
380 unsigned Attrs = NewProperty->getPropertyAttributes();
381 Attrs = Attrs & ~AtomicityMask;
382 if (OldIsAtomic)
383 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
384 else
385 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
386
387 NewProperty->overwritePropertyAttributes(Attrs);
388 return;
389 }
390
Douglas Gregor429183e2015-12-09 22:57:32 +0000391 // One of the properties is atomic; if it's a readonly property, and
392 // 'atomic' wasn't explicitly specified, we're okay.
393 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
394 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
395 return;
396
397 // Diagnose the conflict.
398 const IdentifierInfo *OldContextName;
399 auto *OldDC = OldProperty->getDeclContext();
400 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
401 OldContextName = Category->getClassInterface()->getIdentifier();
402 else
403 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
404
405 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
406 << NewProperty->getDeclName() << "atomic"
407 << OldContextName;
408 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
409}
410
Douglas Gregor90d34422013-01-21 19:05:22 +0000411ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000412Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000413 SourceLocation AtLoc,
414 SourceLocation LParenLoc,
415 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000416 Selector GetterSel, Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000417 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000418 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000419 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000420 QualType T,
421 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000423 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000424 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000425 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000426 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000427 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
428
Ted Kremenek959e8302010-03-12 02:31:10 +0000429 // We need to look in the @interface to see if the @property was
430 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000431 if (!CCPrimary) {
432 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000433 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000434 }
435
Manman Ren5b786402016-01-28 18:49:28 +0000436 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
437 (Attributes & ObjCDeclSpec::DQ_PR_class);
438
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000439 // Find the property in the extended class's primary class or
440 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000441 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
442 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000443
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000444 // If we found a property in an extension, complain.
445 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
446 Diag(AtLoc, diag::err_duplicate_property);
447 Diag(PIDecl->getLocation(), diag::note_property_declare);
448 return nullptr;
449 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000450
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000451 // Check for consistency with the previous declaration, if there is one.
452 if (PIDecl) {
453 // A readonly property declared in the primary class can be refined
454 // by adding a readwrite property within an extension.
455 // Anything else is an error.
456 if (!(PIDecl->isReadOnly() && isReadWrite)) {
457 // Tailor the diagnostics for the common case where a readwrite
458 // property is declared both in the @interface and the continuation.
459 // This is a common error where the user often intended the original
460 // declaration to be readonly.
461 unsigned diag =
462 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
463 (PIDecl->getPropertyAttributesAsWritten() &
464 ObjCPropertyDecl::OBJC_PR_readwrite)
465 ? diag::err_use_continuation_class_redeclaration_readwrite
466 : diag::err_use_continuation_class;
467 Diag(AtLoc, diag)
468 << CCPrimary->getDeclName();
469 Diag(PIDecl->getLocation(), diag::note_property_declare);
470 return nullptr;
471 }
472
473 // Check for consistency of getters.
474 if (PIDecl->getGetterName() != GetterSel) {
475 // If the getter was written explicitly, complain.
476 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
477 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
478 << PIDecl->getGetterName() << GetterSel;
479 Diag(PIDecl->getLocation(), diag::note_property_declare);
480 }
481
482 // Always adopt the getter from the original declaration.
483 GetterSel = PIDecl->getGetterName();
484 Attributes |= ObjCDeclSpec::DQ_PR_getter;
485 }
486
487 // Check consistency of ownership.
488 unsigned ExistingOwnership
489 = getOwnershipRule(PIDecl->getPropertyAttributes());
490 unsigned NewOwnership = getOwnershipRule(Attributes);
491 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
492 // If the ownership was written explicitly, complain.
493 if (getOwnershipRule(AttributesAsWritten)) {
494 Diag(AtLoc, diag::warn_property_attr_mismatch);
495 Diag(PIDecl->getLocation(), diag::note_property_declare);
496 }
497
498 // Take the ownership from the original property.
499 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
500 }
501
502 // If the redeclaration is 'weak' but the original property is not,
503 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
504 !(PIDecl->getPropertyAttributesAsWritten()
505 & ObjCPropertyDecl::OBJC_PR_weak) &&
506 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
507 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
508 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
509 Diag(PIDecl->getLocation(), diag::note_property_declare);
510 }
511 }
512
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000513 // Create a new ObjCPropertyDecl with the DeclContext being
514 // the class extension.
515 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
516 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000517 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000518 Attributes, AttributesAsWritten,
519 T, TSI, MethodImplKind, DC);
520
521 // If there was no declaration of a property with the same name in
522 // the primary class, we're done.
523 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000524 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000525 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000526 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000527
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000528 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
529 bool IncompatibleObjC = false;
530 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000531 // Relax the strict type matching for property type in continuation class.
532 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000533 // as it narrows the object type in its primary class property. Note that
534 // this conversion is safe only because the wider type is for a 'readonly'
535 // property in primary class and 'narrowed' type for a 'readwrite' property
536 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000537 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
538 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
539 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
540 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
541 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000542 ConvertedType, IncompatibleObjC))
543 || IncompatibleObjC) {
544 Diag(AtLoc,
545 diag::err_type_mismatch_continuation_class) << PDecl->getType();
546 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000547 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000548 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000549 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000550
551 // Check that atomicity of property in class extension matches the previous
552 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000553 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000554
Douglas Gregore17765e2015-11-03 17:02:34 +0000555 // Make sure getter/setter are appropriately synthesized.
556 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000557 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000558}
559
560ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
561 ObjCContainerDecl *CDecl,
562 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000563 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000564 FieldDeclarator &FD,
565 Selector GetterSel,
566 Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000567 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000568 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000569 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000570 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000571 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000572 tok::ObjCKeywordKind MethodImplKind,
573 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000574 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000575
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000576 // Property defaults to 'assign' if it is readwrite, unless this is ARC
577 // and the type is retainable.
578 bool isAssign;
579 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
580 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
581 isAssign = true;
582 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
583 isAssign = false;
584 } else {
585 isAssign = (!getLangOpts().ObjCAutoRefCount ||
586 !T->isObjCRetainableType());
587 }
588
589 // Issue a warning if property is 'assign' as default and its
590 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000591 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000592 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000593 if (const ObjCObjectPointerType *ObjPtrTy =
594 T->getAs<ObjCObjectPointerType>()) {
595 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
596 if (IDecl)
597 if (ObjCProtocolDecl* PNSCopying =
598 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
599 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
600 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000601 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000602 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000603
604 if (T->isObjCObjectType()) {
605 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000606 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000607 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
608 << FixItHint::CreateInsertion(StarLoc, "*");
609 T = Context.getObjCObjectPointerType(T);
610 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
611 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
612 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000613
Ted Kremenek959e8302010-03-12 02:31:10 +0000614 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000615 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
616 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000617 PropertyId, AtLoc,
618 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000619
Manman Ren5b786402016-01-28 18:49:28 +0000620 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
621 (Attributes & ObjCDeclSpec::DQ_PR_class);
622 // Class property and instance property can have the same name.
623 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
624 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000625 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000626 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000627 PDecl->setInvalidDecl();
628 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000629 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000630 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000631 if (lexicalDC)
632 PDecl->setLexicalDeclContext(lexicalDC);
633 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000634
635 if (T->isArrayType() || T->isFunctionType()) {
636 Diag(AtLoc, diag::err_property_type) << T;
637 PDecl->setInvalidDecl();
638 }
639
640 ProcessDeclAttributes(S, PDecl, FD.D);
641
642 // Regardless of setter/getter attribute, we save the default getter/setter
643 // selector names in anticipation of declaration of setter/getter methods.
644 PDecl->setGetterName(GetterSel);
645 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000646 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000647 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000648
Bill Wendling44426052012-12-20 19:22:21 +0000649 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000650 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
651
Bill Wendling44426052012-12-20 19:22:21 +0000652 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000653 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
654
Bill Wendling44426052012-12-20 19:22:21 +0000655 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000656 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
657
658 if (isReadWrite)
659 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
660
Bill Wendling44426052012-12-20 19:22:21 +0000661 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000662 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
663
Bill Wendling44426052012-12-20 19:22:21 +0000664 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000665 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
666
Bill Wendling44426052012-12-20 19:22:21 +0000667 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000668 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
669
Bill Wendling44426052012-12-20 19:22:21 +0000670 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000671 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
672
Bill Wendling44426052012-12-20 19:22:21 +0000673 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000674 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
675
Ted Kremenekac597f32010-03-12 00:46:40 +0000676 if (isAssign)
677 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
678
John McCall43192862011-09-13 18:31:23 +0000679 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000680 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000682 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000683 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000684
John McCall31168b02011-06-15 23:02:42 +0000685 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000686 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000687 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
688 if (isAssign)
689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
690
Ted Kremenekac597f32010-03-12 00:46:40 +0000691 if (MethodImplKind == tok::objc_required)
692 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
693 else if (MethodImplKind == tok::objc_optional)
694 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000695
Douglas Gregor813a0662015-06-19 18:14:38 +0000696 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
698
Douglas Gregor849ebc22015-06-19 18:14:46 +0000699 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
700 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
701
Manman Ren387ff7f2016-01-26 18:52:43 +0000702 if (Attributes & ObjCDeclSpec::DQ_PR_class)
703 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
704
Ted Kremenek959e8302010-03-12 02:31:10 +0000705 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000706}
707
John McCall31168b02011-06-15 23:02:42 +0000708static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
709 ObjCPropertyDecl *property,
710 ObjCIvarDecl *ivar) {
711 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
712
John McCall31168b02011-06-15 23:02:42 +0000713 QualType ivarType = ivar->getType();
714 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000715
John McCall43192862011-09-13 18:31:23 +0000716 // The lifetime implied by the property's attributes.
717 Qualifiers::ObjCLifetime propertyLifetime =
718 getImpliedARCOwnership(property->getPropertyAttributes(),
719 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000720
John McCall43192862011-09-13 18:31:23 +0000721 // We're fine if they match.
722 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000723
John McCall460ce582015-10-22 18:38:17 +0000724 // None isn't a valid lifetime for an object ivar in ARC, and
725 // __autoreleasing is never valid; don't diagnose twice.
726 if ((ivarLifetime == Qualifiers::OCL_None &&
727 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000728 ivarLifetime == Qualifiers::OCL_Autoreleasing)
729 return;
John McCall31168b02011-06-15 23:02:42 +0000730
John McCalld8561f02012-08-20 23:36:59 +0000731 // If the ivar is private, and it's implicitly __unsafe_unretained
732 // becaues of its type, then pretend it was actually implicitly
733 // __strong. This is only sound because we're processing the
734 // property implementation before parsing any method bodies.
735 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
736 propertyLifetime == Qualifiers::OCL_Strong &&
737 ivar->getAccessControl() == ObjCIvarDecl::Private) {
738 SplitQualType split = ivarType.split();
739 if (split.Quals.hasObjCLifetime()) {
740 assert(ivarType->isObjCARCImplicitlyUnretainedType());
741 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
742 ivarType = S.Context.getQualifiedType(split);
743 ivar->setType(ivarType);
744 return;
745 }
746 }
747
John McCall43192862011-09-13 18:31:23 +0000748 switch (propertyLifetime) {
749 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000750 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000751 << property->getDeclName()
752 << ivar->getDeclName()
753 << ivarLifetime;
754 break;
John McCall31168b02011-06-15 23:02:42 +0000755
John McCall43192862011-09-13 18:31:23 +0000756 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000757 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000758 << property->getDeclName()
759 << ivar->getDeclName();
760 break;
John McCall31168b02011-06-15 23:02:42 +0000761
John McCall43192862011-09-13 18:31:23 +0000762 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000763 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000764 << property->getDeclName()
765 << ivar->getDeclName()
766 << ((property->getPropertyAttributesAsWritten()
767 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
768 break;
John McCall31168b02011-06-15 23:02:42 +0000769
John McCall43192862011-09-13 18:31:23 +0000770 case Qualifiers::OCL_Autoreleasing:
771 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000772
John McCall43192862011-09-13 18:31:23 +0000773 case Qualifiers::OCL_None:
774 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000775 return;
776 }
777
778 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000779 if (propertyImplLoc.isValid())
780 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000781}
782
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000783/// setImpliedPropertyAttributeForReadOnlyProperty -
784/// This routine evaludates life-time attributes for a 'readonly'
785/// property with no known lifetime of its own, using backing
786/// 'ivar's attribute, if any. If no backing 'ivar', property's
787/// life-time is assumed 'strong'.
788static void setImpliedPropertyAttributeForReadOnlyProperty(
789 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
790 Qualifiers::ObjCLifetime propertyLifetime =
791 getImpliedARCOwnership(property->getPropertyAttributes(),
792 property->getType());
793 if (propertyLifetime != Qualifiers::OCL_None)
794 return;
795
796 if (!ivar) {
797 // if no backing ivar, make property 'strong'.
798 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
799 return;
800 }
801 // property assumes owenership of backing ivar.
802 QualType ivarType = ivar->getType();
803 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
804 if (ivarLifetime == Qualifiers::OCL_Strong)
805 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
806 else if (ivarLifetime == Qualifiers::OCL_Weak)
807 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
808 return;
809}
Ted Kremenekac597f32010-03-12 00:46:40 +0000810
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000811/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
812/// in inherited protocols with mismatched types. Since any of them can
813/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000814static void
815DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
816 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000817 ObjCPropertyDecl *Property) {
818 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000819 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
820 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000821 PDecl->collectInheritedProtocolProperties(Property, PropMap);
822 }
823 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
824 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000825 for (const auto *PI : SDecl->all_referenced_protocols()) {
826 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000827 PDecl->collectInheritedProtocolProperties(Property, PropMap);
828 }
829 SDecl = SDecl->getSuperClass();
830 }
831
832 if (PropMap.empty())
833 return;
834
835 QualType RHSType = S.Context.getCanonicalType(Property->getType());
836 bool FirsTime = true;
837 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
838 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
839 ObjCPropertyDecl *Prop = I->second;
840 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
841 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
842 bool IncompatibleObjC = false;
843 QualType ConvertedType;
844 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
845 || IncompatibleObjC) {
846 if (FirsTime) {
847 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
848 << Property->getType();
849 FirsTime = false;
850 }
851 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
852 << Prop->getType();
853 }
854 }
855 }
856 if (!FirsTime && AtLoc.isValid())
857 S.Diag(AtLoc, diag::note_property_synthesize);
858}
859
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000860/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +0000861static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
862 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000863 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
864
865 // If this is a readwrite property in a class extension that refines
866 // a readonly property in the original class definition, check it as
867 // well.
868
869 // If it's a readonly property, we're not interested.
870 if (Prop->isReadOnly()) return false;
871
872 // Is it declared in an extension?
873 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
874 if (!Category || !Category->IsClassExtension()) return false;
875
876 // Find the corresponding property in the primary class definition.
877 auto OrigClass = Category->getClassInterface();
878 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
879 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
880 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
881 }
882
Douglas Gregor02535432015-12-18 00:52:31 +0000883 // Look through all of the protocols.
884 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +0000885 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
886 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +0000887 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
888 }
889
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000890 return false;
891}
892
Ted Kremenekac597f32010-03-12 00:46:40 +0000893/// ActOnPropertyImplDecl - This routine performs semantic checks and
894/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000895/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000896///
John McCall48871652010-08-21 09:40:31 +0000897Decl *Sema::ActOnPropertyImplDecl(Scope *S,
898 SourceLocation AtLoc,
899 SourceLocation PropertyLoc,
900 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000901 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000902 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +0000903 SourceLocation PropertyIvarLoc,
904 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000905 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000906 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000907 // Make sure we have a context for the property implementation declaration.
908 if (!ClassImpDecl) {
909 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000910 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000911 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000912 if (PropertyIvarLoc.isInvalid())
913 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000914 SourceLocation PropertyDiagLoc = PropertyLoc;
915 if (PropertyDiagLoc.isInvalid())
916 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000917 ObjCPropertyDecl *property = nullptr;
918 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000919 // Find the class or category class where this property must have
920 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000921 ObjCImplementationDecl *IC = nullptr;
922 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000923 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
924 IDecl = IC->getClassInterface();
925 // We always synthesize an interface for an implementation
926 // without an interface decl. So, IDecl is always non-zero.
927 assert(IDecl &&
928 "ActOnPropertyImplDecl - @implementation without @interface");
929
930 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +0000931 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +0000932 if (!property) {
933 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000934 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000935 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000936 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000937 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
938 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000939 if (AtLoc.isValid())
940 Diag(AtLoc, diag::warn_implicit_atomic_property);
941 else
942 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
943 Diag(property->getLocation(), diag::note_property_declare);
944 }
945
Ted Kremenekac597f32010-03-12 00:46:40 +0000946 if (const ObjCCategoryDecl *CD =
947 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
948 if (!CD->IsClassExtension()) {
949 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
950 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000951 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000952 }
953 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000954 if (Synthesize&&
955 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
956 property->hasAttr<IBOutletAttr>() &&
957 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000958 bool ReadWriteProperty = false;
959 // Search into the class extensions and see if 'readonly property is
960 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000961 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000962 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
963 if (!R.empty())
964 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
965 PIkind = ExtProp->getPropertyAttributesAsWritten();
966 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
967 ReadWriteProperty = true;
968 break;
969 }
970 }
971 }
972
973 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000974 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000975 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000976 SourceLocation readonlyLoc;
977 if (LocPropertyAttribute(Context, "readonly",
978 property->getLParenLoc(), readonlyLoc)) {
979 SourceLocation endLoc =
980 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
981 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
982 Diag(property->getLocation(),
983 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
984 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
985 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000986 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000987 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000988 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
989 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000990
Ted Kremenekac597f32010-03-12 00:46:40 +0000991 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
992 if (Synthesize) {
993 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000994 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000995 }
996 IDecl = CatImplClass->getClassInterface();
997 if (!IDecl) {
998 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000999 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001000 }
1001 ObjCCategoryDecl *Category =
1002 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1003
1004 // If category for this implementation not found, it is an error which
1005 // has already been reported eralier.
1006 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001007 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001008 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001009 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001010 if (!property) {
1011 Diag(PropertyLoc, diag::error_bad_category_property_decl)
1012 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001013 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001014 }
1015 } else {
1016 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001017 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001018 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001019 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001020 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001021 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001022 // Check that we have a valid, previously declared ivar for @synthesize
1023 if (Synthesize) {
1024 // @synthesize
1025 if (!PropertyIvar)
1026 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001027 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1028 ObjCInterfaceDecl *ClassDeclared;
1029 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1030 QualType PropType = property->getType();
1031 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001032
1033 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001034 diag::err_incomplete_synthesized_property,
1035 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001036 Diag(property->getLocation(), diag::note_property_declare);
1037 CompleteTypeErr = true;
1038 }
1039
David Blaikiebbafb8a2012-03-11 07:00:24 +00001040 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001041 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001042 ObjCPropertyDecl::OBJC_PR_readonly) &&
1043 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001044 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1045 }
1046
John McCall31168b02011-06-15 23:02:42 +00001047 ObjCPropertyDecl::PropertyAttributeKind kind
1048 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001049
John McCall460ce582015-10-22 18:38:17 +00001050 bool isARCWeak = false;
1051 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1052 // Add GC __weak to the ivar type if the property is weak.
1053 if (getLangOpts().getGC() != LangOptions::NonGC) {
1054 assert(!getLangOpts().ObjCAutoRefCount);
1055 if (PropertyIvarType.isObjCGCStrong()) {
1056 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1057 Diag(property->getLocation(), diag::note_property_declare);
1058 } else {
1059 PropertyIvarType =
1060 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1061 }
1062
1063 // Otherwise, check whether ARC __weak is enabled and works with
1064 // the property type.
John McCall43192862011-09-13 18:31:23 +00001065 } else {
John McCall460ce582015-10-22 18:38:17 +00001066 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001067 // Only complain here when synthesizing an ivar.
1068 if (!Ivar) {
1069 Diag(PropertyDiagLoc,
1070 getLangOpts().ObjCWeakRuntime
1071 ? diag::err_synthesizing_arc_weak_property_disabled
1072 : diag::err_synthesizing_arc_weak_property_no_runtime);
1073 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001074 }
John McCallb61e14e2015-10-27 04:54:50 +00001075 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001076 } else {
1077 isARCWeak = true;
1078 if (const ObjCObjectPointerType *ObjT =
1079 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1080 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1081 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1082 Diag(property->getLocation(),
1083 diag::err_arc_weak_unavailable_property)
1084 << PropertyIvarType;
1085 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1086 << ClassImpDecl->getName();
1087 }
1088 }
1089 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001090 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001091 }
John McCall460ce582015-10-22 18:38:17 +00001092
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001093 if (AtLoc.isInvalid()) {
1094 // Check when default synthesizing a property that there is
1095 // an ivar matching property name and issue warning; since this
1096 // is the most common case of not using an ivar used for backing
1097 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001098 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001099 ObjCIvarDecl *originalIvar =
1100 IDecl->lookupInstanceVariable(property->getIdentifier(),
1101 ClassDeclared);
1102 if (originalIvar) {
1103 Diag(PropertyDiagLoc,
1104 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001105 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001106 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001107 Diag(property->getLocation(), diag::note_property_declare);
1108 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001109 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001110 }
1111
1112 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001113 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001114 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001115 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001116 !PropertyIvarType.getObjCLifetime() &&
1117 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001118
John McCall43192862011-09-13 18:31:23 +00001119 // It's an error if we have to do this and the user didn't
1120 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001121 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001122 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001123 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001124 diag::err_arc_objc_property_default_assign_on_object);
1125 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001126 } else {
1127 Qualifiers::ObjCLifetime lifetime =
1128 getImpliedARCOwnership(kind, PropertyIvarType);
1129 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001130
John McCall31168b02011-06-15 23:02:42 +00001131 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001132 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001133 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1134 }
John McCall31168b02011-06-15 23:02:42 +00001135 }
1136
Abramo Bagnaradff19302011-03-08 08:55:46 +00001137 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001138 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001139 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001140 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001141 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001142 if (RequireNonAbstractType(PropertyIvarLoc,
1143 PropertyIvarType,
1144 diag::err_abstract_type_in_decl,
1145 AbstractSynthesizedIvarType)) {
1146 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001147 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001148 } else if (CompleteTypeErr)
1149 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001150 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001151 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001152
John McCall5fb5df92012-06-20 06:18:46 +00001153 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001154 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1155 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001156 // Note! I deliberately want it to fall thru so, we have a
1157 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001158 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001159 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001160 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001161 << property->getDeclName() << Ivar->getDeclName()
1162 << ClassDeclared->getDeclName();
1163 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001164 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001165 // Note! I deliberately want it to fall thru so more errors are caught.
1166 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001167 property->setPropertyIvarDecl(Ivar);
1168
Ted Kremenekac597f32010-03-12 00:46:40 +00001169 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1170
1171 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001172 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001173 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001174 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001175 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001176 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001177 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001178 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001179 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001180 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1181 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001182 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001183 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001184 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001185 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001186 << property->getDeclName() << PropType
1187 << Ivar->getDeclName() << IvarType;
1188 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001189 // Note! I deliberately want it to fall thru so, we have a
1190 // a property implementation and to avoid future warnings.
1191 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001192 else {
1193 // FIXME! Rules for properties are somewhat different that those
1194 // for assignments. Use a new routine to consolidate all cases;
1195 // specifically for property redeclarations as well as for ivars.
1196 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1197 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1198 if (lhsType != rhsType &&
1199 lhsType->isArithmeticType()) {
1200 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1201 << property->getDeclName() << PropType
1202 << Ivar->getDeclName() << IvarType;
1203 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1204 // Fall thru - see previous comment
1205 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001206 }
1207 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001208 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001209 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001210 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001211 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001212 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001213 // Fall thru - see previous comment
1214 }
John McCall31168b02011-06-15 23:02:42 +00001215 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001216 if ((property->getType()->isObjCObjectPointerType() ||
1217 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001218 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001219 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001220 << property->getDeclName() << Ivar->getDeclName();
1221 // Fall thru - see previous comment
1222 }
1223 }
John McCall460ce582015-10-22 18:38:17 +00001224 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1225 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001226 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001227 } else if (PropertyIvar)
1228 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001229 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001230
Ted Kremenekac597f32010-03-12 00:46:40 +00001231 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1232 ObjCPropertyImplDecl *PIDecl =
1233 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1234 property,
1235 (Synthesize ?
1236 ObjCPropertyImplDecl::Synthesize
1237 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001238 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001239
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001240 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001241 PIDecl->setInvalidDecl();
1242
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001243 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1244 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001245 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001246 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001247 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1248 // returned by the getter as it must conform to C++'s copy-return rules.
1249 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001250 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001251 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1252 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001253 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001254 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001255 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001256 Expr *LoadSelfExpr =
1257 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001258 CK_LValueToRValue, SelfExpr, nullptr,
1259 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001260 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001261 new (Context) ObjCIvarRefExpr(Ivar,
1262 Ivar->getUsageType(SelfDecl->getType()),
1263 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001264 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001265 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001266 ExprResult Res = PerformCopyInitialization(
1267 InitializedEntity::InitializeResult(PropertyDiagLoc,
1268 getterMethod->getReturnType(),
1269 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001270 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001271 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001272 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001273 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001274 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001275 PIDecl->setGetterCXXConstructor(ResExpr);
1276 }
1277 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001278 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1279 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1280 Diag(getterMethod->getLocation(),
1281 diag::warn_property_getter_owning_mismatch);
1282 Diag(property->getLocation(), diag::note_property_declare);
1283 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001284 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1285 switch (getterMethod->getMethodFamily()) {
1286 case OMF_retain:
1287 case OMF_retainCount:
1288 case OMF_release:
1289 case OMF_autorelease:
1290 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1291 << 1 << getterMethod->getSelector();
1292 break;
1293 default:
1294 break;
1295 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001296 }
1297 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1298 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001299 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1300 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001301 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001302 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001303 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1304 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001305 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001306 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001307 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001308 Expr *LoadSelfExpr =
1309 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001310 CK_LValueToRValue, SelfExpr, nullptr,
1311 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001312 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001313 new (Context) ObjCIvarRefExpr(Ivar,
1314 Ivar->getUsageType(SelfDecl->getType()),
1315 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001316 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001317 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001318 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1319 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001320 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001321 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1322 VK_LValue, PropertyDiagLoc);
1323 MarkDeclRefReferenced(rhs);
1324 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001325 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001326 if (property->getPropertyAttributes() &
1327 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001328 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001329 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001330 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1331 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001332 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001333 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001334 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001335 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001336 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001337 Diag(FuncDecl->getLocStart(),
1338 diag::note_callee_decl) << FuncDecl;
1339 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001340 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001341 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001342 }
1343 }
1344
Ted Kremenekac597f32010-03-12 00:46:40 +00001345 if (IC) {
1346 if (Synthesize)
1347 if (ObjCPropertyImplDecl *PPIDecl =
1348 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1349 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1350 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1351 << PropertyIvar;
1352 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1353 }
1354
1355 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001356 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Ted Kremenekac597f32010-03-12 00:46:40 +00001357 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1358 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001359 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001360 }
1361 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001362 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001363 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001364 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001365 // Diagnose if an ivar was lazily synthesdized due to a previous
1366 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001367 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001368 ObjCInterfaceDecl *ClassDeclared=nullptr;
1369 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001370 if (!Synthesize)
1371 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1372 else {
1373 if (PropertyIvar && PropertyIvar != PropertyId)
1374 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1375 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001376 // Issue diagnostics only if Ivar belongs to current class.
1377 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001378 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001379 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1380 << PropertyId;
1381 Ivar->setInvalidDecl();
1382 }
1383 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001384 } else {
1385 if (Synthesize)
1386 if (ObjCPropertyImplDecl *PPIDecl =
1387 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001388 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001389 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1390 << PropertyIvar;
1391 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1392 }
1393
1394 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001395 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001396 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001397 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001398 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001399 }
1400 CatImplClass->addPropertyImplementation(PIDecl);
1401 }
1402
John McCall48871652010-08-21 09:40:31 +00001403 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001404}
1405
1406//===----------------------------------------------------------------------===//
1407// Helper methods.
1408//===----------------------------------------------------------------------===//
1409
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001410/// DiagnosePropertyMismatch - Compares two properties for their
1411/// attributes and types and warns on a variety of inconsistencies.
1412///
1413void
1414Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1415 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001416 const IdentifierInfo *inheritedName,
1417 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001418 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001419 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001420 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001421 SuperProperty->getPropertyAttributes();
1422
1423 // We allow readonly properties without an explicit ownership
1424 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1425 // to be overridden by a property with any explicit ownership in the subclass.
1426 if (!OverridingProtocolProperty &&
1427 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1428 ;
1429 else {
1430 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1431 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1432 Diag(Property->getLocation(), diag::warn_readonly_property)
1433 << Property->getDeclName() << inheritedName;
1434 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1435 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001436 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001437 << Property->getDeclName() << "copy" << inheritedName;
1438 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1439 unsigned CAttrRetain =
1440 (CAttr &
1441 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1442 unsigned SAttrRetain =
1443 (SAttr &
1444 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1445 bool CStrong = (CAttrRetain != 0);
1446 bool SStrong = (SAttrRetain != 0);
1447 if (CStrong != SStrong)
1448 Diag(Property->getLocation(), diag::warn_property_attribute)
1449 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1450 }
John McCall31168b02011-06-15 23:02:42 +00001451 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001452
Douglas Gregor429183e2015-12-09 22:57:32 +00001453 // Check for nonatomic; note that nonatomic is effectively
1454 // meaningless for readonly properties, so don't diagnose if the
1455 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001456 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001457 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001458 Diag(Property->getLocation(), diag::warn_property_attribute)
1459 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001460 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1461 }
1462 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001463 Diag(Property->getLocation(), diag::warn_property_attribute)
1464 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001465 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1466 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001467
1468 QualType LHSType =
1469 Context.getCanonicalType(SuperProperty->getType());
1470 QualType RHSType =
1471 Context.getCanonicalType(Property->getType());
1472
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001473 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001474 // Do cases not handled in above.
1475 // FIXME. For future support of covariant property types, revisit this.
1476 bool IncompatibleObjC = false;
1477 QualType ConvertedType;
1478 if (!isObjCPointerConversion(RHSType, LHSType,
1479 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001480 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001481 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1482 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001483 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1484 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001485 }
1486}
1487
1488bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1489 ObjCMethodDecl *GetterMethod,
1490 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001491 if (!GetterMethod)
1492 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001493 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001494 QualType PropertyIvarType = property->getType().getNonReferenceType();
1495 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1496 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001497 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1498 const ObjCObjectPointerType *getterObjCPtr = nullptr;
1499 if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) &&
1500 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1501 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001502 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001503 != Compatible) {
1504 Diag(Loc, diag::error_property_accessor_type)
1505 << property->getDeclName() << PropertyIvarType
1506 << GetterMethod->getSelector() << GetterType;
1507 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1508 return true;
1509 } else {
1510 compat = true;
1511 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1512 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1513 if (lhsType != rhsType && lhsType->isArithmeticType())
1514 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001515 }
1516 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001517
1518 if (!compat) {
1519 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1520 << property->getDeclName()
1521 << GetterMethod->getSelector();
1522 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1523 return true;
1524 }
1525
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001526 return false;
1527}
1528
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001529/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001530/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001531static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1532 ObjCContainerDecl::PropertyMap &PropMap,
1533 ObjCContainerDecl::PropertyMap &SuperPropMap,
1534 bool IncludeProtocols = true) {
1535
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001536 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Rena7a8b1f2016-01-26 18:05:23 +00001537 for (auto *Prop : IDecl->instance_properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001538 PropMap[Prop->getIdentifier()] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001539
1540 // Collect the properties from visible extensions.
1541 for (auto *Ext : IDecl->visible_extensions())
1542 CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
1543
Ted Kremenek204c3c52014-02-22 00:02:03 +00001544 if (IncludeProtocols) {
1545 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001546 for (auto *PI : IDecl->all_referenced_protocols())
1547 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001548 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001549 }
1550 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Rena7a8b1f2016-01-26 18:05:23 +00001551 for (auto *Prop : CATDecl->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001552 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001553 if (IncludeProtocols) {
1554 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001555 for (auto *PI : CATDecl->protocols())
1556 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001557 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001558 }
1559 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Rena7a8b1f2016-01-26 18:05:23 +00001560 for (auto *Prop : PDecl->instance_properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001561 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1562 // Exclude property for protocols which conform to class's super-class,
1563 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001564 if (!PropertyFromSuper ||
1565 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001566 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1567 if (!PropEntry)
1568 PropEntry = Prop;
1569 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001570 }
1571 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001572 for (auto *PI : PDecl->protocols())
1573 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001574 }
1575}
1576
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001577/// CollectSuperClassPropertyImplementations - This routine collects list of
1578/// properties to be implemented in super class(s) and also coming from their
1579/// conforming protocols.
1580static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001581 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001582 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001583 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001584 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001585 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001586 SDecl = SDecl->getSuperClass();
1587 }
1588 }
1589}
1590
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001591/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1592/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1593/// declared in class 'IFace'.
1594bool
1595Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1596 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1597 if (!IV->getSynthesize())
1598 return false;
1599 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1600 Method->isInstanceMethod());
1601 if (!IMD || !IMD->isPropertyAccessor())
1602 return false;
1603
1604 // look up a property declaration whose one of its accessors is implemented
1605 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001606 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001607 if ((Property->getGetterName() == IMD->getSelector() ||
1608 Property->getSetterName() == IMD->getSelector()) &&
1609 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001610 return true;
1611 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001612 // Also look up property declaration in class extension whose one of its
1613 // accessors is implemented by this method.
1614 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001615 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001616 if ((Property->getGetterName() == IMD->getSelector() ||
1617 Property->getSetterName() == IMD->getSelector()) &&
1618 (Property->getPropertyIvarDecl() == IV))
1619 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001620 return false;
1621}
1622
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001623static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1624 ObjCPropertyDecl *Prop) {
1625 bool SuperClassImplementsGetter = false;
1626 bool SuperClassImplementsSetter = false;
1627 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1628 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001629
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001630 while (IDecl->getSuperClass()) {
1631 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1632 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1633 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001634
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001635 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1636 SuperClassImplementsSetter = true;
1637 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1638 return true;
1639 IDecl = IDecl->getSuperClass();
1640 }
1641 return false;
1642}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001643
James Dennett2a4d13c2012-06-15 07:13:21 +00001644/// \brief Default synthesizes all properties which must be synthesized
1645/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001646void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1647 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001648
Anna Zaks673d76b2012-10-18 19:17:53 +00001649 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001650 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1651 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001652 if (PropMap.empty())
1653 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001654 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001655 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1656
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001657 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1658 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001659 // Is there a matching property synthesize/dynamic?
1660 if (Prop->isInvalidDecl() ||
1661 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1662 continue;
1663 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001664 if (IMPDecl->FindPropertyImplDecl(
1665 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001666 continue;
1667 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1668 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1669 continue;
1670 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1671 continue;
1672 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001673 if (ObjCPropertyImplDecl *PID =
1674 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001675 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1676 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001677 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001678 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001679 continue;
1680 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001681 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001682 if (ObjCProtocolDecl *Proto =
1683 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001684 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001685 // Suppress the warning if class's superclass implements property's
1686 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001687 // Or, if property is going to be implemented in its super class.
1688 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001689 Diag(IMPDecl->getLocation(),
1690 diag::warn_auto_synthesizing_protocol_property)
1691 << Prop << Proto;
1692 Diag(Prop->getLocation(), diag::note_property_declare);
1693 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001694 continue;
1695 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001696 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001697 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001698 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1699 (PropInSuperClass->getPropertyAttributes() &
1700 ObjCPropertyDecl::OBJC_PR_readonly) &&
1701 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1702 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1703 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1704 << Prop->getIdentifier();
1705 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1706 }
1707 else {
1708 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1709 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001710 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001711 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1712 }
1713 continue;
1714 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001715 // We use invalid SourceLocations for the synthesized ivars since they
1716 // aren't really synthesized at a particular location; they just exist.
1717 // Saying that they are located at the @implementation isn't really going
1718 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001719 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1720 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1721 true,
1722 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001723 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001724 Prop->getLocation(), Prop->getQueryKind()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001725 if (PIDecl) {
1726 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001727 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001728 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001729 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001730}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001731
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001732void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001733 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001734 return;
1735 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1736 if (!IC)
1737 return;
1738 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001739 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001740 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001741}
1742
Ted Kremenek7e812952014-02-21 19:41:30 +00001743static void DiagnoseUnimplementedAccessor(Sema &S,
1744 ObjCInterfaceDecl *PrimaryClass,
1745 Selector Method,
1746 ObjCImplDecl* IMPDecl,
1747 ObjCContainerDecl *CDecl,
1748 ObjCCategoryDecl *C,
1749 ObjCPropertyDecl *Prop,
1750 Sema::SelectorSet &SMap) {
1751 // When reporting on missing property setter/getter implementation in
1752 // categories, do not report when they are declared in primary class,
1753 // class's protocol, or one of it super classes. This is because,
1754 // the class is going to implement them.
1755 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001756 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001757 !PrimaryClass->lookupPropertyAccessor(Method, C,
1758 Prop->isClassProperty()))) {
Ted Kremenek7e812952014-02-21 19:41:30 +00001759 S.Diag(IMPDecl->getLocation(),
1760 isa<ObjCCategoryDecl>(CDecl) ?
1761 diag::warn_setter_getter_impl_required_in_category :
1762 diag::warn_setter_getter_impl_required)
1763 << Prop->getDeclName() << Method;
1764 S.Diag(Prop->getLocation(),
1765 diag::note_property_declare);
1766 if (S.LangOpts.ObjCDefaultSynthProperties &&
1767 S.LangOpts.ObjCRuntime.isNonFragile())
1768 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1769 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1770 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1771 }
1772}
1773
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001774void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001775 ObjCContainerDecl *CDecl,
1776 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001777 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001778 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1779
Ted Kremenek348e88c2014-02-21 19:41:34 +00001780 if (!SynthesizeProperties) {
1781 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001782 // Gather properties which need not be implemented in this class
1783 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001784 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001785 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1786 // For categories, no need to implement properties declared in
1787 // its primary class (and its super classes) if property is
1788 // declared in one of those containers.
1789 if ((IDecl = C->getClassInterface())) {
1790 ObjCInterfaceDecl::PropertyDeclOrder PO;
1791 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1792 }
1793 }
1794 if (IDecl)
1795 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1796
1797 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1798 }
1799
Ted Kremenek38882022014-02-21 19:41:39 +00001800 // Scan the @interface to see if any of the protocols it adopts
1801 // require an explicit implementation, via attribute
1802 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001803 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001804 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001805
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001806 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001807 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1808 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001809 // Lazily construct a set of all the properties in the @interface
1810 // of the class, without looking at the superclass. We cannot
1811 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001812 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001813 // as scans the adopted protocols. This work only triggers for protocols
1814 // with the attribute, which is very rare, and only occurs when
1815 // analyzing the @implementation.
1816 if (!LazyMap) {
1817 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1818 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1819 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1820 /* IncludeProtocols */ false);
1821 }
Ted Kremenek38882022014-02-21 19:41:39 +00001822 // Add the properties of 'PDecl' to the list of properties that
1823 // need to be implemented.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001824 for (auto *PropDecl : PDecl->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001825 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001826 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001827 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001828 }
1829 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001830 }
Ted Kremenek38882022014-02-21 19:41:39 +00001831
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001832 if (PropMap.empty())
1833 return;
1834
1835 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001836 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001837 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001838
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001839 SelectorSet InsMap;
1840 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001841 for (const auto *I : IMPDecl->instance_methods())
1842 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001843
1844 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001845 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001846 if (C && !C->IsClassExtension())
1847 if ((PrimaryClass = C->getClassInterface()))
1848 // Report unimplemented properties in the category as well.
1849 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1850 // When reporting on missing setter/getters, do not report when
1851 // setter/getter is implemented in category's primary class
1852 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001853 for (const auto *I : IMP->instance_methods())
1854 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001855 }
1856
Anna Zaks673d76b2012-10-18 19:17:53 +00001857 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001858 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1859 ObjCPropertyDecl *Prop = P->second;
1860 // Is there a matching propery synthesize/dynamic?
1861 if (Prop->isInvalidDecl() ||
1862 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001863 PropImplMap.count(Prop) ||
1864 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001865 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001866
1867 // Diagnose unimplemented getters and setters.
1868 DiagnoseUnimplementedAccessor(*this,
1869 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1870 if (!Prop->isReadOnly())
1871 DiagnoseUnimplementedAccessor(*this,
1872 PrimaryClass, Prop->getSetterName(),
1873 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001874 }
1875}
1876
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001877void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001878 for (const auto *propertyImpl : impDecl->property_impls()) {
1879 const auto *property = propertyImpl->getPropertyDecl();
1880
1881 // Warn about null_resettable properties with synthesized setters,
1882 // because the setter won't properly handle nil.
1883 if (propertyImpl->getPropertyImplementation()
1884 == ObjCPropertyImplDecl::Synthesize &&
1885 (property->getPropertyAttributes() &
1886 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1887 property->getGetterMethodDecl() &&
1888 property->getSetterMethodDecl()) {
1889 auto *getterMethod = property->getGetterMethodDecl();
1890 auto *setterMethod = property->getSetterMethodDecl();
1891 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1892 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1893 SourceLocation loc = propertyImpl->getLocation();
1894 if (loc.isInvalid())
1895 loc = impDecl->getLocStart();
1896
1897 Diag(loc, diag::warn_null_resettable_setter)
1898 << setterMethod->getSelector() << property->getDeclName();
1899 }
1900 }
1901 }
1902}
1903
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904void
1905Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001906 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001907 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001908 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001909 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001910 ObjCContainerDecl::PropertyMap PM;
Manman Rena7a8b1f2016-01-26 18:05:23 +00001911 for (auto *Prop : IDecl->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001912 PM[Prop->getIdentifier()] = Prop;
1913 for (const auto *Ext : IDecl->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001914 for (auto *Prop : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001915 PM[Prop->getIdentifier()] = Prop;
1916
Manman Renefe1bac2016-01-27 20:00:32 +00001917 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1918 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001919 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001920 ObjCMethodDecl *GetterMethod = nullptr;
1921 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001922 bool LookedUpGetterSetter = false;
1923
Bill Wendling44426052012-12-20 19:22:21 +00001924 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001925 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001926
John McCall43192862011-09-13 18:31:23 +00001927 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1928 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00001929 GetterMethod = Property->isClassProperty() ?
1930 IMPDecl->getClassMethod(Property->getGetterName()) :
1931 IMPDecl->getInstanceMethod(Property->getGetterName());
1932 SetterMethod = Property->isClassProperty() ?
1933 IMPDecl->getClassMethod(Property->getSetterName()) :
1934 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001935 LookedUpGetterSetter = true;
1936 if (GetterMethod) {
1937 Diag(GetterMethod->getLocation(),
1938 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001939 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001940 Diag(Property->getLocation(), diag::note_property_declare);
1941 }
1942 if (SetterMethod) {
1943 Diag(SetterMethod->getLocation(),
1944 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001945 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001946 Diag(Property->getLocation(), diag::note_property_declare);
1947 }
1948 }
1949
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001950 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001951 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1952 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001953 continue;
Manman Ren5b786402016-01-28 18:49:28 +00001954 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
1955 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001956 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1957 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001958 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00001959 GetterMethod = Property->isClassProperty() ?
1960 IMPDecl->getClassMethod(Property->getGetterName()) :
1961 IMPDecl->getInstanceMethod(Property->getGetterName());
1962 SetterMethod = Property->isClassProperty() ?
1963 IMPDecl->getClassMethod(Property->getSetterName()) :
1964 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001965 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001966 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1967 SourceLocation MethodLoc =
1968 (GetterMethod ? GetterMethod->getLocation()
1969 : SetterMethod->getLocation());
1970 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001971 << Property->getIdentifier() << (GetterMethod != nullptr)
1972 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001973 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001974 if (Property->getLParenLoc().isValid() &&
1975 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001976 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001977 SourceLocation AfterLParen =
1978 getLocForEndOfToken(Property->getLParenLoc());
1979 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
1980 : "nonatomic";
1981 Diag(Property->getLocation(),
1982 diag::note_atomic_property_fixup_suggest)
1983 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
1984 } else if (Property->getLParenLoc().isInvalid()) {
1985 //@property id etc.
1986 SourceLocation startLoc =
1987 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1988 Diag(Property->getLocation(),
1989 diag::note_atomic_property_fixup_suggest)
1990 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001991 }
1992 else
1993 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001994 Diag(Property->getLocation(), diag::note_property_declare);
1995 }
1996 }
1997 }
1998}
1999
John McCall31168b02011-06-15 23:02:42 +00002000void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002001 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002002 return;
2003
Aaron Ballmand85eff42014-03-14 15:02:45 +00002004 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002005 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002006 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002007 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002008 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002009 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2010 if (!method)
2011 continue;
2012 ObjCMethodFamily family = method->getMethodFamily();
2013 if (family == OMF_alloc || family == OMF_copy ||
2014 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002015 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002016 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002017 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002018 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002019
2020 // Look for a getter explicitly declared alongside the property.
2021 // If we find one, use its location for the note.
2022 SourceLocation noteLoc = PD->getLocation();
2023 SourceLocation fixItLoc;
2024 for (auto *getterRedecl : method->redecls()) {
2025 if (getterRedecl->isImplicit())
2026 continue;
2027 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2028 continue;
2029 noteLoc = getterRedecl->getLocation();
2030 fixItLoc = getterRedecl->getLocEnd();
2031 }
2032
2033 Preprocessor &PP = getPreprocessor();
2034 TokenValue tokens[] = {
2035 tok::kw___attribute, tok::l_paren, tok::l_paren,
2036 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2037 PP.getIdentifierInfo("none"), tok::r_paren,
2038 tok::r_paren, tok::r_paren
2039 };
2040 StringRef spelling = "__attribute__((objc_method_family(none)))";
2041 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2042 if (!macroName.empty())
2043 spelling = macroName;
2044
2045 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2046 << method->getDeclName() << spelling;
2047 if (fixItLoc.isValid()) {
2048 SmallString<64> fixItText(" ");
2049 fixItText += spelling;
2050 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2051 }
John McCall31168b02011-06-15 23:02:42 +00002052 }
2053 }
2054 }
2055}
2056
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002057void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002058 const ObjCImplementationDecl *ImplD,
2059 const ObjCInterfaceDecl *IFD) {
2060 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002061 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2062 if (!SuperD)
2063 return;
2064
2065 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002066 for (const auto *I : ImplD->instance_methods())
2067 if (I->getMethodFamily() == OMF_init)
2068 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002069
2070 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2071 SuperD->getDesignatedInitializers(DesignatedInits);
2072 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2073 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2074 const ObjCMethodDecl *MD = *I;
2075 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002076 bool Ignore = false;
2077 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2078 Ignore = IMD->isUnavailable();
2079 }
2080 if (!Ignore) {
2081 Diag(ImplD->getLocation(),
2082 diag::warn_objc_implementation_missing_designated_init_override)
2083 << MD->getSelector();
2084 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2085 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002086 }
2087 }
2088}
2089
John McCallad31b5f2010-11-10 07:01:40 +00002090/// AddPropertyAttrs - Propagates attributes from a property to the
2091/// implicitly-declared getter or setter for that property.
2092static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2093 ObjCPropertyDecl *Property) {
2094 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002095 for (const auto *A : Property->attrs()) {
2096 if (isa<DeprecatedAttr>(A) ||
2097 isa<UnavailableAttr>(A) ||
2098 isa<AvailabilityAttr>(A))
2099 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002100 }
John McCallad31b5f2010-11-10 07:01:40 +00002101}
2102
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002103/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2104/// have the property type and issue diagnostics if they don't.
2105/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002106/// appropriate lookup tables.
2107void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002108 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002109 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002110 if (CD->isInvalidDecl())
2111 return;
2112
Manman Rend36f7d52016-01-27 20:10:32 +00002113 bool IsClassProperty = property->isClassProperty();
2114 GetterMethod = IsClassProperty ?
2115 CD->getClassMethod(property->getGetterName()) :
2116 CD->getInstanceMethod(property->getGetterName());
2117
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002118 // if setter or getter is not found in class extension, it might be
2119 // in the primary class.
2120 if (!GetterMethod)
2121 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2122 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002123 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2124 getClassMethod(property->getGetterName()) :
2125 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002126 getInstanceMethod(property->getGetterName());
2127
Manman Rend36f7d52016-01-27 20:10:32 +00002128 SetterMethod = IsClassProperty ?
2129 CD->getClassMethod(property->getSetterName()) :
2130 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002131 if (!SetterMethod)
2132 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2133 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002134 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2135 getClassMethod(property->getSetterName()) :
2136 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002137 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002138 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2139 property->getLocation());
2140
2141 if (SetterMethod) {
2142 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2143 property->getPropertyAttributes();
2144 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002145 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2146 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2148 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002149 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002150 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2151 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002152 Diag(property->getLocation(),
2153 diag::warn_accessor_property_type_mismatch)
2154 << property->getDeclName()
2155 << SetterMethod->getSelector();
2156 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2157 }
2158 }
2159
2160 // Synthesize getter/setter methods if none exist.
2161 // Find the default getter and if one not found, add one.
2162 // FIXME: The synthesized property we set here is misleading. We almost always
2163 // synthesize these methods unless the user explicitly provided prototypes
2164 // (which is odd, but allowed). Sema should be typechecking that the
2165 // declarations jive in that situation (which it is not currently).
2166 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002167 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002168 // Declare a getter method and add it to the list of methods
2169 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002170 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002171
Douglas Gregor849ebc22015-06-19 18:14:46 +00002172 // If the property is null_resettable, the getter returns nonnull.
2173 QualType resultTy = property->getType();
2174 if (property->getPropertyAttributes() &
2175 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2176 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002177 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002178 if (*nullability == NullabilityKind::Unspecified)
2179 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2180 modifiedTy, modifiedTy);
2181 }
2182 }
2183
Ted Kremenek2f075632010-09-21 20:52:59 +00002184 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2185 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002186 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002187 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002188 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002189 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002190 (property->getPropertyImplementation() ==
2191 ObjCPropertyDecl::Optional) ?
2192 ObjCMethodDecl::Optional :
2193 ObjCMethodDecl::Required);
2194 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002195
2196 AddPropertyAttrs(*this, GetterMethod, property);
2197
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002198 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002199 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2200 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002201
2202 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2203 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002204 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002205
2206 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002207 GetterMethod->addAttr(
2208 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2209 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002210
2211 if (getLangOpts().ObjCAutoRefCount)
2212 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002213 } else
2214 // A user declared getter will be synthesize when @synthesize of
2215 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002216 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002217 property->setGetterMethodDecl(GetterMethod);
2218
2219 // Skip setter if property is read-only.
2220 if (!property->isReadOnly()) {
2221 // Find the default setter and if one not found, add one.
2222 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002223 // No instance/class method of same name as property setter name was
2224 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002225 // Declare a setter method and add it to the list of methods
2226 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002227 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002228
2229 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002230 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002231 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002232 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002233 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002234 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002235 /*isImplicitlyDeclared=*/true,
2236 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002237 (property->getPropertyImplementation() ==
2238 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002239 ObjCMethodDecl::Optional :
2240 ObjCMethodDecl::Required);
2241
Douglas Gregor849ebc22015-06-19 18:14:46 +00002242 // If the property is null_resettable, the setter accepts a
2243 // nullable value.
2244 QualType paramTy = property->getType().getUnqualifiedType();
2245 if (property->getPropertyAttributes() &
2246 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2247 QualType modifiedTy = paramTy;
2248 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2249 if (*nullability == NullabilityKind::Unspecified)
2250 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2251 modifiedTy, modifiedTy);
2252 }
2253 }
2254
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002255 // Invent the arguments for the setter. We don't bother making a
2256 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002257 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2258 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002259 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002260 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002261 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002262 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002263 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002264 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002265
2266 AddPropertyAttrs(*this, SetterMethod, property);
2267
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002268 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002269 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002270 SetterMethod->addAttr(
2271 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2272 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002273 // It's possible for the user to have set a very odd custom
2274 // setter selector that causes it to have a method family.
2275 if (getLangOpts().ObjCAutoRefCount)
2276 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002277 } else
2278 // A user declared setter will be synthesize when @synthesize of
2279 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002280 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002281 property->setSetterMethodDecl(SetterMethod);
2282 }
2283 // Add any synthesized methods to the global pool. This allows us to
2284 // handle the following, which is supported by GCC (and part of the design).
2285 //
2286 // @interface Foo
2287 // @property double bar;
2288 // @end
2289 //
2290 // void thisIsUnfortunate() {
2291 // id foo;
2292 // double bar = [foo bar];
2293 // }
2294 //
Manman Rend36f7d52016-01-27 20:10:32 +00002295 if (!IsClassProperty) {
2296 if (GetterMethod)
2297 AddInstanceMethodToGlobalPool(GetterMethod);
2298 if (SetterMethod)
2299 AddInstanceMethodToGlobalPool(SetterMethod);
2300 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002301
2302 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2303 if (!CurrentClass) {
2304 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2305 CurrentClass = Cat->getClassInterface();
2306 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2307 CurrentClass = Impl->getClassInterface();
2308 }
2309 if (GetterMethod)
2310 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2311 if (SetterMethod)
2312 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002313}
2314
John McCall48871652010-08-21 09:40:31 +00002315void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002316 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002317 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002318 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002319 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002320 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002321 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002322
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002323 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2324 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2325 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2326 << "readonly" << "readwrite";
2327
2328 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2329 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002330
2331 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002332 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002333 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2334 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002335 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002336 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002337 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2338 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2339 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002340 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002341 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002342 }
2343
2344 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002345 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2346 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002347 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2348 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002349 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002350 }
Bill Wendling44426052012-12-20 19:22:21 +00002351 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002352 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2353 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002354 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002355 }
Bill Wendling44426052012-12-20 19:22:21 +00002356 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002357 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2358 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002359 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002360 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002361 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002362 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002363 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2364 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002365 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002366 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002367 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002368 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002369 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2370 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002371 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2372 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002373 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002374 }
Bill Wendling44426052012-12-20 19:22:21 +00002375 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002376 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2377 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002378 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002379 }
Bill Wendling44426052012-12-20 19:22:21 +00002380 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002381 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2382 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002383 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002384 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002385 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002386 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002387 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2388 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002389 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002390 }
Bill Wendling44426052012-12-20 19:22:21 +00002391 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2392 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002393 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2394 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002395 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002396 }
Bill Wendling44426052012-12-20 19:22:21 +00002397 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002398 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2399 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002400 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002401 }
Bill Wendling44426052012-12-20 19:22:21 +00002402 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002403 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2404 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002405 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002406 }
2407 }
Bill Wendling44426052012-12-20 19:22:21 +00002408 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2409 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002410 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2411 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002412 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002413 }
Bill Wendling44426052012-12-20 19:22:21 +00002414 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2415 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002416 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2417 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002418 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002419 }
2420
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002421 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002422 // 'weak' and 'nonnull' are mutually exclusive.
2423 if (auto nullability = PropertyTy->getNullability(Context)) {
2424 if (*nullability == NullabilityKind::NonNull)
2425 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2426 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002427 }
2428 }
2429
Bill Wendling44426052012-12-20 19:22:21 +00002430 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2431 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002432 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2433 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002434 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002435 }
2436
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002437 // Warn if user supplied no assignment attribute, property is
2438 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002439 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2440 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2441 // do nothing
2442 } else if (getLangOpts().ObjCAutoRefCount) {
2443 // With arc, @property definitions should default to strong when
2444 // not specified.
2445 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2446 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002447 bool isAnyClassTy =
2448 (PropertyTy->isObjCClassType() ||
2449 PropertyTy->isObjCQualifiedClassType());
2450 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2451 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002452 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002453 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002454 else if (propertyInPrimaryClass) {
2455 // Don't issue warning on property with no life time in class
2456 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002457 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002458 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002459 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002460
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002461 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002462 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002463 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002464 }
John McCallb61e14e2015-10-27 04:54:50 +00002465 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002466
2467 // FIXME: Implement warning dependent on NSCopying being
2468 // implemented. See also:
2469 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2470 // (please trim this list while you are at it).
2471 }
2472
Bill Wendling44426052012-12-20 19:22:21 +00002473 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2474 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002475 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002476 && PropertyTy->isBlockPointerType())
2477 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002478 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2479 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2480 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002481 PropertyTy->isBlockPointerType())
2482 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002483
Bill Wendling44426052012-12-20 19:22:21 +00002484 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2485 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002486 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2487
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002488}