blob: 322469499f19206a99534a729923132f4d161eb2 [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallb61e14e2015-10-27 04:54:50 +000064/// Check the internal consistency of a property declaration with
65/// an explicit ownership qualifier.
66static void checkPropertyDeclWithOwnership(Sema &S,
67 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000068 if (property->isInvalidDecl()) return;
69
70 ObjCPropertyDecl::PropertyAttributeKind propertyKind
71 = property->getPropertyAttributes();
72 Qualifiers::ObjCLifetime propertyLifetime
73 = property->getType().getObjCLifetime();
74
John McCallb61e14e2015-10-27 04:54:50 +000075 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000076
John McCall43192862011-09-13 18:31:23 +000077 Qualifiers::ObjCLifetime expectedLifetime
78 = getImpliedARCOwnership(propertyKind, property->getType());
79 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000080 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000081 // attribute. That's okay, but restore reasonable invariants by
82 // setting the property attribute according to the lifetime
83 // qualifier.
84 ObjCPropertyDecl::PropertyAttributeKind attr;
85 if (propertyLifetime == Qualifiers::OCL_Strong) {
86 attr = ObjCPropertyDecl::OBJC_PR_strong;
87 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
88 attr = ObjCPropertyDecl::OBJC_PR_weak;
89 } else {
90 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
91 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
92 }
93 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000094 return;
95 }
96
97 if (propertyLifetime == expectedLifetime) return;
98
99 property->setInvalidDecl();
100 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000101 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000102 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000103 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000104 << propertyLifetime;
105}
106
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// \brief Check this Objective-C property against a property declared in the
108/// given protocol.
109static void
110CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
111 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000113 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000114 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000115 return;
116
117 // Look for a property with the same name.
118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
119 for (unsigned I = 0, N = R.size(); I != N; ++I) {
120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000122 return;
123 }
124 }
125
126 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000127 for (auto *P : Proto->protocols())
128 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129}
130
John McCallb61e14e2015-10-27 04:54:50 +0000131static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
132 // In GC mode, just look for the __weak qualifier.
133 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
135
136 // In ARC/MRC, look for an explicit ownership qualifier.
137 // For some reason, this only applies to __weak.
138 } else if (auto ownership = T.getObjCLifetime()) {
139 switch (ownership) {
140 case Qualifiers::OCL_Weak:
141 return ObjCDeclSpec::DQ_PR_weak;
142 case Qualifiers::OCL_Strong:
143 return ObjCDeclSpec::DQ_PR_strong;
144 case Qualifiers::OCL_ExplicitNone:
145 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
146 case Qualifiers::OCL_Autoreleasing:
147 case Qualifiers::OCL_None:
148 return 0;
149 }
150 llvm_unreachable("bad qualifier");
151 }
152
153 return 0;
154}
155
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000156static const unsigned OwnershipMask =
157 (ObjCPropertyDecl::OBJC_PR_assign |
158 ObjCPropertyDecl::OBJC_PR_retain |
159 ObjCPropertyDecl::OBJC_PR_copy |
160 ObjCPropertyDecl::OBJC_PR_weak |
161 ObjCPropertyDecl::OBJC_PR_strong |
162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
163
John McCallb61e14e2015-10-27 04:54:50 +0000164static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000165 unsigned result = attr & OwnershipMask;
166
167 // From an ownership perspective, assign and unsafe_unretained are
168 // identical; make sure one also implies the other.
169 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
171 result |= ObjCPropertyDecl::OBJC_PR_assign |
172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
173 }
174
175 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000176}
177
John McCall48871652010-08-21 09:40:31 +0000178Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000179 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000180 FieldDeclarator &FD,
181 ObjCDeclSpec &ODS,
182 Selector GetterSel,
183 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000184 tok::ObjCKeywordKind MethodImplKind,
185 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000186 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
189 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000190 if (!getOwnershipRule(Attributes)) {
191 Attributes |= deducePropertyOwnershipFromType(*this, T);
192 }
Bill Wendling44426052012-12-20 19:22:21 +0000193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000194 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000196
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000199 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000201 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000203 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000204 isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000205 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000206 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000207 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000208 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000209 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000210 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000211 }
212
213 if (!Res) {
214 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000215 GetterSel, SetterSel, isReadWrite,
Douglas Gregor90d34422013-01-21 19:05:22 +0000216 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000217 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000218 if (lexicalDC)
219 Res->setLexicalDeclContext(lexicalDC);
220 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000221
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000222 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000223 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000224 (isa<ObjCInterfaceDecl>(ClassDecl) ||
225 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000226
John McCallb61e14e2015-10-27 04:54:50 +0000227 // Check consistency if the type has explicit ownership qualification.
228 if (Res->getType().getObjCLifetime())
229 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000230
Douglas Gregorb8982092013-01-21 19:42:21 +0000231 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000232 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 // For a class, compare the property against a property in our superclass.
234 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000235 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
236 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000237 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000238 for (unsigned I = 0, N = R.size(); I != N; ++I) {
239 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000240 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000241 FoundInSuper = true;
242 break;
243 }
244 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000245 if (FoundInSuper)
246 break;
247 else
248 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000249 }
250
251 if (FoundInSuper) {
252 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000253 for (auto *P : CurrentInterfaceDecl->protocols()) {
254 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000255 }
256 } else {
257 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000258 for (auto *P : IFace->all_referenced_protocols()) {
259 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000260 }
261 }
262 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000263 // We don't check if class extension. Because properties in class extension
264 // are meant to override some of the attributes and checking has already done
265 // when property in class extension is constructed.
266 if (!Cat->IsClassExtension())
267 for (auto *P : Cat->protocols())
268 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000269 } else {
270 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000271 for (auto *P : Proto->protocols())
272 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000273 }
274
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000275 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000276 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000277}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000278
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000280makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_class)
307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000308
309 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
310}
311
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000312static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000313 SourceLocation LParenLoc, SourceLocation &Loc) {
314 if (LParenLoc.isMacroID())
315 return false;
316
317 SourceManager &SM = Context.getSourceManager();
318 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
319 // Try to load the file buffer.
320 bool invalidTemp = false;
321 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
322 if (invalidTemp)
323 return false;
324 const char *tokenBegin = file.data() + locInfo.second;
325
326 // Lex from the start of the given location.
327 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
328 Context.getLangOpts(),
329 file.begin(), tokenBegin, file.end());
330 Token Tok;
331 do {
332 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000333 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000334 Loc = Tok.getLocation();
335 return true;
336 }
337 } while (Tok.isNot(tok::r_paren));
338 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000339}
340
Douglas Gregor429183e2015-12-09 22:57:32 +0000341/// Check for a mismatch in the atomicity of the given properties.
342static void checkAtomicPropertyMismatch(Sema &S,
343 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000344 ObjCPropertyDecl *NewProperty,
345 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000346 // If the atomicity of both matches, we're done.
347 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000348 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
349 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000350 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000351 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000353 if (OldIsAtomic == NewIsAtomic) return;
354
355 // Determine whether the given property is readonly and implicitly
356 // atomic.
357 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
358 // Is it readonly?
359 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000360 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000361
362 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000363 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000364
365 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000366 if (Property->getPropertyAttributesAsWritten() &
367 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000368 return false;
369
370 return true;
371 };
372
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000373 // If we're allowed to propagate atomicity, and the new property did
374 // not specify atomicity at all, propagate.
375 const unsigned AtomicityMask =
376 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
377 if (PropagateAtomicity &&
378 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
379 unsigned Attrs = NewProperty->getPropertyAttributes();
380 Attrs = Attrs & ~AtomicityMask;
381 if (OldIsAtomic)
382 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
383 else
384 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
385
386 NewProperty->overwritePropertyAttributes(Attrs);
387 return;
388 }
389
Douglas Gregor429183e2015-12-09 22:57:32 +0000390 // One of the properties is atomic; if it's a readonly property, and
391 // 'atomic' wasn't explicitly specified, we're okay.
392 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
393 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
394 return;
395
396 // Diagnose the conflict.
397 const IdentifierInfo *OldContextName;
398 auto *OldDC = OldProperty->getDeclContext();
399 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
400 OldContextName = Category->getClassInterface()->getIdentifier();
401 else
402 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
403
404 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
405 << NewProperty->getDeclName() << "atomic"
406 << OldContextName;
407 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
408}
409
Douglas Gregor90d34422013-01-21 19:05:22 +0000410ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000411Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000412 SourceLocation AtLoc,
413 SourceLocation LParenLoc,
414 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000415 Selector GetterSel, Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000416 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000417 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000418 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000419 QualType T,
420 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000421 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000422 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000423 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000424 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000425 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000426 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
427
Ted Kremenek959e8302010-03-12 02:31:10 +0000428 // We need to look in the @interface to see if the @property was
429 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 if (!CCPrimary) {
431 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000432 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 }
434
Manman Ren5b786402016-01-28 18:49:28 +0000435 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
436 (Attributes & ObjCDeclSpec::DQ_PR_class);
437
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000438 // Find the property in the extended class's primary class or
439 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000440 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
441 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000442
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000443 // If we found a property in an extension, complain.
444 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
445 Diag(AtLoc, diag::err_duplicate_property);
446 Diag(PIDecl->getLocation(), diag::note_property_declare);
447 return nullptr;
448 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000449
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000450 // Check for consistency with the previous declaration, if there is one.
451 if (PIDecl) {
452 // A readonly property declared in the primary class can be refined
453 // by adding a readwrite property within an extension.
454 // Anything else is an error.
455 if (!(PIDecl->isReadOnly() && isReadWrite)) {
456 // Tailor the diagnostics for the common case where a readwrite
457 // property is declared both in the @interface and the continuation.
458 // This is a common error where the user often intended the original
459 // declaration to be readonly.
460 unsigned diag =
461 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
462 (PIDecl->getPropertyAttributesAsWritten() &
463 ObjCPropertyDecl::OBJC_PR_readwrite)
464 ? diag::err_use_continuation_class_redeclaration_readwrite
465 : diag::err_use_continuation_class;
466 Diag(AtLoc, diag)
467 << CCPrimary->getDeclName();
468 Diag(PIDecl->getLocation(), diag::note_property_declare);
469 return nullptr;
470 }
471
472 // Check for consistency of getters.
473 if (PIDecl->getGetterName() != GetterSel) {
474 // If the getter was written explicitly, complain.
475 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
476 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
477 << PIDecl->getGetterName() << GetterSel;
478 Diag(PIDecl->getLocation(), diag::note_property_declare);
479 }
480
481 // Always adopt the getter from the original declaration.
482 GetterSel = PIDecl->getGetterName();
483 Attributes |= ObjCDeclSpec::DQ_PR_getter;
484 }
485
486 // Check consistency of ownership.
487 unsigned ExistingOwnership
488 = getOwnershipRule(PIDecl->getPropertyAttributes());
489 unsigned NewOwnership = getOwnershipRule(Attributes);
490 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
491 // If the ownership was written explicitly, complain.
492 if (getOwnershipRule(AttributesAsWritten)) {
493 Diag(AtLoc, diag::warn_property_attr_mismatch);
494 Diag(PIDecl->getLocation(), diag::note_property_declare);
495 }
496
497 // Take the ownership from the original property.
498 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
499 }
500
501 // If the redeclaration is 'weak' but the original property is not,
502 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
503 !(PIDecl->getPropertyAttributesAsWritten()
504 & ObjCPropertyDecl::OBJC_PR_weak) &&
505 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
506 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
507 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
509 }
510 }
511
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000512 // Create a new ObjCPropertyDecl with the DeclContext being
513 // the class extension.
514 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
515 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000516 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000517 Attributes, AttributesAsWritten,
518 T, TSI, MethodImplKind, DC);
519
520 // If there was no declaration of a property with the same name in
521 // the primary class, we're done.
522 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000523 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000524 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000525 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000526
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000527 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
528 bool IncompatibleObjC = false;
529 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000530 // Relax the strict type matching for property type in continuation class.
531 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000532 // as it narrows the object type in its primary class property. Note that
533 // this conversion is safe only because the wider type is for a 'readonly'
534 // property in primary class and 'narrowed' type for a 'readwrite' property
535 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000536 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
537 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
538 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
539 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
540 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000541 ConvertedType, IncompatibleObjC))
542 || IncompatibleObjC) {
543 Diag(AtLoc,
544 diag::err_type_mismatch_continuation_class) << PDecl->getType();
545 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000546 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000547 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000548 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000549
550 // Check that atomicity of property in class extension matches the previous
551 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000552 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000553
Douglas Gregore17765e2015-11-03 17:02:34 +0000554 // Make sure getter/setter are appropriately synthesized.
555 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000556 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000557}
558
559ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
560 ObjCContainerDecl *CDecl,
561 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000562 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000563 FieldDeclarator &FD,
564 Selector GetterSel,
565 Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000566 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000567 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000568 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000569 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000570 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000571 tok::ObjCKeywordKind MethodImplKind,
572 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000574
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000575 // Property defaults to 'assign' if it is readwrite, unless this is ARC
576 // and the type is retainable.
577 bool isAssign;
578 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
579 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
580 isAssign = true;
581 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
582 isAssign = false;
583 } else {
584 isAssign = (!getLangOpts().ObjCAutoRefCount ||
585 !T->isObjCRetainableType());
586 }
587
588 // Issue a warning if property is 'assign' as default and its
589 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000590 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000591 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000592 if (const ObjCObjectPointerType *ObjPtrTy =
593 T->getAs<ObjCObjectPointerType>()) {
594 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
595 if (IDecl)
596 if (ObjCProtocolDecl* PNSCopying =
597 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
598 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
599 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000600 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000601 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000602
603 if (T->isObjCObjectType()) {
604 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000605 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000606 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
607 << FixItHint::CreateInsertion(StarLoc, "*");
608 T = Context.getObjCObjectPointerType(T);
609 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
610 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
611 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000612
Ted Kremenek959e8302010-03-12 02:31:10 +0000613 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000614 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
615 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000616 PropertyId, AtLoc,
617 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000618
Manman Ren5b786402016-01-28 18:49:28 +0000619 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
620 (Attributes & ObjCDeclSpec::DQ_PR_class);
621 // Class property and instance property can have the same name.
622 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
623 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000624 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000625 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000626 PDecl->setInvalidDecl();
627 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000628 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000629 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000630 if (lexicalDC)
631 PDecl->setLexicalDeclContext(lexicalDC);
632 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000633
634 if (T->isArrayType() || T->isFunctionType()) {
635 Diag(AtLoc, diag::err_property_type) << T;
636 PDecl->setInvalidDecl();
637 }
638
639 ProcessDeclAttributes(S, PDecl, FD.D);
640
641 // Regardless of setter/getter attribute, we save the default getter/setter
642 // selector names in anticipation of declaration of setter/getter methods.
643 PDecl->setGetterName(GetterSel);
644 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000645 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000646 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000647
Bill Wendling44426052012-12-20 19:22:21 +0000648 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000649 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
650
Bill Wendling44426052012-12-20 19:22:21 +0000651 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000652 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
653
Bill Wendling44426052012-12-20 19:22:21 +0000654 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000655 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
656
657 if (isReadWrite)
658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
659
Bill Wendling44426052012-12-20 19:22:21 +0000660 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
662
Bill Wendling44426052012-12-20 19:22:21 +0000663 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
665
Bill Wendling44426052012-12-20 19:22:21 +0000666 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
668
Bill Wendling44426052012-12-20 19:22:21 +0000669 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
671
Bill Wendling44426052012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
674
Ted Kremenekac597f32010-03-12 00:46:40 +0000675 if (isAssign)
676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
677
John McCall43192862011-09-13 18:31:23 +0000678 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000679 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000680 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000681 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000683
John McCall31168b02011-06-15 23:02:42 +0000684 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000685 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000686 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
687 if (isAssign)
688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
689
Ted Kremenekac597f32010-03-12 00:46:40 +0000690 if (MethodImplKind == tok::objc_required)
691 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
692 else if (MethodImplKind == tok::objc_optional)
693 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000694
Douglas Gregor813a0662015-06-19 18:14:38 +0000695 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
697
Douglas Gregor849ebc22015-06-19 18:14:46 +0000698 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
699 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
700
Manman Ren387ff7f2016-01-26 18:52:43 +0000701 if (Attributes & ObjCDeclSpec::DQ_PR_class)
702 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
703
Ted Kremenek959e8302010-03-12 02:31:10 +0000704 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000705}
706
John McCall31168b02011-06-15 23:02:42 +0000707static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
708 ObjCPropertyDecl *property,
709 ObjCIvarDecl *ivar) {
710 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
711
John McCall31168b02011-06-15 23:02:42 +0000712 QualType ivarType = ivar->getType();
713 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000714
John McCall43192862011-09-13 18:31:23 +0000715 // The lifetime implied by the property's attributes.
716 Qualifiers::ObjCLifetime propertyLifetime =
717 getImpliedARCOwnership(property->getPropertyAttributes(),
718 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000719
John McCall43192862011-09-13 18:31:23 +0000720 // We're fine if they match.
721 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall460ce582015-10-22 18:38:17 +0000723 // None isn't a valid lifetime for an object ivar in ARC, and
724 // __autoreleasing is never valid; don't diagnose twice.
725 if ((ivarLifetime == Qualifiers::OCL_None &&
726 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000727 ivarLifetime == Qualifiers::OCL_Autoreleasing)
728 return;
John McCall31168b02011-06-15 23:02:42 +0000729
John McCalld8561f02012-08-20 23:36:59 +0000730 // If the ivar is private, and it's implicitly __unsafe_unretained
731 // becaues of its type, then pretend it was actually implicitly
732 // __strong. This is only sound because we're processing the
733 // property implementation before parsing any method bodies.
734 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
735 propertyLifetime == Qualifiers::OCL_Strong &&
736 ivar->getAccessControl() == ObjCIvarDecl::Private) {
737 SplitQualType split = ivarType.split();
738 if (split.Quals.hasObjCLifetime()) {
739 assert(ivarType->isObjCARCImplicitlyUnretainedType());
740 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
741 ivarType = S.Context.getQualifiedType(split);
742 ivar->setType(ivarType);
743 return;
744 }
745 }
746
John McCall43192862011-09-13 18:31:23 +0000747 switch (propertyLifetime) {
748 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000749 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000750 << property->getDeclName()
751 << ivar->getDeclName()
752 << ivarLifetime;
753 break;
John McCall31168b02011-06-15 23:02:42 +0000754
John McCall43192862011-09-13 18:31:23 +0000755 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000756 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000757 << property->getDeclName()
758 << ivar->getDeclName();
759 break;
John McCall31168b02011-06-15 23:02:42 +0000760
John McCall43192862011-09-13 18:31:23 +0000761 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000762 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000763 << property->getDeclName()
764 << ivar->getDeclName()
765 << ((property->getPropertyAttributesAsWritten()
766 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
767 break;
John McCall31168b02011-06-15 23:02:42 +0000768
John McCall43192862011-09-13 18:31:23 +0000769 case Qualifiers::OCL_Autoreleasing:
770 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000771
John McCall43192862011-09-13 18:31:23 +0000772 case Qualifiers::OCL_None:
773 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000774 return;
775 }
776
777 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000778 if (propertyImplLoc.isValid())
779 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000780}
781
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000782/// setImpliedPropertyAttributeForReadOnlyProperty -
783/// This routine evaludates life-time attributes for a 'readonly'
784/// property with no known lifetime of its own, using backing
785/// 'ivar's attribute, if any. If no backing 'ivar', property's
786/// life-time is assumed 'strong'.
787static void setImpliedPropertyAttributeForReadOnlyProperty(
788 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
789 Qualifiers::ObjCLifetime propertyLifetime =
790 getImpliedARCOwnership(property->getPropertyAttributes(),
791 property->getType());
792 if (propertyLifetime != Qualifiers::OCL_None)
793 return;
794
795 if (!ivar) {
796 // if no backing ivar, make property 'strong'.
797 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
798 return;
799 }
800 // property assumes owenership of backing ivar.
801 QualType ivarType = ivar->getType();
802 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
803 if (ivarLifetime == Qualifiers::OCL_Strong)
804 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
805 else if (ivarLifetime == Qualifiers::OCL_Weak)
806 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000807}
Ted Kremenekac597f32010-03-12 00:46:40 +0000808
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000809/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
810/// in inherited protocols with mismatched types. Since any of them can
811/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000812static void
813DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
814 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000815 ObjCPropertyDecl *Property) {
816 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000817 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
818 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000819 PDecl->collectInheritedProtocolProperties(Property, PropMap);
820 }
821 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
822 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000823 for (const auto *PI : SDecl->all_referenced_protocols()) {
824 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000825 PDecl->collectInheritedProtocolProperties(Property, PropMap);
826 }
827 SDecl = SDecl->getSuperClass();
828 }
829
830 if (PropMap.empty())
831 return;
832
833 QualType RHSType = S.Context.getCanonicalType(Property->getType());
834 bool FirsTime = true;
835 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
836 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
837 ObjCPropertyDecl *Prop = I->second;
838 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
839 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
840 bool IncompatibleObjC = false;
841 QualType ConvertedType;
842 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
843 || IncompatibleObjC) {
844 if (FirsTime) {
845 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
846 << Property->getType();
847 FirsTime = false;
848 }
849 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
850 << Prop->getType();
851 }
852 }
853 }
854 if (!FirsTime && AtLoc.isValid())
855 S.Diag(AtLoc, diag::note_property_synthesize);
856}
857
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000858/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +0000859static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
860 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000861 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
862
863 // If this is a readwrite property in a class extension that refines
864 // a readonly property in the original class definition, check it as
865 // well.
866
867 // If it's a readonly property, we're not interested.
868 if (Prop->isReadOnly()) return false;
869
870 // Is it declared in an extension?
871 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
872 if (!Category || !Category->IsClassExtension()) return false;
873
874 // Find the corresponding property in the primary class definition.
875 auto OrigClass = Category->getClassInterface();
876 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
877 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
878 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
879 }
880
Douglas Gregor02535432015-12-18 00:52:31 +0000881 // Look through all of the protocols.
882 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +0000883 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
884 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +0000885 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
886 }
887
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000888 return false;
889}
890
Ted Kremenekac597f32010-03-12 00:46:40 +0000891/// ActOnPropertyImplDecl - This routine performs semantic checks and
892/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000893/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000894///
John McCall48871652010-08-21 09:40:31 +0000895Decl *Sema::ActOnPropertyImplDecl(Scope *S,
896 SourceLocation AtLoc,
897 SourceLocation PropertyLoc,
898 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000899 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000900 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +0000901 SourceLocation PropertyIvarLoc,
902 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000903 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000904 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000905 // Make sure we have a context for the property implementation declaration.
906 if (!ClassImpDecl) {
907 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000908 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000909 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000910 if (PropertyIvarLoc.isInvalid())
911 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000912 SourceLocation PropertyDiagLoc = PropertyLoc;
913 if (PropertyDiagLoc.isInvalid())
914 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000915 ObjCPropertyDecl *property = nullptr;
916 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000917 // Find the class or category class where this property must have
918 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000919 ObjCImplementationDecl *IC = nullptr;
920 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000921 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
922 IDecl = IC->getClassInterface();
923 // We always synthesize an interface for an implementation
924 // without an interface decl. So, IDecl is always non-zero.
925 assert(IDecl &&
926 "ActOnPropertyImplDecl - @implementation without @interface");
927
928 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +0000929 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +0000930 if (!property) {
931 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000932 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000933 }
Manman Rendfef4062016-01-29 19:16:39 +0000934 if (property->isClassProperty() && Synthesize) {
935 Diag(PropertyLoc, diag::error_synthesize_on_class_property) << PropertyId;
936 return nullptr;
937 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000938 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000939 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
940 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000941 if (AtLoc.isValid())
942 Diag(AtLoc, diag::warn_implicit_atomic_property);
943 else
944 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
945 Diag(property->getLocation(), diag::note_property_declare);
946 }
947
Ted Kremenekac597f32010-03-12 00:46:40 +0000948 if (const ObjCCategoryDecl *CD =
949 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
950 if (!CD->IsClassExtension()) {
951 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
952 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000953 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000954 }
955 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000956 if (Synthesize&&
957 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
958 property->hasAttr<IBOutletAttr>() &&
959 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000960 bool ReadWriteProperty = false;
961 // Search into the class extensions and see if 'readonly property is
962 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000963 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000964 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
965 if (!R.empty())
966 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
967 PIkind = ExtProp->getPropertyAttributesAsWritten();
968 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
969 ReadWriteProperty = true;
970 break;
971 }
972 }
973 }
974
975 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000976 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000977 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000978 SourceLocation readonlyLoc;
979 if (LocPropertyAttribute(Context, "readonly",
980 property->getLParenLoc(), readonlyLoc)) {
981 SourceLocation endLoc =
982 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
983 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
984 Diag(property->getLocation(),
985 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
986 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
987 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000988 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000989 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000990 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
991 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000992
Ted Kremenekac597f32010-03-12 00:46:40 +0000993 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
994 if (Synthesize) {
995 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000996 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000997 }
998 IDecl = CatImplClass->getClassInterface();
999 if (!IDecl) {
1000 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001001 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001002 }
1003 ObjCCategoryDecl *Category =
1004 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1005
1006 // If category for this implementation not found, it is an error which
1007 // has already been reported eralier.
1008 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001009 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001010 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001011 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001012 if (!property) {
1013 Diag(PropertyLoc, diag::error_bad_category_property_decl)
1014 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001015 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001016 }
1017 } else {
1018 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001019 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001020 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001021 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001022 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001023 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001024 // Check that we have a valid, previously declared ivar for @synthesize
1025 if (Synthesize) {
1026 // @synthesize
1027 if (!PropertyIvar)
1028 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001029 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1030 ObjCInterfaceDecl *ClassDeclared;
1031 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1032 QualType PropType = property->getType();
1033 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001034
1035 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001036 diag::err_incomplete_synthesized_property,
1037 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001038 Diag(property->getLocation(), diag::note_property_declare);
1039 CompleteTypeErr = true;
1040 }
1041
David Blaikiebbafb8a2012-03-11 07:00:24 +00001042 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001043 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001044 ObjCPropertyDecl::OBJC_PR_readonly) &&
1045 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001046 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1047 }
1048
John McCall31168b02011-06-15 23:02:42 +00001049 ObjCPropertyDecl::PropertyAttributeKind kind
1050 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001051
John McCall460ce582015-10-22 18:38:17 +00001052 bool isARCWeak = false;
1053 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1054 // Add GC __weak to the ivar type if the property is weak.
1055 if (getLangOpts().getGC() != LangOptions::NonGC) {
1056 assert(!getLangOpts().ObjCAutoRefCount);
1057 if (PropertyIvarType.isObjCGCStrong()) {
1058 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1059 Diag(property->getLocation(), diag::note_property_declare);
1060 } else {
1061 PropertyIvarType =
1062 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1063 }
1064
1065 // Otherwise, check whether ARC __weak is enabled and works with
1066 // the property type.
John McCall43192862011-09-13 18:31:23 +00001067 } else {
John McCall460ce582015-10-22 18:38:17 +00001068 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001069 // Only complain here when synthesizing an ivar.
1070 if (!Ivar) {
1071 Diag(PropertyDiagLoc,
1072 getLangOpts().ObjCWeakRuntime
1073 ? diag::err_synthesizing_arc_weak_property_disabled
1074 : diag::err_synthesizing_arc_weak_property_no_runtime);
1075 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001076 }
John McCallb61e14e2015-10-27 04:54:50 +00001077 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001078 } else {
1079 isARCWeak = true;
1080 if (const ObjCObjectPointerType *ObjT =
1081 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1082 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1083 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1084 Diag(property->getLocation(),
1085 diag::err_arc_weak_unavailable_property)
1086 << PropertyIvarType;
1087 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1088 << ClassImpDecl->getName();
1089 }
1090 }
1091 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001092 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001093 }
John McCall460ce582015-10-22 18:38:17 +00001094
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001095 if (AtLoc.isInvalid()) {
1096 // Check when default synthesizing a property that there is
1097 // an ivar matching property name and issue warning; since this
1098 // is the most common case of not using an ivar used for backing
1099 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001100 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001101 ObjCIvarDecl *originalIvar =
1102 IDecl->lookupInstanceVariable(property->getIdentifier(),
1103 ClassDeclared);
1104 if (originalIvar) {
1105 Diag(PropertyDiagLoc,
1106 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001107 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001108 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001109 Diag(property->getLocation(), diag::note_property_declare);
1110 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001111 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001112 }
1113
1114 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001115 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001116 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001117 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001118 !PropertyIvarType.getObjCLifetime() &&
1119 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001120
John McCall43192862011-09-13 18:31:23 +00001121 // It's an error if we have to do this and the user didn't
1122 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001123 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001124 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001125 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001126 diag::err_arc_objc_property_default_assign_on_object);
1127 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001128 } else {
1129 Qualifiers::ObjCLifetime lifetime =
1130 getImpliedARCOwnership(kind, PropertyIvarType);
1131 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001132
John McCall31168b02011-06-15 23:02:42 +00001133 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001134 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001135 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1136 }
John McCall31168b02011-06-15 23:02:42 +00001137 }
1138
Abramo Bagnaradff19302011-03-08 08:55:46 +00001139 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001140 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001141 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001142 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001143 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001144 if (RequireNonAbstractType(PropertyIvarLoc,
1145 PropertyIvarType,
1146 diag::err_abstract_type_in_decl,
1147 AbstractSynthesizedIvarType)) {
1148 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001149 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001150 } else if (CompleteTypeErr)
1151 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001152 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001153 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001154
John McCall5fb5df92012-06-20 06:18:46 +00001155 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001156 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1157 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001158 // Note! I deliberately want it to fall thru so, we have a
1159 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001160 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001161 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001162 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001163 << property->getDeclName() << Ivar->getDeclName()
1164 << ClassDeclared->getDeclName();
1165 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001166 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001167 // Note! I deliberately want it to fall thru so more errors are caught.
1168 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001169 property->setPropertyIvarDecl(Ivar);
1170
Ted Kremenekac597f32010-03-12 00:46:40 +00001171 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1172
1173 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001174 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001175 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001176 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001177 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001178 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001179 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001180 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001181 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001182 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1183 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001184 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001185 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001186 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001187 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001188 << property->getDeclName() << PropType
1189 << Ivar->getDeclName() << IvarType;
1190 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001191 // Note! I deliberately want it to fall thru so, we have a
1192 // a property implementation and to avoid future warnings.
1193 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001194 else {
1195 // FIXME! Rules for properties are somewhat different that those
1196 // for assignments. Use a new routine to consolidate all cases;
1197 // specifically for property redeclarations as well as for ivars.
1198 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1199 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1200 if (lhsType != rhsType &&
1201 lhsType->isArithmeticType()) {
1202 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1203 << property->getDeclName() << PropType
1204 << Ivar->getDeclName() << IvarType;
1205 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1206 // Fall thru - see previous comment
1207 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001208 }
1209 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001210 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001211 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001212 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001213 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001214 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001215 // Fall thru - see previous comment
1216 }
John McCall31168b02011-06-15 23:02:42 +00001217 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001218 if ((property->getType()->isObjCObjectPointerType() ||
1219 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001220 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001221 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001222 << property->getDeclName() << Ivar->getDeclName();
1223 // Fall thru - see previous comment
1224 }
1225 }
John McCall460ce582015-10-22 18:38:17 +00001226 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1227 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001228 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001229 } else if (PropertyIvar)
1230 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001231 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001232
Ted Kremenekac597f32010-03-12 00:46:40 +00001233 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1234 ObjCPropertyImplDecl *PIDecl =
1235 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1236 property,
1237 (Synthesize ?
1238 ObjCPropertyImplDecl::Synthesize
1239 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001240 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001241
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001242 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001243 PIDecl->setInvalidDecl();
1244
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001245 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1246 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001247 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001248 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001249 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1250 // returned by the getter as it must conform to C++'s copy-return rules.
1251 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001252 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001253 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1254 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001255 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001256 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001257 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001258 Expr *LoadSelfExpr =
1259 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001260 CK_LValueToRValue, SelfExpr, nullptr,
1261 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001262 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001263 new (Context) ObjCIvarRefExpr(Ivar,
1264 Ivar->getUsageType(SelfDecl->getType()),
1265 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001266 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001267 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001268 ExprResult Res = PerformCopyInitialization(
1269 InitializedEntity::InitializeResult(PropertyDiagLoc,
1270 getterMethod->getReturnType(),
1271 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001272 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001273 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001274 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001275 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001276 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001277 PIDecl->setGetterCXXConstructor(ResExpr);
1278 }
1279 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001280 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1281 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1282 Diag(getterMethod->getLocation(),
1283 diag::warn_property_getter_owning_mismatch);
1284 Diag(property->getLocation(), diag::note_property_declare);
1285 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001286 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1287 switch (getterMethod->getMethodFamily()) {
1288 case OMF_retain:
1289 case OMF_retainCount:
1290 case OMF_release:
1291 case OMF_autorelease:
1292 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1293 << 1 << getterMethod->getSelector();
1294 break;
1295 default:
1296 break;
1297 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001298 }
1299 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1300 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001301 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1302 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001303 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001304 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001305 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1306 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001307 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001308 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001309 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001310 Expr *LoadSelfExpr =
1311 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001312 CK_LValueToRValue, SelfExpr, nullptr,
1313 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001314 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001315 new (Context) ObjCIvarRefExpr(Ivar,
1316 Ivar->getUsageType(SelfDecl->getType()),
1317 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001318 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001319 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001320 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1321 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001322 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001323 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1324 VK_LValue, PropertyDiagLoc);
1325 MarkDeclRefReferenced(rhs);
1326 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001327 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001328 if (property->getPropertyAttributes() &
1329 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001330 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001331 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001332 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1333 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001334 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001335 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001336 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001337 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001338 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001339 Diag(FuncDecl->getLocStart(),
1340 diag::note_callee_decl) << FuncDecl;
1341 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001342 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001343 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001344 }
1345 }
1346
Ted Kremenekac597f32010-03-12 00:46:40 +00001347 if (IC) {
1348 if (Synthesize)
1349 if (ObjCPropertyImplDecl *PPIDecl =
1350 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1351 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1352 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1353 << PropertyIvar;
1354 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1355 }
1356
1357 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001358 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Ted Kremenekac597f32010-03-12 00:46:40 +00001359 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1360 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001361 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001362 }
1363 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001364 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001365 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001366 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001367 // Diagnose if an ivar was lazily synthesdized due to a previous
1368 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001369 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001370 ObjCInterfaceDecl *ClassDeclared=nullptr;
1371 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001372 if (!Synthesize)
1373 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1374 else {
1375 if (PropertyIvar && PropertyIvar != PropertyId)
1376 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1377 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001378 // Issue diagnostics only if Ivar belongs to current class.
1379 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001380 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001381 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1382 << PropertyId;
1383 Ivar->setInvalidDecl();
1384 }
1385 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001386 } else {
1387 if (Synthesize)
1388 if (ObjCPropertyImplDecl *PPIDecl =
1389 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001390 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001391 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1392 << PropertyIvar;
1393 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1394 }
1395
1396 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001397 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001398 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001399 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001400 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001401 }
1402 CatImplClass->addPropertyImplementation(PIDecl);
1403 }
1404
John McCall48871652010-08-21 09:40:31 +00001405 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001406}
1407
1408//===----------------------------------------------------------------------===//
1409// Helper methods.
1410//===----------------------------------------------------------------------===//
1411
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001412/// DiagnosePropertyMismatch - Compares two properties for their
1413/// attributes and types and warns on a variety of inconsistencies.
1414///
1415void
1416Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1417 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001418 const IdentifierInfo *inheritedName,
1419 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001420 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001421 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001422 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001423 SuperProperty->getPropertyAttributes();
1424
1425 // We allow readonly properties without an explicit ownership
1426 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1427 // to be overridden by a property with any explicit ownership in the subclass.
1428 if (!OverridingProtocolProperty &&
1429 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1430 ;
1431 else {
1432 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1433 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1434 Diag(Property->getLocation(), diag::warn_readonly_property)
1435 << Property->getDeclName() << inheritedName;
1436 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1437 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001438 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001439 << Property->getDeclName() << "copy" << inheritedName;
1440 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1441 unsigned CAttrRetain =
1442 (CAttr &
1443 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1444 unsigned SAttrRetain =
1445 (SAttr &
1446 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1447 bool CStrong = (CAttrRetain != 0);
1448 bool SStrong = (SAttrRetain != 0);
1449 if (CStrong != SStrong)
1450 Diag(Property->getLocation(), diag::warn_property_attribute)
1451 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1452 }
John McCall31168b02011-06-15 23:02:42 +00001453 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001454
Douglas Gregor429183e2015-12-09 22:57:32 +00001455 // Check for nonatomic; note that nonatomic is effectively
1456 // meaningless for readonly properties, so don't diagnose if the
1457 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001458 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001459 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001460 Diag(Property->getLocation(), diag::warn_property_attribute)
1461 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001462 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1463 }
1464 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001465 Diag(Property->getLocation(), diag::warn_property_attribute)
1466 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001467 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1468 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001469
1470 QualType LHSType =
1471 Context.getCanonicalType(SuperProperty->getType());
1472 QualType RHSType =
1473 Context.getCanonicalType(Property->getType());
1474
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001475 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001476 // Do cases not handled in above.
1477 // FIXME. For future support of covariant property types, revisit this.
1478 bool IncompatibleObjC = false;
1479 QualType ConvertedType;
1480 if (!isObjCPointerConversion(RHSType, LHSType,
1481 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001482 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001483 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1484 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001485 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1486 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001487 }
1488}
1489
1490bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1491 ObjCMethodDecl *GetterMethod,
1492 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001493 if (!GetterMethod)
1494 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001495 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001496 QualType PropertyIvarType = property->getType().getNonReferenceType();
1497 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1498 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001499 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1500 const ObjCObjectPointerType *getterObjCPtr = nullptr;
1501 if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) &&
1502 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1503 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001504 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001505 != Compatible) {
1506 Diag(Loc, diag::error_property_accessor_type)
1507 << property->getDeclName() << PropertyIvarType
1508 << GetterMethod->getSelector() << GetterType;
1509 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1510 return true;
1511 } else {
1512 compat = true;
1513 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1514 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1515 if (lhsType != rhsType && lhsType->isArithmeticType())
1516 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001517 }
1518 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001519
1520 if (!compat) {
1521 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1522 << property->getDeclName()
1523 << GetterMethod->getSelector();
1524 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1525 return true;
1526 }
1527
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001528 return false;
1529}
1530
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001531/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001532/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001533static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1534 ObjCContainerDecl::PropertyMap &PropMap,
1535 ObjCContainerDecl::PropertyMap &SuperPropMap,
1536 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001537 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001538 for (auto *Prop : IDecl->properties())
1539 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1540 Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001541
1542 // Collect the properties from visible extensions.
1543 for (auto *Ext : IDecl->visible_extensions())
1544 CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
1545
Ted Kremenek204c3c52014-02-22 00:02:03 +00001546 if (IncludeProtocols) {
1547 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001548 for (auto *PI : IDecl->all_referenced_protocols())
1549 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001550 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001551 }
1552 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001553 for (auto *Prop : CATDecl->properties())
1554 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1555 Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001556 if (IncludeProtocols) {
1557 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001558 for (auto *PI : CATDecl->protocols())
1559 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001560 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001561 }
1562 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001563 for (auto *Prop : PDecl->properties()) {
1564 ObjCPropertyDecl *PropertyFromSuper =
1565 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1566 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001567 // Exclude property for protocols which conform to class's super-class,
1568 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001569 if (!PropertyFromSuper ||
1570 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001571 ObjCPropertyDecl *&PropEntry =
1572 PropMap[std::make_pair(Prop->getIdentifier(),
1573 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001574 if (!PropEntry)
1575 PropEntry = Prop;
1576 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001577 }
1578 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001579 for (auto *PI : PDecl->protocols())
1580 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001581 }
1582}
1583
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001584/// CollectSuperClassPropertyImplementations - This routine collects list of
1585/// properties to be implemented in super class(s) and also coming from their
1586/// conforming protocols.
1587static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001588 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001589 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001590 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001591 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001592 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001593 SDecl = SDecl->getSuperClass();
1594 }
1595 }
1596}
1597
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001598/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1599/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1600/// declared in class 'IFace'.
1601bool
1602Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1603 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1604 if (!IV->getSynthesize())
1605 return false;
1606 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1607 Method->isInstanceMethod());
1608 if (!IMD || !IMD->isPropertyAccessor())
1609 return false;
1610
1611 // look up a property declaration whose one of its accessors is implemented
1612 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001613 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001614 if ((Property->getGetterName() == IMD->getSelector() ||
1615 Property->getSetterName() == IMD->getSelector()) &&
1616 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001617 return true;
1618 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001619 // Also look up property declaration in class extension whose one of its
1620 // accessors is implemented by this method.
1621 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001622 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001623 if ((Property->getGetterName() == IMD->getSelector() ||
1624 Property->getSetterName() == IMD->getSelector()) &&
1625 (Property->getPropertyIvarDecl() == IV))
1626 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001627 return false;
1628}
1629
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001630static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1631 ObjCPropertyDecl *Prop) {
1632 bool SuperClassImplementsGetter = false;
1633 bool SuperClassImplementsSetter = false;
1634 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1635 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001636
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001637 while (IDecl->getSuperClass()) {
1638 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1639 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1640 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001641
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001642 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1643 SuperClassImplementsSetter = true;
1644 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1645 return true;
1646 IDecl = IDecl->getSuperClass();
1647 }
1648 return false;
1649}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001650
James Dennett2a4d13c2012-06-15 07:13:21 +00001651/// \brief Default synthesizes all properties which must be synthesized
1652/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001653void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1654 ObjCInterfaceDecl *IDecl) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001655 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001656 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1657 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001658 if (PropMap.empty())
1659 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001660 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001661 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1662
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001663 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1664 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001665 // Is there a matching property synthesize/dynamic?
1666 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001667 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001668 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1669 continue;
1670 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001671 if (IMPDecl->FindPropertyImplDecl(
1672 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001673 continue;
1674 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1675 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1676 continue;
1677 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1678 continue;
1679 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001680 if (ObjCPropertyImplDecl *PID =
1681 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001682 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1683 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001684 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001685 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001686 continue;
1687 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001688 ObjCPropertyDecl *PropInSuperClass =
1689 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1690 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001691 if (ObjCProtocolDecl *Proto =
1692 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001693 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001694 // Suppress the warning if class's superclass implements property's
1695 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001696 // Or, if property is going to be implemented in its super class.
1697 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001698 Diag(IMPDecl->getLocation(),
1699 diag::warn_auto_synthesizing_protocol_property)
1700 << Prop << Proto;
1701 Diag(Prop->getLocation(), diag::note_property_declare);
1702 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001703 continue;
1704 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001705 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001706 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001707 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1708 (PropInSuperClass->getPropertyAttributes() &
1709 ObjCPropertyDecl::OBJC_PR_readonly) &&
1710 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1711 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1712 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1713 << Prop->getIdentifier();
1714 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1715 }
1716 else {
1717 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1718 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001719 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001720 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1721 }
1722 continue;
1723 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001724 // We use invalid SourceLocations for the synthesized ivars since they
1725 // aren't really synthesized at a particular location; they just exist.
1726 // Saying that they are located at the @implementation isn't really going
1727 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001728 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1729 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1730 true,
1731 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001732 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001733 Prop->getLocation(), Prop->getQueryKind()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001734 if (PIDecl) {
1735 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001736 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001737 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001738 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001739}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001740
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001741void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001742 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001743 return;
1744 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1745 if (!IC)
1746 return;
1747 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001748 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001749 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001750}
1751
Ted Kremenek7e812952014-02-21 19:41:30 +00001752static void DiagnoseUnimplementedAccessor(Sema &S,
1753 ObjCInterfaceDecl *PrimaryClass,
1754 Selector Method,
1755 ObjCImplDecl* IMPDecl,
1756 ObjCContainerDecl *CDecl,
1757 ObjCCategoryDecl *C,
1758 ObjCPropertyDecl *Prop,
1759 Sema::SelectorSet &SMap) {
1760 // When reporting on missing property setter/getter implementation in
1761 // categories, do not report when they are declared in primary class,
1762 // class's protocol, or one of it super classes. This is because,
1763 // the class is going to implement them.
1764 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001765 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001766 !PrimaryClass->lookupPropertyAccessor(Method, C,
1767 Prop->isClassProperty()))) {
Ted Kremenek7e812952014-02-21 19:41:30 +00001768 S.Diag(IMPDecl->getLocation(),
1769 isa<ObjCCategoryDecl>(CDecl) ?
1770 diag::warn_setter_getter_impl_required_in_category :
1771 diag::warn_setter_getter_impl_required)
1772 << Prop->getDeclName() << Method;
1773 S.Diag(Prop->getLocation(),
1774 diag::note_property_declare);
1775 if (S.LangOpts.ObjCDefaultSynthProperties &&
1776 S.LangOpts.ObjCRuntime.isNonFragile())
1777 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1778 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1779 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1780 }
1781}
1782
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001783void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001784 ObjCContainerDecl *CDecl,
1785 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001786 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001787 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1788
Ted Kremenek348e88c2014-02-21 19:41:34 +00001789 if (!SynthesizeProperties) {
1790 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001791 // Gather properties which need not be implemented in this class
1792 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001793 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001794 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1795 // For categories, no need to implement properties declared in
1796 // its primary class (and its super classes) if property is
1797 // declared in one of those containers.
1798 if ((IDecl = C->getClassInterface())) {
1799 ObjCInterfaceDecl::PropertyDeclOrder PO;
1800 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1801 }
1802 }
1803 if (IDecl)
1804 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1805
1806 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1807 }
1808
Ted Kremenek38882022014-02-21 19:41:39 +00001809 // Scan the @interface to see if any of the protocols it adopts
1810 // require an explicit implementation, via attribute
1811 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001812 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001813 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001814
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001815 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001816 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1817 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001818 // Lazily construct a set of all the properties in the @interface
1819 // of the class, without looking at the superclass. We cannot
1820 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001821 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001822 // as scans the adopted protocols. This work only triggers for protocols
1823 // with the attribute, which is very rare, and only occurs when
1824 // analyzing the @implementation.
1825 if (!LazyMap) {
1826 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1827 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1828 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1829 /* IncludeProtocols */ false);
1830 }
Ted Kremenek38882022014-02-21 19:41:39 +00001831 // Add the properties of 'PDecl' to the list of properties that
1832 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00001833 for (auto *PropDecl : PDecl->properties()) {
1834 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
1835 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001836 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001837 PropMap[std::make_pair(PropDecl->getIdentifier(),
1838 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001839 }
1840 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001841 }
Ted Kremenek38882022014-02-21 19:41:39 +00001842
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001843 if (PropMap.empty())
1844 return;
1845
1846 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001847 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001848 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001849
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001850 SelectorSet InsMap;
1851 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001852 for (const auto *I : IMPDecl->methods())
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001853 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001854
1855 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001856 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001857 if (C && !C->IsClassExtension())
1858 if ((PrimaryClass = C->getClassInterface()))
1859 // Report unimplemented properties in the category as well.
1860 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1861 // When reporting on missing setter/getters, do not report when
1862 // setter/getter is implemented in category's primary class
1863 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001864 for (const auto *I : IMP->methods())
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001865 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001866 }
1867
Anna Zaks673d76b2012-10-18 19:17:53 +00001868 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001869 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1870 ObjCPropertyDecl *Prop = P->second;
1871 // Is there a matching propery synthesize/dynamic?
1872 if (Prop->isInvalidDecl() ||
1873 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001874 PropImplMap.count(Prop) ||
1875 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001876 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001877
1878 // Diagnose unimplemented getters and setters.
1879 DiagnoseUnimplementedAccessor(*this,
1880 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1881 if (!Prop->isReadOnly())
1882 DiagnoseUnimplementedAccessor(*this,
1883 PrimaryClass, Prop->getSetterName(),
1884 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001885 }
1886}
1887
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001888void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001889 for (const auto *propertyImpl : impDecl->property_impls()) {
1890 const auto *property = propertyImpl->getPropertyDecl();
1891
1892 // Warn about null_resettable properties with synthesized setters,
1893 // because the setter won't properly handle nil.
1894 if (propertyImpl->getPropertyImplementation()
1895 == ObjCPropertyImplDecl::Synthesize &&
1896 (property->getPropertyAttributes() &
1897 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1898 property->getGetterMethodDecl() &&
1899 property->getSetterMethodDecl()) {
1900 auto *getterMethod = property->getGetterMethodDecl();
1901 auto *setterMethod = property->getSetterMethodDecl();
1902 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1903 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1904 SourceLocation loc = propertyImpl->getLocation();
1905 if (loc.isInvalid())
1906 loc = impDecl->getLocStart();
1907
1908 Diag(loc, diag::warn_null_resettable_setter)
1909 << setterMethod->getSelector() << property->getDeclName();
1910 }
1911 }
1912 }
1913}
1914
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001915void
1916Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001917 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001918 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001919 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001920 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001921 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00001922 for (auto *Prop : IDecl->properties())
1923 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001924 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00001925 for (auto *Prop : Ext->properties())
1926 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001927
Manman Renefe1bac2016-01-27 20:00:32 +00001928 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1929 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001930 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001931 ObjCMethodDecl *GetterMethod = nullptr;
1932 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001933 bool LookedUpGetterSetter = false;
1934
Bill Wendling44426052012-12-20 19:22:21 +00001935 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001936 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001937
John McCall43192862011-09-13 18:31:23 +00001938 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1939 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00001940 GetterMethod = Property->isClassProperty() ?
1941 IMPDecl->getClassMethod(Property->getGetterName()) :
1942 IMPDecl->getInstanceMethod(Property->getGetterName());
1943 SetterMethod = Property->isClassProperty() ?
1944 IMPDecl->getClassMethod(Property->getSetterName()) :
1945 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001946 LookedUpGetterSetter = true;
1947 if (GetterMethod) {
1948 Diag(GetterMethod->getLocation(),
1949 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001950 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001951 Diag(Property->getLocation(), diag::note_property_declare);
1952 }
1953 if (SetterMethod) {
1954 Diag(SetterMethod->getLocation(),
1955 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001956 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001957 Diag(Property->getLocation(), diag::note_property_declare);
1958 }
1959 }
1960
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001961 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001962 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1963 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001964 continue;
Manman Ren5b786402016-01-28 18:49:28 +00001965 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
1966 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001967 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1968 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001969 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00001970 GetterMethod = Property->isClassProperty() ?
1971 IMPDecl->getClassMethod(Property->getGetterName()) :
1972 IMPDecl->getInstanceMethod(Property->getGetterName());
1973 SetterMethod = Property->isClassProperty() ?
1974 IMPDecl->getClassMethod(Property->getSetterName()) :
1975 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001976 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001977 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1978 SourceLocation MethodLoc =
1979 (GetterMethod ? GetterMethod->getLocation()
1980 : SetterMethod->getLocation());
1981 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001982 << Property->getIdentifier() << (GetterMethod != nullptr)
1983 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001984 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001985 if (Property->getLParenLoc().isValid() &&
1986 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001987 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001988 SourceLocation AfterLParen =
1989 getLocForEndOfToken(Property->getLParenLoc());
1990 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
1991 : "nonatomic";
1992 Diag(Property->getLocation(),
1993 diag::note_atomic_property_fixup_suggest)
1994 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
1995 } else if (Property->getLParenLoc().isInvalid()) {
1996 //@property id etc.
1997 SourceLocation startLoc =
1998 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1999 Diag(Property->getLocation(),
2000 diag::note_atomic_property_fixup_suggest)
2001 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002002 }
2003 else
2004 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002005 Diag(Property->getLocation(), diag::note_property_declare);
2006 }
2007 }
2008 }
2009}
2010
John McCall31168b02011-06-15 23:02:42 +00002011void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002012 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002013 return;
2014
Aaron Ballmand85eff42014-03-14 15:02:45 +00002015 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002016 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002017 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002018 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002019 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002020 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2021 if (!method)
2022 continue;
2023 ObjCMethodFamily family = method->getMethodFamily();
2024 if (family == OMF_alloc || family == OMF_copy ||
2025 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002026 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002027 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002028 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002029 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002030
2031 // Look for a getter explicitly declared alongside the property.
2032 // If we find one, use its location for the note.
2033 SourceLocation noteLoc = PD->getLocation();
2034 SourceLocation fixItLoc;
2035 for (auto *getterRedecl : method->redecls()) {
2036 if (getterRedecl->isImplicit())
2037 continue;
2038 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2039 continue;
2040 noteLoc = getterRedecl->getLocation();
2041 fixItLoc = getterRedecl->getLocEnd();
2042 }
2043
2044 Preprocessor &PP = getPreprocessor();
2045 TokenValue tokens[] = {
2046 tok::kw___attribute, tok::l_paren, tok::l_paren,
2047 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2048 PP.getIdentifierInfo("none"), tok::r_paren,
2049 tok::r_paren, tok::r_paren
2050 };
2051 StringRef spelling = "__attribute__((objc_method_family(none)))";
2052 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2053 if (!macroName.empty())
2054 spelling = macroName;
2055
2056 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2057 << method->getDeclName() << spelling;
2058 if (fixItLoc.isValid()) {
2059 SmallString<64> fixItText(" ");
2060 fixItText += spelling;
2061 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2062 }
John McCall31168b02011-06-15 23:02:42 +00002063 }
2064 }
2065 }
2066}
2067
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002068void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002069 const ObjCImplementationDecl *ImplD,
2070 const ObjCInterfaceDecl *IFD) {
2071 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002072 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2073 if (!SuperD)
2074 return;
2075
2076 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002077 for (const auto *I : ImplD->instance_methods())
2078 if (I->getMethodFamily() == OMF_init)
2079 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002080
2081 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2082 SuperD->getDesignatedInitializers(DesignatedInits);
2083 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2084 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2085 const ObjCMethodDecl *MD = *I;
2086 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002087 bool Ignore = false;
2088 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2089 Ignore = IMD->isUnavailable();
2090 }
2091 if (!Ignore) {
2092 Diag(ImplD->getLocation(),
2093 diag::warn_objc_implementation_missing_designated_init_override)
2094 << MD->getSelector();
2095 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2096 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002097 }
2098 }
2099}
2100
John McCallad31b5f2010-11-10 07:01:40 +00002101/// AddPropertyAttrs - Propagates attributes from a property to the
2102/// implicitly-declared getter or setter for that property.
2103static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2104 ObjCPropertyDecl *Property) {
2105 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002106 for (const auto *A : Property->attrs()) {
2107 if (isa<DeprecatedAttr>(A) ||
2108 isa<UnavailableAttr>(A) ||
2109 isa<AvailabilityAttr>(A))
2110 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002111 }
John McCallad31b5f2010-11-10 07:01:40 +00002112}
2113
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002114/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2115/// have the property type and issue diagnostics if they don't.
2116/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002117/// appropriate lookup tables.
2118void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002119 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002120 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002121 if (CD->isInvalidDecl())
2122 return;
2123
Manman Rend36f7d52016-01-27 20:10:32 +00002124 bool IsClassProperty = property->isClassProperty();
2125 GetterMethod = IsClassProperty ?
2126 CD->getClassMethod(property->getGetterName()) :
2127 CD->getInstanceMethod(property->getGetterName());
2128
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002129 // if setter or getter is not found in class extension, it might be
2130 // in the primary class.
2131 if (!GetterMethod)
2132 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2133 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002134 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2135 getClassMethod(property->getGetterName()) :
2136 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002137 getInstanceMethod(property->getGetterName());
2138
Manman Rend36f7d52016-01-27 20:10:32 +00002139 SetterMethod = IsClassProperty ?
2140 CD->getClassMethod(property->getSetterName()) :
2141 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002142 if (!SetterMethod)
2143 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2144 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002145 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2146 getClassMethod(property->getSetterName()) :
2147 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002148 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002149 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2150 property->getLocation());
2151
2152 if (SetterMethod) {
2153 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2154 property->getPropertyAttributes();
2155 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002156 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2157 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002158 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2159 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002160 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002161 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2162 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002163 Diag(property->getLocation(),
2164 diag::warn_accessor_property_type_mismatch)
2165 << property->getDeclName()
2166 << SetterMethod->getSelector();
2167 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2168 }
2169 }
2170
2171 // Synthesize getter/setter methods if none exist.
2172 // Find the default getter and if one not found, add one.
2173 // FIXME: The synthesized property we set here is misleading. We almost always
2174 // synthesize these methods unless the user explicitly provided prototypes
2175 // (which is odd, but allowed). Sema should be typechecking that the
2176 // declarations jive in that situation (which it is not currently).
2177 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002178 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002179 // Declare a getter method and add it to the list of methods
2180 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002181 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002182
Douglas Gregor849ebc22015-06-19 18:14:46 +00002183 // If the property is null_resettable, the getter returns nonnull.
2184 QualType resultTy = property->getType();
2185 if (property->getPropertyAttributes() &
2186 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2187 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002188 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002189 if (*nullability == NullabilityKind::Unspecified)
2190 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2191 modifiedTy, modifiedTy);
2192 }
2193 }
2194
Ted Kremenek2f075632010-09-21 20:52:59 +00002195 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2196 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002197 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002198 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002199 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002200 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002201 (property->getPropertyImplementation() ==
2202 ObjCPropertyDecl::Optional) ?
2203 ObjCMethodDecl::Optional :
2204 ObjCMethodDecl::Required);
2205 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002206
2207 AddPropertyAttrs(*this, GetterMethod, property);
2208
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002209 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002210 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2211 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002212
2213 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2214 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002215 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002216
2217 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002218 GetterMethod->addAttr(
2219 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2220 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002221
2222 if (getLangOpts().ObjCAutoRefCount)
2223 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002224 } else
2225 // A user declared getter will be synthesize when @synthesize of
2226 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002227 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002228 property->setGetterMethodDecl(GetterMethod);
2229
2230 // Skip setter if property is read-only.
2231 if (!property->isReadOnly()) {
2232 // Find the default setter and if one not found, add one.
2233 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002234 // No instance/class method of same name as property setter name was
2235 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002236 // Declare a setter method and add it to the list of methods
2237 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002238 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002239
2240 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002241 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002242 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002243 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002244 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002245 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002246 /*isImplicitlyDeclared=*/true,
2247 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002248 (property->getPropertyImplementation() ==
2249 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002250 ObjCMethodDecl::Optional :
2251 ObjCMethodDecl::Required);
2252
Douglas Gregor849ebc22015-06-19 18:14:46 +00002253 // If the property is null_resettable, the setter accepts a
2254 // nullable value.
2255 QualType paramTy = property->getType().getUnqualifiedType();
2256 if (property->getPropertyAttributes() &
2257 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2258 QualType modifiedTy = paramTy;
2259 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2260 if (*nullability == NullabilityKind::Unspecified)
2261 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2262 modifiedTy, modifiedTy);
2263 }
2264 }
2265
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002266 // Invent the arguments for the setter. We don't bother making a
2267 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002268 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2269 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002270 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002271 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002272 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002273 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002274 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002275 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002276
2277 AddPropertyAttrs(*this, SetterMethod, property);
2278
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002279 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002280 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002281 SetterMethod->addAttr(
2282 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2283 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002284 // It's possible for the user to have set a very odd custom
2285 // setter selector that causes it to have a method family.
2286 if (getLangOpts().ObjCAutoRefCount)
2287 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002288 } else
2289 // A user declared setter will be synthesize when @synthesize of
2290 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002291 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002292 property->setSetterMethodDecl(SetterMethod);
2293 }
2294 // Add any synthesized methods to the global pool. This allows us to
2295 // handle the following, which is supported by GCC (and part of the design).
2296 //
2297 // @interface Foo
2298 // @property double bar;
2299 // @end
2300 //
2301 // void thisIsUnfortunate() {
2302 // id foo;
2303 // double bar = [foo bar];
2304 // }
2305 //
Manman Rend36f7d52016-01-27 20:10:32 +00002306 if (!IsClassProperty) {
2307 if (GetterMethod)
2308 AddInstanceMethodToGlobalPool(GetterMethod);
2309 if (SetterMethod)
2310 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002311 } else {
2312 if (GetterMethod)
2313 AddFactoryMethodToGlobalPool(GetterMethod);
2314 if (SetterMethod)
2315 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002316 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002317
2318 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2319 if (!CurrentClass) {
2320 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2321 CurrentClass = Cat->getClassInterface();
2322 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2323 CurrentClass = Impl->getClassInterface();
2324 }
2325 if (GetterMethod)
2326 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2327 if (SetterMethod)
2328 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002329}
2330
John McCall48871652010-08-21 09:40:31 +00002331void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002332 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002333 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002334 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002335 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002336 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002337 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002338
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002339 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2340 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2341 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2342 << "readonly" << "readwrite";
2343
2344 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2345 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002346
2347 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002348 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002349 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2350 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002351 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002352 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002353 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2354 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2355 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002356 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002357 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002358 }
2359
2360 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002361 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2362 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002363 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2364 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002365 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002366 }
Bill Wendling44426052012-12-20 19:22:21 +00002367 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002368 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2369 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002370 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002371 }
Bill Wendling44426052012-12-20 19:22:21 +00002372 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002373 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2374 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002375 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002376 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002377 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002378 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002379 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2380 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002381 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002382 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002383 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002384 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002385 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2386 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002387 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2388 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002389 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002390 }
Bill Wendling44426052012-12-20 19:22:21 +00002391 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002392 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2393 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002394 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002395 }
Bill Wendling44426052012-12-20 19:22:21 +00002396 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002397 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2398 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002399 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002400 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002401 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002402 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002403 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2404 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002405 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002406 }
Bill Wendling44426052012-12-20 19:22:21 +00002407 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2408 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002409 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2410 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002411 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002412 }
Bill Wendling44426052012-12-20 19:22:21 +00002413 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002414 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2415 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002416 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002417 }
Bill Wendling44426052012-12-20 19:22:21 +00002418 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002419 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2420 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002421 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002422 }
2423 }
Bill Wendling44426052012-12-20 19:22:21 +00002424 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2425 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002426 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2427 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002428 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002429 }
Bill Wendling44426052012-12-20 19:22:21 +00002430 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2431 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002432 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2433 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002434 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002435 }
2436
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002437 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002438 // 'weak' and 'nonnull' are mutually exclusive.
2439 if (auto nullability = PropertyTy->getNullability(Context)) {
2440 if (*nullability == NullabilityKind::NonNull)
2441 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2442 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002443 }
2444 }
2445
Bill Wendling44426052012-12-20 19:22:21 +00002446 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2447 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002448 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2449 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002450 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002451 }
2452
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002453 // Warn if user supplied no assignment attribute, property is
2454 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002455 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2456 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2457 // do nothing
2458 } else if (getLangOpts().ObjCAutoRefCount) {
2459 // With arc, @property definitions should default to strong when
2460 // not specified.
2461 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2462 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002463 bool isAnyClassTy =
2464 (PropertyTy->isObjCClassType() ||
2465 PropertyTy->isObjCQualifiedClassType());
2466 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2467 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002468 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002469 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002470 else if (propertyInPrimaryClass) {
2471 // Don't issue warning on property with no life time in class
2472 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002473 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002474 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002475 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002476
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002477 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002478 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002479 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002480 }
John McCallb61e14e2015-10-27 04:54:50 +00002481 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002482
2483 // FIXME: Implement warning dependent on NSCopying being
2484 // implemented. See also:
2485 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2486 // (please trim this list while you are at it).
2487 }
2488
Bill Wendling44426052012-12-20 19:22:21 +00002489 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2490 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002491 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002492 && PropertyTy->isBlockPointerType())
2493 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002494 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2495 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2496 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002497 PropertyTy->isBlockPointerType())
2498 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002499
Bill Wendling44426052012-12-20 19:22:21 +00002500 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2501 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002502 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002503}