blob: 87f7baaf568f2e16ba6d02c75de40f7ea9a20b0d [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek7a7a0802010-03-12 00:38:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis for Objective C @property and
10// @synthesize declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000015#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000019#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000025
26using namespace clang;
27
Ted Kremenekac597f32010-03-12 00:46:40 +000028//===----------------------------------------------------------------------===//
29// Grammar actions.
30//===----------------------------------------------------------------------===//
31
John McCall43192862011-09-13 18:31:23 +000032/// getImpliedARCOwnership - Given a set of property attributes and a
33/// type, infer an expected lifetime. The type's ownership qualification
34/// is not considered.
35///
36/// Returns OCL_None if the attributes as stated do not imply an ownership.
37/// Never returns OCL_Autoreleasing.
38static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
41 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
43 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000046 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
48 return Qualifiers::OCL_Weak;
49 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
50 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
55 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
56 type->isObjCRetainableType()) {
57 return Qualifiers::OCL_ExplicitNone;
58 }
59
60 return Qualifiers::OCL_None;
61}
62
John McCallb61e14e2015-10-27 04:54:50 +000063/// Check the internal consistency of a property declaration with
64/// an explicit ownership qualifier.
65static void checkPropertyDeclWithOwnership(Sema &S,
66 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000067 if (property->isInvalidDecl()) return;
68
69 ObjCPropertyDecl::PropertyAttributeKind propertyKind
70 = property->getPropertyAttributes();
71 Qualifiers::ObjCLifetime propertyLifetime
72 = property->getType().getObjCLifetime();
73
John McCallb61e14e2015-10-27 04:54:50 +000074 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000075
John McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000106/// Check this Objective-C property against a property declared in the
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// given protocol.
108static void
109CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
110 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000111 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000112 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000113 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000114 return;
115
116 // Look for a property with the same name.
117 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
118 for (unsigned I = 0, N = R.size(); I != N; ++I) {
119 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000120 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000121 return;
122 }
123 }
124
125 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000126 for (auto *P : Proto->protocols())
127 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000128}
129
John McCallb61e14e2015-10-27 04:54:50 +0000130static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
131 // In GC mode, just look for the __weak qualifier.
132 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
133 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
134
135 // In ARC/MRC, look for an explicit ownership qualifier.
136 // For some reason, this only applies to __weak.
137 } else if (auto ownership = T.getObjCLifetime()) {
138 switch (ownership) {
139 case Qualifiers::OCL_Weak:
140 return ObjCDeclSpec::DQ_PR_weak;
141 case Qualifiers::OCL_Strong:
142 return ObjCDeclSpec::DQ_PR_strong;
143 case Qualifiers::OCL_ExplicitNone:
144 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
145 case Qualifiers::OCL_Autoreleasing:
146 case Qualifiers::OCL_None:
147 return 0;
148 }
149 llvm_unreachable("bad qualifier");
150 }
151
152 return 0;
153}
154
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000155static const unsigned OwnershipMask =
156 (ObjCPropertyDecl::OBJC_PR_assign |
157 ObjCPropertyDecl::OBJC_PR_retain |
158 ObjCPropertyDecl::OBJC_PR_copy |
159 ObjCPropertyDecl::OBJC_PR_weak |
160 ObjCPropertyDecl::OBJC_PR_strong |
161 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
162
John McCallb61e14e2015-10-27 04:54:50 +0000163static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000164 unsigned result = attr & OwnershipMask;
165
166 // From an ownership perspective, assign and unsafe_unretained are
167 // identical; make sure one also implies the other.
168 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
169 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
170 result |= ObjCPropertyDecl::OBJC_PR_assign |
171 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
172 }
173
174 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000175}
176
John McCall48871652010-08-21 09:40:31 +0000177Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000178 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000179 FieldDeclarator &FD,
180 ObjCDeclSpec &ODS,
181 Selector GetterSel,
182 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000183 tok::ObjCKeywordKind MethodImplKind,
184 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000185 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000186 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000187 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
188 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000189 if (!getOwnershipRule(Attributes)) {
190 Attributes |= deducePropertyOwnershipFromType(*this, T);
191 }
Bill Wendling44426052012-12-20 19:22:21 +0000192 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000193 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000194 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000195
Douglas Gregor90d34422013-01-21 19:05:22 +0000196 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000197 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000198 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000199 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000200 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000202 FD,
203 GetterSel, ODS.getGetterNameLoc(),
204 SetterSel, ODS.getSetterNameLoc(),
205 isReadWrite, Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000206 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000207 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000208 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000209 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000210 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000211 }
212
213 if (!Res) {
214 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000215 GetterSel, ODS.getGetterNameLoc(), SetterSel,
216 ODS.getSetterNameLoc(), isReadWrite, Attributes,
217 ODS.getPropertyAttributes(), T, TSI,
218 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000219 if (lexicalDC)
220 Res->setLexicalDeclContext(lexicalDC);
221 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000222
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000223 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000224 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000225 (isa<ObjCInterfaceDecl>(ClassDecl) ||
226 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000227
John McCallb61e14e2015-10-27 04:54:50 +0000228 // Check consistency if the type has explicit ownership qualification.
229 if (Res->getType().getObjCLifetime())
230 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000231
Douglas Gregorb8982092013-01-21 19:42:21 +0000232 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000233 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000234 // For a class, compare the property against a property in our superclass.
235 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000236 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
237 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000238 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000239 for (unsigned I = 0, N = R.size(); I != N; ++I) {
240 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000241 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000242 FoundInSuper = true;
243 break;
244 }
245 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000246 if (FoundInSuper)
247 break;
248 else
249 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000250 }
251
252 if (FoundInSuper) {
253 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000254 for (auto *P : CurrentInterfaceDecl->protocols()) {
255 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000256 }
257 } else {
258 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000259 for (auto *P : IFace->all_referenced_protocols()) {
260 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000261 }
262 }
263 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000264 // We don't check if class extension. Because properties in class extension
265 // are meant to override some of the attributes and checking has already done
266 // when property in class extension is constructed.
267 if (!Cat->IsClassExtension())
268 for (auto *P : Cat->protocols())
269 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000270 } else {
271 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000272 for (auto *P : Proto->protocols())
273 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000274 }
275
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000276 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000277 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000278}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000279
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000280static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000281makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000282 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000283 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000284 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000285 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000286 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000287 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000288 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000289 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000290 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000291 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000292 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000293 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000294 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000295 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000296 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000297 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000298 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000299 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000300 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000301 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000302 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000303 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000304 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000305 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000306 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000307 if (Attributes & ObjCDeclSpec::DQ_PR_class)
308 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800309 if (Attributes & ObjCDeclSpec::DQ_PR_direct)
310 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_direct;
Fangrui Song6907ce22018-07-30 19:24:48 +0000311
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000312 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
313}
314
Fangrui Song6907ce22018-07-30 19:24:48 +0000315static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000316 SourceLocation LParenLoc, SourceLocation &Loc) {
317 if (LParenLoc.isMacroID())
318 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000319
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000320 SourceManager &SM = Context.getSourceManager();
321 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
322 // Try to load the file buffer.
323 bool invalidTemp = false;
324 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
325 if (invalidTemp)
326 return false;
327 const char *tokenBegin = file.data() + locInfo.second;
Fangrui Song6907ce22018-07-30 19:24:48 +0000328
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000329 // Lex from the start of the given location.
330 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
331 Context.getLangOpts(),
332 file.begin(), tokenBegin, file.end());
333 Token Tok;
334 do {
335 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000336 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000337 Loc = Tok.getLocation();
338 return true;
339 }
340 } while (Tok.isNot(tok::r_paren));
341 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000342}
343
Douglas Gregor429183e2015-12-09 22:57:32 +0000344/// Check for a mismatch in the atomicity of the given properties.
345static void checkAtomicPropertyMismatch(Sema &S,
346 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000347 ObjCPropertyDecl *NewProperty,
348 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000349 // If the atomicity of both matches, we're done.
350 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000351 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000353 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000354 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
355 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000356 if (OldIsAtomic == NewIsAtomic) return;
357
358 // Determine whether the given property is readonly and implicitly
359 // atomic.
360 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
361 // Is it readonly?
362 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000363 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000364
365 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000366 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000367
368 // Was 'atomic' specified directly?
Fangrui Song6907ce22018-07-30 19:24:48 +0000369 if (Property->getPropertyAttributesAsWritten() &
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000370 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000371 return false;
372
373 return true;
374 };
375
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000376 // If we're allowed to propagate atomicity, and the new property did
377 // not specify atomicity at all, propagate.
378 const unsigned AtomicityMask =
379 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
380 if (PropagateAtomicity &&
381 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
382 unsigned Attrs = NewProperty->getPropertyAttributes();
383 Attrs = Attrs & ~AtomicityMask;
384 if (OldIsAtomic)
385 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
Fangrui Song6907ce22018-07-30 19:24:48 +0000386 else
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000387 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
388
389 NewProperty->overwritePropertyAttributes(Attrs);
390 return;
391 }
392
Douglas Gregor429183e2015-12-09 22:57:32 +0000393 // One of the properties is atomic; if it's a readonly property, and
394 // 'atomic' wasn't explicitly specified, we're okay.
395 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
396 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
397 return;
398
399 // Diagnose the conflict.
400 const IdentifierInfo *OldContextName;
401 auto *OldDC = OldProperty->getDeclContext();
402 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
403 OldContextName = Category->getClassInterface()->getIdentifier();
404 else
405 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
406
407 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
408 << NewProperty->getDeclName() << "atomic"
409 << OldContextName;
410 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
411}
412
Douglas Gregor90d34422013-01-21 19:05:22 +0000413ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000414Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000415 SourceLocation AtLoc,
416 SourceLocation LParenLoc,
417 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000418 Selector GetterSel,
419 SourceLocation GetterNameLoc,
420 Selector SetterSel,
421 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000423 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000424 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000425 QualType T,
426 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000428 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000429 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000430 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000431 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000432 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
Fangrui Song6907ce22018-07-30 19:24:48 +0000433
Ted Kremenek959e8302010-03-12 02:31:10 +0000434 // We need to look in the @interface to see if the @property was
435 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000436 if (!CCPrimary) {
437 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000438 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000439 }
440
Manman Ren5b786402016-01-28 18:49:28 +0000441 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
442 (Attributes & ObjCDeclSpec::DQ_PR_class);
443
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000444 // Find the property in the extended class's primary class or
445 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000446 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
447 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000448
Fangrui Song6907ce22018-07-30 19:24:48 +0000449 // If we found a property in an extension, complain.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000450 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
451 Diag(AtLoc, diag::err_duplicate_property);
452 Diag(PIDecl->getLocation(), diag::note_property_declare);
453 return nullptr;
454 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000455
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000456 // Check for consistency with the previous declaration, if there is one.
457 if (PIDecl) {
458 // A readonly property declared in the primary class can be refined
459 // by adding a readwrite property within an extension.
460 // Anything else is an error.
461 if (!(PIDecl->isReadOnly() && isReadWrite)) {
462 // Tailor the diagnostics for the common case where a readwrite
463 // property is declared both in the @interface and the continuation.
464 // This is a common error where the user often intended the original
465 // declaration to be readonly.
466 unsigned diag =
467 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
468 (PIDecl->getPropertyAttributesAsWritten() &
469 ObjCPropertyDecl::OBJC_PR_readwrite)
470 ? diag::err_use_continuation_class_redeclaration_readwrite
471 : diag::err_use_continuation_class;
472 Diag(AtLoc, diag)
473 << CCPrimary->getDeclName();
474 Diag(PIDecl->getLocation(), diag::note_property_declare);
475 return nullptr;
476 }
477
478 // Check for consistency of getters.
479 if (PIDecl->getGetterName() != GetterSel) {
480 // If the getter was written explicitly, complain.
481 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
482 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
483 << PIDecl->getGetterName() << GetterSel;
484 Diag(PIDecl->getLocation(), diag::note_property_declare);
485 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000486
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000487 // Always adopt the getter from the original declaration.
488 GetterSel = PIDecl->getGetterName();
489 Attributes |= ObjCDeclSpec::DQ_PR_getter;
490 }
491
492 // Check consistency of ownership.
493 unsigned ExistingOwnership
494 = getOwnershipRule(PIDecl->getPropertyAttributes());
495 unsigned NewOwnership = getOwnershipRule(Attributes);
496 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
497 // If the ownership was written explicitly, complain.
498 if (getOwnershipRule(AttributesAsWritten)) {
499 Diag(AtLoc, diag::warn_property_attr_mismatch);
500 Diag(PIDecl->getLocation(), diag::note_property_declare);
501 }
502
503 // Take the ownership from the original property.
504 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
505 }
506
Fangrui Song6907ce22018-07-30 19:24:48 +0000507 // If the redeclaration is 'weak' but the original property is not,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000508 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
509 !(PIDecl->getPropertyAttributesAsWritten()
510 & ObjCPropertyDecl::OBJC_PR_weak) &&
511 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
512 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
513 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
514 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fangrui Song6907ce22018-07-30 19:24:48 +0000515 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000516 }
517
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000518 // Create a new ObjCPropertyDecl with the DeclContext being
519 // the class extension.
520 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000521 FD, GetterSel, GetterNameLoc,
522 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000523 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000524 Attributes, AttributesAsWritten,
525 T, TSI, MethodImplKind, DC);
526
527 // If there was no declaration of a property with the same name in
528 // the primary class, we're done.
529 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000530 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000531 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000532 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000533
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000534 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
535 bool IncompatibleObjC = false;
536 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000537 // Relax the strict type matching for property type in continuation class.
538 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000539 // as it narrows the object type in its primary class property. Note that
540 // this conversion is safe only because the wider type is for a 'readonly'
541 // property in primary class and 'narrowed' type for a 'readwrite' property
542 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000543 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
544 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
545 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
546 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
547 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000548 ConvertedType, IncompatibleObjC))
549 || IncompatibleObjC) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000550 Diag(AtLoc,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000551 diag::err_type_mismatch_continuation_class) << PDecl->getType();
552 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000553 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000554 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000555 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000556
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000557 // Check that atomicity of property in class extension matches the previous
558 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000559 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000560
Douglas Gregore17765e2015-11-03 17:02:34 +0000561 // Make sure getter/setter are appropriately synthesized.
562 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000563 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000564}
565
566ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
567 ObjCContainerDecl *CDecl,
568 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000569 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000570 FieldDeclarator &FD,
571 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000572 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000574 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000575 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000576 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000577 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000578 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000579 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000580 tok::ObjCKeywordKind MethodImplKind,
581 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000582 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000583
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000584 // Property defaults to 'assign' if it is readwrite, unless this is ARC
585 // and the type is retainable.
586 bool isAssign;
587 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
588 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
589 isAssign = true;
590 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
591 isAssign = false;
592 } else {
593 isAssign = (!getLangOpts().ObjCAutoRefCount ||
594 !T->isObjCRetainableType());
595 }
596
597 // Issue a warning if property is 'assign' as default and its
598 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000599 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000600 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000601 if (const ObjCObjectPointerType *ObjPtrTy =
602 T->getAs<ObjCObjectPointerType>()) {
603 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
604 if (IDecl)
605 if (ObjCProtocolDecl* PNSCopying =
606 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
607 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
608 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000609 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000610 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000611
612 if (T->isObjCObjectType()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000613 SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000614 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000615 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
616 << FixItHint::CreateInsertion(StarLoc, "*");
617 T = Context.getObjCObjectPointerType(T);
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000618 SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
Eli Friedman999af7b2013-07-09 01:38:07 +0000619 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
620 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000621
George Burgess IV00f70bd2018-03-01 05:43:23 +0000622 DeclContext *DC = CDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000623 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
624 FD.D.getIdentifierLoc(),
Fangrui Song6907ce22018-07-30 19:24:48 +0000625 PropertyId, AtLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +0000626 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000627
Manman Ren5b786402016-01-28 18:49:28 +0000628 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
629 (Attributes & ObjCDeclSpec::DQ_PR_class);
630 // Class property and instance property can have the same name.
631 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
632 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000633 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000634 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000635 PDecl->setInvalidDecl();
636 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000637 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000638 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000639 if (lexicalDC)
640 PDecl->setLexicalDeclContext(lexicalDC);
641 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000642
643 if (T->isArrayType() || T->isFunctionType()) {
644 Diag(AtLoc, diag::err_property_type) << T;
645 PDecl->setInvalidDecl();
646 }
647
648 ProcessDeclAttributes(S, PDecl, FD.D);
649
650 // Regardless of setter/getter attribute, we save the default getter/setter
651 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000652 PDecl->setGetterName(GetterSel, GetterNameLoc);
653 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000654 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000655 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000656
Bill Wendling44426052012-12-20 19:22:21 +0000657 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
659
Bill Wendling44426052012-12-20 19:22:21 +0000660 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
662
Bill Wendling44426052012-12-20 19:22:21 +0000663 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
665
666 if (isReadWrite)
667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
668
Bill Wendling44426052012-12-20 19:22:21 +0000669 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
671
Bill Wendling44426052012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
674
Bill Wendling44426052012-12-20 19:22:21 +0000675 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
677
Bill Wendling44426052012-12-20 19:22:21 +0000678 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000679 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
680
Bill Wendling44426052012-12-20 19:22:21 +0000681 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
683
Ted Kremenekac597f32010-03-12 00:46:40 +0000684 if (isAssign)
685 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
686
John McCall43192862011-09-13 18:31:23 +0000687 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000688 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000690 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000691 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000692
John McCall31168b02011-06-15 23:02:42 +0000693 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000694 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000695 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
696 if (isAssign)
697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
698
Ted Kremenekac597f32010-03-12 00:46:40 +0000699 if (MethodImplKind == tok::objc_required)
700 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
701 else if (MethodImplKind == tok::objc_optional)
702 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000703
Douglas Gregor813a0662015-06-19 18:14:38 +0000704 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
705 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
706
Douglas Gregor849ebc22015-06-19 18:14:46 +0000707 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
708 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
709
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800710 if (Attributes & ObjCDeclSpec::DQ_PR_class)
Manman Ren387ff7f2016-01-26 18:52:43 +0000711 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
712
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800713 if ((Attributes & ObjCDeclSpec::DQ_PR_direct) ||
714 CDecl->hasAttr<ObjCDirectMembersAttr>()) {
715 if (isa<ObjCProtocolDecl>(CDecl)) {
716 Diag(PDecl->getLocation(), diag::err_objc_direct_on_protocol) << true;
717 } else if (getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
718 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_direct);
719 } else {
720 Diag(PDecl->getLocation(), diag::warn_objc_direct_property_ignored)
721 << PDecl->getDeclName();
722 }
723 }
724
Ted Kremenek959e8302010-03-12 02:31:10 +0000725 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000726}
727
John McCall31168b02011-06-15 23:02:42 +0000728static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
729 ObjCPropertyDecl *property,
730 ObjCIvarDecl *ivar) {
731 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
732
John McCall31168b02011-06-15 23:02:42 +0000733 QualType ivarType = ivar->getType();
734 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000735
John McCall43192862011-09-13 18:31:23 +0000736 // The lifetime implied by the property's attributes.
737 Qualifiers::ObjCLifetime propertyLifetime =
738 getImpliedARCOwnership(property->getPropertyAttributes(),
739 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000740
John McCall43192862011-09-13 18:31:23 +0000741 // We're fine if they match.
742 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000743
John McCall460ce582015-10-22 18:38:17 +0000744 // None isn't a valid lifetime for an object ivar in ARC, and
745 // __autoreleasing is never valid; don't diagnose twice.
746 if ((ivarLifetime == Qualifiers::OCL_None &&
747 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000748 ivarLifetime == Qualifiers::OCL_Autoreleasing)
749 return;
John McCall31168b02011-06-15 23:02:42 +0000750
John McCalld8561f02012-08-20 23:36:59 +0000751 // If the ivar is private, and it's implicitly __unsafe_unretained
Nico Weber138a8152019-08-21 15:52:44 +0000752 // because of its type, then pretend it was actually implicitly
John McCalld8561f02012-08-20 23:36:59 +0000753 // __strong. This is only sound because we're processing the
754 // property implementation before parsing any method bodies.
755 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
756 propertyLifetime == Qualifiers::OCL_Strong &&
757 ivar->getAccessControl() == ObjCIvarDecl::Private) {
758 SplitQualType split = ivarType.split();
759 if (split.Quals.hasObjCLifetime()) {
760 assert(ivarType->isObjCARCImplicitlyUnretainedType());
761 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
762 ivarType = S.Context.getQualifiedType(split);
763 ivar->setType(ivarType);
764 return;
765 }
766 }
767
John McCall43192862011-09-13 18:31:23 +0000768 switch (propertyLifetime) {
769 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000770 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000771 << property->getDeclName()
772 << ivar->getDeclName()
773 << ivarLifetime;
774 break;
John McCall31168b02011-06-15 23:02:42 +0000775
John McCall43192862011-09-13 18:31:23 +0000776 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000777 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000778 << property->getDeclName()
779 << ivar->getDeclName();
780 break;
John McCall31168b02011-06-15 23:02:42 +0000781
John McCall43192862011-09-13 18:31:23 +0000782 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000783 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000784 << property->getDeclName()
785 << ivar->getDeclName()
Fangrui Song6907ce22018-07-30 19:24:48 +0000786 << ((property->getPropertyAttributesAsWritten()
John McCall43192862011-09-13 18:31:23 +0000787 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
788 break;
John McCall31168b02011-06-15 23:02:42 +0000789
John McCall43192862011-09-13 18:31:23 +0000790 case Qualifiers::OCL_Autoreleasing:
791 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000792
John McCall43192862011-09-13 18:31:23 +0000793 case Qualifiers::OCL_None:
794 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000795 return;
796 }
797
798 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000799 if (propertyImplLoc.isValid())
800 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000801}
802
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000803/// setImpliedPropertyAttributeForReadOnlyProperty -
804/// This routine evaludates life-time attributes for a 'readonly'
805/// property with no known lifetime of its own, using backing
806/// 'ivar's attribute, if any. If no backing 'ivar', property's
807/// life-time is assumed 'strong'.
808static void setImpliedPropertyAttributeForReadOnlyProperty(
809 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000810 Qualifiers::ObjCLifetime propertyLifetime =
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000811 getImpliedARCOwnership(property->getPropertyAttributes(),
812 property->getType());
813 if (propertyLifetime != Qualifiers::OCL_None)
814 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000815
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000816 if (!ivar) {
817 // if no backing ivar, make property 'strong'.
818 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
819 return;
820 }
821 // property assumes owenership of backing ivar.
822 QualType ivarType = ivar->getType();
823 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
824 if (ivarLifetime == Qualifiers::OCL_Strong)
825 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
826 else if (ivarLifetime == Qualifiers::OCL_Weak)
827 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000828}
Ted Kremenekac597f32010-03-12 00:46:40 +0000829
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000830static bool
831isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
832 ObjCPropertyDecl::PropertyAttributeKind Kind) {
833 return (Attr1 & Kind) != (Attr2 & Kind);
834}
835
836static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
837 unsigned Kinds) {
838 return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
839}
840
841/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
842/// property declaration that should be synthesised in all of the inherited
843/// protocols. It also diagnoses properties declared in inherited protocols with
844/// mismatched types or attributes, since any of them can be candidate for
845/// synthesis.
846static ObjCPropertyDecl *
847SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000848 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000849 ObjCPropertyDecl *Property) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000850 assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
851 "Expected a property from a protocol");
852 ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
853 ObjCInterfaceDecl::PropertyDeclOrder Properties;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000854 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
855 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000856 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
857 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000858 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000859 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000860 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000861 for (const auto *PI : SDecl->all_referenced_protocols()) {
862 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000863 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
864 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000865 }
866 SDecl = SDecl->getSuperClass();
867 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000868 }
869
870 if (Properties.empty())
871 return Property;
872
873 ObjCPropertyDecl *OriginalProperty = Property;
874 size_t SelectedIndex = 0;
875 for (const auto &Prop : llvm::enumerate(Properties)) {
876 // Select the 'readwrite' property if such property exists.
877 if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
878 Property = Prop.value();
879 SelectedIndex = Prop.index();
880 }
881 }
882 if (Property != OriginalProperty) {
883 // Check that the old property is compatible with the new one.
884 Properties[SelectedIndex] = OriginalProperty;
885 }
886
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000887 QualType RHSType = S.Context.getCanonicalType(Property->getType());
Alex Lorenz34d070f2017-08-22 10:38:07 +0000888 unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000889 enum MismatchKind {
890 IncompatibleType = 0,
891 HasNoExpectedAttribute,
892 HasUnexpectedAttribute,
893 DifferentGetter,
894 DifferentSetter
895 };
896 // Represents a property from another protocol that conflicts with the
897 // selected declaration.
898 struct MismatchingProperty {
899 const ObjCPropertyDecl *Prop;
900 MismatchKind Kind;
901 StringRef AttributeName;
902 };
903 SmallVector<MismatchingProperty, 4> Mismatches;
904 for (ObjCPropertyDecl *Prop : Properties) {
905 // Verify the property attributes.
Alex Lorenz34d070f2017-08-22 10:38:07 +0000906 unsigned Attr = Prop->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000907 if (Attr != OriginalAttributes) {
908 auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
909 MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
910 : HasUnexpectedAttribute;
911 Mismatches.push_back({Prop, Kind, AttributeName});
912 };
Alex Lorenz61372552018-05-02 22:40:19 +0000913 // The ownership might be incompatible unless the property has no explicit
914 // ownership.
915 bool HasOwnership = (Attr & (ObjCPropertyDecl::OBJC_PR_retain |
916 ObjCPropertyDecl::OBJC_PR_strong |
917 ObjCPropertyDecl::OBJC_PR_copy |
918 ObjCPropertyDecl::OBJC_PR_assign |
919 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
920 ObjCPropertyDecl::OBJC_PR_weak)) != 0;
921 if (HasOwnership &&
922 isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000923 ObjCPropertyDecl::OBJC_PR_copy)) {
924 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
925 continue;
926 }
Alex Lorenz61372552018-05-02 22:40:19 +0000927 if (HasOwnership && areIncompatiblePropertyAttributes(
928 OriginalAttributes, Attr,
929 ObjCPropertyDecl::OBJC_PR_retain |
930 ObjCPropertyDecl::OBJC_PR_strong)) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000931 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
932 ObjCPropertyDecl::OBJC_PR_strong),
933 "retain (or strong)");
934 continue;
935 }
936 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
937 ObjCPropertyDecl::OBJC_PR_atomic)) {
938 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
939 continue;
940 }
941 }
942 if (Property->getGetterName() != Prop->getGetterName()) {
943 Mismatches.push_back({Prop, DifferentGetter, ""});
944 continue;
945 }
946 if (!Property->isReadOnly() && !Prop->isReadOnly() &&
947 Property->getSetterName() != Prop->getSetterName()) {
948 Mismatches.push_back({Prop, DifferentSetter, ""});
949 continue;
950 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000951 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
952 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
953 bool IncompatibleObjC = false;
954 QualType ConvertedType;
955 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
956 || IncompatibleObjC) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000957 Mismatches.push_back({Prop, IncompatibleType, ""});
958 continue;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000959 }
960 }
961 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000962
963 if (Mismatches.empty())
964 return Property;
965
966 // Diagnose incompability.
967 {
968 bool HasIncompatibleAttributes = false;
969 for (const auto &Note : Mismatches)
970 HasIncompatibleAttributes =
971 Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
972 // Promote the warning to an error if there are incompatible attributes or
973 // incompatible types together with readwrite/readonly incompatibility.
974 auto Diag = S.Diag(Property->getLocation(),
975 Property != OriginalProperty || HasIncompatibleAttributes
976 ? diag::err_protocol_property_mismatch
977 : diag::warn_protocol_property_mismatch);
978 Diag << Mismatches[0].Kind;
979 switch (Mismatches[0].Kind) {
980 case IncompatibleType:
981 Diag << Property->getType();
982 break;
983 case HasNoExpectedAttribute:
984 case HasUnexpectedAttribute:
985 Diag << Mismatches[0].AttributeName;
986 break;
987 case DifferentGetter:
988 Diag << Property->getGetterName();
989 break;
990 case DifferentSetter:
991 Diag << Property->getSetterName();
992 break;
993 }
994 }
995 for (const auto &Note : Mismatches) {
996 auto Diag =
997 S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
998 << Note.Kind;
999 switch (Note.Kind) {
1000 case IncompatibleType:
1001 Diag << Note.Prop->getType();
1002 break;
1003 case HasNoExpectedAttribute:
1004 case HasUnexpectedAttribute:
1005 Diag << Note.AttributeName;
1006 break;
1007 case DifferentGetter:
1008 Diag << Note.Prop->getGetterName();
1009 break;
1010 case DifferentSetter:
1011 Diag << Note.Prop->getSetterName();
1012 break;
1013 }
1014 }
1015 if (AtLoc.isValid())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001016 S.Diag(AtLoc, diag::note_property_synthesize);
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001017
1018 return Property;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001019}
1020
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001021/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001022static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1023 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001024 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
1025
1026 // If this is a readwrite property in a class extension that refines
1027 // a readonly property in the original class definition, check it as
1028 // well.
1029
1030 // If it's a readonly property, we're not interested.
1031 if (Prop->isReadOnly()) return false;
1032
1033 // Is it declared in an extension?
1034 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1035 if (!Category || !Category->IsClassExtension()) return false;
1036
1037 // Find the corresponding property in the primary class definition.
1038 auto OrigClass = Category->getClassInterface();
1039 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1040 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1041 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1042 }
1043
Douglas Gregor02535432015-12-18 00:52:31 +00001044 // Look through all of the protocols.
1045 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +00001046 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1047 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +00001048 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1049 }
1050
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001051 return false;
1052}
1053
Adrian Prantl2073dd22019-11-04 14:28:14 -08001054/// Create a synthesized property accessor stub inside the \@implementation.
1055static ObjCMethodDecl *
1056RedeclarePropertyAccessor(ASTContext &Context, ObjCImplementationDecl *Impl,
1057 ObjCMethodDecl *AccessorDecl, SourceLocation AtLoc,
1058 SourceLocation PropertyLoc) {
1059 ObjCMethodDecl *Decl = AccessorDecl;
1060 ObjCMethodDecl *ImplDecl = ObjCMethodDecl::Create(
1061 Context, AtLoc, PropertyLoc, Decl->getSelector(), Decl->getReturnType(),
1062 Decl->getReturnTypeSourceInfo(), Impl, Decl->isInstanceMethod(),
1063 Decl->isVariadic(), Decl->isPropertyAccessor(), /* isSynthesized*/ true,
1064 Decl->isImplicit(), Decl->isDefined(), Decl->getImplementationControl(),
1065 Decl->hasRelatedResultType());
1066 ImplDecl->getMethodFamily();
1067 if (Decl->hasAttrs())
1068 ImplDecl->setAttrs(Decl->getAttrs());
1069 ImplDecl->setSelfDecl(Decl->getSelfDecl());
1070 ImplDecl->setCmdDecl(Decl->getCmdDecl());
1071 SmallVector<SourceLocation, 1> SelLocs;
1072 Decl->getSelectorLocs(SelLocs);
1073 ImplDecl->setMethodParams(Context, Decl->parameters(), SelLocs);
1074 ImplDecl->setLexicalDeclContext(Impl);
1075 ImplDecl->setDefined(false);
1076 return ImplDecl;
1077}
1078
Ted Kremenekac597f32010-03-12 00:46:40 +00001079/// ActOnPropertyImplDecl - This routine performs semantic checks and
1080/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +00001081/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +00001082///
John McCall48871652010-08-21 09:40:31 +00001083Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1084 SourceLocation AtLoc,
1085 SourceLocation PropertyLoc,
1086 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +00001087 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001088 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +00001089 SourceLocation PropertyIvarLoc,
1090 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +00001091 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +00001092 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +00001093 // Make sure we have a context for the property implementation declaration.
1094 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001095 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001096 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001097 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001098 if (PropertyIvarLoc.isInvalid())
1099 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +00001100 SourceLocation PropertyDiagLoc = PropertyLoc;
1101 if (PropertyDiagLoc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001102 PropertyDiagLoc = ClassImpDecl->getBeginLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00001103 ObjCPropertyDecl *property = nullptr;
1104 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001105 // Find the class or category class where this property must have
1106 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00001107 ObjCImplementationDecl *IC = nullptr;
1108 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001109 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1110 IDecl = IC->getClassInterface();
1111 // We always synthesize an interface for an implementation
1112 // without an interface decl. So, IDecl is always non-zero.
1113 assert(IDecl &&
1114 "ActOnPropertyImplDecl - @implementation without @interface");
1115
1116 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +00001117 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001118 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001119 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001120 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 }
Manman Rendfef4062016-01-29 19:16:39 +00001122 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001123 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +00001124 return nullptr;
1125 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001126 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +00001127 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
1128 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001129 if (AtLoc.isValid())
1130 Diag(AtLoc, diag::warn_implicit_atomic_property);
1131 else
1132 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1133 Diag(property->getLocation(), diag::note_property_declare);
1134 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001135
Ted Kremenekac597f32010-03-12 00:46:40 +00001136 if (const ObjCCategoryDecl *CD =
1137 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1138 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +00001139 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001140 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +00001141 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001142 }
1143 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001144 if (Synthesize&&
1145 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
1146 property->hasAttr<IBOutletAttr>() &&
1147 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001148 bool ReadWriteProperty = false;
1149 // Search into the class extensions and see if 'readonly property is
1150 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001151 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001152 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1153 if (!R.empty())
1154 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1155 PIkind = ExtProp->getPropertyAttributesAsWritten();
1156 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
1157 ReadWriteProperty = true;
1158 break;
1159 }
1160 }
1161 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001162
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001163 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +00001164 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +00001165 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001166 SourceLocation readonlyLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00001167 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001168 property->getLParenLoc(), readonlyLoc)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001169 SourceLocation endLoc =
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001170 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1171 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001172 Diag(property->getLocation(),
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001173 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1174 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1175 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +00001176 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001177 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001178 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001179 property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1180 property);
1181
Ted Kremenekac597f32010-03-12 00:46:40 +00001182 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1183 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001184 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001185 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001186 }
1187 IDecl = CatImplClass->getClassInterface();
1188 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001189 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001190 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001191 }
1192 ObjCCategoryDecl *Category =
1193 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1194
1195 // If category for this implementation not found, it is an error which
1196 // has already been reported eralier.
1197 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001198 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001199 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001200 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001201 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001202 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001203 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001204 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001205 }
1206 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001207 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001208 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001209 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001210 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001211 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001212 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001213 // Check that we have a valid, previously declared ivar for @synthesize
1214 if (Synthesize) {
1215 // @synthesize
1216 if (!PropertyIvar)
1217 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001218 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1219 ObjCInterfaceDecl *ClassDeclared;
1220 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1221 QualType PropType = property->getType();
1222 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001223
1224 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001225 diag::err_incomplete_synthesized_property,
1226 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001227 Diag(property->getLocation(), diag::note_property_declare);
1228 CompleteTypeErr = true;
1229 }
1230
David Blaikiebbafb8a2012-03-11 07:00:24 +00001231 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001232 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001233 ObjCPropertyDecl::OBJC_PR_readonly) &&
1234 PropertyIvarType->isObjCRetainableType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001235 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001236 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001237
1238 ObjCPropertyDecl::PropertyAttributeKind kind
John McCall31168b02011-06-15 23:02:42 +00001239 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001240
John McCall460ce582015-10-22 18:38:17 +00001241 bool isARCWeak = false;
1242 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1243 // Add GC __weak to the ivar type if the property is weak.
1244 if (getLangOpts().getGC() != LangOptions::NonGC) {
1245 assert(!getLangOpts().ObjCAutoRefCount);
1246 if (PropertyIvarType.isObjCGCStrong()) {
1247 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1248 Diag(property->getLocation(), diag::note_property_declare);
1249 } else {
1250 PropertyIvarType =
1251 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1252 }
1253
1254 // Otherwise, check whether ARC __weak is enabled and works with
1255 // the property type.
John McCall43192862011-09-13 18:31:23 +00001256 } else {
John McCall460ce582015-10-22 18:38:17 +00001257 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001258 // Only complain here when synthesizing an ivar.
1259 if (!Ivar) {
1260 Diag(PropertyDiagLoc,
1261 getLangOpts().ObjCWeakRuntime
1262 ? diag::err_synthesizing_arc_weak_property_disabled
1263 : diag::err_synthesizing_arc_weak_property_no_runtime);
1264 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001265 }
John McCallb61e14e2015-10-27 04:54:50 +00001266 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001267 } else {
1268 isARCWeak = true;
1269 if (const ObjCObjectPointerType *ObjT =
1270 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1271 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1272 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1273 Diag(property->getLocation(),
1274 diag::err_arc_weak_unavailable_property)
1275 << PropertyIvarType;
1276 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1277 << ClassImpDecl->getName();
1278 }
1279 }
1280 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001281 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001282 }
John McCall460ce582015-10-22 18:38:17 +00001283
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001284 if (AtLoc.isInvalid()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001285 // Check when default synthesizing a property that there is
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001286 // an ivar matching property name and issue warning; since this
1287 // is the most common case of not using an ivar used for backing
1288 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001289 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001290 ObjCIvarDecl *originalIvar =
1291 IDecl->lookupInstanceVariable(property->getIdentifier(),
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001292 ClassDeclared);
1293 if (originalIvar) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001294 Diag(PropertyDiagLoc,
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001295 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001296 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001297 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001298 Diag(property->getLocation(), diag::note_property_declare);
1299 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001300 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001301 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001302
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001303 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001304 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001305 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001306 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001307 !PropertyIvarType.getObjCLifetime() &&
1308 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001309
John McCall43192862011-09-13 18:31:23 +00001310 // It's an error if we have to do this and the user didn't
1311 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001312 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001313 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001314 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001315 diag::err_arc_objc_property_default_assign_on_object);
1316 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001317 } else {
1318 Qualifiers::ObjCLifetime lifetime =
1319 getImpliedARCOwnership(kind, PropertyIvarType);
1320 assert(lifetime && "no lifetime for property?");
Fangrui Song6907ce22018-07-30 19:24:48 +00001321
John McCall31168b02011-06-15 23:02:42 +00001322 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001323 qs.addObjCLifetime(lifetime);
Fangrui Song6907ce22018-07-30 19:24:48 +00001324 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
John McCall31168b02011-06-15 23:02:42 +00001325 }
John McCall31168b02011-06-15 23:02:42 +00001326 }
1327
Abramo Bagnaradff19302011-03-08 08:55:46 +00001328 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001329 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001330 PropertyIvarType, /*TInfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001331 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001332 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001333 if (RequireNonAbstractType(PropertyIvarLoc,
1334 PropertyIvarType,
1335 diag::err_abstract_type_in_decl,
1336 AbstractSynthesizedIvarType)) {
1337 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001338 // An abstract type is as bad as an incomplete type.
1339 CompleteTypeErr = true;
1340 }
Volodymyr Sapsai30680e92017-10-23 22:01:41 +00001341 if (!CompleteTypeErr) {
1342 const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
1343 if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) {
1344 Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
1345 << PropertyIvarType;
1346 CompleteTypeErr = true; // suppress later diagnostics about the ivar
1347 }
1348 }
Richard Smith81f5ade2016-12-15 02:28:18 +00001349 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001350 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001351 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001352 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001353
John McCall5fb5df92012-06-20 06:18:46 +00001354 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001355 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001356 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001357 // Note! I deliberately want it to fall thru so, we have a
1358 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001359 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001360 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001361 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001362 << property->getDeclName() << Ivar->getDeclName()
1363 << ClassDeclared->getDeclName();
1364 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001365 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001366 // Note! I deliberately want it to fall thru so more errors are caught.
1367 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001368 property->setPropertyIvarDecl(Ivar);
1369
Ted Kremenekac597f32010-03-12 00:46:40 +00001370 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1371
1372 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001373 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001374 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001375 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001376 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001377 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001378 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001379 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001380 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001381 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1382 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001383 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001384 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001385 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001386 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001387 << property->getDeclName() << PropType
1388 << Ivar->getDeclName() << IvarType;
1389 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001390 // Note! I deliberately want it to fall thru so, we have a
1391 // a property implementation and to avoid future warnings.
1392 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001393 else {
1394 // FIXME! Rules for properties are somewhat different that those
1395 // for assignments. Use a new routine to consolidate all cases;
1396 // specifically for property redeclarations as well as for ivars.
1397 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1398 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1399 if (lhsType != rhsType &&
1400 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001401 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001402 << property->getDeclName() << PropType
1403 << Ivar->getDeclName() << IvarType;
1404 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1405 // Fall thru - see previous comment
1406 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001407 }
1408 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001409 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001410 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001411 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001412 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001413 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001414 // Fall thru - see previous comment
1415 }
John McCall31168b02011-06-15 23:02:42 +00001416 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001417 if ((property->getType()->isObjCObjectPointerType() ||
1418 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001419 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001420 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001421 << property->getDeclName() << Ivar->getDeclName();
1422 // Fall thru - see previous comment
1423 }
1424 }
John McCall460ce582015-10-22 18:38:17 +00001425 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1426 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001427 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001428 } else if (PropertyIvar)
1429 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001430 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001431
Ted Kremenekac597f32010-03-12 00:46:40 +00001432 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1433 ObjCPropertyImplDecl *PIDecl =
1434 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1435 property,
1436 (Synthesize ?
1437 ObjCPropertyImplDecl::Synthesize
1438 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001439 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001440
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001441 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001442 PIDecl->setInvalidDecl();
1443
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001444 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1445 getterMethod->createImplicitParams(Context, IDecl);
Adrian Prantl2073dd22019-11-04 14:28:14 -08001446
1447 // Redeclare the getter within the implementation as DeclContext.
1448 if (Synthesize) {
1449 // If the method hasn't been overridden, create a synthesized implementation.
1450 ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1451 getterMethod->getSelector(), getterMethod->isInstanceMethod());
1452 if (!OMD)
1453 OMD = RedeclarePropertyAccessor(Context, IC, getterMethod, AtLoc,
1454 PropertyLoc);
1455 PIDecl->setGetterMethodDecl(OMD);
1456 }
1457
Eli Friedman169ec352012-05-01 22:26:06 +00001458 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001459 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001460 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1461 // returned by the getter as it must conform to C++'s copy-return rules.
1462 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001463 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001464 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001465 DeclRefExpr *SelfExpr = new (Context)
1466 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1467 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001468 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001469 Expr *LoadSelfExpr =
1470 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001471 CK_LValueToRValue, SelfExpr, nullptr,
1472 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001473 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001474 new (Context) ObjCIvarRefExpr(Ivar,
1475 Ivar->getUsageType(SelfDecl->getType()),
1476 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001477 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001478 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001479 ExprResult Res = PerformCopyInitialization(
1480 InitializedEntity::InitializeResult(PropertyDiagLoc,
1481 getterMethod->getReturnType(),
1482 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001483 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001484 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001485 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001486 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001487 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001488 PIDecl->setGetterCXXConstructor(ResExpr);
1489 }
1490 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001491 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1492 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001493 Diag(getterMethod->getLocation(),
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001494 diag::warn_property_getter_owning_mismatch);
1495 Diag(property->getLocation(), diag::note_property_declare);
1496 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001497 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1498 switch (getterMethod->getMethodFamily()) {
1499 case OMF_retain:
1500 case OMF_retainCount:
1501 case OMF_release:
1502 case OMF_autorelease:
1503 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1504 << 1 << getterMethod->getSelector();
1505 break;
1506 default:
1507 break;
1508 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001509 }
Adrian Prantl2073dd22019-11-04 14:28:14 -08001510
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001511 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1512 setterMethod->createImplicitParams(Context, IDecl);
Adrian Prantl2073dd22019-11-04 14:28:14 -08001513
1514 // Redeclare the setter within the implementation as DeclContext.
1515 if (Synthesize) {
1516 ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1517 setterMethod->getSelector(), setterMethod->isInstanceMethod());
1518 if (!OMD)
1519 OMD = RedeclarePropertyAccessor(Context, IC, setterMethod,
1520 AtLoc, PropertyLoc);
1521 PIDecl->setSetterMethodDecl(OMD);
1522 }
1523
Eli Friedman169ec352012-05-01 22:26:06 +00001524 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1525 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001526 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001527 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001528 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001529 DeclRefExpr *SelfExpr = new (Context)
1530 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1531 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001532 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001533 Expr *LoadSelfExpr =
1534 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001535 CK_LValueToRValue, SelfExpr, nullptr,
1536 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001537 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001538 new (Context) ObjCIvarRefExpr(Ivar,
1539 Ivar->getUsageType(SelfDecl->getType()),
1540 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001541 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001542 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001543 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1544 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001545 QualType T = Param->getType().getNonReferenceType();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001546 DeclRefExpr *rhs = new (Context)
1547 DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001548 MarkDeclRefReferenced(rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001549 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001550 BO_Assign, lhs, rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001551 if (property->getPropertyAttributes() &
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001552 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001553 Expr *callExpr = Res.getAs<Expr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001554 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001555 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1556 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001557 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001558 if (property->getType()->isReferenceType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001559 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001560 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001561 << property->getType();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001562 Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl)
1563 << FuncDecl;
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001564 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001565 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001566 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001567 }
1568 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001569
Ted Kremenekac597f32010-03-12 00:46:40 +00001570 if (IC) {
1571 if (Synthesize)
1572 if (ObjCPropertyImplDecl *PPIDecl =
1573 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001574 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001575 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1576 << PropertyIvar;
1577 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1578 }
1579
1580 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001581 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001582 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001583 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001584 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001585 }
1586 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001587 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001588 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001589 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001590 // Diagnose if an ivar was lazily synthesdized due to a previous
1591 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001592 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001593 ObjCInterfaceDecl *ClassDeclared=nullptr;
1594 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001595 if (!Synthesize)
1596 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1597 else {
1598 if (PropertyIvar && PropertyIvar != PropertyId)
1599 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1600 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001601 // Issue diagnostics only if Ivar belongs to current class.
Fangrui Song6907ce22018-07-30 19:24:48 +00001602 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001603 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001604 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
Fariborz Jahanian18722982010-07-17 00:59:30 +00001605 << PropertyId;
1606 Ivar->setInvalidDecl();
1607 }
1608 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001609 } else {
1610 if (Synthesize)
1611 if (ObjCPropertyImplDecl *PPIDecl =
1612 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001613 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001614 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1615 << PropertyIvar;
1616 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1617 }
1618
1619 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001620 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001621 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001622 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001623 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001624 }
1625 CatImplClass->addPropertyImplementation(PIDecl);
1626 }
1627
John McCall48871652010-08-21 09:40:31 +00001628 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001629}
1630
1631//===----------------------------------------------------------------------===//
1632// Helper methods.
1633//===----------------------------------------------------------------------===//
1634
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001635/// DiagnosePropertyMismatch - Compares two properties for their
1636/// attributes and types and warns on a variety of inconsistencies.
1637///
1638void
1639Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1640 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001641 const IdentifierInfo *inheritedName,
1642 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001643 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001644 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001645 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001646 SuperProperty->getPropertyAttributes();
Fangrui Song6907ce22018-07-30 19:24:48 +00001647
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001648 // We allow readonly properties without an explicit ownership
1649 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1650 // to be overridden by a property with any explicit ownership in the subclass.
1651 if (!OverridingProtocolProperty &&
1652 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1653 ;
1654 else {
1655 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1656 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1657 Diag(Property->getLocation(), diag::warn_readonly_property)
1658 << Property->getDeclName() << inheritedName;
1659 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1660 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001661 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001662 << Property->getDeclName() << "copy" << inheritedName;
1663 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1664 unsigned CAttrRetain =
1665 (CAttr &
1666 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1667 unsigned SAttrRetain =
1668 (SAttr &
1669 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1670 bool CStrong = (CAttrRetain != 0);
1671 bool SStrong = (SAttrRetain != 0);
1672 if (CStrong != SStrong)
1673 Diag(Property->getLocation(), diag::warn_property_attribute)
1674 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1675 }
John McCall31168b02011-06-15 23:02:42 +00001676 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001677
Douglas Gregor429183e2015-12-09 22:57:32 +00001678 // Check for nonatomic; note that nonatomic is effectively
1679 // meaningless for readonly properties, so don't diagnose if the
1680 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001681 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Alex Lorenz05a63ee2017-10-06 19:24:26 +00001682 // Readonly properties from protocols can be implemented as "readwrite"
1683 // with a custom setter name.
1684 if (Property->getSetterName() != SuperProperty->getSetterName() &&
1685 !(SuperProperty->isReadOnly() &&
1686 isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001687 Diag(Property->getLocation(), diag::warn_property_attribute)
1688 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001689 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1690 }
1691 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001692 Diag(Property->getLocation(), diag::warn_property_attribute)
1693 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001694 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1695 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001696
1697 QualType LHSType =
1698 Context.getCanonicalType(SuperProperty->getType());
1699 QualType RHSType =
1700 Context.getCanonicalType(Property->getType());
1701
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001702 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001703 // Do cases not handled in above.
1704 // FIXME. For future support of covariant property types, revisit this.
1705 bool IncompatibleObjC = false;
1706 QualType ConvertedType;
Fangrui Song6907ce22018-07-30 19:24:48 +00001707 if (!isObjCPointerConversion(RHSType, LHSType,
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001708 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001709 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001710 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1711 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001712 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1713 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001714 }
1715}
1716
1717bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1718 ObjCMethodDecl *GetterMethod,
1719 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001720 if (!GetterMethod)
1721 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001722 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001723 QualType PropertyRValueType =
1724 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1725 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001726 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001727 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1728 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001729 if ((propertyObjCPtr =
1730 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001731 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1732 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001733 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001734 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001735 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001736 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001737 << GetterMethod->getSelector() << GetterType;
1738 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1739 return true;
1740 } else {
1741 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001742 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001743 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1744 if (lhsType != rhsType && lhsType->isArithmeticType())
1745 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001746 }
1747 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001748
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001749 if (!compat) {
1750 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1751 << property->getDeclName()
1752 << GetterMethod->getSelector();
1753 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1754 return true;
1755 }
1756
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001757 return false;
1758}
1759
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001760/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001761/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001762static void
1763CollectImmediateProperties(ObjCContainerDecl *CDecl,
1764 ObjCContainerDecl::PropertyMap &PropMap,
1765 ObjCContainerDecl::PropertyMap &SuperPropMap,
1766 bool CollectClassPropsOnly = false,
1767 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001768 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001769 for (auto *Prop : IDecl->properties()) {
1770 if (CollectClassPropsOnly && !Prop->isClassProperty())
1771 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001772 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1773 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001774 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001775
1776 // Collect the properties from visible extensions.
1777 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001778 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1779 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001780
Ted Kremenek204c3c52014-02-22 00:02:03 +00001781 if (IncludeProtocols) {
1782 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001783 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001784 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1785 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001786 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001787 }
1788 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001789 for (auto *Prop : CATDecl->properties()) {
1790 if (CollectClassPropsOnly && !Prop->isClassProperty())
1791 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001792 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1793 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001794 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001795 if (IncludeProtocols) {
1796 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001797 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001798 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1799 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001800 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001801 }
1802 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001803 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001804 if (CollectClassPropsOnly && !Prop->isClassProperty())
1805 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001806 ObjCPropertyDecl *PropertyFromSuper =
1807 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1808 Prop->isClassProperty())];
Fangrui Song6907ce22018-07-30 19:24:48 +00001809 // Exclude property for protocols which conform to class's super-class,
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001810 // as super-class has to implement the property.
Fangrui Song6907ce22018-07-30 19:24:48 +00001811 if (!PropertyFromSuper ||
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001812 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001813 ObjCPropertyDecl *&PropEntry =
1814 PropMap[std::make_pair(Prop->getIdentifier(),
1815 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001816 if (!PropEntry)
1817 PropEntry = Prop;
1818 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001819 }
Manman Ren16a7d632016-04-12 23:01:55 +00001820 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001821 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001822 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1823 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001824 }
1825}
1826
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001827/// CollectSuperClassPropertyImplementations - This routine collects list of
1828/// properties to be implemented in super class(s) and also coming from their
1829/// conforming protocols.
1830static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001831 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001832 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001833 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001834 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001835 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001836 SDecl = SDecl->getSuperClass();
1837 }
1838 }
1839}
1840
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001841/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1842/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1843/// declared in class 'IFace'.
1844bool
1845Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1846 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1847 if (!IV->getSynthesize())
1848 return false;
1849 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1850 Method->isInstanceMethod());
1851 if (!IMD || !IMD->isPropertyAccessor())
1852 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001853
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001854 // look up a property declaration whose one of its accessors is implemented
1855 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001856 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001857 if ((Property->getGetterName() == IMD->getSelector() ||
1858 Property->getSetterName() == IMD->getSelector()) &&
1859 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001860 return true;
1861 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001862 // Also look up property declaration in class extension whose one of its
1863 // accessors is implemented by this method.
1864 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001865 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001866 if ((Property->getGetterName() == IMD->getSelector() ||
1867 Property->getSetterName() == IMD->getSelector()) &&
1868 (Property->getPropertyIvarDecl() == IV))
1869 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001870 return false;
1871}
1872
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001873static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1874 ObjCPropertyDecl *Prop) {
1875 bool SuperClassImplementsGetter = false;
1876 bool SuperClassImplementsSetter = false;
1877 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1878 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001879
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001880 while (IDecl->getSuperClass()) {
1881 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1882 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1883 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001884
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001885 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1886 SuperClassImplementsSetter = true;
1887 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1888 return true;
1889 IDecl = IDecl->getSuperClass();
1890 }
1891 return false;
1892}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001893
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001894/// Default synthesizes all properties which must be synthesized
James Dennett2a4d13c2012-06-15 07:13:21 +00001895/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001896void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1897 ObjCInterfaceDecl *IDecl,
1898 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001899 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001900 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1901 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001902 if (PropMap.empty())
1903 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001904 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001905 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001906
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001907 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1908 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001909 // Is there a matching property synthesize/dynamic?
1910 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001911 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001912 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1913 continue;
1914 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001915 if (IMPDecl->FindPropertyImplDecl(
1916 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001917 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001918 ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Prop->getGetterName());
1919 if (ImpMethod && !ImpMethod->getBody()) {
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001920 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1921 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001922 ImpMethod = IMPDecl->getInstanceMethod(Prop->getSetterName());
1923 if (ImpMethod && !ImpMethod->getBody())
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001924 continue;
1925 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001926 if (ObjCPropertyImplDecl *PID =
1927 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001928 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1929 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001930 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001931 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001932 continue;
1933 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001934 ObjCPropertyDecl *PropInSuperClass =
1935 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1936 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001937 if (ObjCProtocolDecl *Proto =
1938 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001939 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001940 // Suppress the warning if class's superclass implements property's
1941 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001942 // Or, if property is going to be implemented in its super class.
1943 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001944 Diag(IMPDecl->getLocation(),
1945 diag::warn_auto_synthesizing_protocol_property)
1946 << Prop << Proto;
1947 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001948 std::string FixIt =
1949 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1950 Diag(AtEnd, diag::note_add_synthesize_directive)
1951 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001952 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001953 continue;
1954 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001955 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001956 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001957 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1958 (PropInSuperClass->getPropertyAttributes() &
1959 ObjCPropertyDecl::OBJC_PR_readonly) &&
1960 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1961 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1962 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1963 << Prop->getIdentifier();
1964 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1965 }
1966 else {
1967 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1968 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001969 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001970 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1971 }
1972 continue;
1973 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001974 // We use invalid SourceLocations for the synthesized ivars since they
1975 // aren't really synthesized at a particular location; they just exist.
1976 // Saying that they are located at the @implementation isn't really going
1977 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001978 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1979 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1980 true,
1981 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001982 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001983 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001984 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001985 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001986 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001987 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001988 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001989}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001990
Alex Lorenz6c9af502017-07-03 10:12:24 +00001991void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1992 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00001993 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001994 return;
1995 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1996 if (!IC)
1997 return;
1998 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001999 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00002000 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002001}
2002
Manman Ren08ce7342016-05-18 18:12:34 +00002003static void DiagnoseUnimplementedAccessor(
2004 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
2005 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
2006 ObjCPropertyDecl *Prop,
2007 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
2008 // Check to see if we have a corresponding selector in SMap and with the
2009 // right method type.
Fangrui Song75e74e02019-03-31 08:48:19 +00002010 auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) {
2011 return x->getSelector() == Method &&
2012 x->isClassMethod() == Prop->isClassProperty();
2013 });
Ted Kremenek7e812952014-02-21 19:41:30 +00002014 // When reporting on missing property setter/getter implementation in
2015 // categories, do not report when they are declared in primary class,
2016 // class's protocol, or one of it super classes. This is because,
2017 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00002018 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00002019 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00002020 !PrimaryClass->lookupPropertyAccessor(Method, C,
2021 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00002022 unsigned diag =
2023 isa<ObjCCategoryDecl>(CDecl)
2024 ? (Prop->isClassProperty()
2025 ? diag::warn_impl_required_in_category_for_class_property
2026 : diag::warn_setter_getter_impl_required_in_category)
2027 : (Prop->isClassProperty()
2028 ? diag::warn_impl_required_for_class_property
2029 : diag::warn_setter_getter_impl_required);
2030 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
2031 S.Diag(Prop->getLocation(), diag::note_property_declare);
2032 if (S.LangOpts.ObjCDefaultSynthProperties &&
2033 S.LangOpts.ObjCRuntime.isNonFragile())
2034 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
2035 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
2036 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
2037 }
Ted Kremenek7e812952014-02-21 19:41:30 +00002038}
2039
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002040void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00002041 ObjCContainerDecl *CDecl,
2042 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00002043 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00002044 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
2045
Manman Ren16a7d632016-04-12 23:01:55 +00002046 // Since we don't synthesize class properties, we should emit diagnose even
2047 // if SynthesizeProperties is true.
2048 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2049 // Gather properties which need not be implemented in this class
2050 // or category.
2051 if (!IDecl)
2052 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
2053 // For categories, no need to implement properties declared in
2054 // its primary class (and its super classes) if property is
2055 // declared in one of those containers.
2056 if ((IDecl = C->getClassInterface())) {
2057 ObjCInterfaceDecl::PropertyDeclOrder PO;
2058 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002059 }
Manman Ren16a7d632016-04-12 23:01:55 +00002060 }
2061 if (IDecl)
2062 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00002063
Manman Ren16a7d632016-04-12 23:01:55 +00002064 // When SynthesizeProperties is true, we only check class properties.
2065 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2066 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002067
Ted Kremenek38882022014-02-21 19:41:39 +00002068 // Scan the @interface to see if any of the protocols it adopts
2069 // require an explicit implementation, via attribute
2070 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00002071 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00002072 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002073
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002074 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00002075 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2076 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002077 // Lazily construct a set of all the properties in the @interface
2078 // of the class, without looking at the superclass. We cannot
2079 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00002080 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00002081 // as scans the adopted protocols. This work only triggers for protocols
2082 // with the attribute, which is very rare, and only occurs when
2083 // analyzing the @implementation.
2084 if (!LazyMap) {
2085 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2086 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2087 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002088 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002089 /* IncludeProtocols */ false);
2090 }
Ted Kremenek38882022014-02-21 19:41:39 +00002091 // Add the properties of 'PDecl' to the list of properties that
2092 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002093 for (auto *PropDecl : PDecl->properties()) {
2094 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2095 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002096 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002097 PropMap[std::make_pair(PropDecl->getIdentifier(),
2098 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002099 }
2100 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002101 }
Ted Kremenek38882022014-02-21 19:41:39 +00002102
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002103 if (PropMap.empty())
2104 return;
2105
2106 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002107 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002108 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002109
Manman Ren08ce7342016-05-18 18:12:34 +00002110 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002111 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002112 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002113 InsMap.insert(I);
Fangrui Song6907ce22018-07-30 19:24:48 +00002114
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002115 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002116 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002117 if (C && !C->IsClassExtension())
2118 if ((PrimaryClass = C->getClassInterface()))
2119 // Report unimplemented properties in the category as well.
2120 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2121 // When reporting on missing setter/getters, do not report when
2122 // setter/getter is implemented in category's primary class
2123 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002124 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002125 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002126 }
2127
Anna Zaks673d76b2012-10-18 19:17:53 +00002128 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002129 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2130 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002131 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002132 if (Prop->isInvalidDecl() ||
2133 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002134 PropImplMap.count(Prop) ||
2135 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002136 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002137
2138 // Diagnose unimplemented getters and setters.
2139 DiagnoseUnimplementedAccessor(*this,
2140 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2141 if (!Prop->isReadOnly())
2142 DiagnoseUnimplementedAccessor(*this,
2143 PrimaryClass, Prop->getSetterName(),
2144 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002145 }
2146}
2147
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002148void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002149 for (const auto *propertyImpl : impDecl->property_impls()) {
2150 const auto *property = propertyImpl->getPropertyDecl();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002151 // Warn about null_resettable properties with synthesized setters,
2152 // because the setter won't properly handle nil.
2153 if (propertyImpl->getPropertyImplementation()
2154 == ObjCPropertyImplDecl::Synthesize &&
2155 (property->getPropertyAttributes() &
2156 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2157 property->getGetterMethodDecl() &&
2158 property->getSetterMethodDecl()) {
Adrian Prantl2073dd22019-11-04 14:28:14 -08002159 auto *getterImpl = propertyImpl->getGetterMethodDecl();
2160 auto *setterImpl = propertyImpl->getSetterMethodDecl();
2161 if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) &&
2162 (!setterImpl || setterImpl->isSynthesizedAccessorStub())) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002163 SourceLocation loc = propertyImpl->getLocation();
2164 if (loc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002165 loc = impDecl->getBeginLoc();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002166
2167 Diag(loc, diag::warn_null_resettable_setter)
Adrian Prantl2073dd22019-11-04 14:28:14 -08002168 << setterImpl->getSelector() << property->getDeclName();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002169 }
2170 }
2171 }
2172}
2173
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002174void
2175Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002176 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002177 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002178 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002179 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002180 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002181 for (auto *Prop : IDecl->properties())
2182 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002183 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002184 for (auto *Prop : Ext->properties())
2185 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fangrui Song6907ce22018-07-30 19:24:48 +00002186
Manman Renefe1bac2016-01-27 20:00:32 +00002187 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2188 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002189 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002190 ObjCMethodDecl *GetterMethod = nullptr;
2191 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002192
Bill Wendling44426052012-12-20 19:22:21 +00002193 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002194 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002195
John McCall43192862011-09-13 18:31:23 +00002196 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2197 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002198 GetterMethod = Property->isClassProperty() ?
2199 IMPDecl->getClassMethod(Property->getGetterName()) :
2200 IMPDecl->getInstanceMethod(Property->getGetterName());
2201 SetterMethod = Property->isClassProperty() ?
2202 IMPDecl->getClassMethod(Property->getSetterName()) :
2203 IMPDecl->getInstanceMethod(Property->getSetterName());
Adrian Prantl2073dd22019-11-04 14:28:14 -08002204 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2205 GetterMethod = nullptr;
2206 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2207 SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002208 if (GetterMethod) {
2209 Diag(GetterMethod->getLocation(),
2210 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002211 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002212 Diag(Property->getLocation(), diag::note_property_declare);
2213 }
2214 if (SetterMethod) {
2215 Diag(SetterMethod->getLocation(),
2216 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002217 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002218 Diag(Property->getLocation(), diag::note_property_declare);
2219 }
2220 }
2221
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002222 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00002223 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2224 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002225 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002226 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2227 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002228 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2229 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08002230 GetterMethod = PIDecl->getGetterMethodDecl();
2231 SetterMethod = PIDecl->getSetterMethodDecl();
2232 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2233 GetterMethod = nullptr;
2234 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2235 SetterMethod = nullptr;
2236 if ((bool)GetterMethod ^ (bool)SetterMethod) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002237 SourceLocation MethodLoc =
2238 (GetterMethod ? GetterMethod->getLocation()
2239 : SetterMethod->getLocation());
2240 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002241 << Property->getIdentifier() << (GetterMethod != nullptr)
2242 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002243 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002244 if (Property->getLParenLoc().isValid() &&
2245 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002246 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002247 SourceLocation AfterLParen =
2248 getLocForEndOfToken(Property->getLParenLoc());
2249 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2250 : "nonatomic";
2251 Diag(Property->getLocation(),
2252 diag::note_atomic_property_fixup_suggest)
2253 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2254 } else if (Property->getLParenLoc().isInvalid()) {
2255 //@property id etc.
Fangrui Song6907ce22018-07-30 19:24:48 +00002256 SourceLocation startLoc =
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002257 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2258 Diag(Property->getLocation(),
2259 diag::note_atomic_property_fixup_suggest)
2260 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002261 }
2262 else
2263 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002264 Diag(Property->getLocation(), diag::note_property_declare);
2265 }
2266 }
2267 }
2268}
2269
John McCall31168b02011-06-15 23:02:42 +00002270void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002271 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002272 return;
2273
Aaron Ballmand85eff42014-03-14 15:02:45 +00002274 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002275 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002276 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Adrian Prantl2073dd22019-11-04 14:28:14 -08002277 !PD->isClassProperty()) {
2278 ObjCMethodDecl *IM = PID->getGetterMethodDecl();
2279 if (IM && !IM->isSynthesizedAccessorStub())
2280 continue;
John McCall31168b02011-06-15 23:02:42 +00002281 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2282 if (!method)
2283 continue;
2284 ObjCMethodFamily family = method->getMethodFamily();
2285 if (family == OMF_alloc || family == OMF_copy ||
2286 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002287 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002288 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002289 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002290 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002291
2292 // Look for a getter explicitly declared alongside the property.
2293 // If we find one, use its location for the note.
2294 SourceLocation noteLoc = PD->getLocation();
2295 SourceLocation fixItLoc;
2296 for (auto *getterRedecl : method->redecls()) {
2297 if (getterRedecl->isImplicit())
2298 continue;
2299 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2300 continue;
2301 noteLoc = getterRedecl->getLocation();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002302 fixItLoc = getterRedecl->getEndLoc();
Jordan Rosea34d04d2015-01-16 23:04:31 +00002303 }
2304
2305 Preprocessor &PP = getPreprocessor();
2306 TokenValue tokens[] = {
2307 tok::kw___attribute, tok::l_paren, tok::l_paren,
2308 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2309 PP.getIdentifierInfo("none"), tok::r_paren,
2310 tok::r_paren, tok::r_paren
2311 };
2312 StringRef spelling = "__attribute__((objc_method_family(none)))";
2313 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2314 if (!macroName.empty())
2315 spelling = macroName;
2316
2317 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2318 << method->getDeclName() << spelling;
2319 if (fixItLoc.isValid()) {
2320 SmallString<64> fixItText(" ");
2321 fixItText += spelling;
2322 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2323 }
John McCall31168b02011-06-15 23:02:42 +00002324 }
2325 }
2326 }
2327}
2328
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002329void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002330 const ObjCImplementationDecl *ImplD,
2331 const ObjCInterfaceDecl *IFD) {
2332 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002333 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2334 if (!SuperD)
2335 return;
2336
2337 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002338 for (const auto *I : ImplD->instance_methods())
2339 if (I->getMethodFamily() == OMF_init)
2340 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002341
2342 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2343 SuperD->getDesignatedInitializers(DesignatedInits);
2344 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2345 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2346 const ObjCMethodDecl *MD = *I;
2347 if (!InitSelSet.count(MD->getSelector())) {
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002348 // Don't emit a diagnostic if the overriding method in the subclass is
2349 // marked as unavailable.
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002350 bool Ignore = false;
2351 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2352 Ignore = IMD->isUnavailable();
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002353 } else {
2354 // Check the methods declared in the class extensions too.
2355 for (auto *Ext : IFD->visible_extensions())
2356 if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) {
2357 Ignore = IMD->isUnavailable();
2358 break;
2359 }
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002360 }
2361 if (!Ignore) {
2362 Diag(ImplD->getLocation(),
2363 diag::warn_objc_implementation_missing_designated_init_override)
2364 << MD->getSelector();
2365 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2366 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002367 }
2368 }
2369}
2370
John McCallad31b5f2010-11-10 07:01:40 +00002371/// AddPropertyAttrs - Propagates attributes from a property to the
2372/// implicitly-declared getter or setter for that property.
2373static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2374 ObjCPropertyDecl *Property) {
2375 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002376 for (const auto *A : Property->attrs()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002377 if (isa<DeprecatedAttr>(A) ||
2378 isa<UnavailableAttr>(A) ||
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002379 isa<AvailabilityAttr>(A))
2380 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002381 }
John McCallad31b5f2010-11-10 07:01:40 +00002382}
2383
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002384/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2385/// have the property type and issue diagnostics if they don't.
2386/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002387/// appropriate lookup tables.
2388void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002389 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002390 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002391 if (CD->isInvalidDecl())
2392 return;
2393
Manman Rend36f7d52016-01-27 20:10:32 +00002394 bool IsClassProperty = property->isClassProperty();
2395 GetterMethod = IsClassProperty ?
2396 CD->getClassMethod(property->getGetterName()) :
2397 CD->getInstanceMethod(property->getGetterName());
2398
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002399 // if setter or getter is not found in class extension, it might be
2400 // in the primary class.
2401 if (!GetterMethod)
2402 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2403 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002404 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2405 getClassMethod(property->getGetterName()) :
2406 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002407 getInstanceMethod(property->getGetterName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002408
Manman Rend36f7d52016-01-27 20:10:32 +00002409 SetterMethod = IsClassProperty ?
2410 CD->getClassMethod(property->getSetterName()) :
2411 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002412 if (!SetterMethod)
2413 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2414 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002415 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2416 getClassMethod(property->getSetterName()) :
2417 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002418 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002419 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2420 property->getLocation());
2421
Alex Lorenz535571a2017-03-30 13:33:51 +00002422 if (!property->isReadOnly() && SetterMethod) {
2423 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2424 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002425 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2426 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002427 !Context.hasSameUnqualifiedType(
Fangrui Song6907ce22018-07-30 19:24:48 +00002428 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002429 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002430 Diag(property->getLocation(),
2431 diag::warn_accessor_property_type_mismatch)
2432 << property->getDeclName()
2433 << SetterMethod->getSelector();
2434 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2435 }
2436 }
2437
2438 // Synthesize getter/setter methods if none exist.
2439 // Find the default getter and if one not found, add one.
2440 // FIXME: The synthesized property we set here is misleading. We almost always
2441 // synthesize these methods unless the user explicitly provided prototypes
2442 // (which is odd, but allowed). Sema should be typechecking that the
2443 // declarations jive in that situation (which it is not currently).
2444 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002445 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002446 // Declare a getter method and add it to the list of methods
2447 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002448 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002449
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002450 // The getter returns the declared property type with all qualifiers
2451 // removed.
2452 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2453
Douglas Gregor849ebc22015-06-19 18:14:46 +00002454 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002455 if (property->getPropertyAttributes() &
2456 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2457 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002458 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002459 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002460 resultTy = Context.getAttributedType(attr::TypeNonNull,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002461 modifiedTy, modifiedTy);
2462 }
2463 }
2464
Adrian Prantl2073dd22019-11-04 14:28:14 -08002465 GetterMethod = ObjCMethodDecl::Create(
2466 Context, Loc, Loc, property->getGetterName(), resultTy, nullptr, CD,
2467 !IsClassProperty, /*isVariadic=*/false,
2468 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
2469 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
2470 (property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2471 ? ObjCMethodDecl::Optional
2472 : ObjCMethodDecl::Required);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002473 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002474
2475 AddPropertyAttrs(*this, GetterMethod, property);
2476
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002477 if (property->isDirectProperty())
2478 GetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2479
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002480 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002481 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2482 Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002483
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002484 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2485 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002486 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002487
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002488 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002489 GetterMethod->addAttr(SectionAttr::CreateImplicit(
2490 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2491 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002492
2493 if (getLangOpts().ObjCAutoRefCount)
2494 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002495 } else
2496 // A user declared getter will be synthesize when @synthesize of
2497 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002498 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002499 property->setGetterMethodDecl(GetterMethod);
2500
2501 // Skip setter if property is read-only.
2502 if (!property->isReadOnly()) {
2503 // Find the default setter and if one not found, add one.
2504 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002505 // No instance/class method of same name as property setter name was
2506 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002507 // Declare a setter method and add it to the list of methods
2508 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002509 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002510
2511 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002512 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002513 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002514 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002515 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002516 /*isPropertyAccessor=*/true,
Adrian Prantl2073dd22019-11-04 14:28:14 -08002517 /*isSynthesizedAccessorStub=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002518 /*isImplicitlyDeclared=*/true,
2519 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002520 (property->getPropertyImplementation() ==
2521 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002522 ObjCMethodDecl::Optional :
2523 ObjCMethodDecl::Required);
2524
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002525 // Remove all qualifiers from the setter's parameter type.
2526 QualType paramTy =
2527 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2528
Douglas Gregor849ebc22015-06-19 18:14:46 +00002529 // If the property is null_resettable, the setter accepts a
2530 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002531 if (property->getPropertyAttributes() &
2532 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2533 QualType modifiedTy = paramTy;
2534 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2535 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002536 paramTy = Context.getAttributedType(attr::TypeNullable,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002537 modifiedTy, modifiedTy);
2538 }
2539 }
2540
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002541 // Invent the arguments for the setter. We don't bother making a
2542 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002543 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2544 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002545 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002546 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002547 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002548 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002549 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002550 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002551
2552 AddPropertyAttrs(*this, SetterMethod, property);
2553
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002554 if (property->isDirectProperty())
2555 SetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2556
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002557 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002558 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002559 SetterMethod->addAttr(SectionAttr::CreateImplicit(
2560 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2561 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002562 // It's possible for the user to have set a very odd custom
2563 // setter selector that causes it to have a method family.
2564 if (getLangOpts().ObjCAutoRefCount)
2565 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002566 } else
2567 // A user declared setter will be synthesize when @synthesize of
2568 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002569 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002570 property->setSetterMethodDecl(SetterMethod);
2571 }
2572 // Add any synthesized methods to the global pool. This allows us to
2573 // handle the following, which is supported by GCC (and part of the design).
2574 //
2575 // @interface Foo
2576 // @property double bar;
2577 // @end
2578 //
2579 // void thisIsUnfortunate() {
2580 // id foo;
2581 // double bar = [foo bar];
2582 // }
2583 //
Manman Rend36f7d52016-01-27 20:10:32 +00002584 if (!IsClassProperty) {
2585 if (GetterMethod)
2586 AddInstanceMethodToGlobalPool(GetterMethod);
2587 if (SetterMethod)
2588 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002589 } else {
2590 if (GetterMethod)
2591 AddFactoryMethodToGlobalPool(GetterMethod);
2592 if (SetterMethod)
2593 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002594 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002595
2596 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2597 if (!CurrentClass) {
2598 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2599 CurrentClass = Cat->getClassInterface();
2600 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2601 CurrentClass = Impl->getClassInterface();
2602 }
2603 if (GetterMethod)
2604 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2605 if (SetterMethod)
2606 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002607}
2608
John McCall48871652010-08-21 09:40:31 +00002609void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002610 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002611 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002612 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002613 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002614 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002615 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00002616
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002617 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2618 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2619 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2620 << "readonly" << "readwrite";
Fangrui Song6907ce22018-07-30 19:24:48 +00002621
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002622 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2623 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002624
2625 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002626 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002627 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2628 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002629 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002630 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002631 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2632 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2633 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002634 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002635 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002636 }
2637
John McCall52a503d2018-09-05 19:02:00 +00002638 // Check for assign on object types.
2639 if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2640 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
2641 PropertyTy->isObjCRetainableType() &&
2642 !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2643 Diag(Loc, diag::warn_objc_property_assign_on_object);
2644 }
2645
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002646 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002647 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2648 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002649 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2650 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002651 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002652 }
Bill Wendling44426052012-12-20 19:22:21 +00002653 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002654 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2655 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002656 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002657 }
Bill Wendling44426052012-12-20 19:22:21 +00002658 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002659 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2660 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002661 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002662 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002663 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002664 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002665 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2666 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002667 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002668 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002669 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002670 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002671 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2672 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002673 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2674 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002675 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002676 }
Bill Wendling44426052012-12-20 19:22:21 +00002677 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002678 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2679 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002680 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002681 }
Bill Wendling44426052012-12-20 19:22:21 +00002682 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2684 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002685 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002686 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002687 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002688 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002689 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2690 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002691 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002692 }
Bill Wendling44426052012-12-20 19:22:21 +00002693 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2694 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002695 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2696 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002697 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002698 }
Bill Wendling44426052012-12-20 19:22:21 +00002699 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002700 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2701 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002702 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002703 }
Bill Wendling44426052012-12-20 19:22:21 +00002704 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002705 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2706 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002707 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002708 }
2709 }
Bill Wendling44426052012-12-20 19:22:21 +00002710 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2711 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002712 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2713 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002714 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002715 }
Bill Wendling44426052012-12-20 19:22:21 +00002716 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2717 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002718 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2719 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002720 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002721 }
2722
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002723 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002724 // 'weak' and 'nonnull' are mutually exclusive.
2725 if (auto nullability = PropertyTy->getNullability(Context)) {
2726 if (*nullability == NullabilityKind::NonNull)
2727 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2728 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002729 }
2730 }
2731
Bill Wendling44426052012-12-20 19:22:21 +00002732 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2733 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002734 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2735 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002736 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002737 }
2738
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002739 // Warn if user supplied no assignment attribute, property is
2740 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002741 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2742 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2743 // do nothing
2744 } else if (getLangOpts().ObjCAutoRefCount) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002745 // With arc, @property definitions should default to strong when
John McCallb61e14e2015-10-27 04:54:50 +00002746 // not specified.
2747 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2748 } else if (PropertyTy->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002749 bool isAnyClassTy =
2750 (PropertyTy->isObjCClassType() ||
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002751 PropertyTy->isObjCQualifiedClassType());
2752 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2753 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002754 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002755 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002756 else if (propertyInPrimaryClass) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002757 // Don't issue warning on property with no life time in class
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002758 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002759 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002760 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002761 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002762
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002763 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002764 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002765 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002766 }
John McCallb61e14e2015-10-27 04:54:50 +00002767 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002768
2769 // FIXME: Implement warning dependent on NSCopying being
2770 // implemented. See also:
2771 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2772 // (please trim this list while you are at it).
2773 }
2774
Bill Wendling44426052012-12-20 19:22:21 +00002775 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2776 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002777 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002778 && PropertyTy->isBlockPointerType())
2779 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002780 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2781 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2782 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002783 PropertyTy->isBlockPointerType())
2784 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fangrui Song6907ce22018-07-30 19:24:48 +00002785
Bill Wendling44426052012-12-20 19:22:21 +00002786 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2787 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002788 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002789}