blob: f6717f4cbe5e2fbb3779867e4e0a6079c1ba57dc [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremenek7a7a0802010-03-12 00:38:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis for Objective C @property and
10// @synthesize declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000015#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/ExprCXX.h"
18#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000019#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000021#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000025
26using namespace clang;
27
Ted Kremenekac597f32010-03-12 00:46:40 +000028//===----------------------------------------------------------------------===//
29// Grammar actions.
30//===----------------------------------------------------------------------===//
31
John McCall43192862011-09-13 18:31:23 +000032/// getImpliedARCOwnership - Given a set of property attributes and a
33/// type, infer an expected lifetime. The type's ownership qualification
34/// is not considered.
35///
36/// Returns OCL_None if the attributes as stated do not imply an ownership.
37/// Never returns OCL_Autoreleasing.
38static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
41 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
43 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000046 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
48 return Qualifiers::OCL_Weak;
49 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
50 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
55 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
56 type->isObjCRetainableType()) {
57 return Qualifiers::OCL_ExplicitNone;
58 }
59
60 return Qualifiers::OCL_None;
61}
62
John McCallb61e14e2015-10-27 04:54:50 +000063/// Check the internal consistency of a property declaration with
64/// an explicit ownership qualifier.
65static void checkPropertyDeclWithOwnership(Sema &S,
66 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000067 if (property->isInvalidDecl()) return;
68
69 ObjCPropertyDecl::PropertyAttributeKind propertyKind
70 = property->getPropertyAttributes();
71 Qualifiers::ObjCLifetime propertyLifetime
72 = property->getType().getObjCLifetime();
73
John McCallb61e14e2015-10-27 04:54:50 +000074 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000075
John McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000106/// Check this Objective-C property against a property declared in the
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// given protocol.
108static void
109CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
110 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000111 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000112 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000113 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000114 return;
115
116 // Look for a property with the same name.
117 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
118 for (unsigned I = 0, N = R.size(); I != N; ++I) {
119 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000120 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000121 return;
122 }
123 }
124
125 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000126 for (auto *P : Proto->protocols())
127 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000128}
129
John McCallb61e14e2015-10-27 04:54:50 +0000130static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
131 // In GC mode, just look for the __weak qualifier.
132 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
133 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
134
135 // In ARC/MRC, look for an explicit ownership qualifier.
136 // For some reason, this only applies to __weak.
137 } else if (auto ownership = T.getObjCLifetime()) {
138 switch (ownership) {
139 case Qualifiers::OCL_Weak:
140 return ObjCDeclSpec::DQ_PR_weak;
141 case Qualifiers::OCL_Strong:
142 return ObjCDeclSpec::DQ_PR_strong;
143 case Qualifiers::OCL_ExplicitNone:
144 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
145 case Qualifiers::OCL_Autoreleasing:
146 case Qualifiers::OCL_None:
147 return 0;
148 }
149 llvm_unreachable("bad qualifier");
150 }
151
152 return 0;
153}
154
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000155static const unsigned OwnershipMask =
156 (ObjCPropertyDecl::OBJC_PR_assign |
157 ObjCPropertyDecl::OBJC_PR_retain |
158 ObjCPropertyDecl::OBJC_PR_copy |
159 ObjCPropertyDecl::OBJC_PR_weak |
160 ObjCPropertyDecl::OBJC_PR_strong |
161 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
162
John McCallb61e14e2015-10-27 04:54:50 +0000163static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000164 unsigned result = attr & OwnershipMask;
165
166 // From an ownership perspective, assign and unsafe_unretained are
167 // identical; make sure one also implies the other.
168 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
169 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
170 result |= ObjCPropertyDecl::OBJC_PR_assign |
171 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
172 }
173
174 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000175}
176
John McCall48871652010-08-21 09:40:31 +0000177Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000178 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000179 FieldDeclarator &FD,
180 ObjCDeclSpec &ODS,
181 Selector GetterSel,
182 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000183 tok::ObjCKeywordKind MethodImplKind,
184 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000185 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000186 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000187 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
188 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000189 if (!getOwnershipRule(Attributes)) {
190 Attributes |= deducePropertyOwnershipFromType(*this, T);
191 }
Bill Wendling44426052012-12-20 19:22:21 +0000192 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000193 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000194 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000195
Douglas Gregor90d34422013-01-21 19:05:22 +0000196 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000197 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000198 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000199 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000200 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000202 FD,
203 GetterSel, ODS.getGetterNameLoc(),
204 SetterSel, ODS.getSetterNameLoc(),
205 isReadWrite, Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000206 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000207 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000208 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000209 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000210 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000211 }
212
213 if (!Res) {
214 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000215 GetterSel, ODS.getGetterNameLoc(), SetterSel,
216 ODS.getSetterNameLoc(), isReadWrite, Attributes,
217 ODS.getPropertyAttributes(), T, TSI,
218 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000219 if (lexicalDC)
220 Res->setLexicalDeclContext(lexicalDC);
221 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000222
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000223 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000224 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000225 (isa<ObjCInterfaceDecl>(ClassDecl) ||
226 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000227
John McCallb61e14e2015-10-27 04:54:50 +0000228 // Check consistency if the type has explicit ownership qualification.
229 if (Res->getType().getObjCLifetime())
230 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000231
Douglas Gregorb8982092013-01-21 19:42:21 +0000232 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000233 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000234 // For a class, compare the property against a property in our superclass.
235 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000236 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
237 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000238 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000239 for (unsigned I = 0, N = R.size(); I != N; ++I) {
240 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000241 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000242 FoundInSuper = true;
243 break;
244 }
245 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000246 if (FoundInSuper)
247 break;
248 else
249 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000250 }
251
252 if (FoundInSuper) {
253 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000254 for (auto *P : CurrentInterfaceDecl->protocols()) {
255 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000256 }
257 } else {
258 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000259 for (auto *P : IFace->all_referenced_protocols()) {
260 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000261 }
262 }
263 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000264 // We don't check if class extension. Because properties in class extension
265 // are meant to override some of the attributes and checking has already done
266 // when property in class extension is constructed.
267 if (!Cat->IsClassExtension())
268 for (auto *P : Cat->protocols())
269 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000270 } else {
271 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000272 for (auto *P : Proto->protocols())
273 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000274 }
275
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000276 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000277 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000278}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000279
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000280static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000281makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000282 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000283 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000284 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000285 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000286 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000287 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000288 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000289 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000290 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000291 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000292 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000293 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000294 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000295 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000296 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000297 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000298 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000299 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000300 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000301 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000302 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000303 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000304 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000305 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000306 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000307 if (Attributes & ObjCDeclSpec::DQ_PR_class)
308 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800309 if (Attributes & ObjCDeclSpec::DQ_PR_direct)
310 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_direct;
Fangrui Song6907ce22018-07-30 19:24:48 +0000311
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000312 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
313}
314
Fangrui Song6907ce22018-07-30 19:24:48 +0000315static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000316 SourceLocation LParenLoc, SourceLocation &Loc) {
317 if (LParenLoc.isMacroID())
318 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000319
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000320 SourceManager &SM = Context.getSourceManager();
321 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
322 // Try to load the file buffer.
323 bool invalidTemp = false;
324 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
325 if (invalidTemp)
326 return false;
327 const char *tokenBegin = file.data() + locInfo.second;
Fangrui Song6907ce22018-07-30 19:24:48 +0000328
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000329 // Lex from the start of the given location.
330 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
331 Context.getLangOpts(),
332 file.begin(), tokenBegin, file.end());
333 Token Tok;
334 do {
335 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000336 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000337 Loc = Tok.getLocation();
338 return true;
339 }
340 } while (Tok.isNot(tok::r_paren));
341 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000342}
343
Douglas Gregor429183e2015-12-09 22:57:32 +0000344/// Check for a mismatch in the atomicity of the given properties.
345static void checkAtomicPropertyMismatch(Sema &S,
346 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000347 ObjCPropertyDecl *NewProperty,
348 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000349 // If the atomicity of both matches, we're done.
350 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000351 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000353 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000354 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
355 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000356 if (OldIsAtomic == NewIsAtomic) return;
357
358 // Determine whether the given property is readonly and implicitly
359 // atomic.
360 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
361 // Is it readonly?
362 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000363 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000364
365 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000366 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000367
368 // Was 'atomic' specified directly?
Fangrui Song6907ce22018-07-30 19:24:48 +0000369 if (Property->getPropertyAttributesAsWritten() &
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000370 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000371 return false;
372
373 return true;
374 };
375
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000376 // If we're allowed to propagate atomicity, and the new property did
377 // not specify atomicity at all, propagate.
378 const unsigned AtomicityMask =
379 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
380 if (PropagateAtomicity &&
381 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
382 unsigned Attrs = NewProperty->getPropertyAttributes();
383 Attrs = Attrs & ~AtomicityMask;
384 if (OldIsAtomic)
385 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
Fangrui Song6907ce22018-07-30 19:24:48 +0000386 else
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000387 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
388
389 NewProperty->overwritePropertyAttributes(Attrs);
390 return;
391 }
392
Douglas Gregor429183e2015-12-09 22:57:32 +0000393 // One of the properties is atomic; if it's a readonly property, and
394 // 'atomic' wasn't explicitly specified, we're okay.
395 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
396 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
397 return;
398
399 // Diagnose the conflict.
400 const IdentifierInfo *OldContextName;
401 auto *OldDC = OldProperty->getDeclContext();
402 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
403 OldContextName = Category->getClassInterface()->getIdentifier();
404 else
405 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
406
407 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
408 << NewProperty->getDeclName() << "atomic"
409 << OldContextName;
410 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
411}
412
Douglas Gregor90d34422013-01-21 19:05:22 +0000413ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000414Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000415 SourceLocation AtLoc,
416 SourceLocation LParenLoc,
417 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000418 Selector GetterSel,
419 SourceLocation GetterNameLoc,
420 Selector SetterSel,
421 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000423 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000424 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000425 QualType T,
426 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000428 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000429 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000430 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000431 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000432 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
Fangrui Song6907ce22018-07-30 19:24:48 +0000433
Ted Kremenek959e8302010-03-12 02:31:10 +0000434 // We need to look in the @interface to see if the @property was
435 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000436 if (!CCPrimary) {
437 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000438 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000439 }
440
Manman Ren5b786402016-01-28 18:49:28 +0000441 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
442 (Attributes & ObjCDeclSpec::DQ_PR_class);
443
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000444 // Find the property in the extended class's primary class or
445 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000446 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
447 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000448
Fangrui Song6907ce22018-07-30 19:24:48 +0000449 // If we found a property in an extension, complain.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000450 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
451 Diag(AtLoc, diag::err_duplicate_property);
452 Diag(PIDecl->getLocation(), diag::note_property_declare);
453 return nullptr;
454 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000455
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000456 // Check for consistency with the previous declaration, if there is one.
457 if (PIDecl) {
458 // A readonly property declared in the primary class can be refined
459 // by adding a readwrite property within an extension.
460 // Anything else is an error.
461 if (!(PIDecl->isReadOnly() && isReadWrite)) {
462 // Tailor the diagnostics for the common case where a readwrite
463 // property is declared both in the @interface and the continuation.
464 // This is a common error where the user often intended the original
465 // declaration to be readonly.
466 unsigned diag =
467 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
468 (PIDecl->getPropertyAttributesAsWritten() &
469 ObjCPropertyDecl::OBJC_PR_readwrite)
470 ? diag::err_use_continuation_class_redeclaration_readwrite
471 : diag::err_use_continuation_class;
472 Diag(AtLoc, diag)
473 << CCPrimary->getDeclName();
474 Diag(PIDecl->getLocation(), diag::note_property_declare);
475 return nullptr;
476 }
477
478 // Check for consistency of getters.
479 if (PIDecl->getGetterName() != GetterSel) {
480 // If the getter was written explicitly, complain.
481 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
482 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
483 << PIDecl->getGetterName() << GetterSel;
484 Diag(PIDecl->getLocation(), diag::note_property_declare);
485 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000486
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000487 // Always adopt the getter from the original declaration.
488 GetterSel = PIDecl->getGetterName();
489 Attributes |= ObjCDeclSpec::DQ_PR_getter;
490 }
491
492 // Check consistency of ownership.
493 unsigned ExistingOwnership
494 = getOwnershipRule(PIDecl->getPropertyAttributes());
495 unsigned NewOwnership = getOwnershipRule(Attributes);
496 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
497 // If the ownership was written explicitly, complain.
498 if (getOwnershipRule(AttributesAsWritten)) {
499 Diag(AtLoc, diag::warn_property_attr_mismatch);
500 Diag(PIDecl->getLocation(), diag::note_property_declare);
501 }
502
503 // Take the ownership from the original property.
504 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
505 }
506
Fangrui Song6907ce22018-07-30 19:24:48 +0000507 // If the redeclaration is 'weak' but the original property is not,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000508 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
509 !(PIDecl->getPropertyAttributesAsWritten()
510 & ObjCPropertyDecl::OBJC_PR_weak) &&
511 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
512 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
513 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
514 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fangrui Song6907ce22018-07-30 19:24:48 +0000515 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000516 }
517
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000518 // Create a new ObjCPropertyDecl with the DeclContext being
519 // the class extension.
520 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000521 FD, GetterSel, GetterNameLoc,
522 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000523 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000524 Attributes, AttributesAsWritten,
525 T, TSI, MethodImplKind, DC);
526
527 // If there was no declaration of a property with the same name in
528 // the primary class, we're done.
529 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000530 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000531 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000532 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000533
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000534 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
535 bool IncompatibleObjC = false;
536 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000537 // Relax the strict type matching for property type in continuation class.
538 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000539 // as it narrows the object type in its primary class property. Note that
540 // this conversion is safe only because the wider type is for a 'readonly'
541 // property in primary class and 'narrowed' type for a 'readwrite' property
542 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000543 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
544 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
545 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
546 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
547 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000548 ConvertedType, IncompatibleObjC))
549 || IncompatibleObjC) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000550 Diag(AtLoc,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000551 diag::err_type_mismatch_continuation_class) << PDecl->getType();
552 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000553 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000554 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000555 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000556
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000557 // Check that atomicity of property in class extension matches the previous
558 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000559 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000560
Douglas Gregore17765e2015-11-03 17:02:34 +0000561 // Make sure getter/setter are appropriately synthesized.
562 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000563 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000564}
565
566ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
567 ObjCContainerDecl *CDecl,
568 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000569 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000570 FieldDeclarator &FD,
571 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000572 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000574 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000575 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000576 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000577 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000578 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000579 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000580 tok::ObjCKeywordKind MethodImplKind,
581 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000582 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000583
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000584 // Property defaults to 'assign' if it is readwrite, unless this is ARC
585 // and the type is retainable.
586 bool isAssign;
587 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
588 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
589 isAssign = true;
590 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
591 isAssign = false;
592 } else {
593 isAssign = (!getLangOpts().ObjCAutoRefCount ||
594 !T->isObjCRetainableType());
595 }
596
597 // Issue a warning if property is 'assign' as default and its
598 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000599 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000600 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000601 if (const ObjCObjectPointerType *ObjPtrTy =
602 T->getAs<ObjCObjectPointerType>()) {
603 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
604 if (IDecl)
605 if (ObjCProtocolDecl* PNSCopying =
606 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
607 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
608 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000609 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000610 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000611
612 if (T->isObjCObjectType()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000613 SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000614 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000615 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
616 << FixItHint::CreateInsertion(StarLoc, "*");
617 T = Context.getObjCObjectPointerType(T);
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000618 SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
Eli Friedman999af7b2013-07-09 01:38:07 +0000619 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
620 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000621
George Burgess IV00f70bd2018-03-01 05:43:23 +0000622 DeclContext *DC = CDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000623 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
624 FD.D.getIdentifierLoc(),
Fangrui Song6907ce22018-07-30 19:24:48 +0000625 PropertyId, AtLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +0000626 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000627
Manman Ren5b786402016-01-28 18:49:28 +0000628 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
629 (Attributes & ObjCDeclSpec::DQ_PR_class);
630 // Class property and instance property can have the same name.
631 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
632 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000633 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000634 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000635 PDecl->setInvalidDecl();
636 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000637 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000638 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000639 if (lexicalDC)
640 PDecl->setLexicalDeclContext(lexicalDC);
641 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000642
643 if (T->isArrayType() || T->isFunctionType()) {
644 Diag(AtLoc, diag::err_property_type) << T;
645 PDecl->setInvalidDecl();
646 }
647
648 ProcessDeclAttributes(S, PDecl, FD.D);
649
650 // Regardless of setter/getter attribute, we save the default getter/setter
651 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000652 PDecl->setGetterName(GetterSel, GetterNameLoc);
653 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000654 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000655 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000656
Bill Wendling44426052012-12-20 19:22:21 +0000657 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
659
Bill Wendling44426052012-12-20 19:22:21 +0000660 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
662
Bill Wendling44426052012-12-20 19:22:21 +0000663 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
665
666 if (isReadWrite)
667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
668
Bill Wendling44426052012-12-20 19:22:21 +0000669 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
671
Bill Wendling44426052012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
674
Bill Wendling44426052012-12-20 19:22:21 +0000675 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
677
Bill Wendling44426052012-12-20 19:22:21 +0000678 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000679 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
680
Bill Wendling44426052012-12-20 19:22:21 +0000681 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
683
Ted Kremenekac597f32010-03-12 00:46:40 +0000684 if (isAssign)
685 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
686
John McCall43192862011-09-13 18:31:23 +0000687 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000688 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000690 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000691 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000692
John McCall31168b02011-06-15 23:02:42 +0000693 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000694 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000695 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
696 if (isAssign)
697 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
698
Ted Kremenekac597f32010-03-12 00:46:40 +0000699 if (MethodImplKind == tok::objc_required)
700 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
701 else if (MethodImplKind == tok::objc_optional)
702 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000703
Douglas Gregor813a0662015-06-19 18:14:38 +0000704 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
705 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
706
Douglas Gregor849ebc22015-06-19 18:14:46 +0000707 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
708 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
709
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800710 if (Attributes & ObjCDeclSpec::DQ_PR_class)
Manman Ren387ff7f2016-01-26 18:52:43 +0000711 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
712
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -0800713 if ((Attributes & ObjCDeclSpec::DQ_PR_direct) ||
714 CDecl->hasAttr<ObjCDirectMembersAttr>()) {
715 if (isa<ObjCProtocolDecl>(CDecl)) {
716 Diag(PDecl->getLocation(), diag::err_objc_direct_on_protocol) << true;
717 } else if (getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
718 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_direct);
719 } else {
720 Diag(PDecl->getLocation(), diag::warn_objc_direct_property_ignored)
721 << PDecl->getDeclName();
722 }
723 }
724
Ted Kremenek959e8302010-03-12 02:31:10 +0000725 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000726}
727
John McCall31168b02011-06-15 23:02:42 +0000728static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
729 ObjCPropertyDecl *property,
730 ObjCIvarDecl *ivar) {
731 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
732
John McCall31168b02011-06-15 23:02:42 +0000733 QualType ivarType = ivar->getType();
734 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000735
John McCall43192862011-09-13 18:31:23 +0000736 // The lifetime implied by the property's attributes.
737 Qualifiers::ObjCLifetime propertyLifetime =
738 getImpliedARCOwnership(property->getPropertyAttributes(),
739 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000740
John McCall43192862011-09-13 18:31:23 +0000741 // We're fine if they match.
742 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000743
John McCall460ce582015-10-22 18:38:17 +0000744 // None isn't a valid lifetime for an object ivar in ARC, and
745 // __autoreleasing is never valid; don't diagnose twice.
746 if ((ivarLifetime == Qualifiers::OCL_None &&
747 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000748 ivarLifetime == Qualifiers::OCL_Autoreleasing)
749 return;
John McCall31168b02011-06-15 23:02:42 +0000750
John McCalld8561f02012-08-20 23:36:59 +0000751 // If the ivar is private, and it's implicitly __unsafe_unretained
Nico Weber138a8152019-08-21 15:52:44 +0000752 // because of its type, then pretend it was actually implicitly
John McCalld8561f02012-08-20 23:36:59 +0000753 // __strong. This is only sound because we're processing the
754 // property implementation before parsing any method bodies.
755 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
756 propertyLifetime == Qualifiers::OCL_Strong &&
757 ivar->getAccessControl() == ObjCIvarDecl::Private) {
758 SplitQualType split = ivarType.split();
759 if (split.Quals.hasObjCLifetime()) {
760 assert(ivarType->isObjCARCImplicitlyUnretainedType());
761 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
762 ivarType = S.Context.getQualifiedType(split);
763 ivar->setType(ivarType);
764 return;
765 }
766 }
767
John McCall43192862011-09-13 18:31:23 +0000768 switch (propertyLifetime) {
769 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000770 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000771 << property->getDeclName()
772 << ivar->getDeclName()
773 << ivarLifetime;
774 break;
John McCall31168b02011-06-15 23:02:42 +0000775
John McCall43192862011-09-13 18:31:23 +0000776 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000777 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000778 << property->getDeclName()
779 << ivar->getDeclName();
780 break;
John McCall31168b02011-06-15 23:02:42 +0000781
John McCall43192862011-09-13 18:31:23 +0000782 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000783 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000784 << property->getDeclName()
785 << ivar->getDeclName()
Fangrui Song6907ce22018-07-30 19:24:48 +0000786 << ((property->getPropertyAttributesAsWritten()
John McCall43192862011-09-13 18:31:23 +0000787 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
788 break;
John McCall31168b02011-06-15 23:02:42 +0000789
John McCall43192862011-09-13 18:31:23 +0000790 case Qualifiers::OCL_Autoreleasing:
791 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000792
John McCall43192862011-09-13 18:31:23 +0000793 case Qualifiers::OCL_None:
794 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000795 return;
796 }
797
798 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000799 if (propertyImplLoc.isValid())
800 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000801}
802
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000803/// setImpliedPropertyAttributeForReadOnlyProperty -
804/// This routine evaludates life-time attributes for a 'readonly'
805/// property with no known lifetime of its own, using backing
806/// 'ivar's attribute, if any. If no backing 'ivar', property's
807/// life-time is assumed 'strong'.
808static void setImpliedPropertyAttributeForReadOnlyProperty(
809 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000810 Qualifiers::ObjCLifetime propertyLifetime =
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000811 getImpliedARCOwnership(property->getPropertyAttributes(),
812 property->getType());
813 if (propertyLifetime != Qualifiers::OCL_None)
814 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000815
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000816 if (!ivar) {
817 // if no backing ivar, make property 'strong'.
818 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
819 return;
820 }
821 // property assumes owenership of backing ivar.
822 QualType ivarType = ivar->getType();
823 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
824 if (ivarLifetime == Qualifiers::OCL_Strong)
825 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
826 else if (ivarLifetime == Qualifiers::OCL_Weak)
827 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000828}
Ted Kremenekac597f32010-03-12 00:46:40 +0000829
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000830static bool
831isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
832 ObjCPropertyDecl::PropertyAttributeKind Kind) {
833 return (Attr1 & Kind) != (Attr2 & Kind);
834}
835
836static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
837 unsigned Kinds) {
838 return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
839}
840
841/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
842/// property declaration that should be synthesised in all of the inherited
843/// protocols. It also diagnoses properties declared in inherited protocols with
844/// mismatched types or attributes, since any of them can be candidate for
845/// synthesis.
846static ObjCPropertyDecl *
847SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000848 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000849 ObjCPropertyDecl *Property) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000850 assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
851 "Expected a property from a protocol");
852 ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
853 ObjCInterfaceDecl::PropertyDeclOrder Properties;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000854 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
855 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000856 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
857 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000858 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000859 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000860 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000861 for (const auto *PI : SDecl->all_referenced_protocols()) {
862 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000863 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
864 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000865 }
866 SDecl = SDecl->getSuperClass();
867 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000868 }
869
870 if (Properties.empty())
871 return Property;
872
873 ObjCPropertyDecl *OriginalProperty = Property;
874 size_t SelectedIndex = 0;
875 for (const auto &Prop : llvm::enumerate(Properties)) {
876 // Select the 'readwrite' property if such property exists.
877 if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
878 Property = Prop.value();
879 SelectedIndex = Prop.index();
880 }
881 }
882 if (Property != OriginalProperty) {
883 // Check that the old property is compatible with the new one.
884 Properties[SelectedIndex] = OriginalProperty;
885 }
886
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000887 QualType RHSType = S.Context.getCanonicalType(Property->getType());
Alex Lorenz34d070f2017-08-22 10:38:07 +0000888 unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000889 enum MismatchKind {
890 IncompatibleType = 0,
891 HasNoExpectedAttribute,
892 HasUnexpectedAttribute,
893 DifferentGetter,
894 DifferentSetter
895 };
896 // Represents a property from another protocol that conflicts with the
897 // selected declaration.
898 struct MismatchingProperty {
899 const ObjCPropertyDecl *Prop;
900 MismatchKind Kind;
901 StringRef AttributeName;
902 };
903 SmallVector<MismatchingProperty, 4> Mismatches;
904 for (ObjCPropertyDecl *Prop : Properties) {
905 // Verify the property attributes.
Alex Lorenz34d070f2017-08-22 10:38:07 +0000906 unsigned Attr = Prop->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000907 if (Attr != OriginalAttributes) {
908 auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
909 MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
910 : HasUnexpectedAttribute;
911 Mismatches.push_back({Prop, Kind, AttributeName});
912 };
Alex Lorenz61372552018-05-02 22:40:19 +0000913 // The ownership might be incompatible unless the property has no explicit
914 // ownership.
915 bool HasOwnership = (Attr & (ObjCPropertyDecl::OBJC_PR_retain |
916 ObjCPropertyDecl::OBJC_PR_strong |
917 ObjCPropertyDecl::OBJC_PR_copy |
918 ObjCPropertyDecl::OBJC_PR_assign |
919 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
920 ObjCPropertyDecl::OBJC_PR_weak)) != 0;
921 if (HasOwnership &&
922 isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000923 ObjCPropertyDecl::OBJC_PR_copy)) {
924 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
925 continue;
926 }
Alex Lorenz61372552018-05-02 22:40:19 +0000927 if (HasOwnership && areIncompatiblePropertyAttributes(
928 OriginalAttributes, Attr,
929 ObjCPropertyDecl::OBJC_PR_retain |
930 ObjCPropertyDecl::OBJC_PR_strong)) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000931 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
932 ObjCPropertyDecl::OBJC_PR_strong),
933 "retain (or strong)");
934 continue;
935 }
936 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
937 ObjCPropertyDecl::OBJC_PR_atomic)) {
938 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
939 continue;
940 }
941 }
942 if (Property->getGetterName() != Prop->getGetterName()) {
943 Mismatches.push_back({Prop, DifferentGetter, ""});
944 continue;
945 }
946 if (!Property->isReadOnly() && !Prop->isReadOnly() &&
947 Property->getSetterName() != Prop->getSetterName()) {
948 Mismatches.push_back({Prop, DifferentSetter, ""});
949 continue;
950 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000951 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
952 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
953 bool IncompatibleObjC = false;
954 QualType ConvertedType;
955 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
956 || IncompatibleObjC) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000957 Mismatches.push_back({Prop, IncompatibleType, ""});
958 continue;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000959 }
960 }
961 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000962
963 if (Mismatches.empty())
964 return Property;
965
966 // Diagnose incompability.
967 {
968 bool HasIncompatibleAttributes = false;
969 for (const auto &Note : Mismatches)
970 HasIncompatibleAttributes =
971 Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
972 // Promote the warning to an error if there are incompatible attributes or
973 // incompatible types together with readwrite/readonly incompatibility.
974 auto Diag = S.Diag(Property->getLocation(),
975 Property != OriginalProperty || HasIncompatibleAttributes
976 ? diag::err_protocol_property_mismatch
977 : diag::warn_protocol_property_mismatch);
978 Diag << Mismatches[0].Kind;
979 switch (Mismatches[0].Kind) {
980 case IncompatibleType:
981 Diag << Property->getType();
982 break;
983 case HasNoExpectedAttribute:
984 case HasUnexpectedAttribute:
985 Diag << Mismatches[0].AttributeName;
986 break;
987 case DifferentGetter:
988 Diag << Property->getGetterName();
989 break;
990 case DifferentSetter:
991 Diag << Property->getSetterName();
992 break;
993 }
994 }
995 for (const auto &Note : Mismatches) {
996 auto Diag =
997 S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
998 << Note.Kind;
999 switch (Note.Kind) {
1000 case IncompatibleType:
1001 Diag << Note.Prop->getType();
1002 break;
1003 case HasNoExpectedAttribute:
1004 case HasUnexpectedAttribute:
1005 Diag << Note.AttributeName;
1006 break;
1007 case DifferentGetter:
1008 Diag << Note.Prop->getGetterName();
1009 break;
1010 case DifferentSetter:
1011 Diag << Note.Prop->getSetterName();
1012 break;
1013 }
1014 }
1015 if (AtLoc.isValid())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001016 S.Diag(AtLoc, diag::note_property_synthesize);
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001017
1018 return Property;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001019}
1020
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001021/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001022static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1023 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001024 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
1025
1026 // If this is a readwrite property in a class extension that refines
1027 // a readonly property in the original class definition, check it as
1028 // well.
1029
1030 // If it's a readonly property, we're not interested.
1031 if (Prop->isReadOnly()) return false;
1032
1033 // Is it declared in an extension?
1034 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1035 if (!Category || !Category->IsClassExtension()) return false;
1036
1037 // Find the corresponding property in the primary class definition.
1038 auto OrigClass = Category->getClassInterface();
1039 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1040 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1041 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1042 }
1043
Douglas Gregor02535432015-12-18 00:52:31 +00001044 // Look through all of the protocols.
1045 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +00001046 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1047 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +00001048 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1049 }
1050
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001051 return false;
1052}
1053
Adrian Prantl2073dd22019-11-04 14:28:14 -08001054/// Create a synthesized property accessor stub inside the \@implementation.
1055static ObjCMethodDecl *
1056RedeclarePropertyAccessor(ASTContext &Context, ObjCImplementationDecl *Impl,
1057 ObjCMethodDecl *AccessorDecl, SourceLocation AtLoc,
1058 SourceLocation PropertyLoc) {
1059 ObjCMethodDecl *Decl = AccessorDecl;
1060 ObjCMethodDecl *ImplDecl = ObjCMethodDecl::Create(
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();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +00001129 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 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001146 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();
1158 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
1159 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() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001235 ObjCPropertyDecl::OBJC_PR_readonly) &&
1236 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
1240 ObjCPropertyDecl::PropertyAttributeKind kind
John McCall31168b02011-06-15 23:02:42 +00001241 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001242
John McCall460ce582015-10-22 18:38:17 +00001243 bool isARCWeak = false;
1244 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1245 // 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) &&
John McCall43192862011-09-13 18:31:23 +00001315 !(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 }
1459
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() &
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001554 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
John McCall48871652010-08-21 09:40:31 +00001630 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001631}
1632
1633//===----------------------------------------------------------------------===//
1634// Helper methods.
1635//===----------------------------------------------------------------------===//
1636
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001637/// DiagnosePropertyMismatch - Compares two properties for their
1638/// attributes and types and warns on a variety of inconsistencies.
1639///
1640void
1641Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1642 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001643 const IdentifierInfo *inheritedName,
1644 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001645 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001646 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001647 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001648 SuperProperty->getPropertyAttributes();
Fangrui Song6907ce22018-07-30 19:24:48 +00001649
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001650 // We allow readonly properties without an explicit ownership
1651 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1652 // to be overridden by a property with any explicit ownership in the subclass.
1653 if (!OverridingProtocolProperty &&
1654 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1655 ;
1656 else {
1657 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1658 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1659 Diag(Property->getLocation(), diag::warn_readonly_property)
1660 << Property->getDeclName() << inheritedName;
1661 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1662 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001663 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001664 << Property->getDeclName() << "copy" << inheritedName;
1665 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1666 unsigned CAttrRetain =
1667 (CAttr &
1668 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1669 unsigned SAttrRetain =
1670 (SAttr &
1671 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1672 bool CStrong = (CAttrRetain != 0);
1673 bool SStrong = (SAttrRetain != 0);
1674 if (CStrong != SStrong)
1675 Diag(Property->getLocation(), diag::warn_property_attribute)
1676 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1677 }
John McCall31168b02011-06-15 23:02:42 +00001678 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001679
Douglas Gregor429183e2015-12-09 22:57:32 +00001680 // Check for nonatomic; note that nonatomic is effectively
1681 // meaningless for readonly properties, so don't diagnose if the
1682 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001683 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Alex Lorenz05a63ee2017-10-06 19:24:26 +00001684 // Readonly properties from protocols can be implemented as "readwrite"
1685 // with a custom setter name.
1686 if (Property->getSetterName() != SuperProperty->getSetterName() &&
1687 !(SuperProperty->isReadOnly() &&
1688 isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001689 Diag(Property->getLocation(), diag::warn_property_attribute)
1690 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001691 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1692 }
1693 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001694 Diag(Property->getLocation(), diag::warn_property_attribute)
1695 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001696 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1697 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001698
1699 QualType LHSType =
1700 Context.getCanonicalType(SuperProperty->getType());
1701 QualType RHSType =
1702 Context.getCanonicalType(Property->getType());
1703
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001704 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001705 // Do cases not handled in above.
1706 // FIXME. For future support of covariant property types, revisit this.
1707 bool IncompatibleObjC = false;
1708 QualType ConvertedType;
Fangrui Song6907ce22018-07-30 19:24:48 +00001709 if (!isObjCPointerConversion(RHSType, LHSType,
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001710 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001711 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001712 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1713 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001714 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1715 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001716 }
1717}
1718
1719bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1720 ObjCMethodDecl *GetterMethod,
1721 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001722 if (!GetterMethod)
1723 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001724 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001725 QualType PropertyRValueType =
1726 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1727 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001728 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001729 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1730 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001731 if ((propertyObjCPtr =
1732 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001733 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1734 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001735 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001736 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001737 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001738 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001739 << GetterMethod->getSelector() << GetterType;
1740 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1741 return true;
1742 } else {
1743 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001744 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001745 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1746 if (lhsType != rhsType && lhsType->isArithmeticType())
1747 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001748 }
1749 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001750
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001751 if (!compat) {
1752 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1753 << property->getDeclName()
1754 << GetterMethod->getSelector();
1755 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1756 return true;
1757 }
1758
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001759 return false;
1760}
1761
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001762/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001763/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001764static void
1765CollectImmediateProperties(ObjCContainerDecl *CDecl,
1766 ObjCContainerDecl::PropertyMap &PropMap,
1767 ObjCContainerDecl::PropertyMap &SuperPropMap,
1768 bool CollectClassPropsOnly = false,
1769 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001770 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001771 for (auto *Prop : IDecl->properties()) {
1772 if (CollectClassPropsOnly && !Prop->isClassProperty())
1773 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001774 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1775 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001776 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001777
1778 // Collect the properties from visible extensions.
1779 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001780 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1781 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001782
Ted Kremenek204c3c52014-02-22 00:02:03 +00001783 if (IncludeProtocols) {
1784 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001785 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001786 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1787 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001788 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001789 }
1790 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001791 for (auto *Prop : CATDecl->properties()) {
1792 if (CollectClassPropsOnly && !Prop->isClassProperty())
1793 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001794 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1795 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001796 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001797 if (IncludeProtocols) {
1798 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001799 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001800 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1801 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001802 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001803 }
1804 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001805 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001806 if (CollectClassPropsOnly && !Prop->isClassProperty())
1807 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001808 ObjCPropertyDecl *PropertyFromSuper =
1809 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1810 Prop->isClassProperty())];
Fangrui Song6907ce22018-07-30 19:24:48 +00001811 // Exclude property for protocols which conform to class's super-class,
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001812 // as super-class has to implement the property.
Fangrui Song6907ce22018-07-30 19:24:48 +00001813 if (!PropertyFromSuper ||
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001814 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001815 ObjCPropertyDecl *&PropEntry =
1816 PropMap[std::make_pair(Prop->getIdentifier(),
1817 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001818 if (!PropEntry)
1819 PropEntry = Prop;
1820 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001821 }
Manman Ren16a7d632016-04-12 23:01:55 +00001822 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001823 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001824 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1825 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001826 }
1827}
1828
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001829/// CollectSuperClassPropertyImplementations - This routine collects list of
1830/// properties to be implemented in super class(s) and also coming from their
1831/// conforming protocols.
1832static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001833 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001834 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001835 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001836 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001837 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001838 SDecl = SDecl->getSuperClass();
1839 }
1840 }
1841}
1842
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001843/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1844/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1845/// declared in class 'IFace'.
1846bool
1847Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1848 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1849 if (!IV->getSynthesize())
1850 return false;
1851 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1852 Method->isInstanceMethod());
1853 if (!IMD || !IMD->isPropertyAccessor())
1854 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001855
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001856 // look up a property declaration whose one of its accessors is implemented
1857 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001858 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001859 if ((Property->getGetterName() == IMD->getSelector() ||
1860 Property->getSetterName() == IMD->getSelector()) &&
1861 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001862 return true;
1863 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001864 // Also look up property declaration in class extension whose one of its
1865 // accessors is implemented by this method.
1866 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001867 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001868 if ((Property->getGetterName() == IMD->getSelector() ||
1869 Property->getSetterName() == IMD->getSelector()) &&
1870 (Property->getPropertyIvarDecl() == IV))
1871 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001872 return false;
1873}
1874
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001875static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1876 ObjCPropertyDecl *Prop) {
1877 bool SuperClassImplementsGetter = false;
1878 bool SuperClassImplementsSetter = false;
1879 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1880 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001881
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001882 while (IDecl->getSuperClass()) {
1883 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1884 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1885 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001886
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001887 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1888 SuperClassImplementsSetter = true;
1889 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1890 return true;
1891 IDecl = IDecl->getSuperClass();
1892 }
1893 return false;
1894}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001895
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001896/// Default synthesizes all properties which must be synthesized
James Dennett2a4d13c2012-06-15 07:13:21 +00001897/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001898void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1899 ObjCInterfaceDecl *IDecl,
1900 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001901 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001902 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1903 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001904 if (PropMap.empty())
1905 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001906 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001907 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001908
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001909 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1910 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001911 // Is there a matching property synthesize/dynamic?
1912 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001913 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001914 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1915 continue;
1916 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001917 if (IMPDecl->FindPropertyImplDecl(
1918 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001919 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001920 ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Prop->getGetterName());
1921 if (ImpMethod && !ImpMethod->getBody()) {
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001922 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1923 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08001924 ImpMethod = IMPDecl->getInstanceMethod(Prop->getSetterName());
1925 if (ImpMethod && !ImpMethod->getBody())
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001926 continue;
1927 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001928 if (ObjCPropertyImplDecl *PID =
1929 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001930 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1931 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001932 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001933 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001934 continue;
1935 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001936 ObjCPropertyDecl *PropInSuperClass =
1937 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1938 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001939 if (ObjCProtocolDecl *Proto =
1940 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001941 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001942 // Suppress the warning if class's superclass implements property's
1943 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001944 // Or, if property is going to be implemented in its super class.
1945 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001946 Diag(IMPDecl->getLocation(),
1947 diag::warn_auto_synthesizing_protocol_property)
1948 << Prop << Proto;
1949 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001950 std::string FixIt =
1951 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1952 Diag(AtEnd, diag::note_add_synthesize_directive)
1953 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001954 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001955 continue;
1956 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001957 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001958 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001959 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1960 (PropInSuperClass->getPropertyAttributes() &
1961 ObjCPropertyDecl::OBJC_PR_readonly) &&
1962 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1963 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1964 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1965 << Prop->getIdentifier();
1966 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1967 }
1968 else {
1969 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1970 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001971 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001972 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1973 }
1974 continue;
1975 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001976 // We use invalid SourceLocations for the synthesized ivars since they
1977 // aren't really synthesized at a particular location; they just exist.
1978 // Saying that they are located at the @implementation isn't really going
1979 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001980 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1981 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1982 true,
1983 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001984 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001985 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001986 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001987 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001988 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001989 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001990 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001991}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001992
Alex Lorenz6c9af502017-07-03 10:12:24 +00001993void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1994 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00001995 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001996 return;
1997 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1998 if (!IC)
1999 return;
2000 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00002001 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00002002 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00002003}
2004
Manman Ren08ce7342016-05-18 18:12:34 +00002005static void DiagnoseUnimplementedAccessor(
2006 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
2007 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
2008 ObjCPropertyDecl *Prop,
2009 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
2010 // Check to see if we have a corresponding selector in SMap and with the
2011 // right method type.
Fangrui Song75e74e02019-03-31 08:48:19 +00002012 auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) {
2013 return x->getSelector() == Method &&
2014 x->isClassMethod() == Prop->isClassProperty();
2015 });
Ted Kremenek7e812952014-02-21 19:41:30 +00002016 // When reporting on missing property setter/getter implementation in
2017 // categories, do not report when they are declared in primary class,
2018 // class's protocol, or one of it super classes. This is because,
2019 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00002020 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00002021 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00002022 !PrimaryClass->lookupPropertyAccessor(Method, C,
2023 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00002024 unsigned diag =
2025 isa<ObjCCategoryDecl>(CDecl)
2026 ? (Prop->isClassProperty()
2027 ? diag::warn_impl_required_in_category_for_class_property
2028 : diag::warn_setter_getter_impl_required_in_category)
2029 : (Prop->isClassProperty()
2030 ? diag::warn_impl_required_for_class_property
2031 : diag::warn_setter_getter_impl_required);
2032 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
2033 S.Diag(Prop->getLocation(), diag::note_property_declare);
2034 if (S.LangOpts.ObjCDefaultSynthProperties &&
2035 S.LangOpts.ObjCRuntime.isNonFragile())
2036 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
2037 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
2038 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
2039 }
Ted Kremenek7e812952014-02-21 19:41:30 +00002040}
2041
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002042void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00002043 ObjCContainerDecl *CDecl,
2044 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00002045 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00002046 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
2047
Manman Ren16a7d632016-04-12 23:01:55 +00002048 // Since we don't synthesize class properties, we should emit diagnose even
2049 // if SynthesizeProperties is true.
2050 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2051 // Gather properties which need not be implemented in this class
2052 // or category.
2053 if (!IDecl)
2054 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
2055 // For categories, no need to implement properties declared in
2056 // its primary class (and its super classes) if property is
2057 // declared in one of those containers.
2058 if ((IDecl = C->getClassInterface())) {
2059 ObjCInterfaceDecl::PropertyDeclOrder PO;
2060 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002061 }
Manman Ren16a7d632016-04-12 23:01:55 +00002062 }
2063 if (IDecl)
2064 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00002065
Manman Ren16a7d632016-04-12 23:01:55 +00002066 // When SynthesizeProperties is true, we only check class properties.
2067 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2068 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002069
Ted Kremenek38882022014-02-21 19:41:39 +00002070 // Scan the @interface to see if any of the protocols it adopts
2071 // require an explicit implementation, via attribute
2072 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00002073 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00002074 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002075
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002076 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00002077 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2078 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002079 // Lazily construct a set of all the properties in the @interface
2080 // of the class, without looking at the superclass. We cannot
2081 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00002082 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00002083 // as scans the adopted protocols. This work only triggers for protocols
2084 // with the attribute, which is very rare, and only occurs when
2085 // analyzing the @implementation.
2086 if (!LazyMap) {
2087 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2088 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2089 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002090 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002091 /* IncludeProtocols */ false);
2092 }
Ted Kremenek38882022014-02-21 19:41:39 +00002093 // Add the properties of 'PDecl' to the list of properties that
2094 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002095 for (auto *PropDecl : PDecl->properties()) {
2096 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2097 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002098 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002099 PropMap[std::make_pair(PropDecl->getIdentifier(),
2100 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002101 }
2102 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002103 }
Ted Kremenek38882022014-02-21 19:41:39 +00002104
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002105 if (PropMap.empty())
2106 return;
2107
2108 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002109 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002110 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002111
Manman Ren08ce7342016-05-18 18:12:34 +00002112 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002113 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002114 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002115 InsMap.insert(I);
Fangrui Song6907ce22018-07-30 19:24:48 +00002116
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002117 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002118 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002119 if (C && !C->IsClassExtension())
2120 if ((PrimaryClass = C->getClassInterface()))
2121 // Report unimplemented properties in the category as well.
2122 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2123 // When reporting on missing setter/getters, do not report when
2124 // setter/getter is implemented in category's primary class
2125 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002126 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002127 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002128 }
2129
Anna Zaks673d76b2012-10-18 19:17:53 +00002130 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002131 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2132 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002133 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002134 if (Prop->isInvalidDecl() ||
2135 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002136 PropImplMap.count(Prop) ||
2137 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002138 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002139
2140 // Diagnose unimplemented getters and setters.
2141 DiagnoseUnimplementedAccessor(*this,
2142 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2143 if (!Prop->isReadOnly())
2144 DiagnoseUnimplementedAccessor(*this,
2145 PrimaryClass, Prop->getSetterName(),
2146 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147 }
2148}
2149
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002150void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002151 for (const auto *propertyImpl : impDecl->property_impls()) {
2152 const auto *property = propertyImpl->getPropertyDecl();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002153 // Warn about null_resettable properties with synthesized setters,
2154 // because the setter won't properly handle nil.
2155 if (propertyImpl->getPropertyImplementation()
2156 == ObjCPropertyImplDecl::Synthesize &&
2157 (property->getPropertyAttributes() &
2158 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2159 property->getGetterMethodDecl() &&
2160 property->getSetterMethodDecl()) {
Adrian Prantl2073dd22019-11-04 14:28:14 -08002161 auto *getterImpl = propertyImpl->getGetterMethodDecl();
2162 auto *setterImpl = propertyImpl->getSetterMethodDecl();
2163 if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) &&
2164 (!setterImpl || setterImpl->isSynthesizedAccessorStub())) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002165 SourceLocation loc = propertyImpl->getLocation();
2166 if (loc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002167 loc = impDecl->getBeginLoc();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002168
2169 Diag(loc, diag::warn_null_resettable_setter)
Adrian Prantl2073dd22019-11-04 14:28:14 -08002170 << setterImpl->getSelector() << property->getDeclName();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002171 }
2172 }
2173 }
2174}
2175
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002176void
2177Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002178 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002179 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002180 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002181 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002182 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002183 for (auto *Prop : IDecl->properties())
2184 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002185 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002186 for (auto *Prop : Ext->properties())
2187 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fangrui Song6907ce22018-07-30 19:24:48 +00002188
Manman Renefe1bac2016-01-27 20:00:32 +00002189 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2190 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002191 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002192 ObjCMethodDecl *GetterMethod = nullptr;
2193 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002194
Bill Wendling44426052012-12-20 19:22:21 +00002195 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002196 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002197
John McCall43192862011-09-13 18:31:23 +00002198 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2199 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002200 GetterMethod = Property->isClassProperty() ?
2201 IMPDecl->getClassMethod(Property->getGetterName()) :
2202 IMPDecl->getInstanceMethod(Property->getGetterName());
2203 SetterMethod = Property->isClassProperty() ?
2204 IMPDecl->getClassMethod(Property->getSetterName()) :
2205 IMPDecl->getInstanceMethod(Property->getSetterName());
Adrian Prantl2073dd22019-11-04 14:28:14 -08002206 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2207 GetterMethod = nullptr;
2208 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2209 SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002210 if (GetterMethod) {
2211 Diag(GetterMethod->getLocation(),
2212 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002213 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002214 Diag(Property->getLocation(), diag::note_property_declare);
2215 }
2216 if (SetterMethod) {
2217 Diag(SetterMethod->getLocation(),
2218 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002219 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002220 Diag(Property->getLocation(), diag::note_property_declare);
2221 }
2222 }
2223
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002224 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00002225 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2226 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002227 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002228 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2229 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002230 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2231 continue;
Adrian Prantl2073dd22019-11-04 14:28:14 -08002232 GetterMethod = PIDecl->getGetterMethodDecl();
2233 SetterMethod = PIDecl->getSetterMethodDecl();
2234 if (GetterMethod && GetterMethod->isSynthesizedAccessorStub())
2235 GetterMethod = nullptr;
2236 if (SetterMethod && SetterMethod->isSynthesizedAccessorStub())
2237 SetterMethod = nullptr;
2238 if ((bool)GetterMethod ^ (bool)SetterMethod) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002239 SourceLocation MethodLoc =
2240 (GetterMethod ? GetterMethod->getLocation()
2241 : SetterMethod->getLocation());
2242 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002243 << Property->getIdentifier() << (GetterMethod != nullptr)
2244 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002245 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002246 if (Property->getLParenLoc().isValid() &&
2247 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002248 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002249 SourceLocation AfterLParen =
2250 getLocForEndOfToken(Property->getLParenLoc());
2251 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2252 : "nonatomic";
2253 Diag(Property->getLocation(),
2254 diag::note_atomic_property_fixup_suggest)
2255 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2256 } else if (Property->getLParenLoc().isInvalid()) {
2257 //@property id etc.
Fangrui Song6907ce22018-07-30 19:24:48 +00002258 SourceLocation startLoc =
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002259 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2260 Diag(Property->getLocation(),
2261 diag::note_atomic_property_fixup_suggest)
2262 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002263 }
2264 else
2265 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002266 Diag(Property->getLocation(), diag::note_property_declare);
2267 }
2268 }
2269 }
2270}
2271
John McCall31168b02011-06-15 23:02:42 +00002272void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002273 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002274 return;
2275
Aaron Ballmand85eff42014-03-14 15:02:45 +00002276 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002277 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002278 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Adrian Prantl2073dd22019-11-04 14:28:14 -08002279 !PD->isClassProperty()) {
2280 ObjCMethodDecl *IM = PID->getGetterMethodDecl();
2281 if (IM && !IM->isSynthesizedAccessorStub())
2282 continue;
John McCall31168b02011-06-15 23:02:42 +00002283 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2284 if (!method)
2285 continue;
2286 ObjCMethodFamily family = method->getMethodFamily();
2287 if (family == OMF_alloc || family == OMF_copy ||
2288 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002289 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002290 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002291 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002292 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002293
2294 // Look for a getter explicitly declared alongside the property.
2295 // If we find one, use its location for the note.
2296 SourceLocation noteLoc = PD->getLocation();
2297 SourceLocation fixItLoc;
2298 for (auto *getterRedecl : method->redecls()) {
2299 if (getterRedecl->isImplicit())
2300 continue;
2301 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2302 continue;
2303 noteLoc = getterRedecl->getLocation();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002304 fixItLoc = getterRedecl->getEndLoc();
Jordan Rosea34d04d2015-01-16 23:04:31 +00002305 }
2306
2307 Preprocessor &PP = getPreprocessor();
2308 TokenValue tokens[] = {
2309 tok::kw___attribute, tok::l_paren, tok::l_paren,
2310 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2311 PP.getIdentifierInfo("none"), tok::r_paren,
2312 tok::r_paren, tok::r_paren
2313 };
2314 StringRef spelling = "__attribute__((objc_method_family(none)))";
2315 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2316 if (!macroName.empty())
2317 spelling = macroName;
2318
2319 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2320 << method->getDeclName() << spelling;
2321 if (fixItLoc.isValid()) {
2322 SmallString<64> fixItText(" ");
2323 fixItText += spelling;
2324 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2325 }
John McCall31168b02011-06-15 23:02:42 +00002326 }
2327 }
2328 }
2329}
2330
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002331void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002332 const ObjCImplementationDecl *ImplD,
2333 const ObjCInterfaceDecl *IFD) {
2334 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002335 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2336 if (!SuperD)
2337 return;
2338
2339 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002340 for (const auto *I : ImplD->instance_methods())
2341 if (I->getMethodFamily() == OMF_init)
2342 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002343
2344 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2345 SuperD->getDesignatedInitializers(DesignatedInits);
2346 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2347 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2348 const ObjCMethodDecl *MD = *I;
2349 if (!InitSelSet.count(MD->getSelector())) {
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002350 // Don't emit a diagnostic if the overriding method in the subclass is
2351 // marked as unavailable.
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002352 bool Ignore = false;
2353 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2354 Ignore = IMD->isUnavailable();
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002355 } else {
2356 // Check the methods declared in the class extensions too.
2357 for (auto *Ext : IFD->visible_extensions())
2358 if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) {
2359 Ignore = IMD->isUnavailable();
2360 break;
2361 }
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002362 }
2363 if (!Ignore) {
2364 Diag(ImplD->getLocation(),
2365 diag::warn_objc_implementation_missing_designated_init_override)
2366 << MD->getSelector();
2367 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2368 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002369 }
2370 }
2371}
2372
John McCallad31b5f2010-11-10 07:01:40 +00002373/// AddPropertyAttrs - Propagates attributes from a property to the
2374/// implicitly-declared getter or setter for that property.
2375static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2376 ObjCPropertyDecl *Property) {
2377 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002378 for (const auto *A : Property->attrs()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002379 if (isa<DeprecatedAttr>(A) ||
2380 isa<UnavailableAttr>(A) ||
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002381 isa<AvailabilityAttr>(A))
2382 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002383 }
John McCallad31b5f2010-11-10 07:01:40 +00002384}
2385
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002386/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2387/// have the property type and issue diagnostics if they don't.
2388/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002389/// appropriate lookup tables.
2390void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002391 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002392 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002393 if (CD->isInvalidDecl())
2394 return;
2395
Manman Rend36f7d52016-01-27 20:10:32 +00002396 bool IsClassProperty = property->isClassProperty();
2397 GetterMethod = IsClassProperty ?
2398 CD->getClassMethod(property->getGetterName()) :
2399 CD->getInstanceMethod(property->getGetterName());
2400
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002401 // if setter or getter is not found in class extension, it might be
2402 // in the primary class.
2403 if (!GetterMethod)
2404 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2405 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002406 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2407 getClassMethod(property->getGetterName()) :
2408 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002409 getInstanceMethod(property->getGetterName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002410
Manman Rend36f7d52016-01-27 20:10:32 +00002411 SetterMethod = IsClassProperty ?
2412 CD->getClassMethod(property->getSetterName()) :
2413 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002414 if (!SetterMethod)
2415 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2416 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002417 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2418 getClassMethod(property->getSetterName()) :
2419 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002420 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002421 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2422 property->getLocation());
2423
Alex Lorenz535571a2017-03-30 13:33:51 +00002424 if (!property->isReadOnly() && SetterMethod) {
2425 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2426 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002427 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2428 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002429 !Context.hasSameUnqualifiedType(
Fangrui Song6907ce22018-07-30 19:24:48 +00002430 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002431 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002432 Diag(property->getLocation(),
2433 diag::warn_accessor_property_type_mismatch)
2434 << property->getDeclName()
2435 << SetterMethod->getSelector();
2436 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2437 }
2438 }
2439
2440 // Synthesize getter/setter methods if none exist.
2441 // Find the default getter and if one not found, add one.
2442 // FIXME: The synthesized property we set here is misleading. We almost always
2443 // synthesize these methods unless the user explicitly provided prototypes
2444 // (which is odd, but allowed). Sema should be typechecking that the
2445 // declarations jive in that situation (which it is not currently).
2446 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002447 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002448 // Declare a getter method and add it to the list of methods
2449 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002450 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002451
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002452 // The getter returns the declared property type with all qualifiers
2453 // removed.
2454 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2455
Douglas Gregor849ebc22015-06-19 18:14:46 +00002456 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002457 if (property->getPropertyAttributes() &
2458 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2459 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002460 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002461 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002462 resultTy = Context.getAttributedType(attr::TypeNonNull,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002463 modifiedTy, modifiedTy);
2464 }
2465 }
2466
Adrian Prantl2073dd22019-11-04 14:28:14 -08002467 GetterMethod = ObjCMethodDecl::Create(
2468 Context, Loc, Loc, property->getGetterName(), resultTy, nullptr, CD,
2469 !IsClassProperty, /*isVariadic=*/false,
2470 /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false,
2471 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
2472 (property->getPropertyImplementation() == ObjCPropertyDecl::Optional)
2473 ? ObjCMethodDecl::Optional
2474 : ObjCMethodDecl::Required);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002475 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002476
2477 AddPropertyAttrs(*this, GetterMethod, property);
2478
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002479 if (property->isDirectProperty())
2480 GetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2481
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002482 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002483 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2484 Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002485
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002486 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2487 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002488 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002489
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002490 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002491 GetterMethod->addAttr(SectionAttr::CreateImplicit(
2492 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2493 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002494
2495 if (getLangOpts().ObjCAutoRefCount)
2496 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002497 } else
2498 // A user declared getter will be synthesize when @synthesize of
2499 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002500 GetterMethod->setPropertyAccessor(true);
Pierre Habouzit1646bb82019-12-09 14:27:57 -08002501
2502 GetterMethod->createImplicitParams(Context,
2503 GetterMethod->getClassInterface());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002504 property->setGetterMethodDecl(GetterMethod);
2505
2506 // Skip setter if property is read-only.
2507 if (!property->isReadOnly()) {
2508 // Find the default setter and if one not found, add one.
2509 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002510 // No instance/class method of same name as property setter name was
2511 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002512 // Declare a setter method and add it to the list of methods
2513 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002514 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002515
2516 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002517 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002518 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002519 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002520 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002521 /*isPropertyAccessor=*/true,
Adrian Prantl2073dd22019-11-04 14:28:14 -08002522 /*isSynthesizedAccessorStub=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002523 /*isImplicitlyDeclared=*/true,
2524 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002525 (property->getPropertyImplementation() ==
2526 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002527 ObjCMethodDecl::Optional :
2528 ObjCMethodDecl::Required);
2529
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002530 // Remove all qualifiers from the setter's parameter type.
2531 QualType paramTy =
2532 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2533
Douglas Gregor849ebc22015-06-19 18:14:46 +00002534 // If the property is null_resettable, the setter accepts a
2535 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002536 if (property->getPropertyAttributes() &
2537 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2538 QualType modifiedTy = paramTy;
2539 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2540 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002541 paramTy = Context.getAttributedType(attr::TypeNullable,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002542 modifiedTy, modifiedTy);
2543 }
2544 }
2545
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002546 // Invent the arguments for the setter. We don't bother making a
2547 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002548 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2549 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002550 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002551 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002552 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002553 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002554 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002555 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002556
2557 AddPropertyAttrs(*this, SetterMethod, property);
2558
Pierre Habouzitd4e1ba32019-11-07 23:14:58 -08002559 if (property->isDirectProperty())
2560 SetterMethod->addAttr(ObjCDirectAttr::CreateImplicit(Context, Loc));
2561
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002562 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002563 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Erich Keane6a24e802019-09-13 17:39:31 +00002564 SetterMethod->addAttr(SectionAttr::CreateImplicit(
2565 Context, SA->getName(), Loc, AttributeCommonInfo::AS_GNU,
2566 SectionAttr::GNU_section));
John McCalle48f3892013-04-04 01:38:37 +00002567 // It's possible for the user to have set a very odd custom
2568 // setter selector that causes it to have a method family.
2569 if (getLangOpts().ObjCAutoRefCount)
2570 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002571 } else
2572 // A user declared setter will be synthesize when @synthesize of
2573 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002574 SetterMethod->setPropertyAccessor(true);
Pierre Habouzit1646bb82019-12-09 14:27:57 -08002575
2576 SetterMethod->createImplicitParams(Context,
2577 SetterMethod->getClassInterface());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002578 property->setSetterMethodDecl(SetterMethod);
2579 }
2580 // Add any synthesized methods to the global pool. This allows us to
2581 // handle the following, which is supported by GCC (and part of the design).
2582 //
2583 // @interface Foo
2584 // @property double bar;
2585 // @end
2586 //
2587 // void thisIsUnfortunate() {
2588 // id foo;
2589 // double bar = [foo bar];
2590 // }
2591 //
Manman Rend36f7d52016-01-27 20:10:32 +00002592 if (!IsClassProperty) {
2593 if (GetterMethod)
2594 AddInstanceMethodToGlobalPool(GetterMethod);
2595 if (SetterMethod)
2596 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002597 } else {
2598 if (GetterMethod)
2599 AddFactoryMethodToGlobalPool(GetterMethod);
2600 if (SetterMethod)
2601 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002602 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002603
2604 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2605 if (!CurrentClass) {
2606 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2607 CurrentClass = Cat->getClassInterface();
2608 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2609 CurrentClass = Impl->getClassInterface();
2610 }
2611 if (GetterMethod)
2612 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2613 if (SetterMethod)
2614 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002615}
2616
John McCall48871652010-08-21 09:40:31 +00002617void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002618 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002619 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002620 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002621 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002622 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002623 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00002624
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002625 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2626 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2627 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2628 << "readonly" << "readwrite";
Fangrui Song6907ce22018-07-30 19:24:48 +00002629
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002630 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2631 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002632
2633 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002634 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002635 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2636 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002637 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002638 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002639 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2640 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2641 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002642 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002643 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002644 }
2645
John McCall52a503d2018-09-05 19:02:00 +00002646 // Check for assign on object types.
2647 if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2648 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
2649 PropertyTy->isObjCRetainableType() &&
2650 !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2651 Diag(Loc, diag::warn_objc_property_assign_on_object);
2652 }
2653
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002654 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002655 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2656 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002657 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2658 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002659 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002660 }
Bill Wendling44426052012-12-20 19:22:21 +00002661 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002662 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2663 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002664 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002665 }
Bill Wendling44426052012-12-20 19:22:21 +00002666 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002667 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2668 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002669 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002670 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002671 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002672 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002673 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2674 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002675 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002676 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002677 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002678 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002679 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2680 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002681 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2682 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002683 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002684 }
Bill Wendling44426052012-12-20 19:22:21 +00002685 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002686 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2687 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002688 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002689 }
Bill Wendling44426052012-12-20 19:22:21 +00002690 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002691 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2692 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002693 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002694 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002695 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002696 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002697 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2698 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002699 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002700 }
Bill Wendling44426052012-12-20 19:22:21 +00002701 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2702 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002703 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2704 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002705 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002706 }
Bill Wendling44426052012-12-20 19:22:21 +00002707 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002708 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2709 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002710 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002711 }
Bill Wendling44426052012-12-20 19:22:21 +00002712 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002713 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2714 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002715 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002716 }
2717 }
Bill Wendling44426052012-12-20 19:22:21 +00002718 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2719 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002720 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2721 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002722 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002723 }
Bill Wendling44426052012-12-20 19:22:21 +00002724 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2725 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002726 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2727 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002728 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002729 }
2730
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002731 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002732 // 'weak' and 'nonnull' are mutually exclusive.
2733 if (auto nullability = PropertyTy->getNullability(Context)) {
2734 if (*nullability == NullabilityKind::NonNull)
2735 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2736 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002737 }
2738 }
2739
Bill Wendling44426052012-12-20 19:22:21 +00002740 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2741 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002742 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2743 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002744 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002745 }
2746
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002747 // Warn if user supplied no assignment attribute, property is
2748 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002749 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2750 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2751 // do nothing
2752 } else if (getLangOpts().ObjCAutoRefCount) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002753 // With arc, @property definitions should default to strong when
John McCallb61e14e2015-10-27 04:54:50 +00002754 // not specified.
2755 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2756 } else if (PropertyTy->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002757 bool isAnyClassTy =
2758 (PropertyTy->isObjCClassType() ||
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002759 PropertyTy->isObjCQualifiedClassType());
2760 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2761 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002762 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002763 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002764 else if (propertyInPrimaryClass) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002765 // Don't issue warning on property with no life time in class
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002766 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002767 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002768 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002769 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002770
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002771 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002772 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002773 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002774 }
John McCallb61e14e2015-10-27 04:54:50 +00002775 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002776
2777 // FIXME: Implement warning dependent on NSCopying being
2778 // implemented. See also:
2779 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2780 // (please trim this list while you are at it).
2781 }
2782
Bill Wendling44426052012-12-20 19:22:21 +00002783 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2784 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002785 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002786 && PropertyTy->isBlockPointerType())
2787 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002788 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2789 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2790 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002791 PropertyTy->isBlockPointerType())
2792 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fangrui Song6907ce22018-07-30 19:24:48 +00002793
Bill Wendling44426052012-12-20 19:22:21 +00002794 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2795 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002796 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002797}