blob: 9412d0160048a077301ec523ae58a4f9b3573b8d [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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000107/// Check this Objective-C property against a property declared in the
Douglas Gregorb8982092013-01-21 19:42:21 +0000108/// given protocol.
109static void
110CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
111 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000113 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000114 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000115 return;
116
117 // Look for a property with the same name.
118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
119 for (unsigned I = 0, N = R.size(); I != N; ++I) {
120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000122 return;
123 }
124 }
125
126 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000127 for (auto *P : Proto->protocols())
128 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129}
130
John McCallb61e14e2015-10-27 04:54:50 +0000131static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
132 // In GC mode, just look for the __weak qualifier.
133 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
135
136 // In ARC/MRC, look for an explicit ownership qualifier.
137 // For some reason, this only applies to __weak.
138 } else if (auto ownership = T.getObjCLifetime()) {
139 switch (ownership) {
140 case Qualifiers::OCL_Weak:
141 return ObjCDeclSpec::DQ_PR_weak;
142 case Qualifiers::OCL_Strong:
143 return ObjCDeclSpec::DQ_PR_strong;
144 case Qualifiers::OCL_ExplicitNone:
145 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
146 case Qualifiers::OCL_Autoreleasing:
147 case Qualifiers::OCL_None:
148 return 0;
149 }
150 llvm_unreachable("bad qualifier");
151 }
152
153 return 0;
154}
155
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000156static const unsigned OwnershipMask =
157 (ObjCPropertyDecl::OBJC_PR_assign |
158 ObjCPropertyDecl::OBJC_PR_retain |
159 ObjCPropertyDecl::OBJC_PR_copy |
160 ObjCPropertyDecl::OBJC_PR_weak |
161 ObjCPropertyDecl::OBJC_PR_strong |
162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
163
John McCallb61e14e2015-10-27 04:54:50 +0000164static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000165 unsigned result = attr & OwnershipMask;
166
167 // From an ownership perspective, assign and unsafe_unretained are
168 // identical; make sure one also implies the other.
169 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
171 result |= ObjCPropertyDecl::OBJC_PR_assign |
172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
173 }
174
175 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000176}
177
John McCall48871652010-08-21 09:40:31 +0000178Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000179 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000180 FieldDeclarator &FD,
181 ObjCDeclSpec &ODS,
182 Selector GetterSel,
183 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000184 tok::ObjCKeywordKind MethodImplKind,
185 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000186 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
189 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000190 if (!getOwnershipRule(Attributes)) {
191 Attributes |= deducePropertyOwnershipFromType(*this, T);
192 }
Bill Wendling44426052012-12-20 19:22:21 +0000193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000194 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000196
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000199 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000201 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000203 FD,
204 GetterSel, ODS.getGetterNameLoc(),
205 SetterSel, ODS.getSetterNameLoc(),
206 isReadWrite, Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000207 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000208 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000209 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000210 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000211 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000212 }
213
214 if (!Res) {
215 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000216 GetterSel, ODS.getGetterNameLoc(), SetterSel,
217 ODS.getSetterNameLoc(), isReadWrite, Attributes,
218 ODS.getPropertyAttributes(), T, TSI,
219 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000220 if (lexicalDC)
221 Res->setLexicalDeclContext(lexicalDC);
222 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000223
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000224 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000225 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000226 (isa<ObjCInterfaceDecl>(ClassDecl) ||
227 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000228
John McCallb61e14e2015-10-27 04:54:50 +0000229 // Check consistency if the type has explicit ownership qualification.
230 if (Res->getType().getObjCLifetime())
231 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000232
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000234 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000235 // For a class, compare the property against a property in our superclass.
236 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000237 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
238 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000239 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000240 for (unsigned I = 0, N = R.size(); I != N; ++I) {
241 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000242 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000243 FoundInSuper = true;
244 break;
245 }
246 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000247 if (FoundInSuper)
248 break;
249 else
250 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000251 }
252
253 if (FoundInSuper) {
254 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000255 for (auto *P : CurrentInterfaceDecl->protocols()) {
256 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000257 }
258 } else {
259 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (auto *P : IFace->all_referenced_protocols()) {
261 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000262 }
263 }
264 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000265 // We don't check if class extension. Because properties in class extension
266 // are meant to override some of the attributes and checking has already done
267 // when property in class extension is constructed.
268 if (!Cat->IsClassExtension())
269 for (auto *P : Cat->protocols())
270 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000271 } else {
272 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000273 for (auto *P : Proto->protocols())
274 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000275 }
276
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000277 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000278 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000279}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000280
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000282makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000308 if (Attributes & ObjCDeclSpec::DQ_PR_class)
309 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Fangrui Song6907ce22018-07-30 19:24:48 +0000310
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000311 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
312}
313
Fangrui Song6907ce22018-07-30 19:24:48 +0000314static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000315 SourceLocation LParenLoc, SourceLocation &Loc) {
316 if (LParenLoc.isMacroID())
317 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000318
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000319 SourceManager &SM = Context.getSourceManager();
320 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
321 // Try to load the file buffer.
322 bool invalidTemp = false;
323 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
324 if (invalidTemp)
325 return false;
326 const char *tokenBegin = file.data() + locInfo.second;
Fangrui Song6907ce22018-07-30 19:24:48 +0000327
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000328 // Lex from the start of the given location.
329 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
330 Context.getLangOpts(),
331 file.begin(), tokenBegin, file.end());
332 Token Tok;
333 do {
334 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000335 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000336 Loc = Tok.getLocation();
337 return true;
338 }
339 } while (Tok.isNot(tok::r_paren));
340 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000341}
342
Douglas Gregor429183e2015-12-09 22:57:32 +0000343/// Check for a mismatch in the atomicity of the given properties.
344static void checkAtomicPropertyMismatch(Sema &S,
345 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000346 ObjCPropertyDecl *NewProperty,
347 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000348 // If the atomicity of both matches, we're done.
349 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000350 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
351 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000352 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000353 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
354 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000355 if (OldIsAtomic == NewIsAtomic) return;
356
357 // Determine whether the given property is readonly and implicitly
358 // atomic.
359 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
360 // Is it readonly?
361 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000362 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000363
364 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000365 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000366
367 // Was 'atomic' specified directly?
Fangrui Song6907ce22018-07-30 19:24:48 +0000368 if (Property->getPropertyAttributesAsWritten() &
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000369 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000370 return false;
371
372 return true;
373 };
374
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000375 // If we're allowed to propagate atomicity, and the new property did
376 // not specify atomicity at all, propagate.
377 const unsigned AtomicityMask =
378 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
379 if (PropagateAtomicity &&
380 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
381 unsigned Attrs = NewProperty->getPropertyAttributes();
382 Attrs = Attrs & ~AtomicityMask;
383 if (OldIsAtomic)
384 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
Fangrui Song6907ce22018-07-30 19:24:48 +0000385 else
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000386 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
387
388 NewProperty->overwritePropertyAttributes(Attrs);
389 return;
390 }
391
Douglas Gregor429183e2015-12-09 22:57:32 +0000392 // One of the properties is atomic; if it's a readonly property, and
393 // 'atomic' wasn't explicitly specified, we're okay.
394 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
395 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
396 return;
397
398 // Diagnose the conflict.
399 const IdentifierInfo *OldContextName;
400 auto *OldDC = OldProperty->getDeclContext();
401 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
402 OldContextName = Category->getClassInterface()->getIdentifier();
403 else
404 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
405
406 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
407 << NewProperty->getDeclName() << "atomic"
408 << OldContextName;
409 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
410}
411
Douglas Gregor90d34422013-01-21 19:05:22 +0000412ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000413Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000414 SourceLocation AtLoc,
415 SourceLocation LParenLoc,
416 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000417 Selector GetterSel,
418 SourceLocation GetterNameLoc,
419 Selector SetterSel,
420 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000421 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000422 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000423 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000424 QualType T,
425 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000426 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000427 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000428 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000429 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000431 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
Fangrui Song6907ce22018-07-30 19:24:48 +0000432
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 // We need to look in the @interface to see if the @property was
434 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 if (!CCPrimary) {
436 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000437 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000438 }
439
Manman Ren5b786402016-01-28 18:49:28 +0000440 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
441 (Attributes & ObjCDeclSpec::DQ_PR_class);
442
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000443 // Find the property in the extended class's primary class or
444 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000445 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
446 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000447
Fangrui Song6907ce22018-07-30 19:24:48 +0000448 // If we found a property in an extension, complain.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000449 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
450 Diag(AtLoc, diag::err_duplicate_property);
451 Diag(PIDecl->getLocation(), diag::note_property_declare);
452 return nullptr;
453 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000454
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000455 // Check for consistency with the previous declaration, if there is one.
456 if (PIDecl) {
457 // A readonly property declared in the primary class can be refined
458 // by adding a readwrite property within an extension.
459 // Anything else is an error.
460 if (!(PIDecl->isReadOnly() && isReadWrite)) {
461 // Tailor the diagnostics for the common case where a readwrite
462 // property is declared both in the @interface and the continuation.
463 // This is a common error where the user often intended the original
464 // declaration to be readonly.
465 unsigned diag =
466 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
467 (PIDecl->getPropertyAttributesAsWritten() &
468 ObjCPropertyDecl::OBJC_PR_readwrite)
469 ? diag::err_use_continuation_class_redeclaration_readwrite
470 : diag::err_use_continuation_class;
471 Diag(AtLoc, diag)
472 << CCPrimary->getDeclName();
473 Diag(PIDecl->getLocation(), diag::note_property_declare);
474 return nullptr;
475 }
476
477 // Check for consistency of getters.
478 if (PIDecl->getGetterName() != GetterSel) {
479 // If the getter was written explicitly, complain.
480 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
481 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
482 << PIDecl->getGetterName() << GetterSel;
483 Diag(PIDecl->getLocation(), diag::note_property_declare);
484 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000485
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000486 // Always adopt the getter from the original declaration.
487 GetterSel = PIDecl->getGetterName();
488 Attributes |= ObjCDeclSpec::DQ_PR_getter;
489 }
490
491 // Check consistency of ownership.
492 unsigned ExistingOwnership
493 = getOwnershipRule(PIDecl->getPropertyAttributes());
494 unsigned NewOwnership = getOwnershipRule(Attributes);
495 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
496 // If the ownership was written explicitly, complain.
497 if (getOwnershipRule(AttributesAsWritten)) {
498 Diag(AtLoc, diag::warn_property_attr_mismatch);
499 Diag(PIDecl->getLocation(), diag::note_property_declare);
500 }
501
502 // Take the ownership from the original property.
503 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
504 }
505
Fangrui Song6907ce22018-07-30 19:24:48 +0000506 // If the redeclaration is 'weak' but the original property is not,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000507 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
508 !(PIDecl->getPropertyAttributesAsWritten()
509 & ObjCPropertyDecl::OBJC_PR_weak) &&
510 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
511 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
512 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
513 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fangrui Song6907ce22018-07-30 19:24:48 +0000514 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000515 }
516
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000517 // Create a new ObjCPropertyDecl with the DeclContext being
518 // the class extension.
519 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000520 FD, GetterSel, GetterNameLoc,
521 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000522 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000523 Attributes, AttributesAsWritten,
524 T, TSI, MethodImplKind, DC);
525
526 // If there was no declaration of a property with the same name in
527 // the primary class, we're done.
528 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000529 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000530 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000531 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000532
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000533 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
534 bool IncompatibleObjC = false;
535 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000536 // Relax the strict type matching for property type in continuation class.
537 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000538 // as it narrows the object type in its primary class property. Note that
539 // this conversion is safe only because the wider type is for a 'readonly'
540 // property in primary class and 'narrowed' type for a 'readwrite' property
541 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000542 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
543 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
544 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
545 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
546 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000547 ConvertedType, IncompatibleObjC))
548 || IncompatibleObjC) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000549 Diag(AtLoc,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000550 diag::err_type_mismatch_continuation_class) << PDecl->getType();
551 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000552 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000553 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000554 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000555
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000556 // Check that atomicity of property in class extension matches the previous
557 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000558 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000559
Douglas Gregore17765e2015-11-03 17:02:34 +0000560 // Make sure getter/setter are appropriately synthesized.
561 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000562 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000563}
564
565ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
566 ObjCContainerDecl *CDecl,
567 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000568 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000569 FieldDeclarator &FD,
570 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000571 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000572 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000573 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000574 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000575 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000576 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000577 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000578 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000579 tok::ObjCKeywordKind MethodImplKind,
580 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000581 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000582
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000583 // Property defaults to 'assign' if it is readwrite, unless this is ARC
584 // and the type is retainable.
585 bool isAssign;
586 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
587 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
588 isAssign = true;
589 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
590 isAssign = false;
591 } else {
592 isAssign = (!getLangOpts().ObjCAutoRefCount ||
593 !T->isObjCRetainableType());
594 }
595
596 // Issue a warning if property is 'assign' as default and its
597 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000598 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000599 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000600 if (const ObjCObjectPointerType *ObjPtrTy =
601 T->getAs<ObjCObjectPointerType>()) {
602 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
603 if (IDecl)
604 if (ObjCProtocolDecl* PNSCopying =
605 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
606 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
607 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000609 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000610
611 if (T->isObjCObjectType()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000612 SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000613 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000614 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
615 << FixItHint::CreateInsertion(StarLoc, "*");
616 T = Context.getObjCObjectPointerType(T);
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000617 SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
Eli Friedman999af7b2013-07-09 01:38:07 +0000618 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
619 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000620
George Burgess IV00f70bd2018-03-01 05:43:23 +0000621 DeclContext *DC = CDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
623 FD.D.getIdentifierLoc(),
Fangrui Song6907ce22018-07-30 19:24:48 +0000624 PropertyId, AtLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +0000625 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000626
Manman Ren5b786402016-01-28 18:49:28 +0000627 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
628 (Attributes & ObjCDeclSpec::DQ_PR_class);
629 // Class property and instance property can have the same name.
630 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
631 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000632 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000633 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000634 PDecl->setInvalidDecl();
635 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000636 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000638 if (lexicalDC)
639 PDecl->setLexicalDeclContext(lexicalDC);
640 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000641
642 if (T->isArrayType() || T->isFunctionType()) {
643 Diag(AtLoc, diag::err_property_type) << T;
644 PDecl->setInvalidDecl();
645 }
646
647 ProcessDeclAttributes(S, PDecl, FD.D);
648
649 // Regardless of setter/getter attribute, we save the default getter/setter
650 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000651 PDecl->setGetterName(GetterSel, GetterNameLoc);
652 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000653 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000654 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000655
Bill Wendling44426052012-12-20 19:22:21 +0000656 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000657 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
658
Bill Wendling44426052012-12-20 19:22:21 +0000659 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000660 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
661
Bill Wendling44426052012-12-20 19:22:21 +0000662 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000663 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
664
665 if (isReadWrite)
666 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
667
Bill Wendling44426052012-12-20 19:22:21 +0000668 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000669 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
670
Bill Wendling44426052012-12-20 19:22:21 +0000671 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000672 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
673
Bill Wendling44426052012-12-20 19:22:21 +0000674 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
676
Bill Wendling44426052012-12-20 19:22:21 +0000677 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000678 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
679
Bill Wendling44426052012-12-20 19:22:21 +0000680 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
682
Ted Kremenekac597f32010-03-12 00:46:40 +0000683 if (isAssign)
684 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
685
John McCall43192862011-09-13 18:31:23 +0000686 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000687 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000689 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000690 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000691
John McCall31168b02011-06-15 23:02:42 +0000692 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000693 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000694 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
695 if (isAssign)
696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
697
Ted Kremenekac597f32010-03-12 00:46:40 +0000698 if (MethodImplKind == tok::objc_required)
699 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
700 else if (MethodImplKind == tok::objc_optional)
701 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000702
Douglas Gregor813a0662015-06-19 18:14:38 +0000703 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
704 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
705
Douglas Gregor849ebc22015-06-19 18:14:46 +0000706 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
707 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
708
Manman Ren387ff7f2016-01-26 18:52:43 +0000709 if (Attributes & ObjCDeclSpec::DQ_PR_class)
710 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
711
Ted Kremenek959e8302010-03-12 02:31:10 +0000712 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000713}
714
John McCall31168b02011-06-15 23:02:42 +0000715static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
716 ObjCPropertyDecl *property,
717 ObjCIvarDecl *ivar) {
718 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
719
John McCall31168b02011-06-15 23:02:42 +0000720 QualType ivarType = ivar->getType();
721 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall43192862011-09-13 18:31:23 +0000723 // The lifetime implied by the property's attributes.
724 Qualifiers::ObjCLifetime propertyLifetime =
725 getImpliedARCOwnership(property->getPropertyAttributes(),
726 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000727
John McCall43192862011-09-13 18:31:23 +0000728 // We're fine if they match.
729 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000730
John McCall460ce582015-10-22 18:38:17 +0000731 // None isn't a valid lifetime for an object ivar in ARC, and
732 // __autoreleasing is never valid; don't diagnose twice.
733 if ((ivarLifetime == Qualifiers::OCL_None &&
734 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000735 ivarLifetime == Qualifiers::OCL_Autoreleasing)
736 return;
John McCall31168b02011-06-15 23:02:42 +0000737
John McCalld8561f02012-08-20 23:36:59 +0000738 // If the ivar is private, and it's implicitly __unsafe_unretained
739 // becaues of its type, then pretend it was actually implicitly
740 // __strong. This is only sound because we're processing the
741 // property implementation before parsing any method bodies.
742 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
743 propertyLifetime == Qualifiers::OCL_Strong &&
744 ivar->getAccessControl() == ObjCIvarDecl::Private) {
745 SplitQualType split = ivarType.split();
746 if (split.Quals.hasObjCLifetime()) {
747 assert(ivarType->isObjCARCImplicitlyUnretainedType());
748 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
749 ivarType = S.Context.getQualifiedType(split);
750 ivar->setType(ivarType);
751 return;
752 }
753 }
754
John McCall43192862011-09-13 18:31:23 +0000755 switch (propertyLifetime) {
756 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000757 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000758 << property->getDeclName()
759 << ivar->getDeclName()
760 << ivarLifetime;
761 break;
John McCall31168b02011-06-15 23:02:42 +0000762
John McCall43192862011-09-13 18:31:23 +0000763 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000764 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000765 << property->getDeclName()
766 << ivar->getDeclName();
767 break;
John McCall31168b02011-06-15 23:02:42 +0000768
John McCall43192862011-09-13 18:31:23 +0000769 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000770 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000771 << property->getDeclName()
772 << ivar->getDeclName()
Fangrui Song6907ce22018-07-30 19:24:48 +0000773 << ((property->getPropertyAttributesAsWritten()
John McCall43192862011-09-13 18:31:23 +0000774 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
775 break;
John McCall31168b02011-06-15 23:02:42 +0000776
John McCall43192862011-09-13 18:31:23 +0000777 case Qualifiers::OCL_Autoreleasing:
778 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000779
John McCall43192862011-09-13 18:31:23 +0000780 case Qualifiers::OCL_None:
781 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000782 return;
783 }
784
785 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000786 if (propertyImplLoc.isValid())
787 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000788}
789
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000790/// setImpliedPropertyAttributeForReadOnlyProperty -
791/// This routine evaludates life-time attributes for a 'readonly'
792/// property with no known lifetime of its own, using backing
793/// 'ivar's attribute, if any. If no backing 'ivar', property's
794/// life-time is assumed 'strong'.
795static void setImpliedPropertyAttributeForReadOnlyProperty(
796 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000797 Qualifiers::ObjCLifetime propertyLifetime =
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000798 getImpliedARCOwnership(property->getPropertyAttributes(),
799 property->getType());
800 if (propertyLifetime != Qualifiers::OCL_None)
801 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000802
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000803 if (!ivar) {
804 // if no backing ivar, make property 'strong'.
805 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
806 return;
807 }
808 // property assumes owenership of backing ivar.
809 QualType ivarType = ivar->getType();
810 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
811 if (ivarLifetime == Qualifiers::OCL_Strong)
812 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
813 else if (ivarLifetime == Qualifiers::OCL_Weak)
814 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000815}
Ted Kremenekac597f32010-03-12 00:46:40 +0000816
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000817static bool
818isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
819 ObjCPropertyDecl::PropertyAttributeKind Kind) {
820 return (Attr1 & Kind) != (Attr2 & Kind);
821}
822
823static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
824 unsigned Kinds) {
825 return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
826}
827
828/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
829/// property declaration that should be synthesised in all of the inherited
830/// protocols. It also diagnoses properties declared in inherited protocols with
831/// mismatched types or attributes, since any of them can be candidate for
832/// synthesis.
833static ObjCPropertyDecl *
834SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000835 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000836 ObjCPropertyDecl *Property) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000837 assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
838 "Expected a property from a protocol");
839 ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
840 ObjCInterfaceDecl::PropertyDeclOrder Properties;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000841 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
842 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000843 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
844 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000845 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000846 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000847 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000848 for (const auto *PI : SDecl->all_referenced_protocols()) {
849 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000850 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
851 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000852 }
853 SDecl = SDecl->getSuperClass();
854 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000855 }
856
857 if (Properties.empty())
858 return Property;
859
860 ObjCPropertyDecl *OriginalProperty = Property;
861 size_t SelectedIndex = 0;
862 for (const auto &Prop : llvm::enumerate(Properties)) {
863 // Select the 'readwrite' property if such property exists.
864 if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
865 Property = Prop.value();
866 SelectedIndex = Prop.index();
867 }
868 }
869 if (Property != OriginalProperty) {
870 // Check that the old property is compatible with the new one.
871 Properties[SelectedIndex] = OriginalProperty;
872 }
873
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000874 QualType RHSType = S.Context.getCanonicalType(Property->getType());
Alex Lorenz34d070f2017-08-22 10:38:07 +0000875 unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000876 enum MismatchKind {
877 IncompatibleType = 0,
878 HasNoExpectedAttribute,
879 HasUnexpectedAttribute,
880 DifferentGetter,
881 DifferentSetter
882 };
883 // Represents a property from another protocol that conflicts with the
884 // selected declaration.
885 struct MismatchingProperty {
886 const ObjCPropertyDecl *Prop;
887 MismatchKind Kind;
888 StringRef AttributeName;
889 };
890 SmallVector<MismatchingProperty, 4> Mismatches;
891 for (ObjCPropertyDecl *Prop : Properties) {
892 // Verify the property attributes.
Alex Lorenz34d070f2017-08-22 10:38:07 +0000893 unsigned Attr = Prop->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000894 if (Attr != OriginalAttributes) {
895 auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
896 MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
897 : HasUnexpectedAttribute;
898 Mismatches.push_back({Prop, Kind, AttributeName});
899 };
Alex Lorenz61372552018-05-02 22:40:19 +0000900 // The ownership might be incompatible unless the property has no explicit
901 // ownership.
902 bool HasOwnership = (Attr & (ObjCPropertyDecl::OBJC_PR_retain |
903 ObjCPropertyDecl::OBJC_PR_strong |
904 ObjCPropertyDecl::OBJC_PR_copy |
905 ObjCPropertyDecl::OBJC_PR_assign |
906 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
907 ObjCPropertyDecl::OBJC_PR_weak)) != 0;
908 if (HasOwnership &&
909 isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000910 ObjCPropertyDecl::OBJC_PR_copy)) {
911 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
912 continue;
913 }
Alex Lorenz61372552018-05-02 22:40:19 +0000914 if (HasOwnership && areIncompatiblePropertyAttributes(
915 OriginalAttributes, Attr,
916 ObjCPropertyDecl::OBJC_PR_retain |
917 ObjCPropertyDecl::OBJC_PR_strong)) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000918 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
919 ObjCPropertyDecl::OBJC_PR_strong),
920 "retain (or strong)");
921 continue;
922 }
923 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
924 ObjCPropertyDecl::OBJC_PR_atomic)) {
925 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
926 continue;
927 }
928 }
929 if (Property->getGetterName() != Prop->getGetterName()) {
930 Mismatches.push_back({Prop, DifferentGetter, ""});
931 continue;
932 }
933 if (!Property->isReadOnly() && !Prop->isReadOnly() &&
934 Property->getSetterName() != Prop->getSetterName()) {
935 Mismatches.push_back({Prop, DifferentSetter, ""});
936 continue;
937 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000938 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
939 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
940 bool IncompatibleObjC = false;
941 QualType ConvertedType;
942 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
943 || IncompatibleObjC) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000944 Mismatches.push_back({Prop, IncompatibleType, ""});
945 continue;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000946 }
947 }
948 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000949
950 if (Mismatches.empty())
951 return Property;
952
953 // Diagnose incompability.
954 {
955 bool HasIncompatibleAttributes = false;
956 for (const auto &Note : Mismatches)
957 HasIncompatibleAttributes =
958 Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
959 // Promote the warning to an error if there are incompatible attributes or
960 // incompatible types together with readwrite/readonly incompatibility.
961 auto Diag = S.Diag(Property->getLocation(),
962 Property != OriginalProperty || HasIncompatibleAttributes
963 ? diag::err_protocol_property_mismatch
964 : diag::warn_protocol_property_mismatch);
965 Diag << Mismatches[0].Kind;
966 switch (Mismatches[0].Kind) {
967 case IncompatibleType:
968 Diag << Property->getType();
969 break;
970 case HasNoExpectedAttribute:
971 case HasUnexpectedAttribute:
972 Diag << Mismatches[0].AttributeName;
973 break;
974 case DifferentGetter:
975 Diag << Property->getGetterName();
976 break;
977 case DifferentSetter:
978 Diag << Property->getSetterName();
979 break;
980 }
981 }
982 for (const auto &Note : Mismatches) {
983 auto Diag =
984 S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
985 << Note.Kind;
986 switch (Note.Kind) {
987 case IncompatibleType:
988 Diag << Note.Prop->getType();
989 break;
990 case HasNoExpectedAttribute:
991 case HasUnexpectedAttribute:
992 Diag << Note.AttributeName;
993 break;
994 case DifferentGetter:
995 Diag << Note.Prop->getGetterName();
996 break;
997 case DifferentSetter:
998 Diag << Note.Prop->getSetterName();
999 break;
1000 }
1001 }
1002 if (AtLoc.isValid())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001003 S.Diag(AtLoc, diag::note_property_synthesize);
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001004
1005 return Property;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001006}
1007
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001008/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001009static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1010 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001011 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
1012
1013 // If this is a readwrite property in a class extension that refines
1014 // a readonly property in the original class definition, check it as
1015 // well.
1016
1017 // If it's a readonly property, we're not interested.
1018 if (Prop->isReadOnly()) return false;
1019
1020 // Is it declared in an extension?
1021 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1022 if (!Category || !Category->IsClassExtension()) return false;
1023
1024 // Find the corresponding property in the primary class definition.
1025 auto OrigClass = Category->getClassInterface();
1026 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1027 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1028 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1029 }
1030
Douglas Gregor02535432015-12-18 00:52:31 +00001031 // Look through all of the protocols.
1032 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +00001033 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1034 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +00001035 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1036 }
1037
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001038 return false;
1039}
1040
Ted Kremenekac597f32010-03-12 00:46:40 +00001041/// ActOnPropertyImplDecl - This routine performs semantic checks and
1042/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +00001043/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +00001044///
John McCall48871652010-08-21 09:40:31 +00001045Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1046 SourceLocation AtLoc,
1047 SourceLocation PropertyLoc,
1048 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +00001049 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001050 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +00001051 SourceLocation PropertyIvarLoc,
1052 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +00001053 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +00001054 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +00001055 // Make sure we have a context for the property implementation declaration.
1056 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001057 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001058 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001059 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001060 if (PropertyIvarLoc.isInvalid())
1061 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +00001062 SourceLocation PropertyDiagLoc = PropertyLoc;
1063 if (PropertyDiagLoc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001064 PropertyDiagLoc = ClassImpDecl->getBeginLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00001065 ObjCPropertyDecl *property = nullptr;
1066 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001067 // Find the class or category class where this property must have
1068 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00001069 ObjCImplementationDecl *IC = nullptr;
1070 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001071 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1072 IDecl = IC->getClassInterface();
1073 // We always synthesize an interface for an implementation
1074 // without an interface decl. So, IDecl is always non-zero.
1075 assert(IDecl &&
1076 "ActOnPropertyImplDecl - @implementation without @interface");
1077
1078 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +00001079 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001080 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001081 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001082 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001083 }
Manman Rendfef4062016-01-29 19:16:39 +00001084 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001085 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +00001086 return nullptr;
1087 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001088 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +00001089 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
1090 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001091 if (AtLoc.isValid())
1092 Diag(AtLoc, diag::warn_implicit_atomic_property);
1093 else
1094 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1095 Diag(property->getLocation(), diag::note_property_declare);
1096 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001097
Ted Kremenekac597f32010-03-12 00:46:40 +00001098 if (const ObjCCategoryDecl *CD =
1099 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1100 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +00001101 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001102 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +00001103 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001104 }
1105 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001106 if (Synthesize&&
1107 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
1108 property->hasAttr<IBOutletAttr>() &&
1109 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001110 bool ReadWriteProperty = false;
1111 // Search into the class extensions and see if 'readonly property is
1112 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001113 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001114 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1115 if (!R.empty())
1116 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1117 PIkind = ExtProp->getPropertyAttributesAsWritten();
1118 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
1119 ReadWriteProperty = true;
1120 break;
1121 }
1122 }
1123 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001124
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001125 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +00001126 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +00001127 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001128 SourceLocation readonlyLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00001129 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001130 property->getLParenLoc(), readonlyLoc)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001131 SourceLocation endLoc =
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001132 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1133 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001134 Diag(property->getLocation(),
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001135 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1136 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1137 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +00001138 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001139 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001140 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001141 property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1142 property);
1143
Ted Kremenekac597f32010-03-12 00:46:40 +00001144 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1145 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001146 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001147 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001148 }
1149 IDecl = CatImplClass->getClassInterface();
1150 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001151 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001152 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001153 }
1154 ObjCCategoryDecl *Category =
1155 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1156
1157 // If category for this implementation not found, it is an error which
1158 // has already been reported eralier.
1159 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001160 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001161 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001162 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001163 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001164 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001165 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001166 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001167 }
1168 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001169 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001170 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001171 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001172 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001173 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001174 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001175 // Check that we have a valid, previously declared ivar for @synthesize
1176 if (Synthesize) {
1177 // @synthesize
1178 if (!PropertyIvar)
1179 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001180 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1181 ObjCInterfaceDecl *ClassDeclared;
1182 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1183 QualType PropType = property->getType();
1184 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001185
1186 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001187 diag::err_incomplete_synthesized_property,
1188 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001189 Diag(property->getLocation(), diag::note_property_declare);
1190 CompleteTypeErr = true;
1191 }
1192
David Blaikiebbafb8a2012-03-11 07:00:24 +00001193 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001194 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001195 ObjCPropertyDecl::OBJC_PR_readonly) &&
1196 PropertyIvarType->isObjCRetainableType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001197 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001198 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001199
1200 ObjCPropertyDecl::PropertyAttributeKind kind
John McCall31168b02011-06-15 23:02:42 +00001201 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001202
John McCall460ce582015-10-22 18:38:17 +00001203 bool isARCWeak = false;
1204 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1205 // Add GC __weak to the ivar type if the property is weak.
1206 if (getLangOpts().getGC() != LangOptions::NonGC) {
1207 assert(!getLangOpts().ObjCAutoRefCount);
1208 if (PropertyIvarType.isObjCGCStrong()) {
1209 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1210 Diag(property->getLocation(), diag::note_property_declare);
1211 } else {
1212 PropertyIvarType =
1213 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1214 }
1215
1216 // Otherwise, check whether ARC __weak is enabled and works with
1217 // the property type.
John McCall43192862011-09-13 18:31:23 +00001218 } else {
John McCall460ce582015-10-22 18:38:17 +00001219 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001220 // Only complain here when synthesizing an ivar.
1221 if (!Ivar) {
1222 Diag(PropertyDiagLoc,
1223 getLangOpts().ObjCWeakRuntime
1224 ? diag::err_synthesizing_arc_weak_property_disabled
1225 : diag::err_synthesizing_arc_weak_property_no_runtime);
1226 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001227 }
John McCallb61e14e2015-10-27 04:54:50 +00001228 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001229 } else {
1230 isARCWeak = true;
1231 if (const ObjCObjectPointerType *ObjT =
1232 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1233 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1234 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1235 Diag(property->getLocation(),
1236 diag::err_arc_weak_unavailable_property)
1237 << PropertyIvarType;
1238 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1239 << ClassImpDecl->getName();
1240 }
1241 }
1242 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001243 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001244 }
John McCall460ce582015-10-22 18:38:17 +00001245
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001246 if (AtLoc.isInvalid()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001247 // Check when default synthesizing a property that there is
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001248 // an ivar matching property name and issue warning; since this
1249 // is the most common case of not using an ivar used for backing
1250 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001251 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001252 ObjCIvarDecl *originalIvar =
1253 IDecl->lookupInstanceVariable(property->getIdentifier(),
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001254 ClassDeclared);
1255 if (originalIvar) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001256 Diag(PropertyDiagLoc,
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001257 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001258 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001259 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001260 Diag(property->getLocation(), diag::note_property_declare);
1261 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001262 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001263 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001264
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001265 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001266 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001267 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001268 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001269 !PropertyIvarType.getObjCLifetime() &&
1270 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001271
John McCall43192862011-09-13 18:31:23 +00001272 // It's an error if we have to do this and the user didn't
1273 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001274 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001275 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001276 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001277 diag::err_arc_objc_property_default_assign_on_object);
1278 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001279 } else {
1280 Qualifiers::ObjCLifetime lifetime =
1281 getImpliedARCOwnership(kind, PropertyIvarType);
1282 assert(lifetime && "no lifetime for property?");
Fangrui Song6907ce22018-07-30 19:24:48 +00001283
John McCall31168b02011-06-15 23:02:42 +00001284 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001285 qs.addObjCLifetime(lifetime);
Fangrui Song6907ce22018-07-30 19:24:48 +00001286 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
John McCall31168b02011-06-15 23:02:42 +00001287 }
John McCall31168b02011-06-15 23:02:42 +00001288 }
1289
Abramo Bagnaradff19302011-03-08 08:55:46 +00001290 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001291 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001292 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001293 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001294 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001295 if (RequireNonAbstractType(PropertyIvarLoc,
1296 PropertyIvarType,
1297 diag::err_abstract_type_in_decl,
1298 AbstractSynthesizedIvarType)) {
1299 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001300 // An abstract type is as bad as an incomplete type.
1301 CompleteTypeErr = true;
1302 }
Volodymyr Sapsai30680e92017-10-23 22:01:41 +00001303 if (!CompleteTypeErr) {
1304 const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
1305 if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) {
1306 Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
1307 << PropertyIvarType;
1308 CompleteTypeErr = true; // suppress later diagnostics about the ivar
1309 }
1310 }
Richard Smith81f5ade2016-12-15 02:28:18 +00001311 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001312 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001313 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001314 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001315
John McCall5fb5df92012-06-20 06:18:46 +00001316 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001317 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001318 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001319 // Note! I deliberately want it to fall thru so, we have a
1320 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001321 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001322 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001323 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001324 << property->getDeclName() << Ivar->getDeclName()
1325 << ClassDeclared->getDeclName();
1326 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001327 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001328 // Note! I deliberately want it to fall thru so more errors are caught.
1329 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001330 property->setPropertyIvarDecl(Ivar);
1331
Ted Kremenekac597f32010-03-12 00:46:40 +00001332 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1333
1334 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001335 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001336 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001337 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001338 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001339 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001340 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001341 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001342 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001343 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1344 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001345 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001346 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001347 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001348 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001349 << property->getDeclName() << PropType
1350 << Ivar->getDeclName() << IvarType;
1351 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001352 // Note! I deliberately want it to fall thru so, we have a
1353 // a property implementation and to avoid future warnings.
1354 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001355 else {
1356 // FIXME! Rules for properties are somewhat different that those
1357 // for assignments. Use a new routine to consolidate all cases;
1358 // specifically for property redeclarations as well as for ivars.
1359 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1360 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1361 if (lhsType != rhsType &&
1362 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001363 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001364 << property->getDeclName() << PropType
1365 << Ivar->getDeclName() << IvarType;
1366 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1367 // Fall thru - see previous comment
1368 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001369 }
1370 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001371 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001372 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001373 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001374 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001375 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001376 // Fall thru - see previous comment
1377 }
John McCall31168b02011-06-15 23:02:42 +00001378 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001379 if ((property->getType()->isObjCObjectPointerType() ||
1380 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001381 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001382 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001383 << property->getDeclName() << Ivar->getDeclName();
1384 // Fall thru - see previous comment
1385 }
1386 }
John McCall460ce582015-10-22 18:38:17 +00001387 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1388 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001389 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001390 } else if (PropertyIvar)
1391 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001392 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001393
Ted Kremenekac597f32010-03-12 00:46:40 +00001394 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1395 ObjCPropertyImplDecl *PIDecl =
1396 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1397 property,
1398 (Synthesize ?
1399 ObjCPropertyImplDecl::Synthesize
1400 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001401 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001402
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001403 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001404 PIDecl->setInvalidDecl();
1405
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001406 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1407 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001408 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001409 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001410 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1411 // returned by the getter as it must conform to C++'s copy-return rules.
1412 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001413 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001414 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001415 DeclRefExpr *SelfExpr = new (Context)
1416 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1417 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001418 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001419 Expr *LoadSelfExpr =
1420 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001421 CK_LValueToRValue, SelfExpr, nullptr,
1422 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001423 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001424 new (Context) ObjCIvarRefExpr(Ivar,
1425 Ivar->getUsageType(SelfDecl->getType()),
1426 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001427 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001428 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001429 ExprResult Res = PerformCopyInitialization(
1430 InitializedEntity::InitializeResult(PropertyDiagLoc,
1431 getterMethod->getReturnType(),
1432 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001433 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001434 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001435 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001436 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001437 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001438 PIDecl->setGetterCXXConstructor(ResExpr);
1439 }
1440 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001441 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1442 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001443 Diag(getterMethod->getLocation(),
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001444 diag::warn_property_getter_owning_mismatch);
1445 Diag(property->getLocation(), diag::note_property_declare);
1446 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001447 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1448 switch (getterMethod->getMethodFamily()) {
1449 case OMF_retain:
1450 case OMF_retainCount:
1451 case OMF_release:
1452 case OMF_autorelease:
1453 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1454 << 1 << getterMethod->getSelector();
1455 break;
1456 default:
1457 break;
1458 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001459 }
1460 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1461 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001462 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1463 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001464 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001465 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001466 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001467 DeclRefExpr *SelfExpr = new (Context)
1468 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1469 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001470 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001471 Expr *LoadSelfExpr =
1472 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001473 CK_LValueToRValue, SelfExpr, nullptr,
1474 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001475 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001476 new (Context) ObjCIvarRefExpr(Ivar,
1477 Ivar->getUsageType(SelfDecl->getType()),
1478 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001479 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001480 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001481 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1482 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001483 QualType T = Param->getType().getNonReferenceType();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001484 DeclRefExpr *rhs = new (Context)
1485 DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001486 MarkDeclRefReferenced(rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001487 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001488 BO_Assign, lhs, rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001489 if (property->getPropertyAttributes() &
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001490 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001491 Expr *callExpr = Res.getAs<Expr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001492 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001493 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1494 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001495 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001496 if (property->getType()->isReferenceType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001497 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001498 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001499 << property->getType();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001500 Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl)
1501 << FuncDecl;
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001502 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001503 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001504 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001505 }
1506 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001507
Ted Kremenekac597f32010-03-12 00:46:40 +00001508 if (IC) {
1509 if (Synthesize)
1510 if (ObjCPropertyImplDecl *PPIDecl =
1511 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001512 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001513 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1514 << PropertyIvar;
1515 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1516 }
1517
1518 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001519 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001520 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001521 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001522 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001523 }
1524 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001525 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001526 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001527 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001528 // Diagnose if an ivar was lazily synthesdized due to a previous
1529 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001530 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001531 ObjCInterfaceDecl *ClassDeclared=nullptr;
1532 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001533 if (!Synthesize)
1534 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1535 else {
1536 if (PropertyIvar && PropertyIvar != PropertyId)
1537 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1538 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001539 // Issue diagnostics only if Ivar belongs to current class.
Fangrui Song6907ce22018-07-30 19:24:48 +00001540 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001541 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001542 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
Fariborz Jahanian18722982010-07-17 00:59:30 +00001543 << PropertyId;
1544 Ivar->setInvalidDecl();
1545 }
1546 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001547 } else {
1548 if (Synthesize)
1549 if (ObjCPropertyImplDecl *PPIDecl =
1550 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001551 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001552 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1553 << PropertyIvar;
1554 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1555 }
1556
1557 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001558 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001559 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001560 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001561 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001562 }
1563 CatImplClass->addPropertyImplementation(PIDecl);
1564 }
1565
John McCall48871652010-08-21 09:40:31 +00001566 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001567}
1568
1569//===----------------------------------------------------------------------===//
1570// Helper methods.
1571//===----------------------------------------------------------------------===//
1572
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001573/// DiagnosePropertyMismatch - Compares two properties for their
1574/// attributes and types and warns on a variety of inconsistencies.
1575///
1576void
1577Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1578 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001579 const IdentifierInfo *inheritedName,
1580 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001581 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001582 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001583 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001584 SuperProperty->getPropertyAttributes();
Fangrui Song6907ce22018-07-30 19:24:48 +00001585
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001586 // We allow readonly properties without an explicit ownership
1587 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1588 // to be overridden by a property with any explicit ownership in the subclass.
1589 if (!OverridingProtocolProperty &&
1590 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1591 ;
1592 else {
1593 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1594 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1595 Diag(Property->getLocation(), diag::warn_readonly_property)
1596 << Property->getDeclName() << inheritedName;
1597 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1598 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001599 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001600 << Property->getDeclName() << "copy" << inheritedName;
1601 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1602 unsigned CAttrRetain =
1603 (CAttr &
1604 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1605 unsigned SAttrRetain =
1606 (SAttr &
1607 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1608 bool CStrong = (CAttrRetain != 0);
1609 bool SStrong = (SAttrRetain != 0);
1610 if (CStrong != SStrong)
1611 Diag(Property->getLocation(), diag::warn_property_attribute)
1612 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1613 }
John McCall31168b02011-06-15 23:02:42 +00001614 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001615
Douglas Gregor429183e2015-12-09 22:57:32 +00001616 // Check for nonatomic; note that nonatomic is effectively
1617 // meaningless for readonly properties, so don't diagnose if the
1618 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001619 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Alex Lorenz05a63ee2017-10-06 19:24:26 +00001620 // Readonly properties from protocols can be implemented as "readwrite"
1621 // with a custom setter name.
1622 if (Property->getSetterName() != SuperProperty->getSetterName() &&
1623 !(SuperProperty->isReadOnly() &&
1624 isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001625 Diag(Property->getLocation(), diag::warn_property_attribute)
1626 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001627 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1628 }
1629 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001630 Diag(Property->getLocation(), diag::warn_property_attribute)
1631 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001632 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1633 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001634
1635 QualType LHSType =
1636 Context.getCanonicalType(SuperProperty->getType());
1637 QualType RHSType =
1638 Context.getCanonicalType(Property->getType());
1639
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001640 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001641 // Do cases not handled in above.
1642 // FIXME. For future support of covariant property types, revisit this.
1643 bool IncompatibleObjC = false;
1644 QualType ConvertedType;
Fangrui Song6907ce22018-07-30 19:24:48 +00001645 if (!isObjCPointerConversion(RHSType, LHSType,
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001646 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001647 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001648 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1649 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001650 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1651 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001652 }
1653}
1654
1655bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1656 ObjCMethodDecl *GetterMethod,
1657 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001658 if (!GetterMethod)
1659 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001660 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001661 QualType PropertyRValueType =
1662 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1663 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001664 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001665 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1666 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001667 if ((propertyObjCPtr =
1668 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001669 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1670 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001671 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001672 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001673 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001674 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001675 << GetterMethod->getSelector() << GetterType;
1676 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1677 return true;
1678 } else {
1679 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001680 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001681 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1682 if (lhsType != rhsType && lhsType->isArithmeticType())
1683 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001684 }
1685 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001686
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001687 if (!compat) {
1688 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1689 << property->getDeclName()
1690 << GetterMethod->getSelector();
1691 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1692 return true;
1693 }
1694
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001695 return false;
1696}
1697
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001698/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001699/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001700static void
1701CollectImmediateProperties(ObjCContainerDecl *CDecl,
1702 ObjCContainerDecl::PropertyMap &PropMap,
1703 ObjCContainerDecl::PropertyMap &SuperPropMap,
1704 bool CollectClassPropsOnly = false,
1705 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001706 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001707 for (auto *Prop : IDecl->properties()) {
1708 if (CollectClassPropsOnly && !Prop->isClassProperty())
1709 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001710 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1711 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001712 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001713
1714 // Collect the properties from visible extensions.
1715 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001716 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1717 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001718
Ted Kremenek204c3c52014-02-22 00:02:03 +00001719 if (IncludeProtocols) {
1720 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001721 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001722 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1723 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001724 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001725 }
1726 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001727 for (auto *Prop : CATDecl->properties()) {
1728 if (CollectClassPropsOnly && !Prop->isClassProperty())
1729 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001730 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1731 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001732 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001733 if (IncludeProtocols) {
1734 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001735 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001736 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1737 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001738 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001739 }
1740 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001741 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001742 if (CollectClassPropsOnly && !Prop->isClassProperty())
1743 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001744 ObjCPropertyDecl *PropertyFromSuper =
1745 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1746 Prop->isClassProperty())];
Fangrui Song6907ce22018-07-30 19:24:48 +00001747 // Exclude property for protocols which conform to class's super-class,
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001748 // as super-class has to implement the property.
Fangrui Song6907ce22018-07-30 19:24:48 +00001749 if (!PropertyFromSuper ||
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001750 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001751 ObjCPropertyDecl *&PropEntry =
1752 PropMap[std::make_pair(Prop->getIdentifier(),
1753 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001754 if (!PropEntry)
1755 PropEntry = Prop;
1756 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001757 }
Manman Ren16a7d632016-04-12 23:01:55 +00001758 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001759 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001760 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1761 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001762 }
1763}
1764
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001765/// CollectSuperClassPropertyImplementations - This routine collects list of
1766/// properties to be implemented in super class(s) and also coming from their
1767/// conforming protocols.
1768static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001769 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001770 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001771 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001772 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001773 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001774 SDecl = SDecl->getSuperClass();
1775 }
1776 }
1777}
1778
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001779/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1780/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1781/// declared in class 'IFace'.
1782bool
1783Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1784 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1785 if (!IV->getSynthesize())
1786 return false;
1787 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1788 Method->isInstanceMethod());
1789 if (!IMD || !IMD->isPropertyAccessor())
1790 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001791
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001792 // look up a property declaration whose one of its accessors is implemented
1793 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001794 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001795 if ((Property->getGetterName() == IMD->getSelector() ||
1796 Property->getSetterName() == IMD->getSelector()) &&
1797 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001798 return true;
1799 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001800 // Also look up property declaration in class extension whose one of its
1801 // accessors is implemented by this method.
1802 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001803 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001804 if ((Property->getGetterName() == IMD->getSelector() ||
1805 Property->getSetterName() == IMD->getSelector()) &&
1806 (Property->getPropertyIvarDecl() == IV))
1807 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001808 return false;
1809}
1810
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001811static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1812 ObjCPropertyDecl *Prop) {
1813 bool SuperClassImplementsGetter = false;
1814 bool SuperClassImplementsSetter = false;
1815 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1816 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001817
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001818 while (IDecl->getSuperClass()) {
1819 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1820 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1821 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001822
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001823 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1824 SuperClassImplementsSetter = true;
1825 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1826 return true;
1827 IDecl = IDecl->getSuperClass();
1828 }
1829 return false;
1830}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001831
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001832/// Default synthesizes all properties which must be synthesized
James Dennett2a4d13c2012-06-15 07:13:21 +00001833/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001834void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1835 ObjCInterfaceDecl *IDecl,
1836 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001837 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001838 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1839 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001840 if (PropMap.empty())
1841 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001842 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001843 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001844
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001845 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1846 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001847 // Is there a matching property synthesize/dynamic?
1848 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001849 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001850 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1851 continue;
1852 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001853 if (IMPDecl->FindPropertyImplDecl(
1854 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001855 continue;
1856 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1857 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1858 continue;
1859 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1860 continue;
1861 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001862 if (ObjCPropertyImplDecl *PID =
1863 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001864 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1865 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001866 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001867 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001868 continue;
1869 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001870 ObjCPropertyDecl *PropInSuperClass =
1871 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1872 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001873 if (ObjCProtocolDecl *Proto =
1874 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001875 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001876 // Suppress the warning if class's superclass implements property's
1877 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001878 // Or, if property is going to be implemented in its super class.
1879 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001880 Diag(IMPDecl->getLocation(),
1881 diag::warn_auto_synthesizing_protocol_property)
1882 << Prop << Proto;
1883 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001884 std::string FixIt =
1885 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1886 Diag(AtEnd, diag::note_add_synthesize_directive)
1887 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001888 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001889 continue;
1890 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001891 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001892 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001893 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1894 (PropInSuperClass->getPropertyAttributes() &
1895 ObjCPropertyDecl::OBJC_PR_readonly) &&
1896 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1897 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1898 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1899 << Prop->getIdentifier();
1900 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1901 }
1902 else {
1903 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1904 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001905 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001906 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1907 }
1908 continue;
1909 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001910 // We use invalid SourceLocations for the synthesized ivars since they
1911 // aren't really synthesized at a particular location; they just exist.
1912 // Saying that they are located at the @implementation isn't really going
1913 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001914 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1915 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1916 true,
1917 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001918 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001919 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001920 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001921 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001922 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001923 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001924 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001925}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001926
Alex Lorenz6c9af502017-07-03 10:12:24 +00001927void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1928 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00001929 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001930 return;
1931 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1932 if (!IC)
1933 return;
1934 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001935 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00001936 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001937}
1938
Manman Ren08ce7342016-05-18 18:12:34 +00001939static void DiagnoseUnimplementedAccessor(
1940 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
1941 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
1942 ObjCPropertyDecl *Prop,
1943 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
1944 // Check to see if we have a corresponding selector in SMap and with the
1945 // right method type.
1946 auto I = std::find_if(SMap.begin(), SMap.end(),
1947 [&](const ObjCMethodDecl *x) {
1948 return x->getSelector() == Method &&
1949 x->isClassMethod() == Prop->isClassProperty();
1950 });
Ted Kremenek7e812952014-02-21 19:41:30 +00001951 // When reporting on missing property setter/getter implementation in
1952 // categories, do not report when they are declared in primary class,
1953 // class's protocol, or one of it super classes. This is because,
1954 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00001955 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001956 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001957 !PrimaryClass->lookupPropertyAccessor(Method, C,
1958 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00001959 unsigned diag =
1960 isa<ObjCCategoryDecl>(CDecl)
1961 ? (Prop->isClassProperty()
1962 ? diag::warn_impl_required_in_category_for_class_property
1963 : diag::warn_setter_getter_impl_required_in_category)
1964 : (Prop->isClassProperty()
1965 ? diag::warn_impl_required_for_class_property
1966 : diag::warn_setter_getter_impl_required);
1967 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
1968 S.Diag(Prop->getLocation(), diag::note_property_declare);
1969 if (S.LangOpts.ObjCDefaultSynthProperties &&
1970 S.LangOpts.ObjCRuntime.isNonFragile())
1971 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1972 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1973 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1974 }
Ted Kremenek7e812952014-02-21 19:41:30 +00001975}
1976
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001977void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001978 ObjCContainerDecl *CDecl,
1979 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001980 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001981 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1982
Manman Ren16a7d632016-04-12 23:01:55 +00001983 // Since we don't synthesize class properties, we should emit diagnose even
1984 // if SynthesizeProperties is true.
1985 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1986 // Gather properties which need not be implemented in this class
1987 // or category.
1988 if (!IDecl)
1989 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1990 // For categories, no need to implement properties declared in
1991 // its primary class (and its super classes) if property is
1992 // declared in one of those containers.
1993 if ((IDecl = C->getClassInterface())) {
1994 ObjCInterfaceDecl::PropertyDeclOrder PO;
1995 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001996 }
Manman Ren16a7d632016-04-12 23:01:55 +00001997 }
1998 if (IDecl)
1999 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00002000
Manman Ren16a7d632016-04-12 23:01:55 +00002001 // When SynthesizeProperties is true, we only check class properties.
2002 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2003 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002004
Ted Kremenek38882022014-02-21 19:41:39 +00002005 // Scan the @interface to see if any of the protocols it adopts
2006 // require an explicit implementation, via attribute
2007 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00002008 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00002009 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002010
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002011 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00002012 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2013 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002014 // Lazily construct a set of all the properties in the @interface
2015 // of the class, without looking at the superclass. We cannot
2016 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00002017 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00002018 // as scans the adopted protocols. This work only triggers for protocols
2019 // with the attribute, which is very rare, and only occurs when
2020 // analyzing the @implementation.
2021 if (!LazyMap) {
2022 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2023 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2024 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002025 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002026 /* IncludeProtocols */ false);
2027 }
Ted Kremenek38882022014-02-21 19:41:39 +00002028 // Add the properties of 'PDecl' to the list of properties that
2029 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002030 for (auto *PropDecl : PDecl->properties()) {
2031 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2032 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002033 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002034 PropMap[std::make_pair(PropDecl->getIdentifier(),
2035 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002036 }
2037 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002038 }
Ted Kremenek38882022014-02-21 19:41:39 +00002039
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002040 if (PropMap.empty())
2041 return;
2042
2043 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002044 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002045 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002046
Manman Ren08ce7342016-05-18 18:12:34 +00002047 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002048 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002049 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002050 InsMap.insert(I);
Fangrui Song6907ce22018-07-30 19:24:48 +00002051
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002052 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002053 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002054 if (C && !C->IsClassExtension())
2055 if ((PrimaryClass = C->getClassInterface()))
2056 // Report unimplemented properties in the category as well.
2057 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2058 // When reporting on missing setter/getters, do not report when
2059 // setter/getter is implemented in category's primary class
2060 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002061 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002062 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002063 }
2064
Anna Zaks673d76b2012-10-18 19:17:53 +00002065 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002066 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2067 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002068 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002069 if (Prop->isInvalidDecl() ||
2070 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002071 PropImplMap.count(Prop) ||
2072 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002073 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002074
2075 // Diagnose unimplemented getters and setters.
2076 DiagnoseUnimplementedAccessor(*this,
2077 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2078 if (!Prop->isReadOnly())
2079 DiagnoseUnimplementedAccessor(*this,
2080 PrimaryClass, Prop->getSetterName(),
2081 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002082 }
2083}
2084
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002085void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002086 for (const auto *propertyImpl : impDecl->property_impls()) {
2087 const auto *property = propertyImpl->getPropertyDecl();
2088
2089 // Warn about null_resettable properties with synthesized setters,
2090 // because the setter won't properly handle nil.
2091 if (propertyImpl->getPropertyImplementation()
2092 == ObjCPropertyImplDecl::Synthesize &&
2093 (property->getPropertyAttributes() &
2094 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2095 property->getGetterMethodDecl() &&
2096 property->getSetterMethodDecl()) {
2097 auto *getterMethod = property->getGetterMethodDecl();
2098 auto *setterMethod = property->getSetterMethodDecl();
2099 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
2100 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
2101 SourceLocation loc = propertyImpl->getLocation();
2102 if (loc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002103 loc = impDecl->getBeginLoc();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002104
2105 Diag(loc, diag::warn_null_resettable_setter)
2106 << setterMethod->getSelector() << property->getDeclName();
2107 }
2108 }
2109 }
2110}
2111
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002112void
2113Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002114 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002115 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002116 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002117 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002118 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002119 for (auto *Prop : IDecl->properties())
2120 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002121 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002122 for (auto *Prop : Ext->properties())
2123 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fangrui Song6907ce22018-07-30 19:24:48 +00002124
Manman Renefe1bac2016-01-27 20:00:32 +00002125 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2126 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002127 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002128 ObjCMethodDecl *GetterMethod = nullptr;
2129 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002130 bool LookedUpGetterSetter = false;
2131
Bill Wendling44426052012-12-20 19:22:21 +00002132 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002133 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002134
John McCall43192862011-09-13 18:31:23 +00002135 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2136 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002137 GetterMethod = Property->isClassProperty() ?
2138 IMPDecl->getClassMethod(Property->getGetterName()) :
2139 IMPDecl->getInstanceMethod(Property->getGetterName());
2140 SetterMethod = Property->isClassProperty() ?
2141 IMPDecl->getClassMethod(Property->getSetterName()) :
2142 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002143 LookedUpGetterSetter = true;
2144 if (GetterMethod) {
2145 Diag(GetterMethod->getLocation(),
2146 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002147 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002148 Diag(Property->getLocation(), diag::note_property_declare);
2149 }
2150 if (SetterMethod) {
2151 Diag(SetterMethod->getLocation(),
2152 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002153 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002154 Diag(Property->getLocation(), diag::note_property_declare);
2155 }
2156 }
2157
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002158 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00002159 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2160 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002161 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002162 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2163 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002164 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2165 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002166 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00002167 GetterMethod = Property->isClassProperty() ?
2168 IMPDecl->getClassMethod(Property->getGetterName()) :
2169 IMPDecl->getInstanceMethod(Property->getGetterName());
2170 SetterMethod = Property->isClassProperty() ?
2171 IMPDecl->getClassMethod(Property->getSetterName()) :
2172 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002173 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002174 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
2175 SourceLocation MethodLoc =
2176 (GetterMethod ? GetterMethod->getLocation()
2177 : SetterMethod->getLocation());
2178 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002179 << Property->getIdentifier() << (GetterMethod != nullptr)
2180 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002181 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002182 if (Property->getLParenLoc().isValid() &&
2183 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002184 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002185 SourceLocation AfterLParen =
2186 getLocForEndOfToken(Property->getLParenLoc());
2187 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2188 : "nonatomic";
2189 Diag(Property->getLocation(),
2190 diag::note_atomic_property_fixup_suggest)
2191 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2192 } else if (Property->getLParenLoc().isInvalid()) {
2193 //@property id etc.
Fangrui Song6907ce22018-07-30 19:24:48 +00002194 SourceLocation startLoc =
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002195 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2196 Diag(Property->getLocation(),
2197 diag::note_atomic_property_fixup_suggest)
2198 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002199 }
2200 else
2201 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002202 Diag(Property->getLocation(), diag::note_property_declare);
2203 }
2204 }
2205 }
2206}
2207
John McCall31168b02011-06-15 23:02:42 +00002208void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002209 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002210 return;
2211
Aaron Ballmand85eff42014-03-14 15:02:45 +00002212 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002213 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002214 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002215 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002216 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002217 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2218 if (!method)
2219 continue;
2220 ObjCMethodFamily family = method->getMethodFamily();
2221 if (family == OMF_alloc || family == OMF_copy ||
2222 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002223 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002224 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002225 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002226 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002227
2228 // Look for a getter explicitly declared alongside the property.
2229 // If we find one, use its location for the note.
2230 SourceLocation noteLoc = PD->getLocation();
2231 SourceLocation fixItLoc;
2232 for (auto *getterRedecl : method->redecls()) {
2233 if (getterRedecl->isImplicit())
2234 continue;
2235 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2236 continue;
2237 noteLoc = getterRedecl->getLocation();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002238 fixItLoc = getterRedecl->getEndLoc();
Jordan Rosea34d04d2015-01-16 23:04:31 +00002239 }
2240
2241 Preprocessor &PP = getPreprocessor();
2242 TokenValue tokens[] = {
2243 tok::kw___attribute, tok::l_paren, tok::l_paren,
2244 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2245 PP.getIdentifierInfo("none"), tok::r_paren,
2246 tok::r_paren, tok::r_paren
2247 };
2248 StringRef spelling = "__attribute__((objc_method_family(none)))";
2249 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2250 if (!macroName.empty())
2251 spelling = macroName;
2252
2253 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2254 << method->getDeclName() << spelling;
2255 if (fixItLoc.isValid()) {
2256 SmallString<64> fixItText(" ");
2257 fixItText += spelling;
2258 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2259 }
John McCall31168b02011-06-15 23:02:42 +00002260 }
2261 }
2262 }
2263}
2264
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002265void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002266 const ObjCImplementationDecl *ImplD,
2267 const ObjCInterfaceDecl *IFD) {
2268 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002269 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2270 if (!SuperD)
2271 return;
2272
2273 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002274 for (const auto *I : ImplD->instance_methods())
2275 if (I->getMethodFamily() == OMF_init)
2276 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002277
2278 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2279 SuperD->getDesignatedInitializers(DesignatedInits);
2280 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2281 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2282 const ObjCMethodDecl *MD = *I;
2283 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002284 bool Ignore = false;
2285 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2286 Ignore = IMD->isUnavailable();
2287 }
2288 if (!Ignore) {
2289 Diag(ImplD->getLocation(),
2290 diag::warn_objc_implementation_missing_designated_init_override)
2291 << MD->getSelector();
2292 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2293 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002294 }
2295 }
2296}
2297
John McCallad31b5f2010-11-10 07:01:40 +00002298/// AddPropertyAttrs - Propagates attributes from a property to the
2299/// implicitly-declared getter or setter for that property.
2300static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2301 ObjCPropertyDecl *Property) {
2302 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002303 for (const auto *A : Property->attrs()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002304 if (isa<DeprecatedAttr>(A) ||
2305 isa<UnavailableAttr>(A) ||
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002306 isa<AvailabilityAttr>(A))
2307 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002308 }
John McCallad31b5f2010-11-10 07:01:40 +00002309}
2310
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002311/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2312/// have the property type and issue diagnostics if they don't.
2313/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002314/// appropriate lookup tables.
2315void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002316 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002317 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002318 if (CD->isInvalidDecl())
2319 return;
2320
Manman Rend36f7d52016-01-27 20:10:32 +00002321 bool IsClassProperty = property->isClassProperty();
2322 GetterMethod = IsClassProperty ?
2323 CD->getClassMethod(property->getGetterName()) :
2324 CD->getInstanceMethod(property->getGetterName());
2325
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002326 // if setter or getter is not found in class extension, it might be
2327 // in the primary class.
2328 if (!GetterMethod)
2329 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2330 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002331 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2332 getClassMethod(property->getGetterName()) :
2333 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002334 getInstanceMethod(property->getGetterName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002335
Manman Rend36f7d52016-01-27 20:10:32 +00002336 SetterMethod = IsClassProperty ?
2337 CD->getClassMethod(property->getSetterName()) :
2338 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002339 if (!SetterMethod)
2340 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2341 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002342 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2343 getClassMethod(property->getSetterName()) :
2344 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002345 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002346 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2347 property->getLocation());
2348
Alex Lorenz535571a2017-03-30 13:33:51 +00002349 if (!property->isReadOnly() && SetterMethod) {
2350 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2351 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002352 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2353 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002354 !Context.hasSameUnqualifiedType(
Fangrui Song6907ce22018-07-30 19:24:48 +00002355 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002356 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002357 Diag(property->getLocation(),
2358 diag::warn_accessor_property_type_mismatch)
2359 << property->getDeclName()
2360 << SetterMethod->getSelector();
2361 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2362 }
2363 }
2364
2365 // Synthesize getter/setter methods if none exist.
2366 // Find the default getter and if one not found, add one.
2367 // FIXME: The synthesized property we set here is misleading. We almost always
2368 // synthesize these methods unless the user explicitly provided prototypes
2369 // (which is odd, but allowed). Sema should be typechecking that the
2370 // declarations jive in that situation (which it is not currently).
2371 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002372 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002373 // Declare a getter method and add it to the list of methods
2374 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002375 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002376
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002377 // The getter returns the declared property type with all qualifiers
2378 // removed.
2379 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2380
Douglas Gregor849ebc22015-06-19 18:14:46 +00002381 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002382 if (property->getPropertyAttributes() &
2383 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2384 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002385 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002386 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002387 resultTy = Context.getAttributedType(attr::TypeNonNull,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002388 modifiedTy, modifiedTy);
2389 }
2390 }
2391
Ted Kremenek2f075632010-09-21 20:52:59 +00002392 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2393 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002394 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002395 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002396 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002397 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002398 (property->getPropertyImplementation() ==
2399 ObjCPropertyDecl::Optional) ?
2400 ObjCMethodDecl::Optional :
2401 ObjCMethodDecl::Required);
2402 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002403
2404 AddPropertyAttrs(*this, GetterMethod, property);
2405
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002406 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002407 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2408 Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002409
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002410 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2411 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002412 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002413
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002414 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002415 GetterMethod->addAttr(
2416 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2417 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002418
2419 if (getLangOpts().ObjCAutoRefCount)
2420 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002421 } else
2422 // A user declared getter will be synthesize when @synthesize of
2423 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002424 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002425 property->setGetterMethodDecl(GetterMethod);
2426
2427 // Skip setter if property is read-only.
2428 if (!property->isReadOnly()) {
2429 // Find the default setter and if one not found, add one.
2430 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002431 // No instance/class method of same name as property setter name was
2432 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002433 // Declare a setter method and add it to the list of methods
2434 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002435 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002436
2437 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002438 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002439 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002440 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002441 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002442 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002443 /*isImplicitlyDeclared=*/true,
2444 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002445 (property->getPropertyImplementation() ==
2446 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002447 ObjCMethodDecl::Optional :
2448 ObjCMethodDecl::Required);
2449
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002450 // Remove all qualifiers from the setter's parameter type.
2451 QualType paramTy =
2452 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2453
Douglas Gregor849ebc22015-06-19 18:14:46 +00002454 // If the property is null_resettable, the setter accepts a
2455 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002456 if (property->getPropertyAttributes() &
2457 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2458 QualType modifiedTy = paramTy;
2459 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2460 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002461 paramTy = Context.getAttributedType(attr::TypeNullable,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002462 modifiedTy, modifiedTy);
2463 }
2464 }
2465
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002466 // Invent the arguments for the setter. We don't bother making a
2467 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002468 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2469 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002470 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002471 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002472 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002473 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002474 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002475 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002476
2477 AddPropertyAttrs(*this, SetterMethod, property);
2478
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002479 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002480 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002481 SetterMethod->addAttr(
2482 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2483 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002484 // It's possible for the user to have set a very odd custom
2485 // setter selector that causes it to have a method family.
2486 if (getLangOpts().ObjCAutoRefCount)
2487 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002488 } else
2489 // A user declared setter will be synthesize when @synthesize of
2490 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002491 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002492 property->setSetterMethodDecl(SetterMethod);
2493 }
2494 // Add any synthesized methods to the global pool. This allows us to
2495 // handle the following, which is supported by GCC (and part of the design).
2496 //
2497 // @interface Foo
2498 // @property double bar;
2499 // @end
2500 //
2501 // void thisIsUnfortunate() {
2502 // id foo;
2503 // double bar = [foo bar];
2504 // }
2505 //
Manman Rend36f7d52016-01-27 20:10:32 +00002506 if (!IsClassProperty) {
2507 if (GetterMethod)
2508 AddInstanceMethodToGlobalPool(GetterMethod);
2509 if (SetterMethod)
2510 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002511 } else {
2512 if (GetterMethod)
2513 AddFactoryMethodToGlobalPool(GetterMethod);
2514 if (SetterMethod)
2515 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002516 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002517
2518 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2519 if (!CurrentClass) {
2520 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2521 CurrentClass = Cat->getClassInterface();
2522 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2523 CurrentClass = Impl->getClassInterface();
2524 }
2525 if (GetterMethod)
2526 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2527 if (SetterMethod)
2528 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002529}
2530
John McCall48871652010-08-21 09:40:31 +00002531void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002532 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002533 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002534 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002535 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002536 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002537 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00002538
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002539 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2540 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2541 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2542 << "readonly" << "readwrite";
Fangrui Song6907ce22018-07-30 19:24:48 +00002543
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002544 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2545 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002546
2547 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002548 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002549 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2550 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002551 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002552 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002553 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2554 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2555 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002556 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002557 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002558 }
2559
John McCall52a503d2018-09-05 19:02:00 +00002560 // Check for assign on object types.
2561 if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2562 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
2563 PropertyTy->isObjCRetainableType() &&
2564 !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2565 Diag(Loc, diag::warn_objc_property_assign_on_object);
2566 }
2567
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002568 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002569 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2570 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002571 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2572 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002573 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002574 }
Bill Wendling44426052012-12-20 19:22:21 +00002575 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002576 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2577 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002578 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002579 }
Bill Wendling44426052012-12-20 19:22:21 +00002580 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002581 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2582 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002583 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002584 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002585 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002586 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002587 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2588 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002589 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002590 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002591 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002592 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002593 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2594 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002595 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2596 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002597 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002598 }
Bill Wendling44426052012-12-20 19:22:21 +00002599 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002600 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2601 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002602 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002603 }
Bill Wendling44426052012-12-20 19:22:21 +00002604 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002605 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2606 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002607 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002608 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002609 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002610 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002611 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2612 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002613 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002614 }
Bill Wendling44426052012-12-20 19:22:21 +00002615 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2616 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002617 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2618 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002619 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002620 }
Bill Wendling44426052012-12-20 19:22:21 +00002621 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002622 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2623 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002624 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002625 }
Bill Wendling44426052012-12-20 19:22:21 +00002626 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002627 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2628 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002629 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002630 }
2631 }
Bill Wendling44426052012-12-20 19:22:21 +00002632 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2633 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002634 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2635 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002636 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002637 }
Bill Wendling44426052012-12-20 19:22:21 +00002638 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2639 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002640 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2641 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002642 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002643 }
2644
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002645 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002646 // 'weak' and 'nonnull' are mutually exclusive.
2647 if (auto nullability = PropertyTy->getNullability(Context)) {
2648 if (*nullability == NullabilityKind::NonNull)
2649 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2650 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002651 }
2652 }
2653
Bill Wendling44426052012-12-20 19:22:21 +00002654 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2655 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002656 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2657 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002658 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002659 }
2660
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002661 // Warn if user supplied no assignment attribute, property is
2662 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002663 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2664 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2665 // do nothing
2666 } else if (getLangOpts().ObjCAutoRefCount) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002667 // With arc, @property definitions should default to strong when
John McCallb61e14e2015-10-27 04:54:50 +00002668 // not specified.
2669 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2670 } else if (PropertyTy->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002671 bool isAnyClassTy =
2672 (PropertyTy->isObjCClassType() ||
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002673 PropertyTy->isObjCQualifiedClassType());
2674 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2675 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002676 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002677 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002678 else if (propertyInPrimaryClass) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002679 // Don't issue warning on property with no life time in class
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002680 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002681 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002682 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002683 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002684
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002685 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002686 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002687 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002688 }
John McCallb61e14e2015-10-27 04:54:50 +00002689 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002690
2691 // FIXME: Implement warning dependent on NSCopying being
2692 // implemented. See also:
2693 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2694 // (please trim this list while you are at it).
2695 }
2696
Bill Wendling44426052012-12-20 19:22:21 +00002697 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2698 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002699 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002700 && PropertyTy->isBlockPointerType())
2701 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002702 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2703 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2704 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002705 PropertyTy->isBlockPointerType())
2706 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fangrui Song6907ce22018-07-30 19:24:48 +00002707
Bill Wendling44426052012-12-20 19:22:21 +00002708 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2709 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002710 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002711}