blob: f42daced863b620cc988938e77bf7afc6aa7447b [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;
306
307 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
308}
309
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000310static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000311 SourceLocation LParenLoc, SourceLocation &Loc) {
312 if (LParenLoc.isMacroID())
313 return false;
314
315 SourceManager &SM = Context.getSourceManager();
316 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
317 // Try to load the file buffer.
318 bool invalidTemp = false;
319 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
320 if (invalidTemp)
321 return false;
322 const char *tokenBegin = file.data() + locInfo.second;
323
324 // Lex from the start of the given location.
325 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
326 Context.getLangOpts(),
327 file.begin(), tokenBegin, file.end());
328 Token Tok;
329 do {
330 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000331 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000332 Loc = Tok.getLocation();
333 return true;
334 }
335 } while (Tok.isNot(tok::r_paren));
336 return false;
337
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000338}
339
Douglas Gregor429183e2015-12-09 22:57:32 +0000340/// Check for a mismatch in the atomicity of the given properties.
341static void checkAtomicPropertyMismatch(Sema &S,
342 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000343 ObjCPropertyDecl *NewProperty,
344 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000345 // If the atomicity of both matches, we're done.
346 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000347 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
348 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000349 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000350 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
351 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000352 if (OldIsAtomic == NewIsAtomic) return;
353
354 // Determine whether the given property is readonly and implicitly
355 // atomic.
356 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
357 // Is it readonly?
358 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000359 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000360
361 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000362 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000363
364 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000365 if (Property->getPropertyAttributesAsWritten() &
366 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000367 return false;
368
369 return true;
370 };
371
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000372 // If we're allowed to propagate atomicity, and the new property did
373 // not specify atomicity at all, propagate.
374 const unsigned AtomicityMask =
375 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
376 if (PropagateAtomicity &&
377 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
378 unsigned Attrs = NewProperty->getPropertyAttributes();
379 Attrs = Attrs & ~AtomicityMask;
380 if (OldIsAtomic)
381 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
382 else
383 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
384
385 NewProperty->overwritePropertyAttributes(Attrs);
386 return;
387 }
388
Douglas Gregor429183e2015-12-09 22:57:32 +0000389 // One of the properties is atomic; if it's a readonly property, and
390 // 'atomic' wasn't explicitly specified, we're okay.
391 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
392 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
393 return;
394
395 // Diagnose the conflict.
396 const IdentifierInfo *OldContextName;
397 auto *OldDC = OldProperty->getDeclContext();
398 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
399 OldContextName = Category->getClassInterface()->getIdentifier();
400 else
401 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
402
403 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
404 << NewProperty->getDeclName() << "atomic"
405 << OldContextName;
406 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
407}
408
Douglas Gregor90d34422013-01-21 19:05:22 +0000409ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000410Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000411 SourceLocation AtLoc,
412 SourceLocation LParenLoc,
413 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000414 Selector GetterSel, Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000415 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000416 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000417 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000418 QualType T,
419 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000420 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000421 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000423 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000424 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000425 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
426
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 // We need to look in the @interface to see if the @property was
428 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000429 if (!CCPrimary) {
430 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000431 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000432 }
433
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000434 // Find the property in the extended class's primary class or
435 // extensions.
Ted Kremenek959e8302010-03-12 02:31:10 +0000436 ObjCPropertyDecl *PIDecl =
437 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
438
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000439 // If we found a property in an extension, complain.
440 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
441 Diag(AtLoc, diag::err_duplicate_property);
442 Diag(PIDecl->getLocation(), diag::note_property_declare);
443 return nullptr;
444 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000445
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000446 // Check for consistency with the previous declaration, if there is one.
447 if (PIDecl) {
448 // A readonly property declared in the primary class can be refined
449 // by adding a readwrite property within an extension.
450 // Anything else is an error.
451 if (!(PIDecl->isReadOnly() && isReadWrite)) {
452 // Tailor the diagnostics for the common case where a readwrite
453 // property is declared both in the @interface and the continuation.
454 // This is a common error where the user often intended the original
455 // declaration to be readonly.
456 unsigned diag =
457 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
458 (PIDecl->getPropertyAttributesAsWritten() &
459 ObjCPropertyDecl::OBJC_PR_readwrite)
460 ? diag::err_use_continuation_class_redeclaration_readwrite
461 : diag::err_use_continuation_class;
462 Diag(AtLoc, diag)
463 << CCPrimary->getDeclName();
464 Diag(PIDecl->getLocation(), diag::note_property_declare);
465 return nullptr;
466 }
467
468 // Check for consistency of getters.
469 if (PIDecl->getGetterName() != GetterSel) {
470 // If the getter was written explicitly, complain.
471 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
472 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
473 << PIDecl->getGetterName() << GetterSel;
474 Diag(PIDecl->getLocation(), diag::note_property_declare);
475 }
476
477 // Always adopt the getter from the original declaration.
478 GetterSel = PIDecl->getGetterName();
479 Attributes |= ObjCDeclSpec::DQ_PR_getter;
480 }
481
482 // Check consistency of ownership.
483 unsigned ExistingOwnership
484 = getOwnershipRule(PIDecl->getPropertyAttributes());
485 unsigned NewOwnership = getOwnershipRule(Attributes);
486 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
487 // If the ownership was written explicitly, complain.
488 if (getOwnershipRule(AttributesAsWritten)) {
489 Diag(AtLoc, diag::warn_property_attr_mismatch);
490 Diag(PIDecl->getLocation(), diag::note_property_declare);
491 }
492
493 // Take the ownership from the original property.
494 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
495 }
496
497 // If the redeclaration is 'weak' but the original property is not,
498 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
499 !(PIDecl->getPropertyAttributesAsWritten()
500 & ObjCPropertyDecl::OBJC_PR_weak) &&
501 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
502 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
503 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
504 Diag(PIDecl->getLocation(), diag::note_property_declare);
505 }
506 }
507
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000508 // Create a new ObjCPropertyDecl with the DeclContext being
509 // the class extension.
510 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
511 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000512 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000513 Attributes, AttributesAsWritten,
514 T, TSI, MethodImplKind, DC);
515
516 // If there was no declaration of a property with the same name in
517 // the primary class, we're done.
518 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000519 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000520 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000521 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000522
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000523 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
524 bool IncompatibleObjC = false;
525 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000526 // Relax the strict type matching for property type in continuation class.
527 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000528 // as it narrows the object type in its primary class property. Note that
529 // this conversion is safe only because the wider type is for a 'readonly'
530 // property in primary class and 'narrowed' type for a 'readwrite' property
531 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000532 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
533 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
534 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
535 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
536 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000537 ConvertedType, IncompatibleObjC))
538 || IncompatibleObjC) {
539 Diag(AtLoc,
540 diag::err_type_mismatch_continuation_class) << PDecl->getType();
541 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000542 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000543 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000544 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000545
546 // Check that atomicity of property in class extension matches the previous
547 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000548 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000549
Douglas Gregore17765e2015-11-03 17:02:34 +0000550 // Make sure getter/setter are appropriately synthesized.
551 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000552 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000553}
554
555ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
556 ObjCContainerDecl *CDecl,
557 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000558 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000559 FieldDeclarator &FD,
560 Selector GetterSel,
561 Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000562 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000563 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000564 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000565 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000566 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000567 tok::ObjCKeywordKind MethodImplKind,
568 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000569 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000570
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000571 // Property defaults to 'assign' if it is readwrite, unless this is ARC
572 // and the type is retainable.
573 bool isAssign;
574 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
575 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
576 isAssign = true;
577 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
578 isAssign = false;
579 } else {
580 isAssign = (!getLangOpts().ObjCAutoRefCount ||
581 !T->isObjCRetainableType());
582 }
583
584 // Issue a warning if property is 'assign' as default and its
585 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000586 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000587 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000588 if (const ObjCObjectPointerType *ObjPtrTy =
589 T->getAs<ObjCObjectPointerType>()) {
590 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
591 if (IDecl)
592 if (ObjCProtocolDecl* PNSCopying =
593 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
594 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
595 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000596 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000597 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000598
599 if (T->isObjCObjectType()) {
600 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000601 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000602 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
603 << FixItHint::CreateInsertion(StarLoc, "*");
604 T = Context.getObjCObjectPointerType(T);
605 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
606 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
607 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000608
Ted Kremenek959e8302010-03-12 02:31:10 +0000609 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000610 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
611 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000612 PropertyId, AtLoc,
613 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000614
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000615 if (ObjCPropertyDecl *prevDecl =
616 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000618 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000619 PDecl->setInvalidDecl();
620 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000621 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000623 if (lexicalDC)
624 PDecl->setLexicalDeclContext(lexicalDC);
625 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000626
627 if (T->isArrayType() || T->isFunctionType()) {
628 Diag(AtLoc, diag::err_property_type) << T;
629 PDecl->setInvalidDecl();
630 }
631
632 ProcessDeclAttributes(S, PDecl, FD.D);
633
634 // Regardless of setter/getter attribute, we save the default getter/setter
635 // selector names in anticipation of declaration of setter/getter methods.
636 PDecl->setGetterName(GetterSel);
637 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000638 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000639 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000640
Bill Wendling44426052012-12-20 19:22:21 +0000641 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
643
Bill Wendling44426052012-12-20 19:22:21 +0000644 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
646
Bill Wendling44426052012-12-20 19:22:21 +0000647 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000648 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
649
650 if (isReadWrite)
651 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
652
Bill Wendling44426052012-12-20 19:22:21 +0000653 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000654 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
655
Bill Wendling44426052012-12-20 19:22:21 +0000656 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000657 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
658
Bill Wendling44426052012-12-20 19:22:21 +0000659 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000660 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
661
Bill Wendling44426052012-12-20 19:22:21 +0000662 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000663 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
664
Bill Wendling44426052012-12-20 19:22:21 +0000665 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000666 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
667
Ted Kremenekac597f32010-03-12 00:46:40 +0000668 if (isAssign)
669 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
670
John McCall43192862011-09-13 18:31:23 +0000671 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000674 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000676
John McCall31168b02011-06-15 23:02:42 +0000677 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000678 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000679 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
680 if (isAssign)
681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
682
Ted Kremenekac597f32010-03-12 00:46:40 +0000683 if (MethodImplKind == tok::objc_required)
684 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
685 else if (MethodImplKind == tok::objc_optional)
686 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000687
Douglas Gregor813a0662015-06-19 18:14:38 +0000688 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
690
Douglas Gregor849ebc22015-06-19 18:14:46 +0000691 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
692 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
693
Ted Kremenek959e8302010-03-12 02:31:10 +0000694 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000695}
696
John McCall31168b02011-06-15 23:02:42 +0000697static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
698 ObjCPropertyDecl *property,
699 ObjCIvarDecl *ivar) {
700 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
701
John McCall31168b02011-06-15 23:02:42 +0000702 QualType ivarType = ivar->getType();
703 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000704
John McCall43192862011-09-13 18:31:23 +0000705 // The lifetime implied by the property's attributes.
706 Qualifiers::ObjCLifetime propertyLifetime =
707 getImpliedARCOwnership(property->getPropertyAttributes(),
708 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000709
John McCall43192862011-09-13 18:31:23 +0000710 // We're fine if they match.
711 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000712
John McCall460ce582015-10-22 18:38:17 +0000713 // None isn't a valid lifetime for an object ivar in ARC, and
714 // __autoreleasing is never valid; don't diagnose twice.
715 if ((ivarLifetime == Qualifiers::OCL_None &&
716 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000717 ivarLifetime == Qualifiers::OCL_Autoreleasing)
718 return;
John McCall31168b02011-06-15 23:02:42 +0000719
John McCalld8561f02012-08-20 23:36:59 +0000720 // If the ivar is private, and it's implicitly __unsafe_unretained
721 // becaues of its type, then pretend it was actually implicitly
722 // __strong. This is only sound because we're processing the
723 // property implementation before parsing any method bodies.
724 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
725 propertyLifetime == Qualifiers::OCL_Strong &&
726 ivar->getAccessControl() == ObjCIvarDecl::Private) {
727 SplitQualType split = ivarType.split();
728 if (split.Quals.hasObjCLifetime()) {
729 assert(ivarType->isObjCARCImplicitlyUnretainedType());
730 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
731 ivarType = S.Context.getQualifiedType(split);
732 ivar->setType(ivarType);
733 return;
734 }
735 }
736
John McCall43192862011-09-13 18:31:23 +0000737 switch (propertyLifetime) {
738 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000739 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000740 << property->getDeclName()
741 << ivar->getDeclName()
742 << ivarLifetime;
743 break;
John McCall31168b02011-06-15 23:02:42 +0000744
John McCall43192862011-09-13 18:31:23 +0000745 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000746 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000747 << property->getDeclName()
748 << ivar->getDeclName();
749 break;
John McCall31168b02011-06-15 23:02:42 +0000750
John McCall43192862011-09-13 18:31:23 +0000751 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000752 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000753 << property->getDeclName()
754 << ivar->getDeclName()
755 << ((property->getPropertyAttributesAsWritten()
756 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
757 break;
John McCall31168b02011-06-15 23:02:42 +0000758
John McCall43192862011-09-13 18:31:23 +0000759 case Qualifiers::OCL_Autoreleasing:
760 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000761
John McCall43192862011-09-13 18:31:23 +0000762 case Qualifiers::OCL_None:
763 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000764 return;
765 }
766
767 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000768 if (propertyImplLoc.isValid())
769 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000770}
771
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000772/// setImpliedPropertyAttributeForReadOnlyProperty -
773/// This routine evaludates life-time attributes for a 'readonly'
774/// property with no known lifetime of its own, using backing
775/// 'ivar's attribute, if any. If no backing 'ivar', property's
776/// life-time is assumed 'strong'.
777static void setImpliedPropertyAttributeForReadOnlyProperty(
778 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
779 Qualifiers::ObjCLifetime propertyLifetime =
780 getImpliedARCOwnership(property->getPropertyAttributes(),
781 property->getType());
782 if (propertyLifetime != Qualifiers::OCL_None)
783 return;
784
785 if (!ivar) {
786 // if no backing ivar, make property 'strong'.
787 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
788 return;
789 }
790 // property assumes owenership of backing ivar.
791 QualType ivarType = ivar->getType();
792 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
793 if (ivarLifetime == Qualifiers::OCL_Strong)
794 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
795 else if (ivarLifetime == Qualifiers::OCL_Weak)
796 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
797 return;
798}
Ted Kremenekac597f32010-03-12 00:46:40 +0000799
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000800/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
801/// in inherited protocols with mismatched types. Since any of them can
802/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000803static void
804DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
805 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000806 ObjCPropertyDecl *Property) {
807 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000808 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
809 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000810 PDecl->collectInheritedProtocolProperties(Property, PropMap);
811 }
812 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
813 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000814 for (const auto *PI : SDecl->all_referenced_protocols()) {
815 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000816 PDecl->collectInheritedProtocolProperties(Property, PropMap);
817 }
818 SDecl = SDecl->getSuperClass();
819 }
820
821 if (PropMap.empty())
822 return;
823
824 QualType RHSType = S.Context.getCanonicalType(Property->getType());
825 bool FirsTime = true;
826 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
827 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
828 ObjCPropertyDecl *Prop = I->second;
829 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
830 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
831 bool IncompatibleObjC = false;
832 QualType ConvertedType;
833 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
834 || IncompatibleObjC) {
835 if (FirsTime) {
836 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
837 << Property->getType();
838 FirsTime = false;
839 }
840 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
841 << Prop->getType();
842 }
843 }
844 }
845 if (!FirsTime && AtLoc.isValid())
846 S.Diag(AtLoc, diag::note_property_synthesize);
847}
848
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000849/// Determine whether any storage attributes were written on the property.
850static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop) {
851 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
852
853 // If this is a readwrite property in a class extension that refines
854 // a readonly property in the original class definition, check it as
855 // well.
856
857 // If it's a readonly property, we're not interested.
858 if (Prop->isReadOnly()) return false;
859
860 // Is it declared in an extension?
861 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
862 if (!Category || !Category->IsClassExtension()) return false;
863
864 // Find the corresponding property in the primary class definition.
865 auto OrigClass = Category->getClassInterface();
866 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
867 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
868 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
869 }
870
871 return false;
872}
873
Ted Kremenekac597f32010-03-12 00:46:40 +0000874/// ActOnPropertyImplDecl - This routine performs semantic checks and
875/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000876/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000877///
John McCall48871652010-08-21 09:40:31 +0000878Decl *Sema::ActOnPropertyImplDecl(Scope *S,
879 SourceLocation AtLoc,
880 SourceLocation PropertyLoc,
881 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000882 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000883 IdentifierInfo *PropertyIvar,
884 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000885 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000886 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000887 // Make sure we have a context for the property implementation declaration.
888 if (!ClassImpDecl) {
889 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000890 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000891 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000892 if (PropertyIvarLoc.isInvalid())
893 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000894 SourceLocation PropertyDiagLoc = PropertyLoc;
895 if (PropertyDiagLoc.isInvalid())
896 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000897 ObjCPropertyDecl *property = nullptr;
898 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000899 // Find the class or category class where this property must have
900 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000901 ObjCImplementationDecl *IC = nullptr;
902 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000903 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
904 IDecl = IC->getClassInterface();
905 // We always synthesize an interface for an implementation
906 // without an interface decl. So, IDecl is always non-zero.
907 assert(IDecl &&
908 "ActOnPropertyImplDecl - @implementation without @interface");
909
910 // Look for this property declaration in the @implementation's @interface
911 property = IDecl->FindPropertyDeclaration(PropertyId);
912 if (!property) {
913 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000914 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000915 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000916 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000917 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
918 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000919 if (AtLoc.isValid())
920 Diag(AtLoc, diag::warn_implicit_atomic_property);
921 else
922 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
923 Diag(property->getLocation(), diag::note_property_declare);
924 }
925
Ted Kremenekac597f32010-03-12 00:46:40 +0000926 if (const ObjCCategoryDecl *CD =
927 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
928 if (!CD->IsClassExtension()) {
929 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
930 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000931 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000932 }
933 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000934 if (Synthesize&&
935 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
936 property->hasAttr<IBOutletAttr>() &&
937 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000938 bool ReadWriteProperty = false;
939 // Search into the class extensions and see if 'readonly property is
940 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000941 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000942 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
943 if (!R.empty())
944 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
945 PIkind = ExtProp->getPropertyAttributesAsWritten();
946 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
947 ReadWriteProperty = true;
948 break;
949 }
950 }
951 }
952
953 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000954 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000955 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000956 SourceLocation readonlyLoc;
957 if (LocPropertyAttribute(Context, "readonly",
958 property->getLParenLoc(), readonlyLoc)) {
959 SourceLocation endLoc =
960 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
961 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
962 Diag(property->getLocation(),
963 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
964 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
965 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000966 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000967 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000968 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
969 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000970
Ted Kremenekac597f32010-03-12 00:46:40 +0000971 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
972 if (Synthesize) {
973 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000974 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000975 }
976 IDecl = CatImplClass->getClassInterface();
977 if (!IDecl) {
978 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000979 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000980 }
981 ObjCCategoryDecl *Category =
982 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
983
984 // If category for this implementation not found, it is an error which
985 // has already been reported eralier.
986 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000987 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000988 // Look for this property declaration in @implementation's category
989 property = Category->FindPropertyDeclaration(PropertyId);
990 if (!property) {
991 Diag(PropertyLoc, diag::error_bad_category_property_decl)
992 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000993 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000994 }
995 } else {
996 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000997 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000998 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000999 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001000 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001001 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001002 // Check that we have a valid, previously declared ivar for @synthesize
1003 if (Synthesize) {
1004 // @synthesize
1005 if (!PropertyIvar)
1006 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001007 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1008 ObjCInterfaceDecl *ClassDeclared;
1009 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1010 QualType PropType = property->getType();
1011 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001012
1013 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001014 diag::err_incomplete_synthesized_property,
1015 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001016 Diag(property->getLocation(), diag::note_property_declare);
1017 CompleteTypeErr = true;
1018 }
1019
David Blaikiebbafb8a2012-03-11 07:00:24 +00001020 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001021 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001022 ObjCPropertyDecl::OBJC_PR_readonly) &&
1023 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001024 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1025 }
1026
John McCall31168b02011-06-15 23:02:42 +00001027 ObjCPropertyDecl::PropertyAttributeKind kind
1028 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001029
John McCall460ce582015-10-22 18:38:17 +00001030 bool isARCWeak = false;
1031 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1032 // Add GC __weak to the ivar type if the property is weak.
1033 if (getLangOpts().getGC() != LangOptions::NonGC) {
1034 assert(!getLangOpts().ObjCAutoRefCount);
1035 if (PropertyIvarType.isObjCGCStrong()) {
1036 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1037 Diag(property->getLocation(), diag::note_property_declare);
1038 } else {
1039 PropertyIvarType =
1040 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1041 }
1042
1043 // Otherwise, check whether ARC __weak is enabled and works with
1044 // the property type.
John McCall43192862011-09-13 18:31:23 +00001045 } else {
John McCall460ce582015-10-22 18:38:17 +00001046 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001047 // Only complain here when synthesizing an ivar.
1048 if (!Ivar) {
1049 Diag(PropertyDiagLoc,
1050 getLangOpts().ObjCWeakRuntime
1051 ? diag::err_synthesizing_arc_weak_property_disabled
1052 : diag::err_synthesizing_arc_weak_property_no_runtime);
1053 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001054 }
John McCallb61e14e2015-10-27 04:54:50 +00001055 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001056 } else {
1057 isARCWeak = true;
1058 if (const ObjCObjectPointerType *ObjT =
1059 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1060 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1061 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1062 Diag(property->getLocation(),
1063 diag::err_arc_weak_unavailable_property)
1064 << PropertyIvarType;
1065 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1066 << ClassImpDecl->getName();
1067 }
1068 }
1069 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001070 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001071 }
John McCall460ce582015-10-22 18:38:17 +00001072
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001073 if (AtLoc.isInvalid()) {
1074 // Check when default synthesizing a property that there is
1075 // an ivar matching property name and issue warning; since this
1076 // is the most common case of not using an ivar used for backing
1077 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001078 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001079 ObjCIvarDecl *originalIvar =
1080 IDecl->lookupInstanceVariable(property->getIdentifier(),
1081 ClassDeclared);
1082 if (originalIvar) {
1083 Diag(PropertyDiagLoc,
1084 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001085 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001086 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001087 Diag(property->getLocation(), diag::note_property_declare);
1088 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001089 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001090 }
1091
1092 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001093 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001094 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001095 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001096 !PropertyIvarType.getObjCLifetime() &&
1097 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001098
John McCall43192862011-09-13 18:31:23 +00001099 // It's an error if we have to do this and the user didn't
1100 // explicitly write an ownership attribute on the property.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001101 if (!hasWrittenStorageAttribute(property) &&
John McCall43192862011-09-13 18:31:23 +00001102 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001103 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001104 diag::err_arc_objc_property_default_assign_on_object);
1105 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001106 } else {
1107 Qualifiers::ObjCLifetime lifetime =
1108 getImpliedARCOwnership(kind, PropertyIvarType);
1109 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001110
John McCall31168b02011-06-15 23:02:42 +00001111 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001112 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001113 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1114 }
John McCall31168b02011-06-15 23:02:42 +00001115 }
1116
Abramo Bagnaradff19302011-03-08 08:55:46 +00001117 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001118 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001119 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001120 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001121 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001122 if (RequireNonAbstractType(PropertyIvarLoc,
1123 PropertyIvarType,
1124 diag::err_abstract_type_in_decl,
1125 AbstractSynthesizedIvarType)) {
1126 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001127 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001128 } else if (CompleteTypeErr)
1129 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001130 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001131 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001132
John McCall5fb5df92012-06-20 06:18:46 +00001133 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001134 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1135 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001136 // Note! I deliberately want it to fall thru so, we have a
1137 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001138 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001139 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001140 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001141 << property->getDeclName() << Ivar->getDeclName()
1142 << ClassDeclared->getDeclName();
1143 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001144 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001145 // Note! I deliberately want it to fall thru so more errors are caught.
1146 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001147 property->setPropertyIvarDecl(Ivar);
1148
Ted Kremenekac597f32010-03-12 00:46:40 +00001149 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1150
1151 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001152 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001153 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001154 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001155 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001156 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001157 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001158 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001159 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001160 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1161 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001162 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001163 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001164 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001165 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001166 << property->getDeclName() << PropType
1167 << Ivar->getDeclName() << IvarType;
1168 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001169 // Note! I deliberately want it to fall thru so, we have a
1170 // a property implementation and to avoid future warnings.
1171 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001172 else {
1173 // FIXME! Rules for properties are somewhat different that those
1174 // for assignments. Use a new routine to consolidate all cases;
1175 // specifically for property redeclarations as well as for ivars.
1176 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1177 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1178 if (lhsType != rhsType &&
1179 lhsType->isArithmeticType()) {
1180 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1181 << property->getDeclName() << PropType
1182 << Ivar->getDeclName() << IvarType;
1183 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1184 // Fall thru - see previous comment
1185 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001186 }
1187 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001188 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001189 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001190 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001191 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001192 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001193 // Fall thru - see previous comment
1194 }
John McCall31168b02011-06-15 23:02:42 +00001195 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001196 if ((property->getType()->isObjCObjectPointerType() ||
1197 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001198 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001199 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001200 << property->getDeclName() << Ivar->getDeclName();
1201 // Fall thru - see previous comment
1202 }
1203 }
John McCall460ce582015-10-22 18:38:17 +00001204 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1205 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001206 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001207 } else if (PropertyIvar)
1208 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001209 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001210
Ted Kremenekac597f32010-03-12 00:46:40 +00001211 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1212 ObjCPropertyImplDecl *PIDecl =
1213 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1214 property,
1215 (Synthesize ?
1216 ObjCPropertyImplDecl::Synthesize
1217 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001218 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001219
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001220 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001221 PIDecl->setInvalidDecl();
1222
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001223 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1224 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001225 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001226 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001227 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1228 // returned by the getter as it must conform to C++'s copy-return rules.
1229 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001230 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001231 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1232 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001233 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001234 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001235 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001236 Expr *LoadSelfExpr =
1237 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001238 CK_LValueToRValue, SelfExpr, nullptr,
1239 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001240 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001241 new (Context) ObjCIvarRefExpr(Ivar,
1242 Ivar->getUsageType(SelfDecl->getType()),
1243 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001244 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001245 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001246 ExprResult Res = PerformCopyInitialization(
1247 InitializedEntity::InitializeResult(PropertyDiagLoc,
1248 getterMethod->getReturnType(),
1249 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001250 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001251 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001252 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001253 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001254 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001255 PIDecl->setGetterCXXConstructor(ResExpr);
1256 }
1257 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001258 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1259 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1260 Diag(getterMethod->getLocation(),
1261 diag::warn_property_getter_owning_mismatch);
1262 Diag(property->getLocation(), diag::note_property_declare);
1263 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001264 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1265 switch (getterMethod->getMethodFamily()) {
1266 case OMF_retain:
1267 case OMF_retainCount:
1268 case OMF_release:
1269 case OMF_autorelease:
1270 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1271 << 1 << getterMethod->getSelector();
1272 break;
1273 default:
1274 break;
1275 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001276 }
1277 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1278 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001279 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1280 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001281 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001282 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001283 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1284 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001285 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001286 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001287 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001288 Expr *LoadSelfExpr =
1289 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001290 CK_LValueToRValue, SelfExpr, nullptr,
1291 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001292 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001293 new (Context) ObjCIvarRefExpr(Ivar,
1294 Ivar->getUsageType(SelfDecl->getType()),
1295 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001296 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001297 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001298 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1299 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001300 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001301 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1302 VK_LValue, PropertyDiagLoc);
1303 MarkDeclRefReferenced(rhs);
1304 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001305 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001306 if (property->getPropertyAttributes() &
1307 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001308 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001309 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001310 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1311 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001312 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001313 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001314 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001315 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001316 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001317 Diag(FuncDecl->getLocStart(),
1318 diag::note_callee_decl) << FuncDecl;
1319 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001320 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001321 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001322 }
1323 }
1324
Ted Kremenekac597f32010-03-12 00:46:40 +00001325 if (IC) {
1326 if (Synthesize)
1327 if (ObjCPropertyImplDecl *PPIDecl =
1328 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1329 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1330 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1331 << PropertyIvar;
1332 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1333 }
1334
1335 if (ObjCPropertyImplDecl *PPIDecl
1336 = IC->FindPropertyImplDecl(PropertyId)) {
1337 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1338 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001339 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001340 }
1341 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001342 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001343 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001344 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001345 // Diagnose if an ivar was lazily synthesdized due to a previous
1346 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001347 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001348 ObjCInterfaceDecl *ClassDeclared=nullptr;
1349 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001350 if (!Synthesize)
1351 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1352 else {
1353 if (PropertyIvar && PropertyIvar != PropertyId)
1354 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1355 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001356 // Issue diagnostics only if Ivar belongs to current class.
1357 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001358 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001359 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1360 << PropertyId;
1361 Ivar->setInvalidDecl();
1362 }
1363 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001364 } else {
1365 if (Synthesize)
1366 if (ObjCPropertyImplDecl *PPIDecl =
1367 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001368 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001369 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1370 << PropertyIvar;
1371 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1372 }
1373
1374 if (ObjCPropertyImplDecl *PPIDecl =
1375 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001376 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001377 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001378 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001379 }
1380 CatImplClass->addPropertyImplementation(PIDecl);
1381 }
1382
John McCall48871652010-08-21 09:40:31 +00001383 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001384}
1385
1386//===----------------------------------------------------------------------===//
1387// Helper methods.
1388//===----------------------------------------------------------------------===//
1389
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001390/// DiagnosePropertyMismatch - Compares two properties for their
1391/// attributes and types and warns on a variety of inconsistencies.
1392///
1393void
1394Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1395 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001396 const IdentifierInfo *inheritedName,
1397 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001398 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001399 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001400 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001401 SuperProperty->getPropertyAttributes();
1402
1403 // We allow readonly properties without an explicit ownership
1404 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1405 // to be overridden by a property with any explicit ownership in the subclass.
1406 if (!OverridingProtocolProperty &&
1407 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1408 ;
1409 else {
1410 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1411 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1412 Diag(Property->getLocation(), diag::warn_readonly_property)
1413 << Property->getDeclName() << inheritedName;
1414 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1415 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001416 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001417 << Property->getDeclName() << "copy" << inheritedName;
1418 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1419 unsigned CAttrRetain =
1420 (CAttr &
1421 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1422 unsigned SAttrRetain =
1423 (SAttr &
1424 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1425 bool CStrong = (CAttrRetain != 0);
1426 bool SStrong = (SAttrRetain != 0);
1427 if (CStrong != SStrong)
1428 Diag(Property->getLocation(), diag::warn_property_attribute)
1429 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1430 }
John McCall31168b02011-06-15 23:02:42 +00001431 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001432
Douglas Gregor429183e2015-12-09 22:57:32 +00001433 // Check for nonatomic; note that nonatomic is effectively
1434 // meaningless for readonly properties, so don't diagnose if the
1435 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001436 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001437 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001438 Diag(Property->getLocation(), diag::warn_property_attribute)
1439 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001440 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1441 }
1442 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001443 Diag(Property->getLocation(), diag::warn_property_attribute)
1444 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001445 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1446 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001447
1448 QualType LHSType =
1449 Context.getCanonicalType(SuperProperty->getType());
1450 QualType RHSType =
1451 Context.getCanonicalType(Property->getType());
1452
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001453 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001454 // Do cases not handled in above.
1455 // FIXME. For future support of covariant property types, revisit this.
1456 bool IncompatibleObjC = false;
1457 QualType ConvertedType;
1458 if (!isObjCPointerConversion(RHSType, LHSType,
1459 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001460 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001461 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1462 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001463 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1464 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001465 }
1466}
1467
1468bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1469 ObjCMethodDecl *GetterMethod,
1470 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001471 if (!GetterMethod)
1472 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001473 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001474 QualType PropertyIvarType = property->getType().getNonReferenceType();
1475 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1476 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001477 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1478 const ObjCObjectPointerType *getterObjCPtr = nullptr;
1479 if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) &&
1480 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1481 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001482 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001483 != Compatible) {
1484 Diag(Loc, diag::error_property_accessor_type)
1485 << property->getDeclName() << PropertyIvarType
1486 << GetterMethod->getSelector() << GetterType;
1487 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1488 return true;
1489 } else {
1490 compat = true;
1491 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1492 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1493 if (lhsType != rhsType && lhsType->isArithmeticType())
1494 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001495 }
1496 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001497
1498 if (!compat) {
1499 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1500 << property->getDeclName()
1501 << GetterMethod->getSelector();
1502 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1503 return true;
1504 }
1505
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001506 return false;
1507}
1508
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001509/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001510/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001511static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1512 ObjCContainerDecl::PropertyMap &PropMap,
1513 ObjCContainerDecl::PropertyMap &SuperPropMap,
1514 bool IncludeProtocols = true) {
1515
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001516 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001517 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001518 PropMap[Prop->getIdentifier()] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001519
1520 // Collect the properties from visible extensions.
1521 for (auto *Ext : IDecl->visible_extensions())
1522 CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
1523
Ted Kremenek204c3c52014-02-22 00:02:03 +00001524 if (IncludeProtocols) {
1525 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001526 for (auto *PI : IDecl->all_referenced_protocols())
1527 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001528 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001529 }
1530 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001531 for (auto *Prop : CATDecl->properties())
1532 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001533 if (IncludeProtocols) {
1534 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001535 for (auto *PI : CATDecl->protocols())
1536 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001537 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001538 }
1539 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001540 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001541 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1542 // Exclude property for protocols which conform to class's super-class,
1543 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001544 if (!PropertyFromSuper ||
1545 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001546 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1547 if (!PropEntry)
1548 PropEntry = Prop;
1549 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001550 }
1551 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001552 for (auto *PI : PDecl->protocols())
1553 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001554 }
1555}
1556
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001557/// CollectSuperClassPropertyImplementations - This routine collects list of
1558/// properties to be implemented in super class(s) and also coming from their
1559/// conforming protocols.
1560static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001561 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001562 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001563 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001564 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001565 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001566 SDecl = SDecl->getSuperClass();
1567 }
1568 }
1569}
1570
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001571/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1572/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1573/// declared in class 'IFace'.
1574bool
1575Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1576 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1577 if (!IV->getSynthesize())
1578 return false;
1579 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1580 Method->isInstanceMethod());
1581 if (!IMD || !IMD->isPropertyAccessor())
1582 return false;
1583
1584 // look up a property declaration whose one of its accessors is implemented
1585 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001586 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001587 if ((Property->getGetterName() == IMD->getSelector() ||
1588 Property->getSetterName() == IMD->getSelector()) &&
1589 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001590 return true;
1591 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001592 // Also look up property declaration in class extension whose one of its
1593 // accessors is implemented by this method.
1594 for (const auto *Ext : IFace->known_extensions())
1595 for (const auto *Property : Ext->properties())
1596 if ((Property->getGetterName() == IMD->getSelector() ||
1597 Property->getSetterName() == IMD->getSelector()) &&
1598 (Property->getPropertyIvarDecl() == IV))
1599 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001600 return false;
1601}
1602
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001603static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1604 ObjCPropertyDecl *Prop) {
1605 bool SuperClassImplementsGetter = false;
1606 bool SuperClassImplementsSetter = false;
1607 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1608 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001609
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001610 while (IDecl->getSuperClass()) {
1611 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1612 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1613 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001614
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001615 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1616 SuperClassImplementsSetter = true;
1617 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1618 return true;
1619 IDecl = IDecl->getSuperClass();
1620 }
1621 return false;
1622}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001623
James Dennett2a4d13c2012-06-15 07:13:21 +00001624/// \brief Default synthesizes all properties which must be synthesized
1625/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001626void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1627 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001628
Anna Zaks673d76b2012-10-18 19:17:53 +00001629 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001630 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1631 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001632 if (PropMap.empty())
1633 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001634 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001635 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1636
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001637 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1638 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001639 // Is there a matching property synthesize/dynamic?
1640 if (Prop->isInvalidDecl() ||
1641 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1642 continue;
1643 // Property may have been synthesized by user.
1644 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1645 continue;
1646 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1647 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1648 continue;
1649 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1650 continue;
1651 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001652 if (ObjCPropertyImplDecl *PID =
1653 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001654 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1655 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001656 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001657 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001658 continue;
1659 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001660 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001661 if (ObjCProtocolDecl *Proto =
1662 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001663 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001664 // Suppress the warning if class's superclass implements property's
1665 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001666 // Or, if property is going to be implemented in its super class.
1667 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001668 Diag(IMPDecl->getLocation(),
1669 diag::warn_auto_synthesizing_protocol_property)
1670 << Prop << Proto;
1671 Diag(Prop->getLocation(), diag::note_property_declare);
1672 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001673 continue;
1674 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001675 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001676 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001677 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1678 (PropInSuperClass->getPropertyAttributes() &
1679 ObjCPropertyDecl::OBJC_PR_readonly) &&
1680 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1681 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1682 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1683 << Prop->getIdentifier();
1684 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1685 }
1686 else {
1687 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1688 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001689 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001690 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1691 }
1692 continue;
1693 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001694 // We use invalid SourceLocations for the synthesized ivars since they
1695 // aren't really synthesized at a particular location; they just exist.
1696 // Saying that they are located at the @implementation isn't really going
1697 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001698 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1699 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1700 true,
1701 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001702 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001703 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001704 if (PIDecl) {
1705 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001706 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001707 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001708 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001709}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001710
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001711void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001712 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001713 return;
1714 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1715 if (!IC)
1716 return;
1717 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001718 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001719 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001720}
1721
Ted Kremenek7e812952014-02-21 19:41:30 +00001722static void DiagnoseUnimplementedAccessor(Sema &S,
1723 ObjCInterfaceDecl *PrimaryClass,
1724 Selector Method,
1725 ObjCImplDecl* IMPDecl,
1726 ObjCContainerDecl *CDecl,
1727 ObjCCategoryDecl *C,
1728 ObjCPropertyDecl *Prop,
1729 Sema::SelectorSet &SMap) {
1730 // When reporting on missing property setter/getter implementation in
1731 // categories, do not report when they are declared in primary class,
1732 // class's protocol, or one of it super classes. This is because,
1733 // the class is going to implement them.
1734 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001735 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001736 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1737 S.Diag(IMPDecl->getLocation(),
1738 isa<ObjCCategoryDecl>(CDecl) ?
1739 diag::warn_setter_getter_impl_required_in_category :
1740 diag::warn_setter_getter_impl_required)
1741 << Prop->getDeclName() << Method;
1742 S.Diag(Prop->getLocation(),
1743 diag::note_property_declare);
1744 if (S.LangOpts.ObjCDefaultSynthProperties &&
1745 S.LangOpts.ObjCRuntime.isNonFragile())
1746 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1747 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1748 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1749 }
1750}
1751
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001752void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001753 ObjCContainerDecl *CDecl,
1754 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001755 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001756 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1757
Ted Kremenek348e88c2014-02-21 19:41:34 +00001758 if (!SynthesizeProperties) {
1759 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001760 // Gather properties which need not be implemented in this class
1761 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001762 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001763 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1764 // For categories, no need to implement properties declared in
1765 // its primary class (and its super classes) if property is
1766 // declared in one of those containers.
1767 if ((IDecl = C->getClassInterface())) {
1768 ObjCInterfaceDecl::PropertyDeclOrder PO;
1769 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1770 }
1771 }
1772 if (IDecl)
1773 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1774
1775 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1776 }
1777
Ted Kremenek38882022014-02-21 19:41:39 +00001778 // Scan the @interface to see if any of the protocols it adopts
1779 // require an explicit implementation, via attribute
1780 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001781 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001782 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001783
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001784 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001785 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1786 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001787 // Lazily construct a set of all the properties in the @interface
1788 // of the class, without looking at the superclass. We cannot
1789 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001790 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001791 // as scans the adopted protocols. This work only triggers for protocols
1792 // with the attribute, which is very rare, and only occurs when
1793 // analyzing the @implementation.
1794 if (!LazyMap) {
1795 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1796 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1797 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1798 /* IncludeProtocols */ false);
1799 }
Ted Kremenek38882022014-02-21 19:41:39 +00001800 // Add the properties of 'PDecl' to the list of properties that
1801 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001802 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001803 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001804 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001805 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001806 }
1807 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001808 }
Ted Kremenek38882022014-02-21 19:41:39 +00001809
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001810 if (PropMap.empty())
1811 return;
1812
1813 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001814 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001815 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001816
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001817 SelectorSet InsMap;
1818 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001819 for (const auto *I : IMPDecl->instance_methods())
1820 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001821
1822 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001823 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001824 if (C && !C->IsClassExtension())
1825 if ((PrimaryClass = C->getClassInterface()))
1826 // Report unimplemented properties in the category as well.
1827 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1828 // When reporting on missing setter/getters, do not report when
1829 // setter/getter is implemented in category's primary class
1830 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001831 for (const auto *I : IMP->instance_methods())
1832 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001833 }
1834
Anna Zaks673d76b2012-10-18 19:17:53 +00001835 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001836 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1837 ObjCPropertyDecl *Prop = P->second;
1838 // Is there a matching propery synthesize/dynamic?
1839 if (Prop->isInvalidDecl() ||
1840 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001841 PropImplMap.count(Prop) ||
1842 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001843 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001844
1845 // Diagnose unimplemented getters and setters.
1846 DiagnoseUnimplementedAccessor(*this,
1847 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1848 if (!Prop->isReadOnly())
1849 DiagnoseUnimplementedAccessor(*this,
1850 PrimaryClass, Prop->getSetterName(),
1851 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001852 }
1853}
1854
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001855void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001856 for (const auto *propertyImpl : impDecl->property_impls()) {
1857 const auto *property = propertyImpl->getPropertyDecl();
1858
1859 // Warn about null_resettable properties with synthesized setters,
1860 // because the setter won't properly handle nil.
1861 if (propertyImpl->getPropertyImplementation()
1862 == ObjCPropertyImplDecl::Synthesize &&
1863 (property->getPropertyAttributes() &
1864 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1865 property->getGetterMethodDecl() &&
1866 property->getSetterMethodDecl()) {
1867 auto *getterMethod = property->getGetterMethodDecl();
1868 auto *setterMethod = property->getSetterMethodDecl();
1869 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1870 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1871 SourceLocation loc = propertyImpl->getLocation();
1872 if (loc.isInvalid())
1873 loc = impDecl->getLocStart();
1874
1875 Diag(loc, diag::warn_null_resettable_setter)
1876 << setterMethod->getSelector() << property->getDeclName();
1877 }
1878 }
1879 }
1880}
1881
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001882void
1883Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001884 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001885 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001886 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001887 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001888 ObjCContainerDecl::PropertyMap PM;
1889 for (auto *Prop : IDecl->properties())
1890 PM[Prop->getIdentifier()] = Prop;
1891 for (const auto *Ext : IDecl->known_extensions())
1892 for (auto *Prop : Ext->properties())
1893 PM[Prop->getIdentifier()] = Prop;
1894
1895 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1896 I != E; ++I) {
1897 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001898 ObjCMethodDecl *GetterMethod = nullptr;
1899 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001900 bool LookedUpGetterSetter = false;
1901
Bill Wendling44426052012-12-20 19:22:21 +00001902 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001903 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001904
John McCall43192862011-09-13 18:31:23 +00001905 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1906 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001907 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1908 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1909 LookedUpGetterSetter = true;
1910 if (GetterMethod) {
1911 Diag(GetterMethod->getLocation(),
1912 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001913 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001914 Diag(Property->getLocation(), diag::note_property_declare);
1915 }
1916 if (SetterMethod) {
1917 Diag(SetterMethod->getLocation(),
1918 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001919 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001920 Diag(Property->getLocation(), diag::note_property_declare);
1921 }
1922 }
1923
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001924 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001925 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1926 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001927 continue;
1928 if (const ObjCPropertyImplDecl *PIDecl
1929 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1930 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1931 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001932 if (!LookedUpGetterSetter) {
1933 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1934 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001935 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001936 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1937 SourceLocation MethodLoc =
1938 (GetterMethod ? GetterMethod->getLocation()
1939 : SetterMethod->getLocation());
1940 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001941 << Property->getIdentifier() << (GetterMethod != nullptr)
1942 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001943 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001944 if (Property->getLParenLoc().isValid() &&
1945 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001946 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001947 SourceLocation AfterLParen =
1948 getLocForEndOfToken(Property->getLParenLoc());
1949 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
1950 : "nonatomic";
1951 Diag(Property->getLocation(),
1952 diag::note_atomic_property_fixup_suggest)
1953 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
1954 } else if (Property->getLParenLoc().isInvalid()) {
1955 //@property id etc.
1956 SourceLocation startLoc =
1957 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1958 Diag(Property->getLocation(),
1959 diag::note_atomic_property_fixup_suggest)
1960 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001961 }
1962 else
1963 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001964 Diag(Property->getLocation(), diag::note_property_declare);
1965 }
1966 }
1967 }
1968}
1969
John McCall31168b02011-06-15 23:02:42 +00001970void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001971 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001972 return;
1973
Aaron Ballmand85eff42014-03-14 15:02:45 +00001974 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001975 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001976 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1977 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001978 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1979 if (!method)
1980 continue;
1981 ObjCMethodFamily family = method->getMethodFamily();
1982 if (family == OMF_alloc || family == OMF_copy ||
1983 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001984 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001985 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001986 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001987 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001988
1989 // Look for a getter explicitly declared alongside the property.
1990 // If we find one, use its location for the note.
1991 SourceLocation noteLoc = PD->getLocation();
1992 SourceLocation fixItLoc;
1993 for (auto *getterRedecl : method->redecls()) {
1994 if (getterRedecl->isImplicit())
1995 continue;
1996 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1997 continue;
1998 noteLoc = getterRedecl->getLocation();
1999 fixItLoc = getterRedecl->getLocEnd();
2000 }
2001
2002 Preprocessor &PP = getPreprocessor();
2003 TokenValue tokens[] = {
2004 tok::kw___attribute, tok::l_paren, tok::l_paren,
2005 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2006 PP.getIdentifierInfo("none"), tok::r_paren,
2007 tok::r_paren, tok::r_paren
2008 };
2009 StringRef spelling = "__attribute__((objc_method_family(none)))";
2010 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2011 if (!macroName.empty())
2012 spelling = macroName;
2013
2014 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2015 << method->getDeclName() << spelling;
2016 if (fixItLoc.isValid()) {
2017 SmallString<64> fixItText(" ");
2018 fixItText += spelling;
2019 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2020 }
John McCall31168b02011-06-15 23:02:42 +00002021 }
2022 }
2023 }
2024}
2025
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002026void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002027 const ObjCImplementationDecl *ImplD,
2028 const ObjCInterfaceDecl *IFD) {
2029 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002030 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2031 if (!SuperD)
2032 return;
2033
2034 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002035 for (const auto *I : ImplD->instance_methods())
2036 if (I->getMethodFamily() == OMF_init)
2037 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002038
2039 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2040 SuperD->getDesignatedInitializers(DesignatedInits);
2041 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2042 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2043 const ObjCMethodDecl *MD = *I;
2044 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002045 bool Ignore = false;
2046 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2047 Ignore = IMD->isUnavailable();
2048 }
2049 if (!Ignore) {
2050 Diag(ImplD->getLocation(),
2051 diag::warn_objc_implementation_missing_designated_init_override)
2052 << MD->getSelector();
2053 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2054 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002055 }
2056 }
2057}
2058
John McCallad31b5f2010-11-10 07:01:40 +00002059/// AddPropertyAttrs - Propagates attributes from a property to the
2060/// implicitly-declared getter or setter for that property.
2061static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2062 ObjCPropertyDecl *Property) {
2063 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002064 for (const auto *A : Property->attrs()) {
2065 if (isa<DeprecatedAttr>(A) ||
2066 isa<UnavailableAttr>(A) ||
2067 isa<AvailabilityAttr>(A))
2068 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002069 }
John McCallad31b5f2010-11-10 07:01:40 +00002070}
2071
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002072/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2073/// have the property type and issue diagnostics if they don't.
2074/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002075/// appropriate lookup tables.
2076void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002077 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002078 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002079 if (CD->isInvalidDecl())
2080 return;
2081
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002082 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002083 // if setter or getter is not found in class extension, it might be
2084 // in the primary class.
2085 if (!GetterMethod)
2086 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2087 if (CatDecl->IsClassExtension())
2088 GetterMethod = CatDecl->getClassInterface()->
2089 getInstanceMethod(property->getGetterName());
2090
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002091 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002092 if (!SetterMethod)
2093 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2094 if (CatDecl->IsClassExtension())
2095 SetterMethod = CatDecl->getClassInterface()->
2096 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002097 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2098 property->getLocation());
2099
2100 if (SetterMethod) {
2101 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2102 property->getPropertyAttributes();
2103 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002104 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2105 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002106 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2107 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002108 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002109 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2110 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002111 Diag(property->getLocation(),
2112 diag::warn_accessor_property_type_mismatch)
2113 << property->getDeclName()
2114 << SetterMethod->getSelector();
2115 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2116 }
2117 }
2118
2119 // Synthesize getter/setter methods if none exist.
2120 // Find the default getter and if one not found, add one.
2121 // FIXME: The synthesized property we set here is misleading. We almost always
2122 // synthesize these methods unless the user explicitly provided prototypes
2123 // (which is odd, but allowed). Sema should be typechecking that the
2124 // declarations jive in that situation (which it is not currently).
2125 if (!GetterMethod) {
2126 // No instance method of same name as property getter name was found.
2127 // Declare a getter method and add it to the list of methods
2128 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002129 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002130
Douglas Gregor849ebc22015-06-19 18:14:46 +00002131 // If the property is null_resettable, the getter returns nonnull.
2132 QualType resultTy = property->getType();
2133 if (property->getPropertyAttributes() &
2134 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2135 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002136 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002137 if (*nullability == NullabilityKind::Unspecified)
2138 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2139 modifiedTy, modifiedTy);
2140 }
2141 }
2142
Ted Kremenek2f075632010-09-21 20:52:59 +00002143 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2144 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002145 resultTy, nullptr, CD,
Craig Topperc3ec1492014-05-26 06:22:03 +00002146 /*isInstance=*/true, /*isVariadic=*/false,
2147 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002148 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002149 (property->getPropertyImplementation() ==
2150 ObjCPropertyDecl::Optional) ?
2151 ObjCMethodDecl::Optional :
2152 ObjCMethodDecl::Required);
2153 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002154
2155 AddPropertyAttrs(*this, GetterMethod, property);
2156
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002157 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002158 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2159 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002160
2161 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2162 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002163 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002164
2165 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002166 GetterMethod->addAttr(
2167 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2168 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002169
2170 if (getLangOpts().ObjCAutoRefCount)
2171 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002172 } else
2173 // A user declared getter will be synthesize when @synthesize of
2174 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002175 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002176 property->setGetterMethodDecl(GetterMethod);
2177
2178 // Skip setter if property is read-only.
2179 if (!property->isReadOnly()) {
2180 // Find the default setter and if one not found, add one.
2181 if (!SetterMethod) {
2182 // No instance method of same name as property setter name was found.
2183 // Declare a setter method and add it to the list of methods
2184 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002185 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002186
2187 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002188 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002189 property->getSetterName(), Context.VoidTy,
2190 nullptr, CD, /*isInstance=*/true,
2191 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002192 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002193 /*isImplicitlyDeclared=*/true,
2194 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002195 (property->getPropertyImplementation() ==
2196 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002197 ObjCMethodDecl::Optional :
2198 ObjCMethodDecl::Required);
2199
Douglas Gregor849ebc22015-06-19 18:14:46 +00002200 // If the property is null_resettable, the setter accepts a
2201 // nullable value.
2202 QualType paramTy = property->getType().getUnqualifiedType();
2203 if (property->getPropertyAttributes() &
2204 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2205 QualType modifiedTy = paramTy;
2206 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2207 if (*nullability == NullabilityKind::Unspecified)
2208 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2209 modifiedTy, modifiedTy);
2210 }
2211 }
2212
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002213 // Invent the arguments for the setter. We don't bother making a
2214 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002215 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2216 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002217 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002218 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002219 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002220 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002221 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002222 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002223
2224 AddPropertyAttrs(*this, SetterMethod, property);
2225
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002226 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002227 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002228 SetterMethod->addAttr(
2229 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2230 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002231 // It's possible for the user to have set a very odd custom
2232 // setter selector that causes it to have a method family.
2233 if (getLangOpts().ObjCAutoRefCount)
2234 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002235 } else
2236 // A user declared setter will be synthesize when @synthesize of
2237 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002238 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002239 property->setSetterMethodDecl(SetterMethod);
2240 }
2241 // Add any synthesized methods to the global pool. This allows us to
2242 // handle the following, which is supported by GCC (and part of the design).
2243 //
2244 // @interface Foo
2245 // @property double bar;
2246 // @end
2247 //
2248 // void thisIsUnfortunate() {
2249 // id foo;
2250 // double bar = [foo bar];
2251 // }
2252 //
2253 if (GetterMethod)
2254 AddInstanceMethodToGlobalPool(GetterMethod);
2255 if (SetterMethod)
2256 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002257
2258 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2259 if (!CurrentClass) {
2260 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2261 CurrentClass = Cat->getClassInterface();
2262 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2263 CurrentClass = Impl->getClassInterface();
2264 }
2265 if (GetterMethod)
2266 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2267 if (SetterMethod)
2268 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002269}
2270
John McCall48871652010-08-21 09:40:31 +00002271void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002272 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002273 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002274 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002275 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002276 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002277 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002278
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002279 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2280 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2281 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2282 << "readonly" << "readwrite";
2283
2284 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2285 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002286
2287 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002288 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002289 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2290 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002291 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002292 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002293 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2294 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2295 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002296 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002297 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002298 }
2299
2300 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002301 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2302 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002303 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2304 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002305 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002306 }
Bill Wendling44426052012-12-20 19:22:21 +00002307 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002308 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2309 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002310 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002311 }
Bill Wendling44426052012-12-20 19:22:21 +00002312 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002313 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2314 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002315 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002316 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002317 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002318 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002319 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2320 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002321 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002322 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002323 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002324 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002325 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2326 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002327 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2328 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002329 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002330 }
Bill Wendling44426052012-12-20 19:22:21 +00002331 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002332 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2333 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002334 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002335 }
Bill Wendling44426052012-12-20 19:22:21 +00002336 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002337 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2338 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002339 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002340 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002341 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002342 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002343 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2344 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002345 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002346 }
Bill Wendling44426052012-12-20 19:22:21 +00002347 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2348 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002349 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2350 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002351 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002352 }
Bill Wendling44426052012-12-20 19:22:21 +00002353 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002354 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2355 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002356 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002357 }
Bill Wendling44426052012-12-20 19:22:21 +00002358 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002359 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2360 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002361 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002362 }
2363 }
Bill Wendling44426052012-12-20 19:22:21 +00002364 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2365 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002366 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2367 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002368 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002369 }
Bill Wendling44426052012-12-20 19:22:21 +00002370 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2371 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002372 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2373 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002374 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002375 }
2376
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002377 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002378 // 'weak' and 'nonnull' are mutually exclusive.
2379 if (auto nullability = PropertyTy->getNullability(Context)) {
2380 if (*nullability == NullabilityKind::NonNull)
2381 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2382 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002383 }
2384 }
2385
Bill Wendling44426052012-12-20 19:22:21 +00002386 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2387 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002388 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2389 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002390 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002391 }
2392
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002393 // Warn if user supplied no assignment attribute, property is
2394 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002395 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2396 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2397 // do nothing
2398 } else if (getLangOpts().ObjCAutoRefCount) {
2399 // With arc, @property definitions should default to strong when
2400 // not specified.
2401 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2402 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002403 bool isAnyClassTy =
2404 (PropertyTy->isObjCClassType() ||
2405 PropertyTy->isObjCQualifiedClassType());
2406 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2407 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002408 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002409 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002410 else if (propertyInPrimaryClass) {
2411 // Don't issue warning on property with no life time in class
2412 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002413 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002414 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002415 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002416
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002417 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002418 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002419 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002420 }
John McCallb61e14e2015-10-27 04:54:50 +00002421 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002422
2423 // FIXME: Implement warning dependent on NSCopying being
2424 // implemented. See also:
2425 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2426 // (please trim this list while you are at it).
2427 }
2428
Bill Wendling44426052012-12-20 19:22:21 +00002429 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2430 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002431 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002432 && PropertyTy->isBlockPointerType())
2433 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002434 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2435 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2436 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002437 PropertyTy->isBlockPointerType())
2438 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002439
Bill Wendling44426052012-12-20 19:22:21 +00002440 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2441 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002442 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2443
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002444}