blob: 9c7d8ecf7f9bb7c0363359c8cc1e0cb8c9fecd89 [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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -040038static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
John McCall43192862011-09-13 18:31:23 +000041 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
Puyan Lotfibbf386f2020-04-23 00:05:08 -040043 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;
Puyan Lotfibbf386f2020-04-23 00:05:08 -040047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall43192862011-09-13 18:31:23 +000048 return Qualifiers::OCL_Weak;
Puyan Lotfibbf386f2020-04-23 00:05:08 -040049 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
John McCall43192862011-09-13 18:31:23 +000050 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
Puyan Lotfibbf386f2020-04-23 00:05:08 -040055 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
John McCall43192862011-09-13 18:31:23 +000056 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
Puyan Lotfibbf386f2020-04-23 00:05:08 -040069 ObjCPropertyDecl::PropertyAttributeKind propertyKind
70 = property->getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +000071 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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -040083 ObjCPropertyDecl::PropertyAttributeKind attr;
John McCall43192862011-09-13 18:31:23 +000084 if (propertyLifetime == Qualifiers::OCL_Strong) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -040085 attr = ObjCPropertyDecl::OBJC_PR_strong;
John McCall43192862011-09-13 18:31:23 +000086 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -040087 attr = ObjCPropertyDecl::OBJC_PR_weak;
John McCall43192862011-09-13 18:31:23 +000088 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
Puyan Lotfibbf386f2020-04-23 00:05:08 -040090 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
John McCall43192862011-09-13 18:31:23 +000091 }
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) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400133 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
John McCallb61e14e2015-10-27 04:54:50 +0000134
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400135 // In ARC/MRC, look for an explicit ownership qualifier.
136 // For some reason, this only applies to __weak.
John McCallb61e14e2015-10-27 04:54:50 +0000137 } else if (auto ownership = T.getObjCLifetime()) {
138 switch (ownership) {
139 case Qualifiers::OCL_Weak:
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400140 return ObjCDeclSpec::DQ_PR_weak;
John McCallb61e14e2015-10-27 04:54:50 +0000141 case Qualifiers::OCL_Strong:
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400142 return ObjCDeclSpec::DQ_PR_strong;
John McCallb61e14e2015-10-27 04:54:50 +0000143 case Qualifiers::OCL_ExplicitNone:
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400144 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
John McCallb61e14e2015-10-27 04:54:50 +0000145 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 =
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400156 (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);
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000162
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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400168 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
169 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
170 result |= ObjCPropertyDecl::OBJC_PR_assign |
171 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000172 }
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();
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400186 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 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400192 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000193 // default is readwrite!
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400194 !(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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400280static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000281makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000282 unsigned attributesAsWritten = 0;
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400283 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
284 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
285 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
286 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
287 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
288 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
289 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
290 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
291 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
292 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
293 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
294 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
295 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
296 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
297 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
298 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
299 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
300 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
301 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
302 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
303 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
304 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
305 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
306 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
307 if (Attributes & ObjCDeclSpec::DQ_PR_class)
308 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
309 if (Attributes & ObjCDeclSpec::DQ_PR_direct)
310 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_direct;
Fangrui Song6907ce22018-07-30 19:24:48 +0000311
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400312 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000313}
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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400350 bool OldIsAtomic =
351 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 == 0;
353 bool NewIsAtomic =
354 (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();
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400363 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000364
365 // Is it nonatomic?
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400366 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() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400370 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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400378 const unsigned AtomicityMask =
379 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000380 if (PropagateAtomicity &&
381 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
382 unsigned Attrs = NewProperty->getPropertyAttributes();
383 Attrs = Attrs & ~AtomicityMask;
384 if (OldIsAtomic)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400385 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
Fangrui Song6907ce22018-07-30 19:24:48 +0000386 else
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400387 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000388
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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400441 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
442 (Attributes & ObjCDeclSpec::DQ_PR_class);
Manman Ren5b786402016-01-28 18:49:28 +0000443
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 =
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400467 (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;
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000472 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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400481 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();
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400489 Attributes |= ObjCDeclSpec::DQ_PR_getter;
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000490 }
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,
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400508 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
509 !(PIDecl->getPropertyAttributesAsWritten()
510 & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000511 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;
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400587 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
588 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000589 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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400599 if (getLangOpts().getGC() != LangOptions::NonGC &&
600 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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400628 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
629 (Attributes & ObjCDeclSpec::DQ_PR_class);
Manman Ren5b786402016-01-28 18:49:28 +0000630 // 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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400657 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Ted Kremenekac597f32010-03-12 00:46:40 +0000659
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400660 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
Ted Kremenekac597f32010-03-12 00:46:40 +0000662
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400663 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
Ted Kremenekac597f32010-03-12 00:46:40 +0000665
666 if (isReadWrite)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Ted Kremenekac597f32010-03-12 00:46:40 +0000668
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400669 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Ted Kremenekac597f32010-03-12 00:46:40 +0000671
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400672 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
John McCall31168b02011-06-15 23:02:42 +0000674
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400675 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
John McCall31168b02011-06-15 23:02:42 +0000677
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400678 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
679 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
Ted Kremenekac597f32010-03-12 00:46:40 +0000680
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400681 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
John McCall31168b02011-06-15 23:02:42 +0000683
Ted Kremenekac597f32010-03-12 00:46:40 +0000684 if (isAssign)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400685 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
Ted Kremenekac597f32010-03-12 00:46:40 +0000686
John McCall43192862011-09-13 18:31:23 +0000687 // In the semantic attributes, one of nonatomic or atomic is always set.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400688 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000690 else
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400691 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'.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400694 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
695 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000696 if (isAssign)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
John McCall31168b02011-06-15 23:02:42 +0000698
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
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400704 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
705 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
Douglas Gregor813a0662015-06-19 18:14:38 +0000706
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400707 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
708 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000709
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400710 if (Attributes & ObjCDeclSpec::DQ_PR_class)
711 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
Manman Ren387ff7f2016-01-26 18:52:43 +0000712
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400713 if ((Attributes & ObjCDeclSpec::DQ_PR_direct) ||
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800714 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()) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400718 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_direct);
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800719 } 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)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400784 << property->getDeclName()
785 << ivar->getDeclName()
786 << ((property->getPropertyAttributesAsWritten()
787 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
John McCall43192862011-09-13 18:31:23 +0000788 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'.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400818 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000819 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)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400825 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000826 else if (ivarLifetime == Qualifiers::OCL_Weak)
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400827 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000828}
Ted Kremenekac597f32010-03-12 00:46:40 +0000829
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400830static bool
831isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
832 ObjCPropertyDecl::PropertyAttributeKind Kind) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000833 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.
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400915 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;
Alex Lorenz61372552018-05-02 22:40:19 +0000921 if (HasOwnership &&
922 isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400923 ObjCPropertyDecl::OBJC_PR_copy)) {
924 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000925 continue;
926 }
Alex Lorenz61372552018-05-02 22:40:19 +0000927 if (HasOwnership && areIncompatiblePropertyAttributes(
928 OriginalAttributes, Attr,
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400929 ObjCPropertyDecl::OBJC_PR_retain |
930 ObjCPropertyDecl::OBJC_PR_strong)) {
931 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
932 ObjCPropertyDecl::OBJC_PR_strong),
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000933 "retain (or strong)");
934 continue;
935 }
936 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Puyan Lotfibbf386f2020-04-23 00:05:08 -0400937 ObjCPropertyDecl::OBJC_PR_atomic)) {
938 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000939 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(
Adrian Prantla1a9aa12019-12-05 11:25:46 -08001061 Context, AtLoc.isValid() ? AtLoc : Decl->getBeginLoc(),
1062 PropertyLoc.isValid() ? PropertyLoc : Decl->getEndLoc(),
1063 Decl->getSelector(), Decl->getReturnType(),
Adrian Prantl2073dd22019-11-04 14:28:14 -08001064 Decl->getReturnTypeSourceInfo(), Impl, Decl->isInstanceMethod(),
Adrian Prantla1a9aa12019-12-05 11:25:46 -08001065 Decl->isVariadic(), Decl->isPropertyAccessor(),
1066 /* isSynthesized*/ true, Decl->isImplicit(), Decl->isDefined(),
1067 Decl->getImplementationControl(), Decl->hasRelatedResultType());
Adrian Prantl2073dd22019-11-04 14:28:14 -08001068 ImplDecl->getMethodFamily();
1069 if (Decl->hasAttrs())
1070 ImplDecl->setAttrs(Decl->getAttrs());
1071 ImplDecl->setSelfDecl(Decl->getSelfDecl());
1072 ImplDecl->setCmdDecl(Decl->getCmdDecl());
1073 SmallVector<SourceLocation, 1> SelLocs;
1074 Decl->getSelectorLocs(SelLocs);
1075 ImplDecl->setMethodParams(Context, Decl->parameters(), SelLocs);
1076 ImplDecl->setLexicalDeclContext(Impl);
1077 ImplDecl->setDefined(false);
1078 return ImplDecl;
1079}
1080
Ted Kremenekac597f32010-03-12 00:46:40 +00001081/// ActOnPropertyImplDecl - This routine performs semantic checks and
1082/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +00001083/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +00001084///
John McCall48871652010-08-21 09:40:31 +00001085Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1086 SourceLocation AtLoc,
1087 SourceLocation PropertyLoc,
1088 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +00001089 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001090 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +00001091 SourceLocation PropertyIvarLoc,
1092 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +00001093 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +00001094 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +00001095 // Make sure we have a context for the property implementation declaration.
1096 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001097 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001098 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001099 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001100 if (PropertyIvarLoc.isInvalid())
1101 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +00001102 SourceLocation PropertyDiagLoc = PropertyLoc;
1103 if (PropertyDiagLoc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001104 PropertyDiagLoc = ClassImpDecl->getBeginLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00001105 ObjCPropertyDecl *property = nullptr;
1106 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001107 // Find the class or category class where this property must have
1108 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00001109 ObjCImplementationDecl *IC = nullptr;
1110 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1112 IDecl = IC->getClassInterface();
1113 // We always synthesize an interface for an implementation
1114 // without an interface decl. So, IDecl is always non-zero.
1115 assert(IDecl &&
1116 "ActOnPropertyImplDecl - @implementation without @interface");
1117
1118 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +00001119 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001120 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001121 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001122 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001123 }
Manman Rendfef4062016-01-29 19:16:39 +00001124 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001125 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +00001126 return nullptr;
1127 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001128 unsigned PIkind = property->getPropertyAttributesAsWritten();
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001129 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
1130 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001131 if (AtLoc.isValid())
1132 Diag(AtLoc, diag::warn_implicit_atomic_property);
1133 else
1134 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1135 Diag(property->getLocation(), diag::note_property_declare);
1136 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001137
Ted Kremenekac597f32010-03-12 00:46:40 +00001138 if (const ObjCCategoryDecl *CD =
1139 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1140 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +00001141 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001142 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +00001143 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001144 }
1145 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001146 if (Synthesize&&
1147 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
1148 property->hasAttr<IBOutletAttr>() &&
1149 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001150 bool ReadWriteProperty = false;
1151 // Search into the class extensions and see if 'readonly property is
1152 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001153 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001154 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1155 if (!R.empty())
1156 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1157 PIkind = ExtProp->getPropertyAttributesAsWritten();
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001158 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001159 ReadWriteProperty = true;
1160 break;
1161 }
1162 }
1163 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001165 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +00001166 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +00001167 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001168 SourceLocation readonlyLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00001169 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001170 property->getLParenLoc(), readonlyLoc)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001171 SourceLocation endLoc =
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001172 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1173 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001174 Diag(property->getLocation(),
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001175 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1176 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1177 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +00001178 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001179 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001180 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001181 property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1182 property);
1183
Ted Kremenekac597f32010-03-12 00:46:40 +00001184 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1185 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001186 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001187 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001188 }
1189 IDecl = CatImplClass->getClassInterface();
1190 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001191 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001192 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001193 }
1194 ObjCCategoryDecl *Category =
1195 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1196
1197 // If category for this implementation not found, it is an error which
1198 // has already been reported eralier.
1199 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001200 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001201 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001202 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001203 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001204 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001205 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001206 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001207 }
1208 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001209 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001210 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001211 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001212 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001213 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001214 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001215 // Check that we have a valid, previously declared ivar for @synthesize
1216 if (Synthesize) {
1217 // @synthesize
1218 if (!PropertyIvar)
1219 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001220 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1221 ObjCInterfaceDecl *ClassDeclared;
1222 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1223 QualType PropType = property->getType();
1224 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001225
1226 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001227 diag::err_incomplete_synthesized_property,
1228 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001229 Diag(property->getLocation(), diag::note_property_declare);
1230 CompleteTypeErr = true;
1231 }
1232
David Blaikiebbafb8a2012-03-11 07:00:24 +00001233 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001234 (property->getPropertyAttributesAsWritten() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001235 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001236 PropertyIvarType->isObjCRetainableType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001237 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001238 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001239
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001240 ObjCPropertyDecl::PropertyAttributeKind kind
1241 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001242
John McCall460ce582015-10-22 18:38:17 +00001243 bool isARCWeak = false;
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001244 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall460ce582015-10-22 18:38:17 +00001245 // Add GC __weak to the ivar type if the property is weak.
1246 if (getLangOpts().getGC() != LangOptions::NonGC) {
1247 assert(!getLangOpts().ObjCAutoRefCount);
1248 if (PropertyIvarType.isObjCGCStrong()) {
1249 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1250 Diag(property->getLocation(), diag::note_property_declare);
1251 } else {
1252 PropertyIvarType =
1253 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1254 }
1255
1256 // Otherwise, check whether ARC __weak is enabled and works with
1257 // the property type.
John McCall43192862011-09-13 18:31:23 +00001258 } else {
John McCall460ce582015-10-22 18:38:17 +00001259 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001260 // Only complain here when synthesizing an ivar.
1261 if (!Ivar) {
1262 Diag(PropertyDiagLoc,
1263 getLangOpts().ObjCWeakRuntime
1264 ? diag::err_synthesizing_arc_weak_property_disabled
1265 : diag::err_synthesizing_arc_weak_property_no_runtime);
1266 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001267 }
John McCallb61e14e2015-10-27 04:54:50 +00001268 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001269 } else {
1270 isARCWeak = true;
1271 if (const ObjCObjectPointerType *ObjT =
1272 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1273 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1274 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1275 Diag(property->getLocation(),
1276 diag::err_arc_weak_unavailable_property)
1277 << PropertyIvarType;
1278 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1279 << ClassImpDecl->getName();
1280 }
1281 }
1282 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001283 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001284 }
John McCall460ce582015-10-22 18:38:17 +00001285
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001286 if (AtLoc.isInvalid()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001287 // Check when default synthesizing a property that there is
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001288 // an ivar matching property name and issue warning; since this
1289 // is the most common case of not using an ivar used for backing
1290 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001291 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001292 ObjCIvarDecl *originalIvar =
1293 IDecl->lookupInstanceVariable(property->getIdentifier(),
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001294 ClassDeclared);
1295 if (originalIvar) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001296 Diag(PropertyDiagLoc,
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001297 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001298 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001299 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001300 Diag(property->getLocation(), diag::note_property_declare);
1301 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001302 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001303 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001304
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001305 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001306 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001307 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001308 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001309 !PropertyIvarType.getObjCLifetime() &&
1310 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001311
John McCall43192862011-09-13 18:31:23 +00001312 // It's an error if we have to do this and the user didn't
1313 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001314 if (!hasWrittenStorageAttribute(property, QueryKind) &&
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001315 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001316 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001317 diag::err_arc_objc_property_default_assign_on_object);
1318 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001319 } else {
1320 Qualifiers::ObjCLifetime lifetime =
1321 getImpliedARCOwnership(kind, PropertyIvarType);
1322 assert(lifetime && "no lifetime for property?");
Fangrui Song6907ce22018-07-30 19:24:48 +00001323
John McCall31168b02011-06-15 23:02:42 +00001324 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001325 qs.addObjCLifetime(lifetime);
Fangrui Song6907ce22018-07-30 19:24:48 +00001326 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
John McCall31168b02011-06-15 23:02:42 +00001327 }
John McCall31168b02011-06-15 23:02:42 +00001328 }
1329
Abramo Bagnaradff19302011-03-08 08:55:46 +00001330 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001331 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001332 PropertyIvarType, /*TInfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001333 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001334 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001335 if (RequireNonAbstractType(PropertyIvarLoc,
1336 PropertyIvarType,
1337 diag::err_abstract_type_in_decl,
1338 AbstractSynthesizedIvarType)) {
1339 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001340 // An abstract type is as bad as an incomplete type.
1341 CompleteTypeErr = true;
1342 }
Volodymyr Sapsai30680e92017-10-23 22:01:41 +00001343 if (!CompleteTypeErr) {
1344 const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
1345 if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) {
1346 Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
1347 << PropertyIvarType;
1348 CompleteTypeErr = true; // suppress later diagnostics about the ivar
1349 }
1350 }
Richard Smith81f5ade2016-12-15 02:28:18 +00001351 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001352 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001353 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001354 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001355
John McCall5fb5df92012-06-20 06:18:46 +00001356 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001357 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001358 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001359 // Note! I deliberately want it to fall thru so, we have a
1360 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001361 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001362 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001363 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001364 << property->getDeclName() << Ivar->getDeclName()
1365 << ClassDeclared->getDeclName();
1366 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001367 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001368 // Note! I deliberately want it to fall thru so more errors are caught.
1369 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001370 property->setPropertyIvarDecl(Ivar);
1371
Ted Kremenekac597f32010-03-12 00:46:40 +00001372 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1373
1374 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001375 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001376 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001377 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001378 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001379 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001380 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001381 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001382 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001383 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1384 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001385 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001386 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001387 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001388 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001389 << property->getDeclName() << PropType
1390 << Ivar->getDeclName() << IvarType;
1391 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001392 // Note! I deliberately want it to fall thru so, we have a
1393 // a property implementation and to avoid future warnings.
1394 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001395 else {
1396 // FIXME! Rules for properties are somewhat different that those
1397 // for assignments. Use a new routine to consolidate all cases;
1398 // specifically for property redeclarations as well as for ivars.
1399 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1400 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1401 if (lhsType != rhsType &&
1402 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001403 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001404 << property->getDeclName() << PropType
1405 << Ivar->getDeclName() << IvarType;
1406 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1407 // Fall thru - see previous comment
1408 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001409 }
1410 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001411 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001412 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001413 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001414 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001415 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001416 // Fall thru - see previous comment
1417 }
John McCall31168b02011-06-15 23:02:42 +00001418 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001419 if ((property->getType()->isObjCObjectPointerType() ||
1420 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001421 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001422 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001423 << property->getDeclName() << Ivar->getDeclName();
1424 // Fall thru - see previous comment
1425 }
1426 }
John McCall460ce582015-10-22 18:38:17 +00001427 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1428 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001429 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001430 } else if (PropertyIvar)
1431 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001432 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001433
Ted Kremenekac597f32010-03-12 00:46:40 +00001434 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1435 ObjCPropertyImplDecl *PIDecl =
1436 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1437 property,
1438 (Synthesize ?
1439 ObjCPropertyImplDecl::Synthesize
1440 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001441 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001442
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001443 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001444 PIDecl->setInvalidDecl();
1445
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001446 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1447 getterMethod->createImplicitParams(Context, IDecl);
Adrian Prantl2073dd22019-11-04 14:28:14 -08001448
1449 // Redeclare the getter within the implementation as DeclContext.
1450 if (Synthesize) {
1451 // If the method hasn't been overridden, create a synthesized implementation.
1452 ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1453 getterMethod->getSelector(), getterMethod->isInstanceMethod());
1454 if (!OMD)
1455 OMD = RedeclarePropertyAccessor(Context, IC, getterMethod, AtLoc,
1456 PropertyLoc);
1457 PIDecl->setGetterMethodDecl(OMD);
1458 }
Jim Lin466f8842020-02-18 10:48:38 +08001459
Eli Friedman169ec352012-05-01 22:26:06 +00001460 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001461 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001462 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1463 // returned by the getter as it must conform to C++'s copy-return rules.
1464 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001465 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001466 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001467 DeclRefExpr *SelfExpr = new (Context)
1468 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1469 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001470 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001471 Expr *LoadSelfExpr =
1472 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001473 CK_LValueToRValue, SelfExpr, nullptr,
1474 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001475 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001476 new (Context) ObjCIvarRefExpr(Ivar,
1477 Ivar->getUsageType(SelfDecl->getType()),
1478 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001479 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001480 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001481 ExprResult Res = PerformCopyInitialization(
1482 InitializedEntity::InitializeResult(PropertyDiagLoc,
1483 getterMethod->getReturnType(),
1484 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001485 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001486 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001487 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001488 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001489 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001490 PIDecl->setGetterCXXConstructor(ResExpr);
1491 }
1492 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001493 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1494 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001495 Diag(getterMethod->getLocation(),
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001496 diag::warn_property_getter_owning_mismatch);
1497 Diag(property->getLocation(), diag::note_property_declare);
1498 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001499 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1500 switch (getterMethod->getMethodFamily()) {
1501 case OMF_retain:
1502 case OMF_retainCount:
1503 case OMF_release:
1504 case OMF_autorelease:
1505 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1506 << 1 << getterMethod->getSelector();
1507 break;
1508 default:
1509 break;
1510 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001511 }
Adrian Prantl2073dd22019-11-04 14:28:14 -08001512
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001513 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1514 setterMethod->createImplicitParams(Context, IDecl);
Adrian Prantl2073dd22019-11-04 14:28:14 -08001515
1516 // Redeclare the setter within the implementation as DeclContext.
1517 if (Synthesize) {
1518 ObjCMethodDecl *OMD = ClassImpDecl->getMethod(
1519 setterMethod->getSelector(), setterMethod->isInstanceMethod());
1520 if (!OMD)
1521 OMD = RedeclarePropertyAccessor(Context, IC, setterMethod,
1522 AtLoc, PropertyLoc);
1523 PIDecl->setSetterMethodDecl(OMD);
1524 }
1525
Eli Friedman169ec352012-05-01 22:26:06 +00001526 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1527 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001528 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001529 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001530 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001531 DeclRefExpr *SelfExpr = new (Context)
1532 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1533 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001534 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001535 Expr *LoadSelfExpr =
1536 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001537 CK_LValueToRValue, SelfExpr, nullptr,
1538 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001539 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001540 new (Context) ObjCIvarRefExpr(Ivar,
1541 Ivar->getUsageType(SelfDecl->getType()),
1542 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001543 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001544 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001545 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1546 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001547 QualType T = Param->getType().getNonReferenceType();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001548 DeclRefExpr *rhs = new (Context)
1549 DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001550 MarkDeclRefReferenced(rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001551 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001552 BO_Assign, lhs, rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001553 if (property->getPropertyAttributes() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001554 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001555 Expr *callExpr = Res.getAs<Expr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001556 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001557 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1558 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001559 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001560 if (property->getType()->isReferenceType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001561 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001562 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001563 << property->getType();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001564 Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl)
1565 << FuncDecl;
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001566 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001567 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001568 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001569 }
1570 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001571
Ted Kremenekac597f32010-03-12 00:46:40 +00001572 if (IC) {
1573 if (Synthesize)
1574 if (ObjCPropertyImplDecl *PPIDecl =
1575 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001576 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001577 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1578 << PropertyIvar;
1579 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1580 }
1581
1582 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001583 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001584 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001585 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001586 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001587 }
1588 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001589 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001590 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001591 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001592 // Diagnose if an ivar was lazily synthesdized due to a previous
1593 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001594 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001595 ObjCInterfaceDecl *ClassDeclared=nullptr;
1596 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001597 if (!Synthesize)
1598 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1599 else {
1600 if (PropertyIvar && PropertyIvar != PropertyId)
1601 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1602 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001603 // Issue diagnostics only if Ivar belongs to current class.
Fangrui Song6907ce22018-07-30 19:24:48 +00001604 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001605 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001606 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
Fariborz Jahanian18722982010-07-17 00:59:30 +00001607 << PropertyId;
1608 Ivar->setInvalidDecl();
1609 }
1610 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001611 } else {
1612 if (Synthesize)
1613 if (ObjCPropertyImplDecl *PPIDecl =
1614 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001615 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001616 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1617 << PropertyIvar;
1618 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1619 }
1620
1621 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001622 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001623 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001624 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001625 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001626 }
1627 CatImplClass->addPropertyImplementation(PIDecl);
1628 }
1629
Pierre Habouzit3adcc782020-01-30 16:48:11 -08001630 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic &&
1631 PIDecl->getPropertyDecl() &&
1632 PIDecl->getPropertyDecl()->isDirectProperty()) {
1633 Diag(PropertyLoc, diag::err_objc_direct_dynamic_property);
1634 Diag(PIDecl->getPropertyDecl()->getLocation(),
1635 diag::note_previous_declaration);
1636 return nullptr;
1637 }
1638
John McCall48871652010-08-21 09:40:31 +00001639 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001640}
1641
1642//===----------------------------------------------------------------------===//
1643// Helper methods.
1644//===----------------------------------------------------------------------===//
1645
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001646/// DiagnosePropertyMismatch - Compares two properties for their
1647/// attributes and types and warns on a variety of inconsistencies.
1648///
1649void
1650Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1651 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001652 const IdentifierInfo *inheritedName,
1653 bool OverridingProtocolProperty) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001654 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1655 Property->getPropertyAttributes();
1656 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1657 SuperProperty->getPropertyAttributes();
Fangrui Song6907ce22018-07-30 19:24:48 +00001658
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001659 // We allow readonly properties without an explicit ownership
1660 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1661 // to be overridden by a property with any explicit ownership in the subclass.
1662 if (!OverridingProtocolProperty &&
1663 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1664 ;
1665 else {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001666 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1667 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001668 Diag(Property->getLocation(), diag::warn_readonly_property)
1669 << Property->getDeclName() << inheritedName;
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001670 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1671 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001672 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001673 << Property->getDeclName() << "copy" << inheritedName;
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001674 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1675 unsigned CAttrRetain =
1676 (CAttr &
1677 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1678 unsigned SAttrRetain =
1679 (SAttr &
1680 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001681 bool CStrong = (CAttrRetain != 0);
1682 bool SStrong = (SAttrRetain != 0);
1683 if (CStrong != SStrong)
1684 Diag(Property->getLocation(), diag::warn_property_attribute)
1685 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1686 }
John McCall31168b02011-06-15 23:02:42 +00001687 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001688
Douglas Gregor429183e2015-12-09 22:57:32 +00001689 // Check for nonatomic; note that nonatomic is effectively
1690 // meaningless for readonly properties, so don't diagnose if the
1691 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001692 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Alex Lorenz05a63ee2017-10-06 19:24:26 +00001693 // Readonly properties from protocols can be implemented as "readwrite"
1694 // with a custom setter name.
1695 if (Property->getSetterName() != SuperProperty->getSetterName() &&
1696 !(SuperProperty->isReadOnly() &&
1697 isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001698 Diag(Property->getLocation(), diag::warn_property_attribute)
1699 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001700 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1701 }
1702 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001703 Diag(Property->getLocation(), diag::warn_property_attribute)
1704 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001705 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1706 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001707
1708 QualType LHSType =
1709 Context.getCanonicalType(SuperProperty->getType());
1710 QualType RHSType =
1711 Context.getCanonicalType(Property->getType());
1712
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001713 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001714 // Do cases not handled in above.
1715 // FIXME. For future support of covariant property types, revisit this.
1716 bool IncompatibleObjC = false;
1717 QualType ConvertedType;
Fangrui Song6907ce22018-07-30 19:24:48 +00001718 if (!isObjCPointerConversion(RHSType, LHSType,
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001719 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001720 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001721 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1722 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001723 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1724 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001725 }
1726}
1727
1728bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1729 ObjCMethodDecl *GetterMethod,
1730 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001731 if (!GetterMethod)
1732 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001733 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001734 QualType PropertyRValueType =
1735 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1736 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001737 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001738 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1739 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001740 if ((propertyObjCPtr =
1741 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001742 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1743 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001744 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001745 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001746 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001747 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001748 << GetterMethod->getSelector() << GetterType;
1749 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1750 return true;
1751 } else {
1752 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001753 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001754 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1755 if (lhsType != rhsType && lhsType->isArithmeticType())
1756 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001757 }
1758 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001759
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001760 if (!compat) {
1761 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1762 << property->getDeclName()
1763 << GetterMethod->getSelector();
1764 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1765 return true;
1766 }
1767
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001768 return false;
1769}
1770
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001771/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001772/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001773static void
1774CollectImmediateProperties(ObjCContainerDecl *CDecl,
1775 ObjCContainerDecl::PropertyMap &PropMap,
1776 ObjCContainerDecl::PropertyMap &SuperPropMap,
1777 bool CollectClassPropsOnly = false,
1778 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001779 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001780 for (auto *Prop : IDecl->properties()) {
1781 if (CollectClassPropsOnly && !Prop->isClassProperty())
1782 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001783 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1784 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001785 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001786
1787 // Collect the properties from visible extensions.
1788 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001789 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1790 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001791
Ted Kremenek204c3c52014-02-22 00:02:03 +00001792 if (IncludeProtocols) {
1793 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001794 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001795 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1796 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001797 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001798 }
1799 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001800 for (auto *Prop : CATDecl->properties()) {
1801 if (CollectClassPropsOnly && !Prop->isClassProperty())
1802 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001803 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1804 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001805 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001806 if (IncludeProtocols) {
1807 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001808 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001809 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1810 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001811 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001812 }
1813 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001814 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001815 if (CollectClassPropsOnly && !Prop->isClassProperty())
1816 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001817 ObjCPropertyDecl *PropertyFromSuper =
1818 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1819 Prop->isClassProperty())];
Fangrui Song6907ce22018-07-30 19:24:48 +00001820 // Exclude property for protocols which conform to class's super-class,
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001821 // as super-class has to implement the property.
Fangrui Song6907ce22018-07-30 19:24:48 +00001822 if (!PropertyFromSuper ||
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001823 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001824 ObjCPropertyDecl *&PropEntry =
1825 PropMap[std::make_pair(Prop->getIdentifier(),
1826 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001827 if (!PropEntry)
1828 PropEntry = Prop;
1829 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001830 }
Manman Ren16a7d632016-04-12 23:01:55 +00001831 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001832 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001833 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1834 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001835 }
1836}
1837
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001838/// CollectSuperClassPropertyImplementations - This routine collects list of
1839/// properties to be implemented in super class(s) and also coming from their
1840/// conforming protocols.
1841static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001842 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001843 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001844 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001845 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001846 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001847 SDecl = SDecl->getSuperClass();
1848 }
1849 }
1850}
1851
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001852/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1853/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1854/// declared in class 'IFace'.
1855bool
1856Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1857 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1858 if (!IV->getSynthesize())
1859 return false;
1860 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1861 Method->isInstanceMethod());
1862 if (!IMD || !IMD->isPropertyAccessor())
1863 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001864
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001865 // look up a property declaration whose one of its accessors is implemented
1866 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001867 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001868 if ((Property->getGetterName() == IMD->getSelector() ||
1869 Property->getSetterName() == IMD->getSelector()) &&
1870 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001871 return true;
1872 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001873 // Also look up property declaration in class extension whose one of its
1874 // accessors is implemented by this method.
1875 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001876 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001877 if ((Property->getGetterName() == IMD->getSelector() ||
1878 Property->getSetterName() == IMD->getSelector()) &&
1879 (Property->getPropertyIvarDecl() == IV))
1880 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001881 return false;
1882}
1883
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001884static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1885 ObjCPropertyDecl *Prop) {
1886 bool SuperClassImplementsGetter = false;
1887 bool SuperClassImplementsSetter = false;
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001888 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001889 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001890
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001891 while (IDecl->getSuperClass()) {
1892 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1893 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1894 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001895
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001896 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1897 SuperClassImplementsSetter = true;
1898 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1899 return true;
1900 IDecl = IDecl->getSuperClass();
1901 }
1902 return false;
1903}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001904
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001905/// Default synthesizes all properties which must be synthesized
James Dennett2a4d13c2012-06-15 07:13:21 +00001906/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001907void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1908 ObjCInterfaceDecl *IDecl,
1909 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001910 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001911 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1912 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001913 if (PropMap.empty())
1914 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001915 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001916 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001917
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001918 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1919 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001920 // Is there a matching property synthesize/dynamic?
1921 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001922 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001923 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1924 continue;
1925 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001926 if (IMPDecl->FindPropertyImplDecl(
1927 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001928 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001929 ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Prop->getGetterName());
1930 if (ImpMethod && !ImpMethod->getBody()) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001931 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001932 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001933 ImpMethod = IMPDecl->getInstanceMethod(Prop->getSetterName());
1934 if (ImpMethod && !ImpMethod->getBody())
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001935 continue;
1936 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001937 if (ObjCPropertyImplDecl *PID =
1938 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001939 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1940 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001941 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001942 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001943 continue;
1944 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001945 ObjCPropertyDecl *PropInSuperClass =
1946 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1947 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001948 if (ObjCProtocolDecl *Proto =
1949 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001950 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001951 // Suppress the warning if class's superclass implements property's
1952 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001953 // Or, if property is going to be implemented in its super class.
1954 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001955 Diag(IMPDecl->getLocation(),
1956 diag::warn_auto_synthesizing_protocol_property)
1957 << Prop << Proto;
1958 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001959 std::string FixIt =
1960 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1961 Diag(AtEnd, diag::note_add_synthesize_directive)
1962 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001963 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001964 continue;
1965 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001966 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001967 if (PropInSuperClass) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001968 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001969 (PropInSuperClass->getPropertyAttributes() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001970 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001971 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1972 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1973 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1974 << Prop->getIdentifier();
1975 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Puyan Lotfibbf386f2020-04-23 00:05:08 -04001976 }
1977 else {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001978 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1979 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001980 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001981 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1982 }
1983 continue;
1984 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001985 // We use invalid SourceLocations for the synthesized ivars since they
1986 // aren't really synthesized at a particular location; they just exist.
1987 // Saying that they are located at the @implementation isn't really going
1988 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001989 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1990 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1991 true,
1992 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001993 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001994 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001995 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001996 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001997 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001998 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001999 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00002000}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002001
Alex Lorenz6c9af502017-07-03 10:12:24 +00002002void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
2003 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00002004 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002005 return;
2006 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
2007 if (!IC)
2008 return;
2009 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00002010 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00002011 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002012}
2013
Manman Ren08ce7342016-05-18 18:12:34 +00002014static void DiagnoseUnimplementedAccessor(
2015 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
2016 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
2017 ObjCPropertyDecl *Prop,
2018 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
2019 // Check to see if we have a corresponding selector in SMap and with the
2020 // right method type.
Fangrui Song75e74e02019-03-31 08:48:19 +00002021 auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) {
2022 return x->getSelector() == Method &&
2023 x->isClassMethod() == Prop->isClassProperty();
2024 });
Ted Kremenek7e812952014-02-21 19:41:30 +00002025 // When reporting on missing property setter/getter implementation in
2026 // categories, do not report when they are declared in primary class,
2027 // class's protocol, or one of it super classes. This is because,
2028 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00002029 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00002030 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00002031 !PrimaryClass->lookupPropertyAccessor(Method, C,
2032 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00002033 unsigned diag =
2034 isa<ObjCCategoryDecl>(CDecl)
2035 ? (Prop->isClassProperty()
2036 ? diag::warn_impl_required_in_category_for_class_property
2037 : diag::warn_setter_getter_impl_required_in_category)
2038 : (Prop->isClassProperty()
2039 ? diag::warn_impl_required_for_class_property
2040 : diag::warn_setter_getter_impl_required);
2041 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
2042 S.Diag(Prop->getLocation(), diag::note_property_declare);
2043 if (S.LangOpts.ObjCDefaultSynthProperties &&
2044 S.LangOpts.ObjCRuntime.isNonFragile())
2045 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
2046 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
2047 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
2048 }
Ted Kremenek7e812952014-02-21 19:41:30 +00002049}
2050
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002051void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00002052 ObjCContainerDecl *CDecl,
2053 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00002054 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00002055 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
2056
Manman Ren16a7d632016-04-12 23:01:55 +00002057 // Since we don't synthesize class properties, we should emit diagnose even
2058 // if SynthesizeProperties is true.
2059 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2060 // Gather properties which need not be implemented in this class
2061 // or category.
2062 if (!IDecl)
2063 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
2064 // For categories, no need to implement properties declared in
2065 // its primary class (and its super classes) if property is
2066 // declared in one of those containers.
2067 if ((IDecl = C->getClassInterface())) {
2068 ObjCInterfaceDecl::PropertyDeclOrder PO;
2069 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002070 }
Manman Ren16a7d632016-04-12 23:01:55 +00002071 }
2072 if (IDecl)
2073 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00002074
Manman Ren16a7d632016-04-12 23:01:55 +00002075 // When SynthesizeProperties is true, we only check class properties.
2076 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2077 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002078
Ted Kremenek38882022014-02-21 19:41:39 +00002079 // Scan the @interface to see if any of the protocols it adopts
2080 // require an explicit implementation, via attribute
2081 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00002082 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00002083 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002084
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002085 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00002086 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2087 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002088 // Lazily construct a set of all the properties in the @interface
2089 // of the class, without looking at the superclass. We cannot
2090 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00002091 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00002092 // as scans the adopted protocols. This work only triggers for protocols
2093 // with the attribute, which is very rare, and only occurs when
2094 // analyzing the @implementation.
2095 if (!LazyMap) {
2096 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2097 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2098 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002099 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002100 /* IncludeProtocols */ false);
2101 }
Ted Kremenek38882022014-02-21 19:41:39 +00002102 // Add the properties of 'PDecl' to the list of properties that
2103 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002104 for (auto *PropDecl : PDecl->properties()) {
2105 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2106 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002107 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002108 PropMap[std::make_pair(PropDecl->getIdentifier(),
2109 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002110 }
2111 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002112 }
Ted Kremenek38882022014-02-21 19:41:39 +00002113
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002114 if (PropMap.empty())
2115 return;
2116
2117 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002118 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002119 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002120
Manman Ren08ce7342016-05-18 18:12:34 +00002121 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002122 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002123 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002124 InsMap.insert(I);
Fangrui Song6907ce22018-07-30 19:24:48 +00002125
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002126 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002127 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002128 if (C && !C->IsClassExtension())
2129 if ((PrimaryClass = C->getClassInterface()))
2130 // Report unimplemented properties in the category as well.
2131 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2132 // When reporting on missing setter/getters, do not report when
2133 // setter/getter is implemented in category's primary class
2134 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002135 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002136 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002137 }
2138
Anna Zaks673d76b2012-10-18 19:17:53 +00002139 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002140 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2141 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002142 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002143 if (Prop->isInvalidDecl() ||
2144 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002145 PropImplMap.count(Prop) ||
2146 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002148
2149 // Diagnose unimplemented getters and setters.
2150 DiagnoseUnimplementedAccessor(*this,
2151 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2152 if (!Prop->isReadOnly())
2153 DiagnoseUnimplementedAccessor(*this,
2154 PrimaryClass, Prop->getSetterName(),
2155 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002156 }
2157}
2158
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002159void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002160 for (const auto *propertyImpl : impDecl->property_impls()) {
2161 const auto *property = propertyImpl->getPropertyDecl();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002162 // Warn about null_resettable properties with synthesized setters,
2163 // because the setter won't properly handle nil.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002164 if (propertyImpl->getPropertyImplementation()
2165 == ObjCPropertyImplDecl::Synthesize &&
Douglas Gregor849ebc22015-06-19 18:14:46 +00002166 (property->getPropertyAttributes() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002167 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2168 property->getGetterMethodDecl() &&
2169 property->getSetterMethodDecl()) {
Adrian Prantl2073dd22019-11-04 14:28:14 -08002170 auto *getterImpl = propertyImpl->getGetterMethodDecl();
2171 auto *setterImpl = propertyImpl->getSetterMethodDecl();
2172 if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) &&
2173 (!setterImpl || setterImpl->isSynthesizedAccessorStub())) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002174 SourceLocation loc = propertyImpl->getLocation();
2175 if (loc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002176 loc = impDecl->getBeginLoc();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002177
2178 Diag(loc, diag::warn_null_resettable_setter)
Adrian Prantl2073dd22019-11-04 14:28:14 -08002179 << setterImpl->getSelector() << property->getDeclName();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002180 }
2181 }
2182 }
2183}
2184
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002185void
2186Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002187 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002188 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002189 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002190 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002191 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002192 for (auto *Prop : IDecl->properties())
2193 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002194 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002195 for (auto *Prop : Ext->properties())
2196 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fangrui Song6907ce22018-07-30 19:24:48 +00002197
Manman Renefe1bac2016-01-27 20:00:32 +00002198 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2199 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002200 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002201 ObjCMethodDecl *GetterMethod = nullptr;
2202 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002203
Bill Wendling44426052012-12-20 19:22:21 +00002204 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002205 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002206
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002207 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2208 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002209 GetterMethod = Property->isClassProperty() ?
2210 IMPDecl->getClassMethod(Property->getGetterName()) :
2211 IMPDecl->getInstanceMethod(Property->getGetterName());
2212 SetterMethod = Property->isClassProperty() ?
2213 IMPDecl->getClassMethod(Property->getSetterName()) :
2214 IMPDecl->getInstanceMethod(Property->getSetterName());
Adrian Prantl2073dd22019-11-04 14:28:14 -08002215 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2216 GetterMethod = nullptr;
2217 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2218 SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002219 if (GetterMethod) {
2220 Diag(GetterMethod->getLocation(),
2221 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002222 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002223 Diag(Property->getLocation(), diag::note_property_declare);
2224 }
2225 if (SetterMethod) {
2226 Diag(SetterMethod->getLocation(),
2227 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002228 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002229 Diag(Property->getLocation(), diag::note_property_declare);
2230 }
2231 }
2232
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002233 // We only care about readwrite atomic property.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002234 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2235 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002236 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002237 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2238 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002239 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2240 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08002241 GetterMethod = PIDecl->getGetterMethodDecl();
2242 SetterMethod = PIDecl->getSetterMethodDecl();
2243 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2244 GetterMethod = nullptr;
2245 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2246 SetterMethod = nullptr;
2247 if ((bool)GetterMethod ^ (bool)SetterMethod) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002248 SourceLocation MethodLoc =
2249 (GetterMethod ? GetterMethod->getLocation()
2250 : SetterMethod->getLocation());
2251 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002252 << Property->getIdentifier() << (GetterMethod != nullptr)
2253 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002254 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002255 if (Property->getLParenLoc().isValid() &&
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002256 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002257 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002258 SourceLocation AfterLParen =
2259 getLocForEndOfToken(Property->getLParenLoc());
2260 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2261 : "nonatomic";
2262 Diag(Property->getLocation(),
2263 diag::note_atomic_property_fixup_suggest)
2264 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2265 } else if (Property->getLParenLoc().isInvalid()) {
2266 //@property id etc.
Fangrui Song6907ce22018-07-30 19:24:48 +00002267 SourceLocation startLoc =
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002268 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2269 Diag(Property->getLocation(),
2270 diag::note_atomic_property_fixup_suggest)
2271 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002272 }
2273 else
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002274 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002275 Diag(Property->getLocation(), diag::note_property_declare);
2276 }
2277 }
2278 }
2279}
2280
John McCall31168b02011-06-15 23:02:42 +00002281void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002282 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002283 return;
2284
Aaron Ballmand85eff42014-03-14 15:02:45 +00002285 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002286 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002287 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Adrian Prantl2073dd22019-11-04 14:28:14 -08002288 !PD->isClassProperty()) {
2289 ObjCMethodDecl *IM = PID->getGetterMethodDecl();
2290 if (IM && !IM->isSynthesizedAccessorStub())
2291 continue;
John McCall31168b02011-06-15 23:02:42 +00002292 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2293 if (!method)
2294 continue;
2295 ObjCMethodFamily family = method->getMethodFamily();
2296 if (family == OMF_alloc || family == OMF_copy ||
2297 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002298 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002299 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002300 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002301 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002302
2303 // Look for a getter explicitly declared alongside the property.
2304 // If we find one, use its location for the note.
2305 SourceLocation noteLoc = PD->getLocation();
2306 SourceLocation fixItLoc;
2307 for (auto *getterRedecl : method->redecls()) {
2308 if (getterRedecl->isImplicit())
2309 continue;
2310 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2311 continue;
2312 noteLoc = getterRedecl->getLocation();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002313 fixItLoc = getterRedecl->getEndLoc();
Jordan Rosea34d04d2015-01-16 23:04:31 +00002314 }
2315
2316 Preprocessor &PP = getPreprocessor();
2317 TokenValue tokens[] = {
2318 tok::kw___attribute, tok::l_paren, tok::l_paren,
2319 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2320 PP.getIdentifierInfo("none"), tok::r_paren,
2321 tok::r_paren, tok::r_paren
2322 };
2323 StringRef spelling = "__attribute__((objc_method_family(none)))";
2324 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2325 if (!macroName.empty())
2326 spelling = macroName;
2327
2328 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2329 << method->getDeclName() << spelling;
2330 if (fixItLoc.isValid()) {
2331 SmallString<64> fixItText(" ");
2332 fixItText += spelling;
2333 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2334 }
John McCall31168b02011-06-15 23:02:42 +00002335 }
2336 }
2337 }
2338}
2339
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002340void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002341 const ObjCImplementationDecl *ImplD,
2342 const ObjCInterfaceDecl *IFD) {
2343 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002344 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2345 if (!SuperD)
2346 return;
2347
2348 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002349 for (const auto *I : ImplD->instance_methods())
2350 if (I->getMethodFamily() == OMF_init)
2351 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002352
2353 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2354 SuperD->getDesignatedInitializers(DesignatedInits);
2355 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2356 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2357 const ObjCMethodDecl *MD = *I;
2358 if (!InitSelSet.count(MD->getSelector())) {
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002359 // Don't emit a diagnostic if the overriding method in the subclass is
2360 // marked as unavailable.
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002361 bool Ignore = false;
2362 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2363 Ignore = IMD->isUnavailable();
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002364 } else {
2365 // Check the methods declared in the class extensions too.
2366 for (auto *Ext : IFD->visible_extensions())
2367 if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) {
2368 Ignore = IMD->isUnavailable();
2369 break;
2370 }
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002371 }
2372 if (!Ignore) {
2373 Diag(ImplD->getLocation(),
2374 diag::warn_objc_implementation_missing_designated_init_override)
2375 << MD->getSelector();
2376 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2377 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002378 }
2379 }
2380}
2381
John McCallad31b5f2010-11-10 07:01:40 +00002382/// AddPropertyAttrs - Propagates attributes from a property to the
2383/// implicitly-declared getter or setter for that property.
2384static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2385 ObjCPropertyDecl *Property) {
2386 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002387 for (const auto *A : Property->attrs()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002388 if (isa<DeprecatedAttr>(A) ||
2389 isa<UnavailableAttr>(A) ||
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002390 isa<AvailabilityAttr>(A))
2391 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002392 }
John McCallad31b5f2010-11-10 07:01:40 +00002393}
2394
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002395/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2396/// have the property type and issue diagnostics if they don't.
2397/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002398/// appropriate lookup tables.
2399void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002400 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002401 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002402 if (CD->isInvalidDecl())
2403 return;
2404
Manman Rend36f7d52016-01-27 20:10:32 +00002405 bool IsClassProperty = property->isClassProperty();
2406 GetterMethod = IsClassProperty ?
2407 CD->getClassMethod(property->getGetterName()) :
2408 CD->getInstanceMethod(property->getGetterName());
2409
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002410 // if setter or getter is not found in class extension, it might be
2411 // in the primary class.
2412 if (!GetterMethod)
2413 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2414 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002415 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2416 getClassMethod(property->getGetterName()) :
2417 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002418 getInstanceMethod(property->getGetterName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002419
Manman Rend36f7d52016-01-27 20:10:32 +00002420 SetterMethod = IsClassProperty ?
2421 CD->getClassMethod(property->getSetterName()) :
2422 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002423 if (!SetterMethod)
2424 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2425 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002426 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2427 getClassMethod(property->getSetterName()) :
2428 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002429 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002430 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2431 property->getLocation());
2432
Pierre Habouzit3adcc782020-01-30 16:48:11 -08002433 // synthesizing accessors must not result in a direct method that is not
2434 // monomorphic
2435 if (!GetterMethod) {
2436 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) {
2437 auto *ExistingGetter = CatDecl->getClassInterface()->lookupMethod(
2438 property->getGetterName(), !IsClassProperty, true, false, CatDecl);
2439 if (ExistingGetter) {
2440 if (ExistingGetter->isDirectMethod() || property->isDirectProperty()) {
2441 Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl)
2442 << property->isDirectProperty() << 1 /* property */
2443 << ExistingGetter->isDirectMethod()
2444 << ExistingGetter->getDeclName();
2445 Diag(ExistingGetter->getLocation(), diag::note_previous_declaration);
2446 }
2447 }
2448 }
2449 }
2450
2451 if (!property->isReadOnly() && !SetterMethod) {
2452 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) {
2453 auto *ExistingSetter = CatDecl->getClassInterface()->lookupMethod(
2454 property->getSetterName(), !IsClassProperty, true, false, CatDecl);
2455 if (ExistingSetter) {
2456 if (ExistingSetter->isDirectMethod() || property->isDirectProperty()) {
2457 Diag(property->getLocation(), diag::err_objc_direct_duplicate_decl)
2458 << property->isDirectProperty() << 1 /* property */
2459 << ExistingSetter->isDirectMethod()
2460 << ExistingSetter->getDeclName();
2461 Diag(ExistingSetter->getLocation(), diag::note_previous_declaration);
2462 }
2463 }
2464 }
2465 }
2466
Alex Lorenz535571a2017-03-30 13:33:51 +00002467 if (!property->isReadOnly() && SetterMethod) {
2468 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2469 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002470 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2471 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002472 !Context.hasSameUnqualifiedType(
Fangrui Song6907ce22018-07-30 19:24:48 +00002473 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002474 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002475 Diag(property->getLocation(),
2476 diag::warn_accessor_property_type_mismatch)
2477 << property->getDeclName()
2478 << SetterMethod->getSelector();
2479 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2480 }
2481 }
2482
2483 // Synthesize getter/setter methods if none exist.
2484 // Find the default getter and if one not found, add one.
2485 // FIXME: The synthesized property we set here is misleading. We almost always
2486 // synthesize these methods unless the user explicitly provided prototypes
2487 // (which is odd, but allowed). Sema should be typechecking that the
2488 // declarations jive in that situation (which it is not currently).
2489 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002490 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002491 // Declare a getter method and add it to the list of methods
2492 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002493 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002494
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002495 // The getter returns the declared property type with all qualifiers
2496 // removed.
2497 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2498
Douglas Gregor849ebc22015-06-19 18:14:46 +00002499 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002500 if (property->getPropertyAttributes() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002501 ObjCPropertyDecl::OBJC_PR_null_resettable) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002502 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002503 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002504 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002505 resultTy = Context.getAttributedType(attr::TypeNonNull,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002506 modifiedTy, modifiedTy);
2507 }
2508 }
2509
Adrian Prantl2073dd22019-11-04 14:28:14 -08002510 GetterMethod = ObjCMethodDecl::Create(
2511 Context, Loc, Loc, property->getGetterName(), resultTy, nullptr, CD,
2512 !IsClassProperty, /*isVariadic=*/false,
2513 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
2514 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
2515 (property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2516 ? ObjCMethodDecl::Optional
2517 : ObjCMethodDecl::Required);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002518 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002519
2520 AddPropertyAttrs(*this, GetterMethod, property);
2521
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002522 if (property->isDirectProperty())
2523 GetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2524
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002525 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002526 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2527 Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002528
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002529 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2530 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002531 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002532
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002533 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002534 GetterMethod->addAttr(SectionAttr::CreateImplicit(
2535 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2536 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002537
2538 if (getLangOpts().ObjCAutoRefCount)
2539 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002540 } else
2541 // A user declared getter will be synthesize when @synthesize of
2542 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002543 GetterMethod->setPropertyAccessor(true);
Pierre Habouzit1646bb82019-12-09 14:27:57 -08002544
2545 GetterMethod->createImplicitParams(Context,
2546 GetterMethod->getClassInterface());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002547 property->setGetterMethodDecl(GetterMethod);
2548
2549 // Skip setter if property is read-only.
2550 if (!property->isReadOnly()) {
2551 // Find the default setter and if one not found, add one.
2552 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002553 // No instance/class method of same name as property setter name was
2554 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002555 // Declare a setter method and add it to the list of methods
2556 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002557 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002558
2559 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002560 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002561 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002562 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002563 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002564 /*isPropertyAccessor=*/true,
Adrian Prantl2073dd22019-11-04 14:28:14 -08002565 /*isSynthesizedAccessorStub=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002566 /*isImplicitlyDeclared=*/true,
2567 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002568 (property->getPropertyImplementation() ==
2569 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002570 ObjCMethodDecl::Optional :
2571 ObjCMethodDecl::Required);
2572
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002573 // Remove all qualifiers from the setter's parameter type.
2574 QualType paramTy =
2575 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2576
Douglas Gregor849ebc22015-06-19 18:14:46 +00002577 // If the property is null_resettable, the setter accepts a
2578 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002579 if (property->getPropertyAttributes() &
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002580 ObjCPropertyDecl::OBJC_PR_null_resettable) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002581 QualType modifiedTy = paramTy;
2582 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2583 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002584 paramTy = Context.getAttributedType(attr::TypeNullable,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002585 modifiedTy, modifiedTy);
2586 }
2587 }
2588
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002589 // Invent the arguments for the setter. We don't bother making a
2590 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002591 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2592 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002593 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002594 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002595 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002596 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002597 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002598 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002599
2600 AddPropertyAttrs(*this, SetterMethod, property);
2601
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002602 if (property->isDirectProperty())
2603 SetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2604
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002605 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002606 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002607 SetterMethod->addAttr(SectionAttr::CreateImplicit(
2608 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2609 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002610 // It's possible for the user to have set a very odd custom
2611 // setter selector that causes it to have a method family.
2612 if (getLangOpts().ObjCAutoRefCount)
2613 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002614 } else
2615 // A user declared setter will be synthesize when @synthesize of
2616 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002617 SetterMethod->setPropertyAccessor(true);
Pierre Habouzit1646bb82019-12-09 14:27:57 -08002618
2619 SetterMethod->createImplicitParams(Context,
2620 SetterMethod->getClassInterface());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002621 property->setSetterMethodDecl(SetterMethod);
2622 }
2623 // Add any synthesized methods to the global pool. This allows us to
2624 // handle the following, which is supported by GCC (and part of the design).
2625 //
2626 // @interface Foo
2627 // @property double bar;
2628 // @end
2629 //
2630 // void thisIsUnfortunate() {
2631 // id foo;
2632 // double bar = [foo bar];
2633 // }
2634 //
Manman Rend36f7d52016-01-27 20:10:32 +00002635 if (!IsClassProperty) {
2636 if (GetterMethod)
2637 AddInstanceMethodToGlobalPool(GetterMethod);
2638 if (SetterMethod)
2639 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002640 } else {
2641 if (GetterMethod)
2642 AddFactoryMethodToGlobalPool(GetterMethod);
2643 if (SetterMethod)
2644 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002645 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002646
2647 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2648 if (!CurrentClass) {
2649 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2650 CurrentClass = Cat->getClassInterface();
2651 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2652 CurrentClass = Impl->getClassInterface();
2653 }
2654 if (GetterMethod)
2655 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2656 if (SetterMethod)
2657 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002658}
2659
John McCall48871652010-08-21 09:40:31 +00002660void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002661 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002662 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002663 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002664 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002665 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002666 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00002667
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002668 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2669 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002670 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2671 << "readonly" << "readwrite";
Fangrui Song6907ce22018-07-30 19:24:48 +00002672
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002673 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2674 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002675
2676 // Check for copy or retain on non-object types.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002677 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2678 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
John McCall31168b02011-06-15 23:02:42 +00002679 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002680 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002681 Diag(Loc, diag::err_objc_property_requires_object)
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002682 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2683 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2684 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2685 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002686 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002687 }
2688
John McCall52a503d2018-09-05 19:02:00 +00002689 // Check for assign on object types.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002690 if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2691 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
John McCall52a503d2018-09-05 19:02:00 +00002692 PropertyTy->isObjCRetainableType() &&
2693 !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2694 Diag(Loc, diag::warn_objc_property_assign_on_object);
2695 }
2696
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002697 // Check for more than one of { assign, copy, retain }.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002698 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2699 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002700 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2701 << "assign" << "copy";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002702 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002703 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002704 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002705 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2706 << "assign" << "retain";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002707 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002708 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002709 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002710 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2711 << "assign" << "strong";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002712 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002713 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002714 if (getLangOpts().ObjCAutoRefCount &&
2715 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002716 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2717 << "assign" << "weak";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002718 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002719 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002720 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002721 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002722 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2723 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002724 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2725 << "unsafe_unretained" << "copy";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002726 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002727 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002728 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002729 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2730 << "unsafe_unretained" << "retain";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002731 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002732 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002733 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002734 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2735 << "unsafe_unretained" << "strong";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002736 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002737 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002738 if (getLangOpts().ObjCAutoRefCount &&
2739 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002740 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2741 << "unsafe_unretained" << "weak";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002742 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002743 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002744 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2745 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002746 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2747 << "copy" << "retain";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002748 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002749 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002750 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002751 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2752 << "copy" << "strong";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002753 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002754 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002755 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002756 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2757 << "copy" << "weak";
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002758 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002759 }
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002760 }
2761 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2762 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2763 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2764 << "retain" << "weak";
2765 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2766 }
2767 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2768 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2769 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2770 << "strong" << "weak";
2771 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002772 }
2773
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002774 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002775 // 'weak' and 'nonnull' are mutually exclusive.
2776 if (auto nullability = PropertyTy->getNullability(Context)) {
2777 if (*nullability == NullabilityKind::NonNull)
2778 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2779 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002780 }
2781 }
2782
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002783 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2784 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2785 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2786 << "atomic" << "nonatomic";
2787 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002788 }
2789
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002790 // Warn if user supplied no assignment attribute, property is
2791 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002792 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002793 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
John McCallb61e14e2015-10-27 04:54:50 +00002794 // do nothing
2795 } else if (getLangOpts().ObjCAutoRefCount) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002796 // With arc, @property definitions should default to strong when
John McCallb61e14e2015-10-27 04:54:50 +00002797 // not specified.
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002798 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
John McCallb61e14e2015-10-27 04:54:50 +00002799 } else if (PropertyTy->isObjCObjectPointerType()) {
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002800 bool isAnyClassTy =
2801 (PropertyTy->isObjCClassType() ||
2802 PropertyTy->isObjCQualifiedClassType());
2803 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2804 // issue any warning.
2805 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
2806 ;
2807 else if (propertyInPrimaryClass) {
2808 // Don't issue warning on property with no life time in class
2809 // extension as it is inherited from property in primary class.
2810 // Skip this warning in gc-only mode.
2811 if (getLangOpts().getGC() != LangOptions::GCOnly)
2812 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002813
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002814 // If non-gc code warn that this is likely inappropriate.
2815 if (getLangOpts().getGC() == LangOptions::NonGC)
2816 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
2817 }
John McCallb61e14e2015-10-27 04:54:50 +00002818 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002819
2820 // FIXME: Implement warning dependent on NSCopying being
2821 // implemented. See also:
2822 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2823 // (please trim this list while you are at it).
2824 }
2825
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002826 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2827 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
2828 && getLangOpts().getGC() == LangOptions::GCOnly
2829 && PropertyTy->isBlockPointerType())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002830 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002831 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2832 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2833 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002834 PropertyTy->isBlockPointerType())
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002835 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fangrui Song6907ce22018-07-30 19:24:48 +00002836
Puyan Lotfibbf386f2020-04-23 00:05:08 -04002837 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2838 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002839 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002840}