blob: 9166d231647c7cde6b8a09086887015cc570a4cb [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;
Fangrui Song6907ce22018-07-30 19:24:48 +0000309
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000310 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
311}
312
Fangrui Song6907ce22018-07-30 19:24:48 +0000313static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000314 SourceLocation LParenLoc, SourceLocation &Loc) {
315 if (LParenLoc.isMacroID())
316 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000317
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000318 SourceManager &SM = Context.getSourceManager();
319 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
320 // Try to load the file buffer.
321 bool invalidTemp = false;
322 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
323 if (invalidTemp)
324 return false;
325 const char *tokenBegin = file.data() + locInfo.second;
Fangrui Song6907ce22018-07-30 19:24:48 +0000326
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000327 // Lex from the start of the given location.
328 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
329 Context.getLangOpts(),
330 file.begin(), tokenBegin, file.end());
331 Token Tok;
332 do {
333 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000334 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000335 Loc = Tok.getLocation();
336 return true;
337 }
338 } while (Tok.isNot(tok::r_paren));
339 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000340}
341
Douglas Gregor429183e2015-12-09 22:57:32 +0000342/// Check for a mismatch in the atomicity of the given properties.
343static void checkAtomicPropertyMismatch(Sema &S,
344 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000345 ObjCPropertyDecl *NewProperty,
346 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000347 // If the atomicity of both matches, we're done.
348 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000349 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
350 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000351 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000352 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
353 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000354 if (OldIsAtomic == NewIsAtomic) return;
355
356 // Determine whether the given property is readonly and implicitly
357 // atomic.
358 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
359 // Is it readonly?
360 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000361 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000362
363 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000364 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000365
366 // Was 'atomic' specified directly?
Fangrui Song6907ce22018-07-30 19:24:48 +0000367 if (Property->getPropertyAttributesAsWritten() &
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000368 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000369 return false;
370
371 return true;
372 };
373
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000374 // If we're allowed to propagate atomicity, and the new property did
375 // not specify atomicity at all, propagate.
376 const unsigned AtomicityMask =
377 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
378 if (PropagateAtomicity &&
379 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
380 unsigned Attrs = NewProperty->getPropertyAttributes();
381 Attrs = Attrs & ~AtomicityMask;
382 if (OldIsAtomic)
383 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
Fangrui Song6907ce22018-07-30 19:24:48 +0000384 else
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000385 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
386
387 NewProperty->overwritePropertyAttributes(Attrs);
388 return;
389 }
390
Douglas Gregor429183e2015-12-09 22:57:32 +0000391 // One of the properties is atomic; if it's a readonly property, and
392 // 'atomic' wasn't explicitly specified, we're okay.
393 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
394 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
395 return;
396
397 // Diagnose the conflict.
398 const IdentifierInfo *OldContextName;
399 auto *OldDC = OldProperty->getDeclContext();
400 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
401 OldContextName = Category->getClassInterface()->getIdentifier();
402 else
403 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
404
405 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
406 << NewProperty->getDeclName() << "atomic"
407 << OldContextName;
408 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
409}
410
Douglas Gregor90d34422013-01-21 19:05:22 +0000411ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000412Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000413 SourceLocation AtLoc,
414 SourceLocation LParenLoc,
415 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000416 Selector GetterSel,
417 SourceLocation GetterNameLoc,
418 Selector SetterSel,
419 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000420 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000421 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000422 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000423 QualType T,
424 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000425 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000426 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000428 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000429 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000430 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
Fangrui Song6907ce22018-07-30 19:24:48 +0000431
Ted Kremenek959e8302010-03-12 02:31:10 +0000432 // We need to look in the @interface to see if the @property was
433 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000434 if (!CCPrimary) {
435 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000436 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000437 }
438
Manman Ren5b786402016-01-28 18:49:28 +0000439 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
440 (Attributes & ObjCDeclSpec::DQ_PR_class);
441
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000442 // Find the property in the extended class's primary class or
443 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000444 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
445 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000446
Fangrui Song6907ce22018-07-30 19:24:48 +0000447 // If we found a property in an extension, complain.
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000448 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
449 Diag(AtLoc, diag::err_duplicate_property);
450 Diag(PIDecl->getLocation(), diag::note_property_declare);
451 return nullptr;
452 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000453
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000454 // Check for consistency with the previous declaration, if there is one.
455 if (PIDecl) {
456 // A readonly property declared in the primary class can be refined
457 // by adding a readwrite property within an extension.
458 // Anything else is an error.
459 if (!(PIDecl->isReadOnly() && isReadWrite)) {
460 // Tailor the diagnostics for the common case where a readwrite
461 // property is declared both in the @interface and the continuation.
462 // This is a common error where the user often intended the original
463 // declaration to be readonly.
464 unsigned diag =
465 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
466 (PIDecl->getPropertyAttributesAsWritten() &
467 ObjCPropertyDecl::OBJC_PR_readwrite)
468 ? diag::err_use_continuation_class_redeclaration_readwrite
469 : diag::err_use_continuation_class;
470 Diag(AtLoc, diag)
471 << CCPrimary->getDeclName();
472 Diag(PIDecl->getLocation(), diag::note_property_declare);
473 return nullptr;
474 }
475
476 // Check for consistency of getters.
477 if (PIDecl->getGetterName() != GetterSel) {
478 // If the getter was written explicitly, complain.
479 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
480 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
481 << PIDecl->getGetterName() << GetterSel;
482 Diag(PIDecl->getLocation(), diag::note_property_declare);
483 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000484
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000485 // Always adopt the getter from the original declaration.
486 GetterSel = PIDecl->getGetterName();
487 Attributes |= ObjCDeclSpec::DQ_PR_getter;
488 }
489
490 // Check consistency of ownership.
491 unsigned ExistingOwnership
492 = getOwnershipRule(PIDecl->getPropertyAttributes());
493 unsigned NewOwnership = getOwnershipRule(Attributes);
494 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
495 // If the ownership was written explicitly, complain.
496 if (getOwnershipRule(AttributesAsWritten)) {
497 Diag(AtLoc, diag::warn_property_attr_mismatch);
498 Diag(PIDecl->getLocation(), diag::note_property_declare);
499 }
500
501 // Take the ownership from the original property.
502 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
503 }
504
Fangrui Song6907ce22018-07-30 19:24:48 +0000505 // If the redeclaration is 'weak' but the original property is not,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000506 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
507 !(PIDecl->getPropertyAttributesAsWritten()
508 & ObjCPropertyDecl::OBJC_PR_weak) &&
509 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
510 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
511 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
512 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fangrui Song6907ce22018-07-30 19:24:48 +0000513 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000514 }
515
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000516 // Create a new ObjCPropertyDecl with the DeclContext being
517 // the class extension.
518 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000519 FD, GetterSel, GetterNameLoc,
520 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000521 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000522 Attributes, AttributesAsWritten,
523 T, TSI, MethodImplKind, DC);
524
525 // If there was no declaration of a property with the same name in
526 // the primary class, we're done.
527 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000528 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000529 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000530 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000531
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000532 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
533 bool IncompatibleObjC = false;
534 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000535 // Relax the strict type matching for property type in continuation class.
536 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000537 // as it narrows the object type in its primary class property. Note that
538 // this conversion is safe only because the wider type is for a 'readonly'
539 // property in primary class and 'narrowed' type for a 'readwrite' property
540 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000541 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
542 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
543 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
544 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
545 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000546 ConvertedType, IncompatibleObjC))
547 || IncompatibleObjC) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000548 Diag(AtLoc,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000549 diag::err_type_mismatch_continuation_class) << PDecl->getType();
550 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000551 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000552 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000553 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000554
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000555 // Check that atomicity of property in class extension matches the previous
556 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000557 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000558
Douglas Gregore17765e2015-11-03 17:02:34 +0000559 // Make sure getter/setter are appropriately synthesized.
560 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000561 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000562}
563
564ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
565 ObjCContainerDecl *CDecl,
566 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000567 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000568 FieldDeclarator &FD,
569 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000570 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000571 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000572 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000574 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000575 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000576 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000577 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000578 tok::ObjCKeywordKind MethodImplKind,
579 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000580 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000581
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000582 // Property defaults to 'assign' if it is readwrite, unless this is ARC
583 // and the type is retainable.
584 bool isAssign;
585 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
586 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
587 isAssign = true;
588 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
589 isAssign = false;
590 } else {
591 isAssign = (!getLangOpts().ObjCAutoRefCount ||
592 !T->isObjCRetainableType());
593 }
594
595 // Issue a warning if property is 'assign' as default and its
596 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000597 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000598 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000599 if (const ObjCObjectPointerType *ObjPtrTy =
600 T->getAs<ObjCObjectPointerType>()) {
601 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
602 if (IDecl)
603 if (ObjCProtocolDecl* PNSCopying =
604 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
605 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
606 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000607 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000608 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000609
610 if (T->isObjCObjectType()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000611 SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000612 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000613 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
614 << FixItHint::CreateInsertion(StarLoc, "*");
615 T = Context.getObjCObjectPointerType(T);
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000616 SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc();
Eli Friedman999af7b2013-07-09 01:38:07 +0000617 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
618 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000619
George Burgess IV00f70bd2018-03-01 05:43:23 +0000620 DeclContext *DC = CDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000621 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
622 FD.D.getIdentifierLoc(),
Fangrui Song6907ce22018-07-30 19:24:48 +0000623 PropertyId, AtLoc,
Douglas Gregor813a0662015-06-19 18:14:38 +0000624 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000625
Manman Ren5b786402016-01-28 18:49:28 +0000626 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
627 (Attributes & ObjCDeclSpec::DQ_PR_class);
628 // Class property and instance property can have the same name.
629 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
630 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000631 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000632 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000633 PDecl->setInvalidDecl();
634 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000635 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000636 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000637 if (lexicalDC)
638 PDecl->setLexicalDeclContext(lexicalDC);
639 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000640
641 if (T->isArrayType() || T->isFunctionType()) {
642 Diag(AtLoc, diag::err_property_type) << T;
643 PDecl->setInvalidDecl();
644 }
645
646 ProcessDeclAttributes(S, PDecl, FD.D);
647
648 // Regardless of setter/getter attribute, we save the default getter/setter
649 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000650 PDecl->setGetterName(GetterSel, GetterNameLoc);
651 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000652 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000653 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000654
Bill Wendling44426052012-12-20 19:22:21 +0000655 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000656 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
657
Bill Wendling44426052012-12-20 19:22:21 +0000658 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000659 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
660
Bill Wendling44426052012-12-20 19:22:21 +0000661 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000662 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
663
664 if (isReadWrite)
665 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
666
Bill Wendling44426052012-12-20 19:22:21 +0000667 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000668 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
669
Bill Wendling44426052012-12-20 19:22:21 +0000670 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000671 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
672
Bill Wendling44426052012-12-20 19:22:21 +0000673 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000674 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
675
Bill Wendling44426052012-12-20 19:22:21 +0000676 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000677 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
678
Bill Wendling44426052012-12-20 19:22:21 +0000679 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000680 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
681
Ted Kremenekac597f32010-03-12 00:46:40 +0000682 if (isAssign)
683 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
684
John McCall43192862011-09-13 18:31:23 +0000685 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000686 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000687 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000688 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000689 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000690
John McCall31168b02011-06-15 23:02:42 +0000691 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000692 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000693 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
694 if (isAssign)
695 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
696
Ted Kremenekac597f32010-03-12 00:46:40 +0000697 if (MethodImplKind == tok::objc_required)
698 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
699 else if (MethodImplKind == tok::objc_optional)
700 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000701
Douglas Gregor813a0662015-06-19 18:14:38 +0000702 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
703 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
704
Douglas Gregor849ebc22015-06-19 18:14:46 +0000705 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
706 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
707
Manman Ren387ff7f2016-01-26 18:52:43 +0000708 if (Attributes & ObjCDeclSpec::DQ_PR_class)
709 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
710
Ted Kremenek959e8302010-03-12 02:31:10 +0000711 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000712}
713
John McCall31168b02011-06-15 23:02:42 +0000714static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
715 ObjCPropertyDecl *property,
716 ObjCIvarDecl *ivar) {
717 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
718
John McCall31168b02011-06-15 23:02:42 +0000719 QualType ivarType = ivar->getType();
720 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000721
John McCall43192862011-09-13 18:31:23 +0000722 // The lifetime implied by the property's attributes.
723 Qualifiers::ObjCLifetime propertyLifetime =
724 getImpliedARCOwnership(property->getPropertyAttributes(),
725 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000726
John McCall43192862011-09-13 18:31:23 +0000727 // We're fine if they match.
728 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000729
John McCall460ce582015-10-22 18:38:17 +0000730 // None isn't a valid lifetime for an object ivar in ARC, and
731 // __autoreleasing is never valid; don't diagnose twice.
732 if ((ivarLifetime == Qualifiers::OCL_None &&
733 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000734 ivarLifetime == Qualifiers::OCL_Autoreleasing)
735 return;
John McCall31168b02011-06-15 23:02:42 +0000736
John McCalld8561f02012-08-20 23:36:59 +0000737 // If the ivar is private, and it's implicitly __unsafe_unretained
Nico Weber138a8152019-08-21 15:52:44 +0000738 // because of its type, then pretend it was actually implicitly
John McCalld8561f02012-08-20 23:36:59 +0000739 // __strong. This is only sound because we're processing the
740 // property implementation before parsing any method bodies.
741 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
742 propertyLifetime == Qualifiers::OCL_Strong &&
743 ivar->getAccessControl() == ObjCIvarDecl::Private) {
744 SplitQualType split = ivarType.split();
745 if (split.Quals.hasObjCLifetime()) {
746 assert(ivarType->isObjCARCImplicitlyUnretainedType());
747 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
748 ivarType = S.Context.getQualifiedType(split);
749 ivar->setType(ivarType);
750 return;
751 }
752 }
753
John McCall43192862011-09-13 18:31:23 +0000754 switch (propertyLifetime) {
755 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000756 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000757 << property->getDeclName()
758 << ivar->getDeclName()
759 << ivarLifetime;
760 break;
John McCall31168b02011-06-15 23:02:42 +0000761
John McCall43192862011-09-13 18:31:23 +0000762 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000763 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000764 << property->getDeclName()
765 << ivar->getDeclName();
766 break;
John McCall31168b02011-06-15 23:02:42 +0000767
John McCall43192862011-09-13 18:31:23 +0000768 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000769 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000770 << property->getDeclName()
771 << ivar->getDeclName()
Fangrui Song6907ce22018-07-30 19:24:48 +0000772 << ((property->getPropertyAttributesAsWritten()
John McCall43192862011-09-13 18:31:23 +0000773 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
774 break;
John McCall31168b02011-06-15 23:02:42 +0000775
John McCall43192862011-09-13 18:31:23 +0000776 case Qualifiers::OCL_Autoreleasing:
777 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000778
John McCall43192862011-09-13 18:31:23 +0000779 case Qualifiers::OCL_None:
780 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000781 return;
782 }
783
784 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000785 if (propertyImplLoc.isValid())
786 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000787}
788
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000789/// setImpliedPropertyAttributeForReadOnlyProperty -
790/// This routine evaludates life-time attributes for a 'readonly'
791/// property with no known lifetime of its own, using backing
792/// 'ivar's attribute, if any. If no backing 'ivar', property's
793/// life-time is assumed 'strong'.
794static void setImpliedPropertyAttributeForReadOnlyProperty(
795 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000796 Qualifiers::ObjCLifetime propertyLifetime =
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000797 getImpliedARCOwnership(property->getPropertyAttributes(),
798 property->getType());
799 if (propertyLifetime != Qualifiers::OCL_None)
800 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000801
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000802 if (!ivar) {
803 // if no backing ivar, make property 'strong'.
804 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
805 return;
806 }
807 // property assumes owenership of backing ivar.
808 QualType ivarType = ivar->getType();
809 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
810 if (ivarLifetime == Qualifiers::OCL_Strong)
811 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
812 else if (ivarLifetime == Qualifiers::OCL_Weak)
813 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000814}
Ted Kremenekac597f32010-03-12 00:46:40 +0000815
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000816static bool
817isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2,
818 ObjCPropertyDecl::PropertyAttributeKind Kind) {
819 return (Attr1 & Kind) != (Attr2 & Kind);
820}
821
822static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2,
823 unsigned Kinds) {
824 return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0);
825}
826
827/// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate
828/// property declaration that should be synthesised in all of the inherited
829/// protocols. It also diagnoses properties declared in inherited protocols with
830/// mismatched types or attributes, since any of them can be candidate for
831/// synthesis.
832static ObjCPropertyDecl *
833SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc,
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000834 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000835 ObjCPropertyDecl *Property) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000836 assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) &&
837 "Expected a property from a protocol");
838 ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet;
839 ObjCInterfaceDecl::PropertyDeclOrder Properties;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000840 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
841 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000842 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
843 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000844 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000845 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) {
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000846 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000847 for (const auto *PI : SDecl->all_referenced_protocols()) {
848 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000849 PDecl->collectInheritedProtocolProperties(Property, ProtocolSet,
850 Properties);
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000851 }
852 SDecl = SDecl->getSuperClass();
853 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000854 }
855
856 if (Properties.empty())
857 return Property;
858
859 ObjCPropertyDecl *OriginalProperty = Property;
860 size_t SelectedIndex = 0;
861 for (const auto &Prop : llvm::enumerate(Properties)) {
862 // Select the 'readwrite' property if such property exists.
863 if (Property->isReadOnly() && !Prop.value()->isReadOnly()) {
864 Property = Prop.value();
865 SelectedIndex = Prop.index();
866 }
867 }
868 if (Property != OriginalProperty) {
869 // Check that the old property is compatible with the new one.
870 Properties[SelectedIndex] = OriginalProperty;
871 }
872
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000873 QualType RHSType = S.Context.getCanonicalType(Property->getType());
Alex Lorenz34d070f2017-08-22 10:38:07 +0000874 unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000875 enum MismatchKind {
876 IncompatibleType = 0,
877 HasNoExpectedAttribute,
878 HasUnexpectedAttribute,
879 DifferentGetter,
880 DifferentSetter
881 };
882 // Represents a property from another protocol that conflicts with the
883 // selected declaration.
884 struct MismatchingProperty {
885 const ObjCPropertyDecl *Prop;
886 MismatchKind Kind;
887 StringRef AttributeName;
888 };
889 SmallVector<MismatchingProperty, 4> Mismatches;
890 for (ObjCPropertyDecl *Prop : Properties) {
891 // Verify the property attributes.
Alex Lorenz34d070f2017-08-22 10:38:07 +0000892 unsigned Attr = Prop->getPropertyAttributesAsWritten();
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000893 if (Attr != OriginalAttributes) {
894 auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) {
895 MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute
896 : HasUnexpectedAttribute;
897 Mismatches.push_back({Prop, Kind, AttributeName});
898 };
Alex Lorenz61372552018-05-02 22:40:19 +0000899 // The ownership might be incompatible unless the property has no explicit
900 // ownership.
901 bool HasOwnership = (Attr & (ObjCPropertyDecl::OBJC_PR_retain |
902 ObjCPropertyDecl::OBJC_PR_strong |
903 ObjCPropertyDecl::OBJC_PR_copy |
904 ObjCPropertyDecl::OBJC_PR_assign |
905 ObjCPropertyDecl::OBJC_PR_unsafe_unretained |
906 ObjCPropertyDecl::OBJC_PR_weak)) != 0;
907 if (HasOwnership &&
908 isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000909 ObjCPropertyDecl::OBJC_PR_copy)) {
910 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_copy, "copy");
911 continue;
912 }
Alex Lorenz61372552018-05-02 22:40:19 +0000913 if (HasOwnership && areIncompatiblePropertyAttributes(
914 OriginalAttributes, Attr,
915 ObjCPropertyDecl::OBJC_PR_retain |
916 ObjCPropertyDecl::OBJC_PR_strong)) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000917 Diag(OriginalAttributes & (ObjCPropertyDecl::OBJC_PR_retain |
918 ObjCPropertyDecl::OBJC_PR_strong),
919 "retain (or strong)");
920 continue;
921 }
922 if (isIncompatiblePropertyAttribute(OriginalAttributes, Attr,
923 ObjCPropertyDecl::OBJC_PR_atomic)) {
924 Diag(OriginalAttributes & ObjCPropertyDecl::OBJC_PR_atomic, "atomic");
925 continue;
926 }
927 }
928 if (Property->getGetterName() != Prop->getGetterName()) {
929 Mismatches.push_back({Prop, DifferentGetter, ""});
930 continue;
931 }
932 if (!Property->isReadOnly() && !Prop->isReadOnly() &&
933 Property->getSetterName() != Prop->getSetterName()) {
934 Mismatches.push_back({Prop, DifferentSetter, ""});
935 continue;
936 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000937 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
938 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
939 bool IncompatibleObjC = false;
940 QualType ConvertedType;
941 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
942 || IncompatibleObjC) {
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000943 Mismatches.push_back({Prop, IncompatibleType, ""});
944 continue;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000945 }
946 }
947 }
Alex Lorenz50b2dd32017-07-13 11:06:22 +0000948
949 if (Mismatches.empty())
950 return Property;
951
952 // Diagnose incompability.
953 {
954 bool HasIncompatibleAttributes = false;
955 for (const auto &Note : Mismatches)
956 HasIncompatibleAttributes =
957 Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes;
958 // Promote the warning to an error if there are incompatible attributes or
959 // incompatible types together with readwrite/readonly incompatibility.
960 auto Diag = S.Diag(Property->getLocation(),
961 Property != OriginalProperty || HasIncompatibleAttributes
962 ? diag::err_protocol_property_mismatch
963 : diag::warn_protocol_property_mismatch);
964 Diag << Mismatches[0].Kind;
965 switch (Mismatches[0].Kind) {
966 case IncompatibleType:
967 Diag << Property->getType();
968 break;
969 case HasNoExpectedAttribute:
970 case HasUnexpectedAttribute:
971 Diag << Mismatches[0].AttributeName;
972 break;
973 case DifferentGetter:
974 Diag << Property->getGetterName();
975 break;
976 case DifferentSetter:
977 Diag << Property->getSetterName();
978 break;
979 }
980 }
981 for (const auto &Note : Mismatches) {
982 auto Diag =
983 S.Diag(Note.Prop->getLocation(), diag::note_protocol_property_declare)
984 << Note.Kind;
985 switch (Note.Kind) {
986 case IncompatibleType:
987 Diag << Note.Prop->getType();
988 break;
989 case HasNoExpectedAttribute:
990 case HasUnexpectedAttribute:
991 Diag << Note.AttributeName;
992 break;
993 case DifferentGetter:
994 Diag << Note.Prop->getGetterName();
995 break;
996 case DifferentSetter:
997 Diag << Note.Prop->getSetterName();
998 break;
999 }
1000 }
1001 if (AtLoc.isValid())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001002 S.Diag(AtLoc, diag::note_property_synthesize);
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001003
1004 return Property;
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001005}
1006
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001007/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001008static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
1009 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001010 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
1011
1012 // If this is a readwrite property in a class extension that refines
1013 // a readonly property in the original class definition, check it as
1014 // well.
1015
1016 // If it's a readonly property, we're not interested.
1017 if (Prop->isReadOnly()) return false;
1018
1019 // Is it declared in an extension?
1020 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
1021 if (!Category || !Category->IsClassExtension()) return false;
1022
1023 // Find the corresponding property in the primary class definition.
1024 auto OrigClass = Category->getClassInterface();
1025 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
1026 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
1027 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1028 }
1029
Douglas Gregor02535432015-12-18 00:52:31 +00001030 // Look through all of the protocols.
1031 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +00001032 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
1033 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +00001034 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
1035 }
1036
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001037 return false;
1038}
1039
Ted Kremenekac597f32010-03-12 00:46:40 +00001040/// ActOnPropertyImplDecl - This routine performs semantic checks and
1041/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +00001042/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +00001043///
John McCall48871652010-08-21 09:40:31 +00001044Decl *Sema::ActOnPropertyImplDecl(Scope *S,
1045 SourceLocation AtLoc,
1046 SourceLocation PropertyLoc,
1047 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +00001048 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001049 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +00001050 SourceLocation PropertyIvarLoc,
1051 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +00001052 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +00001053 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +00001054 // Make sure we have a context for the property implementation declaration.
1055 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001056 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001057 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001058 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001059 if (PropertyIvarLoc.isInvalid())
1060 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +00001061 SourceLocation PropertyDiagLoc = PropertyLoc;
1062 if (PropertyDiagLoc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001063 PropertyDiagLoc = ClassImpDecl->getBeginLoc();
Craig Topperc3ec1492014-05-26 06:22:03 +00001064 ObjCPropertyDecl *property = nullptr;
1065 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001066 // Find the class or category class where this property must have
1067 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +00001068 ObjCImplementationDecl *IC = nullptr;
1069 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001070 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
1071 IDecl = IC->getClassInterface();
1072 // We always synthesize an interface for an implementation
1073 // without an interface decl. So, IDecl is always non-zero.
1074 assert(IDecl &&
1075 "ActOnPropertyImplDecl - @implementation without @interface");
1076
1077 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +00001078 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001079 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001080 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001081 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001082 }
Manman Rendfef4062016-01-29 19:16:39 +00001083 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001084 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +00001085 return nullptr;
1086 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001087 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +00001088 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
1089 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +00001090 if (AtLoc.isValid())
1091 Diag(AtLoc, diag::warn_implicit_atomic_property);
1092 else
1093 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
1094 Diag(property->getLocation(), diag::note_property_declare);
1095 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001096
Ted Kremenekac597f32010-03-12 00:46:40 +00001097 if (const ObjCCategoryDecl *CD =
1098 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
1099 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +00001100 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001101 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +00001102 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001103 }
1104 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001105 if (Synthesize&&
1106 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
1107 property->hasAttr<IBOutletAttr>() &&
1108 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001109 bool ReadWriteProperty = false;
1110 // Search into the class extensions and see if 'readonly property is
1111 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +00001112 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001113 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
1114 if (!R.empty())
1115 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
1116 PIkind = ExtProp->getPropertyAttributesAsWritten();
1117 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
1118 ReadWriteProperty = true;
1119 break;
1120 }
1121 }
1122 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001123
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001124 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +00001125 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +00001126 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001127 SourceLocation readonlyLoc;
Fangrui Song6907ce22018-07-30 19:24:48 +00001128 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001129 property->getLParenLoc(), readonlyLoc)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001130 SourceLocation endLoc =
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001131 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
1132 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
Fangrui Song6907ce22018-07-30 19:24:48 +00001133 Diag(property->getLocation(),
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +00001134 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
1135 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
1136 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +00001137 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001138 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +00001139 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
Alex Lorenz50b2dd32017-07-13 11:06:22 +00001140 property = SelectPropertyForSynthesisFromProtocols(*this, AtLoc, IDecl,
1141 property);
1142
Ted Kremenekac597f32010-03-12 00:46:40 +00001143 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1144 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001145 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001146 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001147 }
1148 IDecl = CatImplClass->getClassInterface();
1149 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001150 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001151 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001152 }
1153 ObjCCategoryDecl *Category =
1154 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1155
1156 // If category for this implementation not found, it is an error which
1157 // has already been reported eralier.
1158 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001159 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001160 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001161 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001162 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001163 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001164 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001165 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001166 }
1167 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001168 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001169 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001170 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001171 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001172 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001173 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001174 // Check that we have a valid, previously declared ivar for @synthesize
1175 if (Synthesize) {
1176 // @synthesize
1177 if (!PropertyIvar)
1178 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001179 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1180 ObjCInterfaceDecl *ClassDeclared;
1181 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1182 QualType PropType = property->getType();
1183 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001184
1185 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001186 diag::err_incomplete_synthesized_property,
1187 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001188 Diag(property->getLocation(), diag::note_property_declare);
1189 CompleteTypeErr = true;
1190 }
1191
David Blaikiebbafb8a2012-03-11 07:00:24 +00001192 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001193 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001194 ObjCPropertyDecl::OBJC_PR_readonly) &&
1195 PropertyIvarType->isObjCRetainableType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001196 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001197 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001198
1199 ObjCPropertyDecl::PropertyAttributeKind kind
John McCall31168b02011-06-15 23:02:42 +00001200 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001201
John McCall460ce582015-10-22 18:38:17 +00001202 bool isARCWeak = false;
1203 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1204 // Add GC __weak to the ivar type if the property is weak.
1205 if (getLangOpts().getGC() != LangOptions::NonGC) {
1206 assert(!getLangOpts().ObjCAutoRefCount);
1207 if (PropertyIvarType.isObjCGCStrong()) {
1208 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1209 Diag(property->getLocation(), diag::note_property_declare);
1210 } else {
1211 PropertyIvarType =
1212 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1213 }
1214
1215 // Otherwise, check whether ARC __weak is enabled and works with
1216 // the property type.
John McCall43192862011-09-13 18:31:23 +00001217 } else {
John McCall460ce582015-10-22 18:38:17 +00001218 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001219 // Only complain here when synthesizing an ivar.
1220 if (!Ivar) {
1221 Diag(PropertyDiagLoc,
1222 getLangOpts().ObjCWeakRuntime
1223 ? diag::err_synthesizing_arc_weak_property_disabled
1224 : diag::err_synthesizing_arc_weak_property_no_runtime);
1225 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001226 }
John McCallb61e14e2015-10-27 04:54:50 +00001227 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001228 } else {
1229 isARCWeak = true;
1230 if (const ObjCObjectPointerType *ObjT =
1231 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1232 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1233 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1234 Diag(property->getLocation(),
1235 diag::err_arc_weak_unavailable_property)
1236 << PropertyIvarType;
1237 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1238 << ClassImpDecl->getName();
1239 }
1240 }
1241 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001242 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001243 }
John McCall460ce582015-10-22 18:38:17 +00001244
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001245 if (AtLoc.isInvalid()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001246 // Check when default synthesizing a property that there is
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001247 // an ivar matching property name and issue warning; since this
1248 // is the most common case of not using an ivar used for backing
1249 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001250 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fangrui Song6907ce22018-07-30 19:24:48 +00001251 ObjCIvarDecl *originalIvar =
1252 IDecl->lookupInstanceVariable(property->getIdentifier(),
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001253 ClassDeclared);
1254 if (originalIvar) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001255 Diag(PropertyDiagLoc,
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001256 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001257 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001258 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001259 Diag(property->getLocation(), diag::note_property_declare);
1260 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001261 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001262 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001263
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001264 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001265 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001266 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001267 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001268 !PropertyIvarType.getObjCLifetime() &&
1269 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001270
John McCall43192862011-09-13 18:31:23 +00001271 // It's an error if we have to do this and the user didn't
1272 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001273 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001274 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001275 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001276 diag::err_arc_objc_property_default_assign_on_object);
1277 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001278 } else {
1279 Qualifiers::ObjCLifetime lifetime =
1280 getImpliedARCOwnership(kind, PropertyIvarType);
1281 assert(lifetime && "no lifetime for property?");
Fangrui Song6907ce22018-07-30 19:24:48 +00001282
John McCall31168b02011-06-15 23:02:42 +00001283 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001284 qs.addObjCLifetime(lifetime);
Fangrui Song6907ce22018-07-30 19:24:48 +00001285 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
John McCall31168b02011-06-15 23:02:42 +00001286 }
John McCall31168b02011-06-15 23:02:42 +00001287 }
1288
Abramo Bagnaradff19302011-03-08 08:55:46 +00001289 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001290 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Rui Ueyama49a3ad22019-07-16 04:46:31 +00001291 PropertyIvarType, /*TInfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001292 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001293 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001294 if (RequireNonAbstractType(PropertyIvarLoc,
1295 PropertyIvarType,
1296 diag::err_abstract_type_in_decl,
1297 AbstractSynthesizedIvarType)) {
1298 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001299 // An abstract type is as bad as an incomplete type.
1300 CompleteTypeErr = true;
1301 }
Volodymyr Sapsai30680e92017-10-23 22:01:41 +00001302 if (!CompleteTypeErr) {
1303 const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>();
1304 if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) {
1305 Diag(PropertyIvarLoc, diag::err_synthesize_variable_sized_ivar)
1306 << PropertyIvarType;
1307 CompleteTypeErr = true; // suppress later diagnostics about the ivar
1308 }
1309 }
Richard Smith81f5ade2016-12-15 02:28:18 +00001310 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001311 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001312 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001313 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001314
John McCall5fb5df92012-06-20 06:18:46 +00001315 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001316 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001317 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001318 // Note! I deliberately want it to fall thru so, we have a
1319 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001320 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001321 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001322 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001323 << property->getDeclName() << Ivar->getDeclName()
1324 << ClassDeclared->getDeclName();
1325 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001326 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001327 // Note! I deliberately want it to fall thru so more errors are caught.
1328 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001329 property->setPropertyIvarDecl(Ivar);
1330
Ted Kremenekac597f32010-03-12 00:46:40 +00001331 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1332
1333 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001334 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001335 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001336 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001337 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001338 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001339 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001340 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001341 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001342 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1343 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001344 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001345 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001346 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001347 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001348 << property->getDeclName() << PropType
1349 << Ivar->getDeclName() << IvarType;
1350 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001351 // Note! I deliberately want it to fall thru so, we have a
1352 // a property implementation and to avoid future warnings.
1353 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001354 else {
1355 // FIXME! Rules for properties are somewhat different that those
1356 // for assignments. Use a new routine to consolidate all cases;
1357 // specifically for property redeclarations as well as for ivars.
1358 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1359 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1360 if (lhsType != rhsType &&
1361 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001362 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001363 << property->getDeclName() << PropType
1364 << Ivar->getDeclName() << IvarType;
1365 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1366 // Fall thru - see previous comment
1367 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001368 }
1369 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001370 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001371 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001372 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001373 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001374 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001375 // Fall thru - see previous comment
1376 }
John McCall31168b02011-06-15 23:02:42 +00001377 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001378 if ((property->getType()->isObjCObjectPointerType() ||
1379 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001380 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001381 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001382 << property->getDeclName() << Ivar->getDeclName();
1383 // Fall thru - see previous comment
1384 }
1385 }
John McCall460ce582015-10-22 18:38:17 +00001386 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1387 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001388 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001389 } else if (PropertyIvar)
1390 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001391 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
Fangrui Song6907ce22018-07-30 19:24:48 +00001392
Ted Kremenekac597f32010-03-12 00:46:40 +00001393 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1394 ObjCPropertyImplDecl *PIDecl =
1395 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1396 property,
1397 (Synthesize ?
1398 ObjCPropertyImplDecl::Synthesize
1399 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001400 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001401
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001402 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001403 PIDecl->setInvalidDecl();
1404
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001405 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1406 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001407 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001408 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001409 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1410 // returned by the getter as it must conform to C++'s copy-return rules.
1411 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001412 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001413 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001414 DeclRefExpr *SelfExpr = new (Context)
1415 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1416 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001417 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001418 Expr *LoadSelfExpr =
1419 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001420 CK_LValueToRValue, SelfExpr, nullptr,
1421 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001422 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001423 new (Context) ObjCIvarRefExpr(Ivar,
1424 Ivar->getUsageType(SelfDecl->getType()),
1425 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001426 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001427 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001428 ExprResult Res = PerformCopyInitialization(
1429 InitializedEntity::InitializeResult(PropertyDiagLoc,
1430 getterMethod->getReturnType(),
1431 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001432 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001433 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001434 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001435 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001436 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001437 PIDecl->setGetterCXXConstructor(ResExpr);
1438 }
1439 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001440 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1441 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001442 Diag(getterMethod->getLocation(),
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001443 diag::warn_property_getter_owning_mismatch);
1444 Diag(property->getLocation(), diag::note_property_declare);
1445 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001446 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1447 switch (getterMethod->getMethodFamily()) {
1448 case OMF_retain:
1449 case OMF_retainCount:
1450 case OMF_release:
1451 case OMF_autorelease:
1452 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1453 << 1 << getterMethod->getSelector();
1454 break;
1455 default:
1456 break;
1457 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001458 }
1459 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1460 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001461 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1462 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001463 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001464 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001465 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001466 DeclRefExpr *SelfExpr = new (Context)
1467 DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue,
1468 PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001469 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001470 Expr *LoadSelfExpr =
1471 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001472 CK_LValueToRValue, SelfExpr, nullptr,
1473 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001474 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001475 new (Context) ObjCIvarRefExpr(Ivar,
1476 Ivar->getUsageType(SelfDecl->getType()),
1477 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001478 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001479 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001480 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1481 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001482 QualType T = Param->getType().getNonReferenceType();
Bruno Ricci5fc4db72018-12-21 14:10:18 +00001483 DeclRefExpr *rhs = new (Context)
1484 DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001485 MarkDeclRefReferenced(rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001486 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001487 BO_Assign, lhs, rhs);
Fangrui Song6907ce22018-07-30 19:24:48 +00001488 if (property->getPropertyAttributes() &
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001489 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001490 Expr *callExpr = Res.getAs<Expr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001491 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001492 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1493 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001494 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001495 if (property->getType()->isReferenceType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001496 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001497 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001498 << property->getType();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001499 Diag(FuncDecl->getBeginLoc(), diag::note_callee_decl)
1500 << FuncDecl;
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001501 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001502 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001503 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001504 }
1505 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001506
Ted Kremenekac597f32010-03-12 00:46:40 +00001507 if (IC) {
1508 if (Synthesize)
1509 if (ObjCPropertyImplDecl *PPIDecl =
1510 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001511 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001512 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1513 << PropertyIvar;
1514 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1515 }
1516
1517 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001518 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001519 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001520 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001521 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001522 }
1523 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001524 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001525 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001526 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001527 // Diagnose if an ivar was lazily synthesdized due to a previous
1528 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001529 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001530 ObjCInterfaceDecl *ClassDeclared=nullptr;
1531 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001532 if (!Synthesize)
1533 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1534 else {
1535 if (PropertyIvar && PropertyIvar != PropertyId)
1536 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1537 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001538 // Issue diagnostics only if Ivar belongs to current class.
Fangrui Song6907ce22018-07-30 19:24:48 +00001539 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001540 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001541 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
Fariborz Jahanian18722982010-07-17 00:59:30 +00001542 << PropertyId;
1543 Ivar->setInvalidDecl();
1544 }
1545 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001546 } else {
1547 if (Synthesize)
1548 if (ObjCPropertyImplDecl *PPIDecl =
1549 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001550 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001551 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1552 << PropertyIvar;
1553 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1554 }
1555
1556 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001557 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001558 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001559 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001560 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001561 }
1562 CatImplClass->addPropertyImplementation(PIDecl);
1563 }
1564
John McCall48871652010-08-21 09:40:31 +00001565 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001566}
1567
1568//===----------------------------------------------------------------------===//
1569// Helper methods.
1570//===----------------------------------------------------------------------===//
1571
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001572/// DiagnosePropertyMismatch - Compares two properties for their
1573/// attributes and types and warns on a variety of inconsistencies.
1574///
1575void
1576Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1577 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001578 const IdentifierInfo *inheritedName,
1579 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001580 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001581 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001582 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001583 SuperProperty->getPropertyAttributes();
Fangrui Song6907ce22018-07-30 19:24:48 +00001584
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001585 // We allow readonly properties without an explicit ownership
1586 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1587 // to be overridden by a property with any explicit ownership in the subclass.
1588 if (!OverridingProtocolProperty &&
1589 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1590 ;
1591 else {
1592 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1593 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1594 Diag(Property->getLocation(), diag::warn_readonly_property)
1595 << Property->getDeclName() << inheritedName;
1596 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1597 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001598 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001599 << Property->getDeclName() << "copy" << inheritedName;
1600 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1601 unsigned CAttrRetain =
1602 (CAttr &
1603 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1604 unsigned SAttrRetain =
1605 (SAttr &
1606 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1607 bool CStrong = (CAttrRetain != 0);
1608 bool SStrong = (SAttrRetain != 0);
1609 if (CStrong != SStrong)
1610 Diag(Property->getLocation(), diag::warn_property_attribute)
1611 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1612 }
John McCall31168b02011-06-15 23:02:42 +00001613 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001614
Douglas Gregor429183e2015-12-09 22:57:32 +00001615 // Check for nonatomic; note that nonatomic is effectively
1616 // meaningless for readonly properties, so don't diagnose if the
1617 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001618 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Alex Lorenz05a63ee2017-10-06 19:24:26 +00001619 // Readonly properties from protocols can be implemented as "readwrite"
1620 // with a custom setter name.
1621 if (Property->getSetterName() != SuperProperty->getSetterName() &&
1622 !(SuperProperty->isReadOnly() &&
1623 isa<ObjCProtocolDecl>(SuperProperty->getDeclContext()))) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001624 Diag(Property->getLocation(), diag::warn_property_attribute)
1625 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001626 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1627 }
1628 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001629 Diag(Property->getLocation(), diag::warn_property_attribute)
1630 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001631 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1632 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001633
1634 QualType LHSType =
1635 Context.getCanonicalType(SuperProperty->getType());
1636 QualType RHSType =
1637 Context.getCanonicalType(Property->getType());
1638
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001639 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001640 // Do cases not handled in above.
1641 // FIXME. For future support of covariant property types, revisit this.
1642 bool IncompatibleObjC = false;
1643 QualType ConvertedType;
Fangrui Song6907ce22018-07-30 19:24:48 +00001644 if (!isObjCPointerConversion(RHSType, LHSType,
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001645 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001646 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001647 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1648 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001649 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1650 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001651 }
1652}
1653
1654bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1655 ObjCMethodDecl *GetterMethod,
1656 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001657 if (!GetterMethod)
1658 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001659 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001660 QualType PropertyRValueType =
1661 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1662 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001663 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001664 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1665 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001666 if ((propertyObjCPtr =
1667 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001668 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1669 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001670 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001671 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001672 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001673 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001674 << GetterMethod->getSelector() << GetterType;
1675 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1676 return true;
1677 } else {
1678 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001679 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001680 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1681 if (lhsType != rhsType && lhsType->isArithmeticType())
1682 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001683 }
1684 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001685
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001686 if (!compat) {
1687 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1688 << property->getDeclName()
1689 << GetterMethod->getSelector();
1690 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1691 return true;
1692 }
1693
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001694 return false;
1695}
1696
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001697/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001698/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001699static void
1700CollectImmediateProperties(ObjCContainerDecl *CDecl,
1701 ObjCContainerDecl::PropertyMap &PropMap,
1702 ObjCContainerDecl::PropertyMap &SuperPropMap,
1703 bool CollectClassPropsOnly = false,
1704 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001705 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001706 for (auto *Prop : IDecl->properties()) {
1707 if (CollectClassPropsOnly && !Prop->isClassProperty())
1708 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001709 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1710 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001711 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001712
1713 // Collect the properties from visible extensions.
1714 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001715 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1716 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001717
Ted Kremenek204c3c52014-02-22 00:02:03 +00001718 if (IncludeProtocols) {
1719 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001720 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001721 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1722 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001723 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001724 }
1725 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001726 for (auto *Prop : CATDecl->properties()) {
1727 if (CollectClassPropsOnly && !Prop->isClassProperty())
1728 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001729 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1730 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001731 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001732 if (IncludeProtocols) {
1733 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001734 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001735 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1736 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001737 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001738 }
1739 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001740 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001741 if (CollectClassPropsOnly && !Prop->isClassProperty())
1742 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001743 ObjCPropertyDecl *PropertyFromSuper =
1744 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1745 Prop->isClassProperty())];
Fangrui Song6907ce22018-07-30 19:24:48 +00001746 // Exclude property for protocols which conform to class's super-class,
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001747 // as super-class has to implement the property.
Fangrui Song6907ce22018-07-30 19:24:48 +00001748 if (!PropertyFromSuper ||
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001749 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001750 ObjCPropertyDecl *&PropEntry =
1751 PropMap[std::make_pair(Prop->getIdentifier(),
1752 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001753 if (!PropEntry)
1754 PropEntry = Prop;
1755 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001756 }
Manman Ren16a7d632016-04-12 23:01:55 +00001757 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001758 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001759 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1760 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001761 }
1762}
1763
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001764/// CollectSuperClassPropertyImplementations - This routine collects list of
1765/// properties to be implemented in super class(s) and also coming from their
1766/// conforming protocols.
1767static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001768 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001769 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001770 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001771 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001772 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001773 SDecl = SDecl->getSuperClass();
1774 }
1775 }
1776}
1777
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001778/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1779/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1780/// declared in class 'IFace'.
1781bool
1782Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1783 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1784 if (!IV->getSynthesize())
1785 return false;
1786 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1787 Method->isInstanceMethod());
1788 if (!IMD || !IMD->isPropertyAccessor())
1789 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001790
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001791 // look up a property declaration whose one of its accessors is implemented
1792 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001793 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001794 if ((Property->getGetterName() == IMD->getSelector() ||
1795 Property->getSetterName() == IMD->getSelector()) &&
1796 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001797 return true;
1798 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001799 // Also look up property declaration in class extension whose one of its
1800 // accessors is implemented by this method.
1801 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001802 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001803 if ((Property->getGetterName() == IMD->getSelector() ||
1804 Property->getSetterName() == IMD->getSelector()) &&
1805 (Property->getPropertyIvarDecl() == IV))
1806 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001807 return false;
1808}
1809
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001810static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1811 ObjCPropertyDecl *Prop) {
1812 bool SuperClassImplementsGetter = false;
1813 bool SuperClassImplementsSetter = false;
1814 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1815 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001816
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001817 while (IDecl->getSuperClass()) {
1818 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1819 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1820 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001821
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001822 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1823 SuperClassImplementsSetter = true;
1824 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1825 return true;
1826 IDecl = IDecl->getSuperClass();
1827 }
1828 return false;
1829}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001830
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001831/// Default synthesizes all properties which must be synthesized
James Dennett2a4d13c2012-06-15 07:13:21 +00001832/// in class's \@implementation.
Alex Lorenz6c9af502017-07-03 10:12:24 +00001833void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl,
1834 ObjCInterfaceDecl *IDecl,
1835 SourceLocation AtEnd) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001836 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001837 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1838 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001839 if (PropMap.empty())
1840 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001841 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001842 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001843
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001844 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1845 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001846 // Is there a matching property synthesize/dynamic?
1847 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001848 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001849 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1850 continue;
1851 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001852 if (IMPDecl->FindPropertyImplDecl(
1853 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001854 continue;
1855 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1856 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1857 continue;
1858 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1859 continue;
1860 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001861 if (ObjCPropertyImplDecl *PID =
1862 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001863 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1864 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001865 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001866 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001867 continue;
1868 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001869 ObjCPropertyDecl *PropInSuperClass =
1870 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1871 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001872 if (ObjCProtocolDecl *Proto =
1873 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001874 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001875 // Suppress the warning if class's superclass implements property's
1876 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001877 // Or, if property is going to be implemented in its super class.
1878 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001879 Diag(IMPDecl->getLocation(),
1880 diag::warn_auto_synthesizing_protocol_property)
1881 << Prop << Proto;
1882 Diag(Prop->getLocation(), diag::note_property_declare);
Alex Lorenz6c9af502017-07-03 10:12:24 +00001883 std::string FixIt =
1884 (Twine("@synthesize ") + Prop->getName() + ";\n\n").str();
1885 Diag(AtEnd, diag::note_add_synthesize_directive)
1886 << FixItHint::CreateInsertion(AtEnd, FixIt);
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001887 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001888 continue;
1889 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001890 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001891 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001892 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1893 (PropInSuperClass->getPropertyAttributes() &
1894 ObjCPropertyDecl::OBJC_PR_readonly) &&
1895 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1896 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1897 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1898 << Prop->getIdentifier();
1899 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1900 }
1901 else {
1902 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1903 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001904 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001905 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1906 }
1907 continue;
1908 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001909 // We use invalid SourceLocations for the synthesized ivars since they
1910 // aren't really synthesized at a particular location; they just exist.
1911 // Saying that they are located at the @implementation isn't really going
1912 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001913 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1914 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1915 true,
1916 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001917 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001918 Prop->getLocation(), Prop->getQueryKind()));
Alex Lorenz1e23dd62017-08-15 12:40:01 +00001919 if (PIDecl && !Prop->isUnavailable()) {
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001920 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001921 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001922 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001923 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001924}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001925
Alex Lorenz6c9af502017-07-03 10:12:24 +00001926void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D,
1927 SourceLocation AtEnd) {
John McCall5fb5df92012-06-20 06:18:46 +00001928 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001929 return;
1930 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1931 if (!IC)
1932 return;
1933 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001934 if (!IDecl->isObjCRequiresPropertyDefs())
Alex Lorenz6c9af502017-07-03 10:12:24 +00001935 DefaultSynthesizeProperties(S, IC, IDecl, AtEnd);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001936}
1937
Manman Ren08ce7342016-05-18 18:12:34 +00001938static void DiagnoseUnimplementedAccessor(
1939 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
1940 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
1941 ObjCPropertyDecl *Prop,
1942 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
1943 // Check to see if we have a corresponding selector in SMap and with the
1944 // right method type.
Fangrui Song75e74e02019-03-31 08:48:19 +00001945 auto I = llvm::find_if(SMap, [&](const ObjCMethodDecl *x) {
1946 return x->getSelector() == Method &&
1947 x->isClassMethod() == Prop->isClassProperty();
1948 });
Ted Kremenek7e812952014-02-21 19:41:30 +00001949 // When reporting on missing property setter/getter implementation in
1950 // categories, do not report when they are declared in primary class,
1951 // class's protocol, or one of it super classes. This is because,
1952 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00001953 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001954 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001955 !PrimaryClass->lookupPropertyAccessor(Method, C,
1956 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00001957 unsigned diag =
1958 isa<ObjCCategoryDecl>(CDecl)
1959 ? (Prop->isClassProperty()
1960 ? diag::warn_impl_required_in_category_for_class_property
1961 : diag::warn_setter_getter_impl_required_in_category)
1962 : (Prop->isClassProperty()
1963 ? diag::warn_impl_required_for_class_property
1964 : diag::warn_setter_getter_impl_required);
1965 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
1966 S.Diag(Prop->getLocation(), diag::note_property_declare);
1967 if (S.LangOpts.ObjCDefaultSynthProperties &&
1968 S.LangOpts.ObjCRuntime.isNonFragile())
1969 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1970 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1971 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1972 }
Ted Kremenek7e812952014-02-21 19:41:30 +00001973}
1974
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001975void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001976 ObjCContainerDecl *CDecl,
1977 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001978 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001979 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1980
Manman Ren16a7d632016-04-12 23:01:55 +00001981 // Since we don't synthesize class properties, we should emit diagnose even
1982 // if SynthesizeProperties is true.
1983 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1984 // Gather properties which need not be implemented in this class
1985 // or category.
1986 if (!IDecl)
1987 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1988 // For categories, no need to implement properties declared in
1989 // its primary class (and its super classes) if property is
1990 // declared in one of those containers.
1991 if ((IDecl = C->getClassInterface())) {
1992 ObjCInterfaceDecl::PropertyDeclOrder PO;
1993 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001994 }
Manman Ren16a7d632016-04-12 23:01:55 +00001995 }
1996 if (IDecl)
1997 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fangrui Song6907ce22018-07-30 19:24:48 +00001998
Manman Ren16a7d632016-04-12 23:01:55 +00001999 // When SynthesizeProperties is true, we only check class properties.
2000 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
2001 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002002
Ted Kremenek38882022014-02-21 19:41:39 +00002003 // Scan the @interface to see if any of the protocols it adopts
2004 // require an explicit implementation, via attribute
2005 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00002006 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00002007 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002008
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002009 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00002010 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
2011 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00002012 // Lazily construct a set of all the properties in the @interface
2013 // of the class, without looking at the superclass. We cannot
2014 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00002015 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00002016 // as scans the adopted protocols. This work only triggers for protocols
2017 // with the attribute, which is very rare, and only occurs when
2018 // analyzing the @implementation.
2019 if (!LazyMap) {
2020 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
2021 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
2022 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00002023 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00002024 /* IncludeProtocols */ false);
2025 }
Ted Kremenek38882022014-02-21 19:41:39 +00002026 // Add the properties of 'PDecl' to the list of properties that
2027 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00002028 for (auto *PropDecl : PDecl->properties()) {
2029 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
2030 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00002031 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00002032 PropMap[std::make_pair(PropDecl->getIdentifier(),
2033 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00002034 }
2035 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00002036 }
Ted Kremenek38882022014-02-21 19:41:39 +00002037
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002038 if (PropMap.empty())
2039 return;
2040
2041 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002042 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00002043 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002044
Manman Ren08ce7342016-05-18 18:12:34 +00002045 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002046 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002047 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002048 InsMap.insert(I);
Fangrui Song6907ce22018-07-30 19:24:48 +00002049
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002050 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00002051 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002052 if (C && !C->IsClassExtension())
2053 if ((PrimaryClass = C->getClassInterface()))
2054 // Report unimplemented properties in the category as well.
2055 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
2056 // When reporting on missing setter/getters, do not report when
2057 // setter/getter is implemented in category's primary class
2058 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00002059 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00002060 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00002061 }
2062
Anna Zaks673d76b2012-10-18 19:17:53 +00002063 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002064 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
2065 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00002066 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002067 if (Prop->isInvalidDecl() ||
2068 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00002069 PropImplMap.count(Prop) ||
2070 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002071 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00002072
2073 // Diagnose unimplemented getters and setters.
2074 DiagnoseUnimplementedAccessor(*this,
2075 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
2076 if (!Prop->isReadOnly())
2077 DiagnoseUnimplementedAccessor(*this,
2078 PrimaryClass, Prop->getSetterName(),
2079 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002080 }
2081}
2082
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002083void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002084 for (const auto *propertyImpl : impDecl->property_impls()) {
2085 const auto *property = propertyImpl->getPropertyDecl();
2086
2087 // Warn about null_resettable properties with synthesized setters,
2088 // because the setter won't properly handle nil.
2089 if (propertyImpl->getPropertyImplementation()
2090 == ObjCPropertyImplDecl::Synthesize &&
2091 (property->getPropertyAttributes() &
2092 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
2093 property->getGetterMethodDecl() &&
2094 property->getSetterMethodDecl()) {
2095 auto *getterMethod = property->getGetterMethodDecl();
2096 auto *setterMethod = property->getSetterMethodDecl();
2097 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
2098 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
2099 SourceLocation loc = propertyImpl->getLocation();
2100 if (loc.isInvalid())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002101 loc = impDecl->getBeginLoc();
Douglas Gregor849ebc22015-06-19 18:14:46 +00002102
2103 Diag(loc, diag::warn_null_resettable_setter)
2104 << setterMethod->getSelector() << property->getDeclName();
2105 }
2106 }
2107 }
2108}
2109
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002110void
2111Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002112 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002113 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00002114 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002115 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002116 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00002117 for (auto *Prop : IDecl->properties())
2118 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002119 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00002120 for (auto *Prop : Ext->properties())
2121 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Fangrui Song6907ce22018-07-30 19:24:48 +00002122
Manman Renefe1bac2016-01-27 20:00:32 +00002123 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
2124 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002125 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00002126 ObjCMethodDecl *GetterMethod = nullptr;
2127 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002128 bool LookedUpGetterSetter = false;
2129
Bill Wendling44426052012-12-20 19:22:21 +00002130 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00002131 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002132
John McCall43192862011-09-13 18:31:23 +00002133 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
2134 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00002135 GetterMethod = Property->isClassProperty() ?
2136 IMPDecl->getClassMethod(Property->getGetterName()) :
2137 IMPDecl->getInstanceMethod(Property->getGetterName());
2138 SetterMethod = Property->isClassProperty() ?
2139 IMPDecl->getClassMethod(Property->getSetterName()) :
2140 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002141 LookedUpGetterSetter = true;
2142 if (GetterMethod) {
2143 Diag(GetterMethod->getLocation(),
2144 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002145 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002146 Diag(Property->getLocation(), diag::note_property_declare);
2147 }
2148 if (SetterMethod) {
2149 Diag(SetterMethod->getLocation(),
2150 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00002151 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002152 Diag(Property->getLocation(), diag::note_property_declare);
2153 }
2154 }
2155
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002156 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00002157 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
2158 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002159 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002160 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2161 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002162 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2163 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002164 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00002165 GetterMethod = Property->isClassProperty() ?
2166 IMPDecl->getClassMethod(Property->getGetterName()) :
2167 IMPDecl->getInstanceMethod(Property->getGetterName());
2168 SetterMethod = Property->isClassProperty() ?
2169 IMPDecl->getClassMethod(Property->getSetterName()) :
2170 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002171 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002172 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
2173 SourceLocation MethodLoc =
2174 (GetterMethod ? GetterMethod->getLocation()
2175 : SetterMethod->getLocation());
2176 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002177 << Property->getIdentifier() << (GetterMethod != nullptr)
2178 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002179 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002180 if (Property->getLParenLoc().isValid() &&
2181 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002182 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002183 SourceLocation AfterLParen =
2184 getLocForEndOfToken(Property->getLParenLoc());
2185 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2186 : "nonatomic";
2187 Diag(Property->getLocation(),
2188 diag::note_atomic_property_fixup_suggest)
2189 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2190 } else if (Property->getLParenLoc().isInvalid()) {
2191 //@property id etc.
Fangrui Song6907ce22018-07-30 19:24:48 +00002192 SourceLocation startLoc =
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002193 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2194 Diag(Property->getLocation(),
2195 diag::note_atomic_property_fixup_suggest)
2196 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002197 }
2198 else
2199 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002200 Diag(Property->getLocation(), diag::note_property_declare);
2201 }
2202 }
2203 }
2204}
2205
John McCall31168b02011-06-15 23:02:42 +00002206void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002207 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002208 return;
2209
Aaron Ballmand85eff42014-03-14 15:02:45 +00002210 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002211 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002212 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002213 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002214 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002215 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2216 if (!method)
2217 continue;
2218 ObjCMethodFamily family = method->getMethodFamily();
2219 if (family == OMF_alloc || family == OMF_copy ||
2220 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002221 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002222 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002223 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002224 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002225
2226 // Look for a getter explicitly declared alongside the property.
2227 // If we find one, use its location for the note.
2228 SourceLocation noteLoc = PD->getLocation();
2229 SourceLocation fixItLoc;
2230 for (auto *getterRedecl : method->redecls()) {
2231 if (getterRedecl->isImplicit())
2232 continue;
2233 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2234 continue;
2235 noteLoc = getterRedecl->getLocation();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002236 fixItLoc = getterRedecl->getEndLoc();
Jordan Rosea34d04d2015-01-16 23:04:31 +00002237 }
2238
2239 Preprocessor &PP = getPreprocessor();
2240 TokenValue tokens[] = {
2241 tok::kw___attribute, tok::l_paren, tok::l_paren,
2242 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2243 PP.getIdentifierInfo("none"), tok::r_paren,
2244 tok::r_paren, tok::r_paren
2245 };
2246 StringRef spelling = "__attribute__((objc_method_family(none)))";
2247 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2248 if (!macroName.empty())
2249 spelling = macroName;
2250
2251 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2252 << method->getDeclName() << spelling;
2253 if (fixItLoc.isValid()) {
2254 SmallString<64> fixItText(" ");
2255 fixItText += spelling;
2256 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2257 }
John McCall31168b02011-06-15 23:02:42 +00002258 }
2259 }
2260 }
2261}
2262
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002263void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002264 const ObjCImplementationDecl *ImplD,
2265 const ObjCInterfaceDecl *IFD) {
2266 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002267 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2268 if (!SuperD)
2269 return;
2270
2271 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002272 for (const auto *I : ImplD->instance_methods())
2273 if (I->getMethodFamily() == OMF_init)
2274 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002275
2276 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2277 SuperD->getDesignatedInitializers(DesignatedInits);
2278 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2279 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2280 const ObjCMethodDecl *MD = *I;
2281 if (!InitSelSet.count(MD->getSelector())) {
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002282 // Don't emit a diagnostic if the overriding method in the subclass is
2283 // marked as unavailable.
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002284 bool Ignore = false;
2285 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2286 Ignore = IMD->isUnavailable();
Akira Hatanaka78be8b62019-03-01 06:43:20 +00002287 } else {
2288 // Check the methods declared in the class extensions too.
2289 for (auto *Ext : IFD->visible_extensions())
2290 if (auto *IMD = Ext->getInstanceMethod(MD->getSelector())) {
2291 Ignore = IMD->isUnavailable();
2292 break;
2293 }
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002294 }
2295 if (!Ignore) {
2296 Diag(ImplD->getLocation(),
2297 diag::warn_objc_implementation_missing_designated_init_override)
2298 << MD->getSelector();
2299 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2300 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002301 }
2302 }
2303}
2304
John McCallad31b5f2010-11-10 07:01:40 +00002305/// AddPropertyAttrs - Propagates attributes from a property to the
2306/// implicitly-declared getter or setter for that property.
2307static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2308 ObjCPropertyDecl *Property) {
2309 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002310 for (const auto *A : Property->attrs()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002311 if (isa<DeprecatedAttr>(A) ||
2312 isa<UnavailableAttr>(A) ||
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002313 isa<AvailabilityAttr>(A))
2314 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002315 }
John McCallad31b5f2010-11-10 07:01:40 +00002316}
2317
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002318/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2319/// have the property type and issue diagnostics if they don't.
2320/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002321/// appropriate lookup tables.
2322void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002323 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002324 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002325 if (CD->isInvalidDecl())
2326 return;
2327
Manman Rend36f7d52016-01-27 20:10:32 +00002328 bool IsClassProperty = property->isClassProperty();
2329 GetterMethod = IsClassProperty ?
2330 CD->getClassMethod(property->getGetterName()) :
2331 CD->getInstanceMethod(property->getGetterName());
2332
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002333 // if setter or getter is not found in class extension, it might be
2334 // in the primary class.
2335 if (!GetterMethod)
2336 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2337 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002338 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2339 getClassMethod(property->getGetterName()) :
2340 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002341 getInstanceMethod(property->getGetterName());
Fangrui Song6907ce22018-07-30 19:24:48 +00002342
Manman Rend36f7d52016-01-27 20:10:32 +00002343 SetterMethod = IsClassProperty ?
2344 CD->getClassMethod(property->getSetterName()) :
2345 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002346 if (!SetterMethod)
2347 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2348 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002349 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2350 getClassMethod(property->getSetterName()) :
2351 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002352 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002353 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2354 property->getLocation());
2355
Alex Lorenz535571a2017-03-30 13:33:51 +00002356 if (!property->isReadOnly() && SetterMethod) {
2357 if (Context.getCanonicalType(SetterMethod->getReturnType()) !=
2358 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002359 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2360 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002361 !Context.hasSameUnqualifiedType(
Fangrui Song6907ce22018-07-30 19:24:48 +00002362 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002363 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002364 Diag(property->getLocation(),
2365 diag::warn_accessor_property_type_mismatch)
2366 << property->getDeclName()
2367 << SetterMethod->getSelector();
2368 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2369 }
2370 }
2371
2372 // Synthesize getter/setter methods if none exist.
2373 // Find the default getter and if one not found, add one.
2374 // FIXME: The synthesized property we set here is misleading. We almost always
2375 // synthesize these methods unless the user explicitly provided prototypes
2376 // (which is odd, but allowed). Sema should be typechecking that the
2377 // declarations jive in that situation (which it is not currently).
2378 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002379 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002380 // Declare a getter method and add it to the list of methods
2381 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002382 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002383
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002384 // The getter returns the declared property type with all qualifiers
2385 // removed.
2386 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2387
Douglas Gregor849ebc22015-06-19 18:14:46 +00002388 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002389 if (property->getPropertyAttributes() &
2390 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2391 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002392 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002393 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002394 resultTy = Context.getAttributedType(attr::TypeNonNull,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002395 modifiedTy, modifiedTy);
2396 }
2397 }
2398
Ted Kremenek2f075632010-09-21 20:52:59 +00002399 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2400 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002401 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002402 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002403 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002404 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002405 (property->getPropertyImplementation() ==
2406 ObjCPropertyDecl::Optional) ?
2407 ObjCMethodDecl::Optional :
2408 ObjCMethodDecl::Required);
2409 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002410
2411 AddPropertyAttrs(*this, GetterMethod, property);
2412
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002413 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002414 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2415 Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002416
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002417 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2418 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002419 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fangrui Song6907ce22018-07-30 19:24:48 +00002420
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002421 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002422 GetterMethod->addAttr(
2423 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2424 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002425
2426 if (getLangOpts().ObjCAutoRefCount)
2427 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002428 } else
2429 // A user declared getter will be synthesize when @synthesize of
2430 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002431 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002432 property->setGetterMethodDecl(GetterMethod);
2433
2434 // Skip setter if property is read-only.
2435 if (!property->isReadOnly()) {
2436 // Find the default setter and if one not found, add one.
2437 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002438 // No instance/class method of same name as property setter name was
2439 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002440 // Declare a setter method and add it to the list of methods
2441 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002442 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002443
2444 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002445 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002446 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002447 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002448 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002449 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002450 /*isImplicitlyDeclared=*/true,
2451 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002452 (property->getPropertyImplementation() ==
2453 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002454 ObjCMethodDecl::Optional :
2455 ObjCMethodDecl::Required);
2456
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002457 // Remove all qualifiers from the setter's parameter type.
2458 QualType paramTy =
2459 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2460
Douglas Gregor849ebc22015-06-19 18:14:46 +00002461 // If the property is null_resettable, the setter accepts a
2462 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002463 if (property->getPropertyAttributes() &
2464 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2465 QualType modifiedTy = paramTy;
2466 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2467 if (*nullability == NullabilityKind::Unspecified)
Richard Smithe43e2b32018-08-20 21:47:29 +00002468 paramTy = Context.getAttributedType(attr::TypeNullable,
Douglas Gregor849ebc22015-06-19 18:14:46 +00002469 modifiedTy, modifiedTy);
2470 }
2471 }
2472
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002473 // Invent the arguments for the setter. We don't bother making a
2474 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002475 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2476 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002477 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002478 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002479 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002480 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002481 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002482 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002483
2484 AddPropertyAttrs(*this, SetterMethod, property);
2485
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002486 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002487 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002488 SetterMethod->addAttr(
2489 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2490 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002491 // It's possible for the user to have set a very odd custom
2492 // setter selector that causes it to have a method family.
2493 if (getLangOpts().ObjCAutoRefCount)
2494 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002495 } else
2496 // A user declared setter will be synthesize when @synthesize of
2497 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002498 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002499 property->setSetterMethodDecl(SetterMethod);
2500 }
2501 // Add any synthesized methods to the global pool. This allows us to
2502 // handle the following, which is supported by GCC (and part of the design).
2503 //
2504 // @interface Foo
2505 // @property double bar;
2506 // @end
2507 //
2508 // void thisIsUnfortunate() {
2509 // id foo;
2510 // double bar = [foo bar];
2511 // }
2512 //
Manman Rend36f7d52016-01-27 20:10:32 +00002513 if (!IsClassProperty) {
2514 if (GetterMethod)
2515 AddInstanceMethodToGlobalPool(GetterMethod);
2516 if (SetterMethod)
2517 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002518 } else {
2519 if (GetterMethod)
2520 AddFactoryMethodToGlobalPool(GetterMethod);
2521 if (SetterMethod)
2522 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002523 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002524
2525 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2526 if (!CurrentClass) {
2527 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2528 CurrentClass = Cat->getClassInterface();
2529 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2530 CurrentClass = Impl->getClassInterface();
2531 }
2532 if (GetterMethod)
2533 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2534 if (SetterMethod)
2535 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002536}
2537
John McCall48871652010-08-21 09:40:31 +00002538void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002539 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002540 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002541 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002542 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002543 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002544 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00002545
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002546 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2547 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2548 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2549 << "readonly" << "readwrite";
Fangrui Song6907ce22018-07-30 19:24:48 +00002550
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002551 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2552 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002553
2554 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002555 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002556 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2557 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002558 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002559 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002560 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2561 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2562 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002563 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002564 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002565 }
2566
John McCall52a503d2018-09-05 19:02:00 +00002567 // Check for assign on object types.
2568 if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2569 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
2570 PropertyTy->isObjCRetainableType() &&
2571 !PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2572 Diag(Loc, diag::warn_objc_property_assign_on_object);
2573 }
2574
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002575 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002576 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2577 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002578 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2579 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002580 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002581 }
Bill Wendling44426052012-12-20 19:22:21 +00002582 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002583 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2584 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002585 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002586 }
Bill Wendling44426052012-12-20 19:22:21 +00002587 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002588 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2589 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002590 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002591 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002592 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002593 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002594 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2595 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002596 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002597 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002598 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002599 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002600 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2601 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002602 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2603 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002604 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002605 }
Bill Wendling44426052012-12-20 19:22:21 +00002606 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002607 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2608 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002609 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002610 }
Bill Wendling44426052012-12-20 19:22:21 +00002611 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002612 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2613 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002614 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002615 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002616 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002617 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002618 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2619 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002620 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002621 }
Bill Wendling44426052012-12-20 19:22:21 +00002622 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2623 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002624 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2625 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002626 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002627 }
Bill Wendling44426052012-12-20 19:22:21 +00002628 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002629 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2630 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002631 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002632 }
Bill Wendling44426052012-12-20 19:22:21 +00002633 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002634 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2635 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002636 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002637 }
2638 }
Bill Wendling44426052012-12-20 19:22:21 +00002639 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2640 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002641 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2642 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002643 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002644 }
Bill Wendling44426052012-12-20 19:22:21 +00002645 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2646 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002647 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2648 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002649 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002650 }
2651
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002652 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002653 // 'weak' and 'nonnull' are mutually exclusive.
2654 if (auto nullability = PropertyTy->getNullability(Context)) {
2655 if (*nullability == NullabilityKind::NonNull)
2656 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2657 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002658 }
2659 }
2660
Bill Wendling44426052012-12-20 19:22:21 +00002661 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2662 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002663 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2664 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002665 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002666 }
2667
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002668 // Warn if user supplied no assignment attribute, property is
2669 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002670 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2671 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2672 // do nothing
2673 } else if (getLangOpts().ObjCAutoRefCount) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002674 // With arc, @property definitions should default to strong when
John McCallb61e14e2015-10-27 04:54:50 +00002675 // not specified.
2676 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2677 } else if (PropertyTy->isObjCObjectPointerType()) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002678 bool isAnyClassTy =
2679 (PropertyTy->isObjCClassType() ||
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002680 PropertyTy->isObjCQualifiedClassType());
2681 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2682 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002683 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002684 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002685 else if (propertyInPrimaryClass) {
Fangrui Song6907ce22018-07-30 19:24:48 +00002686 // Don't issue warning on property with no life time in class
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002687 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002688 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002689 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002690 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002691
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002692 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002693 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002694 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002695 }
John McCallb61e14e2015-10-27 04:54:50 +00002696 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002697
2698 // FIXME: Implement warning dependent on NSCopying being
2699 // implemented. See also:
2700 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2701 // (please trim this list while you are at it).
2702 }
2703
Bill Wendling44426052012-12-20 19:22:21 +00002704 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2705 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002706 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002707 && PropertyTy->isBlockPointerType())
2708 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002709 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2710 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2711 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002712 PropertyTy->isBlockPointerType())
2713 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fangrui Song6907ce22018-07-30 19:24:48 +00002714
Bill Wendling44426052012-12-20 19:22:21 +00002715 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2716 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002717 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002718}