blob: 3481b82679c2a2ca784c8acde1a35ffba733f5a6 [file] [log] [blame]
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
Jordan Rosea34d04d2015-01-16 23:04:31 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallb61e14e2015-10-27 04:54:50 +000064/// Check the internal consistency of a property declaration with
65/// an explicit ownership qualifier.
66static void checkPropertyDeclWithOwnership(Sema &S,
67 ObjCPropertyDecl *property) {
John McCall31168b02011-06-15 23:02:42 +000068 if (property->isInvalidDecl()) return;
69
70 ObjCPropertyDecl::PropertyAttributeKind propertyKind
71 = property->getPropertyAttributes();
72 Qualifiers::ObjCLifetime propertyLifetime
73 = property->getType().getObjCLifetime();
74
John McCallb61e14e2015-10-27 04:54:50 +000075 assert(propertyLifetime != Qualifiers::OCL_None);
John McCall31168b02011-06-15 23:02:42 +000076
John McCall43192862011-09-13 18:31:23 +000077 Qualifiers::ObjCLifetime expectedLifetime
78 = getImpliedARCOwnership(propertyKind, property->getType());
79 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000080 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000081 // attribute. That's okay, but restore reasonable invariants by
82 // setting the property attribute according to the lifetime
83 // qualifier.
84 ObjCPropertyDecl::PropertyAttributeKind attr;
85 if (propertyLifetime == Qualifiers::OCL_Strong) {
86 attr = ObjCPropertyDecl::OBJC_PR_strong;
87 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
88 attr = ObjCPropertyDecl::OBJC_PR_weak;
89 } else {
90 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
91 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
92 }
93 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000094 return;
95 }
96
97 if (propertyLifetime == expectedLifetime) return;
98
99 property->setInvalidDecl();
100 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000101 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000102 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000103 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000104 << propertyLifetime;
105}
106
Douglas Gregorb8982092013-01-21 19:42:21 +0000107/// \brief Check this Objective-C property against a property declared in the
108/// given protocol.
109static void
110CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
111 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000113 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000114 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000115 return;
116
117 // Look for a property with the same name.
118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
119 for (unsigned I = 0, N = R.size(); I != N; ++I) {
120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000122 return;
123 }
124 }
125
126 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000127 for (auto *P : Proto->protocols())
128 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129}
130
John McCallb61e14e2015-10-27 04:54:50 +0000131static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) {
132 // In GC mode, just look for the __weak qualifier.
133 if (S.getLangOpts().getGC() != LangOptions::NonGC) {
134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak;
135
136 // In ARC/MRC, look for an explicit ownership qualifier.
137 // For some reason, this only applies to __weak.
138 } else if (auto ownership = T.getObjCLifetime()) {
139 switch (ownership) {
140 case Qualifiers::OCL_Weak:
141 return ObjCDeclSpec::DQ_PR_weak;
142 case Qualifiers::OCL_Strong:
143 return ObjCDeclSpec::DQ_PR_strong;
144 case Qualifiers::OCL_ExplicitNone:
145 return ObjCDeclSpec::DQ_PR_unsafe_unretained;
146 case Qualifiers::OCL_Autoreleasing:
147 case Qualifiers::OCL_None:
148 return 0;
149 }
150 llvm_unreachable("bad qualifier");
151 }
152
153 return 0;
154}
155
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000156static const unsigned OwnershipMask =
157 (ObjCPropertyDecl::OBJC_PR_assign |
158 ObjCPropertyDecl::OBJC_PR_retain |
159 ObjCPropertyDecl::OBJC_PR_copy |
160 ObjCPropertyDecl::OBJC_PR_weak |
161 ObjCPropertyDecl::OBJC_PR_strong |
162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
163
John McCallb61e14e2015-10-27 04:54:50 +0000164static unsigned getOwnershipRule(unsigned attr) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000165 unsigned result = attr & OwnershipMask;
166
167 // From an ownership perspective, assign and unsafe_unretained are
168 // identical; make sure one also implies the other.
169 if (result & (ObjCPropertyDecl::OBJC_PR_assign |
170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) {
171 result |= ObjCPropertyDecl::OBJC_PR_assign |
172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
173 }
174
175 return result;
John McCallb61e14e2015-10-27 04:54:50 +0000176}
177
John McCall48871652010-08-21 09:40:31 +0000178Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000179 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000180 FieldDeclarator &FD,
181 ObjCDeclSpec &ODS,
182 Selector GetterSel,
183 Selector SetterSel,
Ted Kremenekcba58492010-09-23 21:18:05 +0000184 tok::ObjCKeywordKind MethodImplKind,
185 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000186 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
189 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000190 if (!getOwnershipRule(Attributes)) {
191 Attributes |= deducePropertyOwnershipFromType(*this, T);
192 }
Bill Wendling44426052012-12-20 19:22:21 +0000193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000194 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000196
Douglas Gregor90d34422013-01-21 19:05:22 +0000197 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000199 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000201 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000203 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000204 isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000205 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,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000215 GetterSel, SetterSel, isReadWrite,
Douglas Gregor90d34422013-01-21 19:05:22 +0000216 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000217 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000218 if (lexicalDC)
219 Res->setLexicalDeclContext(lexicalDC);
220 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000221
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000222 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000223 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000224 (isa<ObjCInterfaceDecl>(ClassDecl) ||
225 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000226
John McCallb61e14e2015-10-27 04:54:50 +0000227 // Check consistency if the type has explicit ownership qualification.
228 if (Res->getType().getObjCLifetime())
229 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000230
Douglas Gregorb8982092013-01-21 19:42:21 +0000231 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000232 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 // For a class, compare the property against a property in our superclass.
234 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000235 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
236 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000237 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000238 for (unsigned I = 0, N = R.size(); I != N; ++I) {
239 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000240 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000241 FoundInSuper = true;
242 break;
243 }
244 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000245 if (FoundInSuper)
246 break;
247 else
248 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000249 }
250
251 if (FoundInSuper) {
252 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000253 for (auto *P : CurrentInterfaceDecl->protocols()) {
254 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000255 }
256 } else {
257 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000258 for (auto *P : IFace->all_referenced_protocols()) {
259 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000260 }
261 }
262 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000263 // We don't check if class extension. Because properties in class extension
264 // are meant to override some of the attributes and checking has already done
265 // when property in class extension is constructed.
266 if (!Cat->IsClassExtension())
267 for (auto *P : Cat->protocols())
268 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000269 } else {
270 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000271 for (auto *P : Proto->protocols())
272 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000273 }
274
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000275 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000276 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000277}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000278
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000280makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_class)
307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000308
309 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
310}
311
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000312static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000313 SourceLocation LParenLoc, SourceLocation &Loc) {
314 if (LParenLoc.isMacroID())
315 return false;
316
317 SourceManager &SM = Context.getSourceManager();
318 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
319 // Try to load the file buffer.
320 bool invalidTemp = false;
321 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
322 if (invalidTemp)
323 return false;
324 const char *tokenBegin = file.data() + locInfo.second;
325
326 // Lex from the start of the given location.
327 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
328 Context.getLangOpts(),
329 file.begin(), tokenBegin, file.end());
330 Token Tok;
331 do {
332 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000333 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000334 Loc = Tok.getLocation();
335 return true;
336 }
337 } while (Tok.isNot(tok::r_paren));
338 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000339}
340
Douglas Gregor429183e2015-12-09 22:57:32 +0000341/// Check for a mismatch in the atomicity of the given properties.
342static void checkAtomicPropertyMismatch(Sema &S,
343 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000344 ObjCPropertyDecl *NewProperty,
345 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000346 // If the atomicity of both matches, we're done.
347 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000348 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
349 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000350 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000351 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
352 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000353 if (OldIsAtomic == NewIsAtomic) return;
354
355 // Determine whether the given property is readonly and implicitly
356 // atomic.
357 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
358 // Is it readonly?
359 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000360 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000361
362 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000363 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000364
365 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000366 if (Property->getPropertyAttributesAsWritten() &
367 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000368 return false;
369
370 return true;
371 };
372
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000373 // If we're allowed to propagate atomicity, and the new property did
374 // not specify atomicity at all, propagate.
375 const unsigned AtomicityMask =
376 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
377 if (PropagateAtomicity &&
378 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
379 unsigned Attrs = NewProperty->getPropertyAttributes();
380 Attrs = Attrs & ~AtomicityMask;
381 if (OldIsAtomic)
382 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
383 else
384 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
385
386 NewProperty->overwritePropertyAttributes(Attrs);
387 return;
388 }
389
Douglas Gregor429183e2015-12-09 22:57:32 +0000390 // One of the properties is atomic; if it's a readonly property, and
391 // 'atomic' wasn't explicitly specified, we're okay.
392 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
393 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
394 return;
395
396 // Diagnose the conflict.
397 const IdentifierInfo *OldContextName;
398 auto *OldDC = OldProperty->getDeclContext();
399 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
400 OldContextName = Category->getClassInterface()->getIdentifier();
401 else
402 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
403
404 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
405 << NewProperty->getDeclName() << "atomic"
406 << OldContextName;
407 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
408}
409
Douglas Gregor90d34422013-01-21 19:05:22 +0000410ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000411Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000412 SourceLocation AtLoc,
413 SourceLocation LParenLoc,
414 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000415 Selector GetterSel, Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000416 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000417 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000418 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000419 QualType T,
420 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000421 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000422 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000423 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000424 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000425 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000426 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
427
Ted Kremenek959e8302010-03-12 02:31:10 +0000428 // We need to look in the @interface to see if the @property was
429 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 if (!CCPrimary) {
431 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000432 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 }
434
Manman Ren5b786402016-01-28 18:49:28 +0000435 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
436 (Attributes & ObjCDeclSpec::DQ_PR_class);
437
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000438 // Find the property in the extended class's primary class or
439 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000440 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
441 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000442
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000443 // If we found a property in an extension, complain.
444 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
445 Diag(AtLoc, diag::err_duplicate_property);
446 Diag(PIDecl->getLocation(), diag::note_property_declare);
447 return nullptr;
448 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000449
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000450 // Check for consistency with the previous declaration, if there is one.
451 if (PIDecl) {
452 // A readonly property declared in the primary class can be refined
453 // by adding a readwrite property within an extension.
454 // Anything else is an error.
455 if (!(PIDecl->isReadOnly() && isReadWrite)) {
456 // Tailor the diagnostics for the common case where a readwrite
457 // property is declared both in the @interface and the continuation.
458 // This is a common error where the user often intended the original
459 // declaration to be readonly.
460 unsigned diag =
461 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
462 (PIDecl->getPropertyAttributesAsWritten() &
463 ObjCPropertyDecl::OBJC_PR_readwrite)
464 ? diag::err_use_continuation_class_redeclaration_readwrite
465 : diag::err_use_continuation_class;
466 Diag(AtLoc, diag)
467 << CCPrimary->getDeclName();
468 Diag(PIDecl->getLocation(), diag::note_property_declare);
469 return nullptr;
470 }
471
472 // Check for consistency of getters.
473 if (PIDecl->getGetterName() != GetterSel) {
474 // If the getter was written explicitly, complain.
475 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
476 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
477 << PIDecl->getGetterName() << GetterSel;
478 Diag(PIDecl->getLocation(), diag::note_property_declare);
479 }
480
481 // Always adopt the getter from the original declaration.
482 GetterSel = PIDecl->getGetterName();
483 Attributes |= ObjCDeclSpec::DQ_PR_getter;
484 }
485
486 // Check consistency of ownership.
487 unsigned ExistingOwnership
488 = getOwnershipRule(PIDecl->getPropertyAttributes());
489 unsigned NewOwnership = getOwnershipRule(Attributes);
490 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
491 // If the ownership was written explicitly, complain.
492 if (getOwnershipRule(AttributesAsWritten)) {
493 Diag(AtLoc, diag::warn_property_attr_mismatch);
494 Diag(PIDecl->getLocation(), diag::note_property_declare);
495 }
496
497 // Take the ownership from the original property.
498 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
499 }
500
501 // If the redeclaration is 'weak' but the original property is not,
502 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
503 !(PIDecl->getPropertyAttributesAsWritten()
504 & ObjCPropertyDecl::OBJC_PR_weak) &&
505 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
506 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
507 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
509 }
510 }
511
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000512 // Create a new ObjCPropertyDecl with the DeclContext being
513 // the class extension.
514 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
515 FD, GetterSel, SetterSel,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000516 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000517 Attributes, AttributesAsWritten,
518 T, TSI, MethodImplKind, DC);
519
520 // If there was no declaration of a property with the same name in
521 // the primary class, we're done.
522 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000523 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000524 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000525 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000526
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000527 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
528 bool IncompatibleObjC = false;
529 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000530 // Relax the strict type matching for property type in continuation class.
531 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000532 // as it narrows the object type in its primary class property. Note that
533 // this conversion is safe only because the wider type is for a 'readonly'
534 // property in primary class and 'narrowed' type for a 'readwrite' property
535 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000536 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
537 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
538 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
539 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
540 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000541 ConvertedType, IncompatibleObjC))
542 || IncompatibleObjC) {
543 Diag(AtLoc,
544 diag::err_type_mismatch_continuation_class) << PDecl->getType();
545 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000546 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000547 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000548 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000549
550 // Check that atomicity of property in class extension matches the previous
551 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000552 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000553
Douglas Gregore17765e2015-11-03 17:02:34 +0000554 // Make sure getter/setter are appropriately synthesized.
555 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000556 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000557}
558
559ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
560 ObjCContainerDecl *CDecl,
561 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000562 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000563 FieldDeclarator &FD,
564 Selector GetterSel,
565 Selector SetterSel,
Ted Kremenek959e8302010-03-12 02:31:10 +0000566 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000567 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000568 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000569 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000570 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000571 tok::ObjCKeywordKind MethodImplKind,
572 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000574
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000575 // Property defaults to 'assign' if it is readwrite, unless this is ARC
576 // and the type is retainable.
577 bool isAssign;
578 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
579 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
580 isAssign = true;
581 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
582 isAssign = false;
583 } else {
584 isAssign = (!getLangOpts().ObjCAutoRefCount ||
585 !T->isObjCRetainableType());
586 }
587
588 // Issue a warning if property is 'assign' as default and its
589 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000590 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000591 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000592 if (const ObjCObjectPointerType *ObjPtrTy =
593 T->getAs<ObjCObjectPointerType>()) {
594 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
595 if (IDecl)
596 if (ObjCProtocolDecl* PNSCopying =
597 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
598 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
599 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000600 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000601 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000602
603 if (T->isObjCObjectType()) {
604 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000605 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000606 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
607 << FixItHint::CreateInsertion(StarLoc, "*");
608 T = Context.getObjCObjectPointerType(T);
609 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
610 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
611 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000612
Ted Kremenek959e8302010-03-12 02:31:10 +0000613 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000614 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
615 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000616 PropertyId, AtLoc,
617 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000618
Manman Ren5b786402016-01-28 18:49:28 +0000619 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
620 (Attributes & ObjCDeclSpec::DQ_PR_class);
621 // Class property and instance property can have the same name.
622 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
623 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000624 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000625 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000626 PDecl->setInvalidDecl();
627 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000628 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000629 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000630 if (lexicalDC)
631 PDecl->setLexicalDeclContext(lexicalDC);
632 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000633
634 if (T->isArrayType() || T->isFunctionType()) {
635 Diag(AtLoc, diag::err_property_type) << T;
636 PDecl->setInvalidDecl();
637 }
638
639 ProcessDeclAttributes(S, PDecl, FD.D);
640
641 // Regardless of setter/getter attribute, we save the default getter/setter
642 // selector names in anticipation of declaration of setter/getter methods.
643 PDecl->setGetterName(GetterSel);
644 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000645 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000646 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000647
Bill Wendling44426052012-12-20 19:22:21 +0000648 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000649 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
650
Bill Wendling44426052012-12-20 19:22:21 +0000651 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000652 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
653
Bill Wendling44426052012-12-20 19:22:21 +0000654 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000655 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
656
657 if (isReadWrite)
658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
659
Bill Wendling44426052012-12-20 19:22:21 +0000660 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
662
Bill Wendling44426052012-12-20 19:22:21 +0000663 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
665
Bill Wendling44426052012-12-20 19:22:21 +0000666 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
668
Bill Wendling44426052012-12-20 19:22:21 +0000669 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
671
Bill Wendling44426052012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
674
Ted Kremenekac597f32010-03-12 00:46:40 +0000675 if (isAssign)
676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
677
John McCall43192862011-09-13 18:31:23 +0000678 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000679 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000680 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000681 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000683
John McCall31168b02011-06-15 23:02:42 +0000684 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000685 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000686 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
687 if (isAssign)
688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
689
Ted Kremenekac597f32010-03-12 00:46:40 +0000690 if (MethodImplKind == tok::objc_required)
691 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
692 else if (MethodImplKind == tok::objc_optional)
693 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000694
Douglas Gregor813a0662015-06-19 18:14:38 +0000695 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
697
Douglas Gregor849ebc22015-06-19 18:14:46 +0000698 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
699 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
700
Manman Ren387ff7f2016-01-26 18:52:43 +0000701 if (Attributes & ObjCDeclSpec::DQ_PR_class)
702 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
703
Ted Kremenek959e8302010-03-12 02:31:10 +0000704 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000705}
706
John McCall31168b02011-06-15 23:02:42 +0000707static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
708 ObjCPropertyDecl *property,
709 ObjCIvarDecl *ivar) {
710 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
711
John McCall31168b02011-06-15 23:02:42 +0000712 QualType ivarType = ivar->getType();
713 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000714
John McCall43192862011-09-13 18:31:23 +0000715 // The lifetime implied by the property's attributes.
716 Qualifiers::ObjCLifetime propertyLifetime =
717 getImpliedARCOwnership(property->getPropertyAttributes(),
718 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000719
John McCall43192862011-09-13 18:31:23 +0000720 // We're fine if they match.
721 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall460ce582015-10-22 18:38:17 +0000723 // None isn't a valid lifetime for an object ivar in ARC, and
724 // __autoreleasing is never valid; don't diagnose twice.
725 if ((ivarLifetime == Qualifiers::OCL_None &&
726 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000727 ivarLifetime == Qualifiers::OCL_Autoreleasing)
728 return;
John McCall31168b02011-06-15 23:02:42 +0000729
John McCalld8561f02012-08-20 23:36:59 +0000730 // If the ivar is private, and it's implicitly __unsafe_unretained
731 // becaues of its type, then pretend it was actually implicitly
732 // __strong. This is only sound because we're processing the
733 // property implementation before parsing any method bodies.
734 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
735 propertyLifetime == Qualifiers::OCL_Strong &&
736 ivar->getAccessControl() == ObjCIvarDecl::Private) {
737 SplitQualType split = ivarType.split();
738 if (split.Quals.hasObjCLifetime()) {
739 assert(ivarType->isObjCARCImplicitlyUnretainedType());
740 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
741 ivarType = S.Context.getQualifiedType(split);
742 ivar->setType(ivarType);
743 return;
744 }
745 }
746
John McCall43192862011-09-13 18:31:23 +0000747 switch (propertyLifetime) {
748 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000749 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000750 << property->getDeclName()
751 << ivar->getDeclName()
752 << ivarLifetime;
753 break;
John McCall31168b02011-06-15 23:02:42 +0000754
John McCall43192862011-09-13 18:31:23 +0000755 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000756 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000757 << property->getDeclName()
758 << ivar->getDeclName();
759 break;
John McCall31168b02011-06-15 23:02:42 +0000760
John McCall43192862011-09-13 18:31:23 +0000761 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000762 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000763 << property->getDeclName()
764 << ivar->getDeclName()
765 << ((property->getPropertyAttributesAsWritten()
766 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
767 break;
John McCall31168b02011-06-15 23:02:42 +0000768
John McCall43192862011-09-13 18:31:23 +0000769 case Qualifiers::OCL_Autoreleasing:
770 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000771
John McCall43192862011-09-13 18:31:23 +0000772 case Qualifiers::OCL_None:
773 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000774 return;
775 }
776
777 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000778 if (propertyImplLoc.isValid())
779 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000780}
781
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000782/// setImpliedPropertyAttributeForReadOnlyProperty -
783/// This routine evaludates life-time attributes for a 'readonly'
784/// property with no known lifetime of its own, using backing
785/// 'ivar's attribute, if any. If no backing 'ivar', property's
786/// life-time is assumed 'strong'.
787static void setImpliedPropertyAttributeForReadOnlyProperty(
788 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
789 Qualifiers::ObjCLifetime propertyLifetime =
790 getImpliedARCOwnership(property->getPropertyAttributes(),
791 property->getType());
792 if (propertyLifetime != Qualifiers::OCL_None)
793 return;
794
795 if (!ivar) {
796 // if no backing ivar, make property 'strong'.
797 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
798 return;
799 }
800 // property assumes owenership of backing ivar.
801 QualType ivarType = ivar->getType();
802 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
803 if (ivarLifetime == Qualifiers::OCL_Strong)
804 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
805 else if (ivarLifetime == Qualifiers::OCL_Weak)
806 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000807}
Ted Kremenekac597f32010-03-12 00:46:40 +0000808
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000809/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
810/// in inherited protocols with mismatched types. Since any of them can
811/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000812static void
813DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
814 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000815 ObjCPropertyDecl *Property) {
816 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000817 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
818 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000819 PDecl->collectInheritedProtocolProperties(Property, PropMap);
820 }
821 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
822 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000823 for (const auto *PI : SDecl->all_referenced_protocols()) {
824 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000825 PDecl->collectInheritedProtocolProperties(Property, PropMap);
826 }
827 SDecl = SDecl->getSuperClass();
828 }
829
830 if (PropMap.empty())
831 return;
832
833 QualType RHSType = S.Context.getCanonicalType(Property->getType());
834 bool FirsTime = true;
835 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
836 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
837 ObjCPropertyDecl *Prop = I->second;
838 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
839 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
840 bool IncompatibleObjC = false;
841 QualType ConvertedType;
842 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
843 || IncompatibleObjC) {
844 if (FirsTime) {
845 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
846 << Property->getType();
847 FirsTime = false;
848 }
849 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
850 << Prop->getType();
851 }
852 }
853 }
854 if (!FirsTime && AtLoc.isValid())
855 S.Diag(AtLoc, diag::note_property_synthesize);
856}
857
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000858/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +0000859static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
860 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000861 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
862
863 // If this is a readwrite property in a class extension that refines
864 // a readonly property in the original class definition, check it as
865 // well.
866
867 // If it's a readonly property, we're not interested.
868 if (Prop->isReadOnly()) return false;
869
870 // Is it declared in an extension?
871 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
872 if (!Category || !Category->IsClassExtension()) return false;
873
874 // Find the corresponding property in the primary class definition.
875 auto OrigClass = Category->getClassInterface();
876 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
877 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
878 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
879 }
880
Douglas Gregor02535432015-12-18 00:52:31 +0000881 // Look through all of the protocols.
882 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +0000883 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
884 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +0000885 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
886 }
887
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000888 return false;
889}
890
Ted Kremenekac597f32010-03-12 00:46:40 +0000891/// ActOnPropertyImplDecl - This routine performs semantic checks and
892/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000893/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000894///
John McCall48871652010-08-21 09:40:31 +0000895Decl *Sema::ActOnPropertyImplDecl(Scope *S,
896 SourceLocation AtLoc,
897 SourceLocation PropertyLoc,
898 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000899 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000900 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +0000901 SourceLocation PropertyIvarLoc,
902 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000903 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000904 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000905 // Make sure we have a context for the property implementation declaration.
906 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +0000907 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000908 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000909 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000910 if (PropertyIvarLoc.isInvalid())
911 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000912 SourceLocation PropertyDiagLoc = PropertyLoc;
913 if (PropertyDiagLoc.isInvalid())
914 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000915 ObjCPropertyDecl *property = nullptr;
916 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000917 // Find the class or category class where this property must have
918 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000919 ObjCImplementationDecl *IC = nullptr;
920 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000921 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
922 IDecl = IC->getClassInterface();
923 // We always synthesize an interface for an implementation
924 // without an interface decl. So, IDecl is always non-zero.
925 assert(IDecl &&
926 "ActOnPropertyImplDecl - @implementation without @interface");
927
928 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +0000929 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +0000930 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +0000931 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000932 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000933 }
Manman Rendfef4062016-01-29 19:16:39 +0000934 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +0000935 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +0000936 return nullptr;
937 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000938 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000939 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
940 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000941 if (AtLoc.isValid())
942 Diag(AtLoc, diag::warn_implicit_atomic_property);
943 else
944 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
945 Diag(property->getLocation(), diag::note_property_declare);
946 }
947
Ted Kremenekac597f32010-03-12 00:46:40 +0000948 if (const ObjCCategoryDecl *CD =
949 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
950 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +0000951 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +0000952 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000953 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000954 }
955 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000956 if (Synthesize&&
957 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
958 property->hasAttr<IBOutletAttr>() &&
959 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000960 bool ReadWriteProperty = false;
961 // Search into the class extensions and see if 'readonly property is
962 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000963 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000964 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
965 if (!R.empty())
966 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
967 PIkind = ExtProp->getPropertyAttributesAsWritten();
968 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
969 ReadWriteProperty = true;
970 break;
971 }
972 }
973 }
974
975 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000976 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000977 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000978 SourceLocation readonlyLoc;
979 if (LocPropertyAttribute(Context, "readonly",
980 property->getLParenLoc(), readonlyLoc)) {
981 SourceLocation endLoc =
982 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
983 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
984 Diag(property->getLocation(),
985 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
986 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
987 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000988 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000989 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000990 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
991 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000992
Ted Kremenekac597f32010-03-12 00:46:40 +0000993 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
994 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +0000995 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000996 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000997 }
998 IDecl = CatImplClass->getClassInterface();
999 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001000 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001001 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001002 }
1003 ObjCCategoryDecl *Category =
1004 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1005
1006 // If category for this implementation not found, it is an error which
1007 // has already been reported eralier.
1008 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001009 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001010 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001011 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001012 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001013 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001014 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001015 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001016 }
1017 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001018 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001019 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001020 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001021 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001022 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001023 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001024 // Check that we have a valid, previously declared ivar for @synthesize
1025 if (Synthesize) {
1026 // @synthesize
1027 if (!PropertyIvar)
1028 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001029 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1030 ObjCInterfaceDecl *ClassDeclared;
1031 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1032 QualType PropType = property->getType();
1033 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001034
1035 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001036 diag::err_incomplete_synthesized_property,
1037 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001038 Diag(property->getLocation(), diag::note_property_declare);
1039 CompleteTypeErr = true;
1040 }
1041
David Blaikiebbafb8a2012-03-11 07:00:24 +00001042 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001043 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001044 ObjCPropertyDecl::OBJC_PR_readonly) &&
1045 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001046 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1047 }
1048
John McCall31168b02011-06-15 23:02:42 +00001049 ObjCPropertyDecl::PropertyAttributeKind kind
1050 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001051
John McCall460ce582015-10-22 18:38:17 +00001052 bool isARCWeak = false;
1053 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1054 // Add GC __weak to the ivar type if the property is weak.
1055 if (getLangOpts().getGC() != LangOptions::NonGC) {
1056 assert(!getLangOpts().ObjCAutoRefCount);
1057 if (PropertyIvarType.isObjCGCStrong()) {
1058 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1059 Diag(property->getLocation(), diag::note_property_declare);
1060 } else {
1061 PropertyIvarType =
1062 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1063 }
1064
1065 // Otherwise, check whether ARC __weak is enabled and works with
1066 // the property type.
John McCall43192862011-09-13 18:31:23 +00001067 } else {
John McCall460ce582015-10-22 18:38:17 +00001068 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001069 // Only complain here when synthesizing an ivar.
1070 if (!Ivar) {
1071 Diag(PropertyDiagLoc,
1072 getLangOpts().ObjCWeakRuntime
1073 ? diag::err_synthesizing_arc_weak_property_disabled
1074 : diag::err_synthesizing_arc_weak_property_no_runtime);
1075 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001076 }
John McCallb61e14e2015-10-27 04:54:50 +00001077 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001078 } else {
1079 isARCWeak = true;
1080 if (const ObjCObjectPointerType *ObjT =
1081 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1082 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1083 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1084 Diag(property->getLocation(),
1085 diag::err_arc_weak_unavailable_property)
1086 << PropertyIvarType;
1087 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1088 << ClassImpDecl->getName();
1089 }
1090 }
1091 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001092 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001093 }
John McCall460ce582015-10-22 18:38:17 +00001094
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001095 if (AtLoc.isInvalid()) {
1096 // Check when default synthesizing a property that there is
1097 // an ivar matching property name and issue warning; since this
1098 // is the most common case of not using an ivar used for backing
1099 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001100 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001101 ObjCIvarDecl *originalIvar =
1102 IDecl->lookupInstanceVariable(property->getIdentifier(),
1103 ClassDeclared);
1104 if (originalIvar) {
1105 Diag(PropertyDiagLoc,
1106 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001107 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001108 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001109 Diag(property->getLocation(), diag::note_property_declare);
1110 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001111 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001112 }
1113
1114 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001115 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001116 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001117 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001118 !PropertyIvarType.getObjCLifetime() &&
1119 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001120
John McCall43192862011-09-13 18:31:23 +00001121 // It's an error if we have to do this and the user didn't
1122 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001123 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001124 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001125 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001126 diag::err_arc_objc_property_default_assign_on_object);
1127 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001128 } else {
1129 Qualifiers::ObjCLifetime lifetime =
1130 getImpliedARCOwnership(kind, PropertyIvarType);
1131 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001132
John McCall31168b02011-06-15 23:02:42 +00001133 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001134 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001135 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1136 }
John McCall31168b02011-06-15 23:02:42 +00001137 }
1138
Abramo Bagnaradff19302011-03-08 08:55:46 +00001139 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001140 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001141 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001142 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001143 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001144 if (RequireNonAbstractType(PropertyIvarLoc,
1145 PropertyIvarType,
1146 diag::err_abstract_type_in_decl,
1147 AbstractSynthesizedIvarType)) {
1148 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001149 // An abstract type is as bad as an incomplete type.
1150 CompleteTypeErr = true;
1151 }
1152 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001153 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001154 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001155 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001156
John McCall5fb5df92012-06-20 06:18:46 +00001157 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001158 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001159 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001160 // Note! I deliberately want it to fall thru so, we have a
1161 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001162 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001163 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001164 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001165 << property->getDeclName() << Ivar->getDeclName()
1166 << ClassDeclared->getDeclName();
1167 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001168 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001169 // Note! I deliberately want it to fall thru so more errors are caught.
1170 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001171 property->setPropertyIvarDecl(Ivar);
1172
Ted Kremenekac597f32010-03-12 00:46:40 +00001173 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1174
1175 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001176 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001177 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001178 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001179 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001180 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001181 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001182 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001183 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001184 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1185 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001186 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001187 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001188 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001189 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001190 << property->getDeclName() << PropType
1191 << Ivar->getDeclName() << IvarType;
1192 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001193 // Note! I deliberately want it to fall thru so, we have a
1194 // a property implementation and to avoid future warnings.
1195 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001196 else {
1197 // FIXME! Rules for properties are somewhat different that those
1198 // for assignments. Use a new routine to consolidate all cases;
1199 // specifically for property redeclarations as well as for ivars.
1200 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1201 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1202 if (lhsType != rhsType &&
1203 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001204 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001205 << property->getDeclName() << PropType
1206 << Ivar->getDeclName() << IvarType;
1207 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1208 // Fall thru - see previous comment
1209 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001210 }
1211 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001212 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001213 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001214 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001215 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001216 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001217 // Fall thru - see previous comment
1218 }
John McCall31168b02011-06-15 23:02:42 +00001219 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001220 if ((property->getType()->isObjCObjectPointerType() ||
1221 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001222 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001223 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001224 << property->getDeclName() << Ivar->getDeclName();
1225 // Fall thru - see previous comment
1226 }
1227 }
John McCall460ce582015-10-22 18:38:17 +00001228 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1229 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001230 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001231 } else if (PropertyIvar)
1232 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001233 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001234
Ted Kremenekac597f32010-03-12 00:46:40 +00001235 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1236 ObjCPropertyImplDecl *PIDecl =
1237 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1238 property,
1239 (Synthesize ?
1240 ObjCPropertyImplDecl::Synthesize
1241 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001242 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001243
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001244 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001245 PIDecl->setInvalidDecl();
1246
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001247 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1248 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001249 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001250 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001251 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1252 // returned by the getter as it must conform to C++'s copy-return rules.
1253 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001254 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001255 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1256 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001257 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001258 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001259 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001260 Expr *LoadSelfExpr =
1261 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001262 CK_LValueToRValue, SelfExpr, nullptr,
1263 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001264 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001265 new (Context) ObjCIvarRefExpr(Ivar,
1266 Ivar->getUsageType(SelfDecl->getType()),
1267 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001268 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001269 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001270 ExprResult Res = PerformCopyInitialization(
1271 InitializedEntity::InitializeResult(PropertyDiagLoc,
1272 getterMethod->getReturnType(),
1273 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001274 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001275 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001276 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001277 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001278 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001279 PIDecl->setGetterCXXConstructor(ResExpr);
1280 }
1281 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001282 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1283 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1284 Diag(getterMethod->getLocation(),
1285 diag::warn_property_getter_owning_mismatch);
1286 Diag(property->getLocation(), diag::note_property_declare);
1287 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001288 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1289 switch (getterMethod->getMethodFamily()) {
1290 case OMF_retain:
1291 case OMF_retainCount:
1292 case OMF_release:
1293 case OMF_autorelease:
1294 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1295 << 1 << getterMethod->getSelector();
1296 break;
1297 default:
1298 break;
1299 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001300 }
1301 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1302 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001303 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1304 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001305 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001306 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001307 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1308 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001309 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001310 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001311 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001312 Expr *LoadSelfExpr =
1313 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001314 CK_LValueToRValue, SelfExpr, nullptr,
1315 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001316 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001317 new (Context) ObjCIvarRefExpr(Ivar,
1318 Ivar->getUsageType(SelfDecl->getType()),
1319 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001320 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001321 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001322 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1323 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001324 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001325 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1326 VK_LValue, PropertyDiagLoc);
1327 MarkDeclRefReferenced(rhs);
1328 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001329 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001330 if (property->getPropertyAttributes() &
1331 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001332 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001333 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001334 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1335 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001336 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001337 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001338 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001339 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001340 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001341 Diag(FuncDecl->getLocStart(),
1342 diag::note_callee_decl) << FuncDecl;
1343 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001344 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001345 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001346 }
1347 }
1348
Ted Kremenekac597f32010-03-12 00:46:40 +00001349 if (IC) {
1350 if (Synthesize)
1351 if (ObjCPropertyImplDecl *PPIDecl =
1352 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001353 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001354 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1355 << PropertyIvar;
1356 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1357 }
1358
1359 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001360 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001361 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001362 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001363 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001364 }
1365 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001366 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001367 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001368 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001369 // Diagnose if an ivar was lazily synthesdized due to a previous
1370 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001371 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001372 ObjCInterfaceDecl *ClassDeclared=nullptr;
1373 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001374 if (!Synthesize)
1375 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1376 else {
1377 if (PropertyIvar && PropertyIvar != PropertyId)
1378 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1379 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001380 // Issue diagnostics only if Ivar belongs to current class.
1381 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001382 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001383 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1384 << PropertyId;
1385 Ivar->setInvalidDecl();
1386 }
1387 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001388 } else {
1389 if (Synthesize)
1390 if (ObjCPropertyImplDecl *PPIDecl =
1391 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001392 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001393 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1394 << PropertyIvar;
1395 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1396 }
1397
1398 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001399 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001400 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001401 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001402 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001403 }
1404 CatImplClass->addPropertyImplementation(PIDecl);
1405 }
1406
John McCall48871652010-08-21 09:40:31 +00001407 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001408}
1409
1410//===----------------------------------------------------------------------===//
1411// Helper methods.
1412//===----------------------------------------------------------------------===//
1413
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001414/// DiagnosePropertyMismatch - Compares two properties for their
1415/// attributes and types and warns on a variety of inconsistencies.
1416///
1417void
1418Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1419 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001420 const IdentifierInfo *inheritedName,
1421 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001422 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001423 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001424 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001425 SuperProperty->getPropertyAttributes();
1426
1427 // We allow readonly properties without an explicit ownership
1428 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1429 // to be overridden by a property with any explicit ownership in the subclass.
1430 if (!OverridingProtocolProperty &&
1431 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1432 ;
1433 else {
1434 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1435 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1436 Diag(Property->getLocation(), diag::warn_readonly_property)
1437 << Property->getDeclName() << inheritedName;
1438 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1439 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001440 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001441 << Property->getDeclName() << "copy" << inheritedName;
1442 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1443 unsigned CAttrRetain =
1444 (CAttr &
1445 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1446 unsigned SAttrRetain =
1447 (SAttr &
1448 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1449 bool CStrong = (CAttrRetain != 0);
1450 bool SStrong = (SAttrRetain != 0);
1451 if (CStrong != SStrong)
1452 Diag(Property->getLocation(), diag::warn_property_attribute)
1453 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1454 }
John McCall31168b02011-06-15 23:02:42 +00001455 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001456
Douglas Gregor429183e2015-12-09 22:57:32 +00001457 // Check for nonatomic; note that nonatomic is effectively
1458 // meaningless for readonly properties, so don't diagnose if the
1459 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001460 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001461 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001462 Diag(Property->getLocation(), diag::warn_property_attribute)
1463 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001464 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1465 }
1466 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001467 Diag(Property->getLocation(), diag::warn_property_attribute)
1468 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001469 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1470 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001471
1472 QualType LHSType =
1473 Context.getCanonicalType(SuperProperty->getType());
1474 QualType RHSType =
1475 Context.getCanonicalType(Property->getType());
1476
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001477 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001478 // Do cases not handled in above.
1479 // FIXME. For future support of covariant property types, revisit this.
1480 bool IncompatibleObjC = false;
1481 QualType ConvertedType;
1482 if (!isObjCPointerConversion(RHSType, LHSType,
1483 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001484 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001485 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1486 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001487 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1488 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001489 }
1490}
1491
1492bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1493 ObjCMethodDecl *GetterMethod,
1494 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001495 if (!GetterMethod)
1496 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001497 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001498 QualType PropertyRValueType =
1499 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1500 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001501 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001502 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1503 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001504 if ((propertyObjCPtr =
1505 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001506 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1507 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001508 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001509 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001510 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001511 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001512 << GetterMethod->getSelector() << GetterType;
1513 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1514 return true;
1515 } else {
1516 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001517 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001518 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1519 if (lhsType != rhsType && lhsType->isArithmeticType())
1520 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001521 }
1522 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001523
1524 if (!compat) {
1525 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1526 << property->getDeclName()
1527 << GetterMethod->getSelector();
1528 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1529 return true;
1530 }
1531
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001532 return false;
1533}
1534
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001535/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001536/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001537static void
1538CollectImmediateProperties(ObjCContainerDecl *CDecl,
1539 ObjCContainerDecl::PropertyMap &PropMap,
1540 ObjCContainerDecl::PropertyMap &SuperPropMap,
1541 bool CollectClassPropsOnly = false,
1542 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001543 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001544 for (auto *Prop : IDecl->properties()) {
1545 if (CollectClassPropsOnly && !Prop->isClassProperty())
1546 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001547 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1548 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001549 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001550
1551 // Collect the properties from visible extensions.
1552 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001553 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1554 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001555
Ted Kremenek204c3c52014-02-22 00:02:03 +00001556 if (IncludeProtocols) {
1557 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001558 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001559 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1560 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001561 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001562 }
1563 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001564 for (auto *Prop : CATDecl->properties()) {
1565 if (CollectClassPropsOnly && !Prop->isClassProperty())
1566 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001567 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1568 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001569 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001570 if (IncludeProtocols) {
1571 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001572 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001573 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1574 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001575 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001576 }
1577 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001578 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001579 if (CollectClassPropsOnly && !Prop->isClassProperty())
1580 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001581 ObjCPropertyDecl *PropertyFromSuper =
1582 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1583 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001584 // Exclude property for protocols which conform to class's super-class,
1585 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001586 if (!PropertyFromSuper ||
1587 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001588 ObjCPropertyDecl *&PropEntry =
1589 PropMap[std::make_pair(Prop->getIdentifier(),
1590 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001591 if (!PropEntry)
1592 PropEntry = Prop;
1593 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001594 }
Manman Ren16a7d632016-04-12 23:01:55 +00001595 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001596 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001597 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1598 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001599 }
1600}
1601
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001602/// CollectSuperClassPropertyImplementations - This routine collects list of
1603/// properties to be implemented in super class(s) and also coming from their
1604/// conforming protocols.
1605static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001606 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001607 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001608 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001609 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001610 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001611 SDecl = SDecl->getSuperClass();
1612 }
1613 }
1614}
1615
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001616/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1617/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1618/// declared in class 'IFace'.
1619bool
1620Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1621 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1622 if (!IV->getSynthesize())
1623 return false;
1624 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1625 Method->isInstanceMethod());
1626 if (!IMD || !IMD->isPropertyAccessor())
1627 return false;
1628
1629 // look up a property declaration whose one of its accessors is implemented
1630 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001631 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001632 if ((Property->getGetterName() == IMD->getSelector() ||
1633 Property->getSetterName() == IMD->getSelector()) &&
1634 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001635 return true;
1636 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001637 // Also look up property declaration in class extension whose one of its
1638 // accessors is implemented by this method.
1639 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001640 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001641 if ((Property->getGetterName() == IMD->getSelector() ||
1642 Property->getSetterName() == IMD->getSelector()) &&
1643 (Property->getPropertyIvarDecl() == IV))
1644 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001645 return false;
1646}
1647
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001648static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1649 ObjCPropertyDecl *Prop) {
1650 bool SuperClassImplementsGetter = false;
1651 bool SuperClassImplementsSetter = false;
1652 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1653 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001654
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001655 while (IDecl->getSuperClass()) {
1656 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1657 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1658 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001659
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001660 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1661 SuperClassImplementsSetter = true;
1662 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1663 return true;
1664 IDecl = IDecl->getSuperClass();
1665 }
1666 return false;
1667}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001668
James Dennett2a4d13c2012-06-15 07:13:21 +00001669/// \brief Default synthesizes all properties which must be synthesized
1670/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001671void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1672 ObjCInterfaceDecl *IDecl) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001673 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001674 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1675 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001676 if (PropMap.empty())
1677 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001678 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001679 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1680
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001681 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1682 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001683 // Is there a matching property synthesize/dynamic?
1684 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001685 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001686 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1687 continue;
1688 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001689 if (IMPDecl->FindPropertyImplDecl(
1690 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001691 continue;
1692 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1693 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1694 continue;
1695 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1696 continue;
1697 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001698 if (ObjCPropertyImplDecl *PID =
1699 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001700 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1701 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001702 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001703 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001704 continue;
1705 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001706 ObjCPropertyDecl *PropInSuperClass =
1707 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1708 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001709 if (ObjCProtocolDecl *Proto =
1710 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001711 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001712 // Suppress the warning if class's superclass implements property's
1713 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001714 // Or, if property is going to be implemented in its super class.
1715 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001716 Diag(IMPDecl->getLocation(),
1717 diag::warn_auto_synthesizing_protocol_property)
1718 << Prop << Proto;
1719 Diag(Prop->getLocation(), diag::note_property_declare);
1720 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001721 continue;
1722 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001723 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001724 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001725 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1726 (PropInSuperClass->getPropertyAttributes() &
1727 ObjCPropertyDecl::OBJC_PR_readonly) &&
1728 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1729 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1730 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1731 << Prop->getIdentifier();
1732 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1733 }
1734 else {
1735 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1736 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001737 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001738 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1739 }
1740 continue;
1741 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001742 // We use invalid SourceLocations for the synthesized ivars since they
1743 // aren't really synthesized at a particular location; they just exist.
1744 // Saying that they are located at the @implementation isn't really going
1745 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001746 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1747 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1748 true,
1749 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001750 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001751 Prop->getLocation(), Prop->getQueryKind()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001752 if (PIDecl) {
1753 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001754 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001755 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001756 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001757}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001758
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001759void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001760 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001761 return;
1762 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1763 if (!IC)
1764 return;
1765 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001766 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001767 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001768}
1769
Manman Ren08ce7342016-05-18 18:12:34 +00001770static void DiagnoseUnimplementedAccessor(
1771 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
1772 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
1773 ObjCPropertyDecl *Prop,
1774 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
1775 // Check to see if we have a corresponding selector in SMap and with the
1776 // right method type.
1777 auto I = std::find_if(SMap.begin(), SMap.end(),
1778 [&](const ObjCMethodDecl *x) {
1779 return x->getSelector() == Method &&
1780 x->isClassMethod() == Prop->isClassProperty();
1781 });
Ted Kremenek7e812952014-02-21 19:41:30 +00001782 // When reporting on missing property setter/getter implementation in
1783 // categories, do not report when they are declared in primary class,
1784 // class's protocol, or one of it super classes. This is because,
1785 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00001786 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001787 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001788 !PrimaryClass->lookupPropertyAccessor(Method, C,
1789 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00001790 unsigned diag =
1791 isa<ObjCCategoryDecl>(CDecl)
1792 ? (Prop->isClassProperty()
1793 ? diag::warn_impl_required_in_category_for_class_property
1794 : diag::warn_setter_getter_impl_required_in_category)
1795 : (Prop->isClassProperty()
1796 ? diag::warn_impl_required_for_class_property
1797 : diag::warn_setter_getter_impl_required);
1798 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
1799 S.Diag(Prop->getLocation(), diag::note_property_declare);
1800 if (S.LangOpts.ObjCDefaultSynthProperties &&
1801 S.LangOpts.ObjCRuntime.isNonFragile())
1802 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1803 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1804 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1805 }
Ted Kremenek7e812952014-02-21 19:41:30 +00001806}
1807
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001808void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001809 ObjCContainerDecl *CDecl,
1810 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001811 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001812 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1813
Manman Ren16a7d632016-04-12 23:01:55 +00001814 // Since we don't synthesize class properties, we should emit diagnose even
1815 // if SynthesizeProperties is true.
1816 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1817 // Gather properties which need not be implemented in this class
1818 // or category.
1819 if (!IDecl)
1820 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1821 // For categories, no need to implement properties declared in
1822 // its primary class (and its super classes) if property is
1823 // declared in one of those containers.
1824 if ((IDecl = C->getClassInterface())) {
1825 ObjCInterfaceDecl::PropertyDeclOrder PO;
1826 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001827 }
Manman Ren16a7d632016-04-12 23:01:55 +00001828 }
1829 if (IDecl)
1830 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001831
Manman Ren16a7d632016-04-12 23:01:55 +00001832 // When SynthesizeProperties is true, we only check class properties.
1833 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
1834 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001835
Ted Kremenek38882022014-02-21 19:41:39 +00001836 // Scan the @interface to see if any of the protocols it adopts
1837 // require an explicit implementation, via attribute
1838 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001839 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001840 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001841
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001842 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001843 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1844 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001845 // Lazily construct a set of all the properties in the @interface
1846 // of the class, without looking at the superclass. We cannot
1847 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001848 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001849 // as scans the adopted protocols. This work only triggers for protocols
1850 // with the attribute, which is very rare, and only occurs when
1851 // analyzing the @implementation.
1852 if (!LazyMap) {
1853 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1854 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1855 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00001856 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00001857 /* IncludeProtocols */ false);
1858 }
Ted Kremenek38882022014-02-21 19:41:39 +00001859 // Add the properties of 'PDecl' to the list of properties that
1860 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00001861 for (auto *PropDecl : PDecl->properties()) {
1862 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
1863 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001864 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001865 PropMap[std::make_pair(PropDecl->getIdentifier(),
1866 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001867 }
1868 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001869 }
Ted Kremenek38882022014-02-21 19:41:39 +00001870
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001871 if (PropMap.empty())
1872 return;
1873
1874 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001875 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001876 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001877
Manman Ren08ce7342016-05-18 18:12:34 +00001878 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001879 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001880 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00001881 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001882
1883 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001884 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001885 if (C && !C->IsClassExtension())
1886 if ((PrimaryClass = C->getClassInterface()))
1887 // Report unimplemented properties in the category as well.
1888 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1889 // When reporting on missing setter/getters, do not report when
1890 // setter/getter is implemented in category's primary class
1891 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001892 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00001893 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001894 }
1895
Anna Zaks673d76b2012-10-18 19:17:53 +00001896 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001897 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1898 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00001899 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001900 if (Prop->isInvalidDecl() ||
1901 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001902 PropImplMap.count(Prop) ||
1903 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001905
1906 // Diagnose unimplemented getters and setters.
1907 DiagnoseUnimplementedAccessor(*this,
1908 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1909 if (!Prop->isReadOnly())
1910 DiagnoseUnimplementedAccessor(*this,
1911 PrimaryClass, Prop->getSetterName(),
1912 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001913 }
1914}
1915
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001916void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001917 for (const auto *propertyImpl : impDecl->property_impls()) {
1918 const auto *property = propertyImpl->getPropertyDecl();
1919
1920 // Warn about null_resettable properties with synthesized setters,
1921 // because the setter won't properly handle nil.
1922 if (propertyImpl->getPropertyImplementation()
1923 == ObjCPropertyImplDecl::Synthesize &&
1924 (property->getPropertyAttributes() &
1925 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1926 property->getGetterMethodDecl() &&
1927 property->getSetterMethodDecl()) {
1928 auto *getterMethod = property->getGetterMethodDecl();
1929 auto *setterMethod = property->getSetterMethodDecl();
1930 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1931 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1932 SourceLocation loc = propertyImpl->getLocation();
1933 if (loc.isInvalid())
1934 loc = impDecl->getLocStart();
1935
1936 Diag(loc, diag::warn_null_resettable_setter)
1937 << setterMethod->getSelector() << property->getDeclName();
1938 }
1939 }
1940 }
1941}
1942
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001943void
1944Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001945 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001946 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001947 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001948 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001949 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00001950 for (auto *Prop : IDecl->properties())
1951 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001952 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00001953 for (auto *Prop : Ext->properties())
1954 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001955
Manman Renefe1bac2016-01-27 20:00:32 +00001956 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1957 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001958 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001959 ObjCMethodDecl *GetterMethod = nullptr;
1960 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001961 bool LookedUpGetterSetter = false;
1962
Bill Wendling44426052012-12-20 19:22:21 +00001963 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001964 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001965
John McCall43192862011-09-13 18:31:23 +00001966 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1967 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00001968 GetterMethod = Property->isClassProperty() ?
1969 IMPDecl->getClassMethod(Property->getGetterName()) :
1970 IMPDecl->getInstanceMethod(Property->getGetterName());
1971 SetterMethod = Property->isClassProperty() ?
1972 IMPDecl->getClassMethod(Property->getSetterName()) :
1973 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001974 LookedUpGetterSetter = true;
1975 if (GetterMethod) {
1976 Diag(GetterMethod->getLocation(),
1977 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001978 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001979 Diag(Property->getLocation(), diag::note_property_declare);
1980 }
1981 if (SetterMethod) {
1982 Diag(SetterMethod->getLocation(),
1983 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001984 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001985 Diag(Property->getLocation(), diag::note_property_declare);
1986 }
1987 }
1988
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001989 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001990 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1991 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001992 continue;
Manman Ren5b786402016-01-28 18:49:28 +00001993 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
1994 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001995 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1996 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001997 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00001998 GetterMethod = Property->isClassProperty() ?
1999 IMPDecl->getClassMethod(Property->getGetterName()) :
2000 IMPDecl->getInstanceMethod(Property->getGetterName());
2001 SetterMethod = Property->isClassProperty() ?
2002 IMPDecl->getClassMethod(Property->getSetterName()) :
2003 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002004 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002005 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
2006 SourceLocation MethodLoc =
2007 (GetterMethod ? GetterMethod->getLocation()
2008 : SetterMethod->getLocation());
2009 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002010 << Property->getIdentifier() << (GetterMethod != nullptr)
2011 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002012 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002013 if (Property->getLParenLoc().isValid() &&
2014 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002015 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002016 SourceLocation AfterLParen =
2017 getLocForEndOfToken(Property->getLParenLoc());
2018 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2019 : "nonatomic";
2020 Diag(Property->getLocation(),
2021 diag::note_atomic_property_fixup_suggest)
2022 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2023 } else if (Property->getLParenLoc().isInvalid()) {
2024 //@property id etc.
2025 SourceLocation startLoc =
2026 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2027 Diag(Property->getLocation(),
2028 diag::note_atomic_property_fixup_suggest)
2029 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002030 }
2031 else
2032 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002033 Diag(Property->getLocation(), diag::note_property_declare);
2034 }
2035 }
2036 }
2037}
2038
John McCall31168b02011-06-15 23:02:42 +00002039void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002040 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002041 return;
2042
Aaron Ballmand85eff42014-03-14 15:02:45 +00002043 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002044 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002045 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002046 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002047 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002048 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2049 if (!method)
2050 continue;
2051 ObjCMethodFamily family = method->getMethodFamily();
2052 if (family == OMF_alloc || family == OMF_copy ||
2053 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002054 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002055 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002056 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002057 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002058
2059 // Look for a getter explicitly declared alongside the property.
2060 // If we find one, use its location for the note.
2061 SourceLocation noteLoc = PD->getLocation();
2062 SourceLocation fixItLoc;
2063 for (auto *getterRedecl : method->redecls()) {
2064 if (getterRedecl->isImplicit())
2065 continue;
2066 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2067 continue;
2068 noteLoc = getterRedecl->getLocation();
2069 fixItLoc = getterRedecl->getLocEnd();
2070 }
2071
2072 Preprocessor &PP = getPreprocessor();
2073 TokenValue tokens[] = {
2074 tok::kw___attribute, tok::l_paren, tok::l_paren,
2075 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2076 PP.getIdentifierInfo("none"), tok::r_paren,
2077 tok::r_paren, tok::r_paren
2078 };
2079 StringRef spelling = "__attribute__((objc_method_family(none)))";
2080 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2081 if (!macroName.empty())
2082 spelling = macroName;
2083
2084 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2085 << method->getDeclName() << spelling;
2086 if (fixItLoc.isValid()) {
2087 SmallString<64> fixItText(" ");
2088 fixItText += spelling;
2089 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2090 }
John McCall31168b02011-06-15 23:02:42 +00002091 }
2092 }
2093 }
2094}
2095
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002096void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002097 const ObjCImplementationDecl *ImplD,
2098 const ObjCInterfaceDecl *IFD) {
2099 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002100 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2101 if (!SuperD)
2102 return;
2103
2104 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002105 for (const auto *I : ImplD->instance_methods())
2106 if (I->getMethodFamily() == OMF_init)
2107 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002108
2109 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2110 SuperD->getDesignatedInitializers(DesignatedInits);
2111 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2112 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2113 const ObjCMethodDecl *MD = *I;
2114 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002115 bool Ignore = false;
2116 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2117 Ignore = IMD->isUnavailable();
2118 }
2119 if (!Ignore) {
2120 Diag(ImplD->getLocation(),
2121 diag::warn_objc_implementation_missing_designated_init_override)
2122 << MD->getSelector();
2123 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2124 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002125 }
2126 }
2127}
2128
John McCallad31b5f2010-11-10 07:01:40 +00002129/// AddPropertyAttrs - Propagates attributes from a property to the
2130/// implicitly-declared getter or setter for that property.
2131static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2132 ObjCPropertyDecl *Property) {
2133 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002134 for (const auto *A : Property->attrs()) {
2135 if (isa<DeprecatedAttr>(A) ||
2136 isa<UnavailableAttr>(A) ||
2137 isa<AvailabilityAttr>(A))
2138 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002139 }
John McCallad31b5f2010-11-10 07:01:40 +00002140}
2141
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002142/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2143/// have the property type and issue diagnostics if they don't.
2144/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002145/// appropriate lookup tables.
2146void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002148 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002149 if (CD->isInvalidDecl())
2150 return;
2151
Manman Rend36f7d52016-01-27 20:10:32 +00002152 bool IsClassProperty = property->isClassProperty();
2153 GetterMethod = IsClassProperty ?
2154 CD->getClassMethod(property->getGetterName()) :
2155 CD->getInstanceMethod(property->getGetterName());
2156
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002157 // if setter or getter is not found in class extension, it might be
2158 // in the primary class.
2159 if (!GetterMethod)
2160 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2161 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002162 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2163 getClassMethod(property->getGetterName()) :
2164 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002165 getInstanceMethod(property->getGetterName());
2166
Manman Rend36f7d52016-01-27 20:10:32 +00002167 SetterMethod = IsClassProperty ?
2168 CD->getClassMethod(property->getSetterName()) :
2169 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002170 if (!SetterMethod)
2171 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2172 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002173 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2174 getClassMethod(property->getSetterName()) :
2175 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002176 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002177 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2178 property->getLocation());
2179
2180 if (SetterMethod) {
2181 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2182 property->getPropertyAttributes();
2183 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002184 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2185 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002186 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2187 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002188 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002189 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2190 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002191 Diag(property->getLocation(),
2192 diag::warn_accessor_property_type_mismatch)
2193 << property->getDeclName()
2194 << SetterMethod->getSelector();
2195 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2196 }
2197 }
2198
2199 // Synthesize getter/setter methods if none exist.
2200 // Find the default getter and if one not found, add one.
2201 // FIXME: The synthesized property we set here is misleading. We almost always
2202 // synthesize these methods unless the user explicitly provided prototypes
2203 // (which is odd, but allowed). Sema should be typechecking that the
2204 // declarations jive in that situation (which it is not currently).
2205 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002206 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002207 // Declare a getter method and add it to the list of methods
2208 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002209 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002210
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002211 // The getter returns the declared property type with all qualifiers
2212 // removed.
2213 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2214
Douglas Gregor849ebc22015-06-19 18:14:46 +00002215 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002216 if (property->getPropertyAttributes() &
2217 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2218 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002219 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002220 if (*nullability == NullabilityKind::Unspecified)
2221 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2222 modifiedTy, modifiedTy);
2223 }
2224 }
2225
Ted Kremenek2f075632010-09-21 20:52:59 +00002226 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2227 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002228 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002229 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002230 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002231 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002232 (property->getPropertyImplementation() ==
2233 ObjCPropertyDecl::Optional) ?
2234 ObjCMethodDecl::Optional :
2235 ObjCMethodDecl::Required);
2236 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002237
2238 AddPropertyAttrs(*this, GetterMethod, property);
2239
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002240 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002241 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2242 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002243
2244 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2245 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002246 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002247
2248 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002249 GetterMethod->addAttr(
2250 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2251 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002252
2253 if (getLangOpts().ObjCAutoRefCount)
2254 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002255 } else
2256 // A user declared getter will be synthesize when @synthesize of
2257 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002258 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002259 property->setGetterMethodDecl(GetterMethod);
2260
2261 // Skip setter if property is read-only.
2262 if (!property->isReadOnly()) {
2263 // Find the default setter and if one not found, add one.
2264 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002265 // No instance/class method of same name as property setter name was
2266 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002267 // Declare a setter method and add it to the list of methods
2268 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002269 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002270
2271 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002272 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002273 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002274 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002275 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002276 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002277 /*isImplicitlyDeclared=*/true,
2278 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002279 (property->getPropertyImplementation() ==
2280 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002281 ObjCMethodDecl::Optional :
2282 ObjCMethodDecl::Required);
2283
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002284 // Remove all qualifiers from the setter's parameter type.
2285 QualType paramTy =
2286 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2287
Douglas Gregor849ebc22015-06-19 18:14:46 +00002288 // If the property is null_resettable, the setter accepts a
2289 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002290 if (property->getPropertyAttributes() &
2291 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2292 QualType modifiedTy = paramTy;
2293 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2294 if (*nullability == NullabilityKind::Unspecified)
2295 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2296 modifiedTy, modifiedTy);
2297 }
2298 }
2299
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002300 // Invent the arguments for the setter. We don't bother making a
2301 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002302 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2303 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002304 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002305 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002306 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002307 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002308 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002309 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002310
2311 AddPropertyAttrs(*this, SetterMethod, property);
2312
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002313 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002314 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002315 SetterMethod->addAttr(
2316 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2317 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002318 // It's possible for the user to have set a very odd custom
2319 // setter selector that causes it to have a method family.
2320 if (getLangOpts().ObjCAutoRefCount)
2321 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002322 } else
2323 // A user declared setter will be synthesize when @synthesize of
2324 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002325 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002326 property->setSetterMethodDecl(SetterMethod);
2327 }
2328 // Add any synthesized methods to the global pool. This allows us to
2329 // handle the following, which is supported by GCC (and part of the design).
2330 //
2331 // @interface Foo
2332 // @property double bar;
2333 // @end
2334 //
2335 // void thisIsUnfortunate() {
2336 // id foo;
2337 // double bar = [foo bar];
2338 // }
2339 //
Manman Rend36f7d52016-01-27 20:10:32 +00002340 if (!IsClassProperty) {
2341 if (GetterMethod)
2342 AddInstanceMethodToGlobalPool(GetterMethod);
2343 if (SetterMethod)
2344 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002345 } else {
2346 if (GetterMethod)
2347 AddFactoryMethodToGlobalPool(GetterMethod);
2348 if (SetterMethod)
2349 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002350 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002351
2352 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2353 if (!CurrentClass) {
2354 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2355 CurrentClass = Cat->getClassInterface();
2356 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2357 CurrentClass = Impl->getClassInterface();
2358 }
2359 if (GetterMethod)
2360 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2361 if (SetterMethod)
2362 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002363}
2364
John McCall48871652010-08-21 09:40:31 +00002365void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002366 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002367 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002368 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002369 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002370 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002371 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002372
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002373 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2374 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2375 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2376 << "readonly" << "readwrite";
2377
2378 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2379 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002380
2381 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002382 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002383 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2384 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002385 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002386 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002387 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2388 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2389 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002390 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002391 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002392 }
2393
2394 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002395 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2396 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002397 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2398 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002399 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002400 }
Bill Wendling44426052012-12-20 19:22:21 +00002401 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002402 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2403 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002404 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002405 }
Bill Wendling44426052012-12-20 19:22:21 +00002406 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002407 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2408 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002409 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002410 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002411 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002412 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002413 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2414 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002415 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002416 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002417 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002418 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002419 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2420 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002421 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2422 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002423 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002424 }
Bill Wendling44426052012-12-20 19:22:21 +00002425 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002426 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2427 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002428 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002429 }
Bill Wendling44426052012-12-20 19:22:21 +00002430 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002431 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2432 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002433 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002434 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002435 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002436 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002437 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2438 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002439 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002440 }
Bill Wendling44426052012-12-20 19:22:21 +00002441 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2442 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002443 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2444 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002445 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002446 }
Bill Wendling44426052012-12-20 19:22:21 +00002447 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002448 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2449 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002450 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002451 }
Bill Wendling44426052012-12-20 19:22:21 +00002452 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002453 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2454 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002455 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002456 }
2457 }
Bill Wendling44426052012-12-20 19:22:21 +00002458 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2459 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002460 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2461 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002462 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002463 }
Bill Wendling44426052012-12-20 19:22:21 +00002464 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2465 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002466 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2467 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002468 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002469 }
2470
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002471 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002472 // 'weak' and 'nonnull' are mutually exclusive.
2473 if (auto nullability = PropertyTy->getNullability(Context)) {
2474 if (*nullability == NullabilityKind::NonNull)
2475 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2476 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002477 }
2478 }
2479
Bill Wendling44426052012-12-20 19:22:21 +00002480 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2481 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002482 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2483 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002484 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002485 }
2486
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002487 // Warn if user supplied no assignment attribute, property is
2488 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002489 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2490 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2491 // do nothing
2492 } else if (getLangOpts().ObjCAutoRefCount) {
2493 // With arc, @property definitions should default to strong when
2494 // not specified.
2495 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2496 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002497 bool isAnyClassTy =
2498 (PropertyTy->isObjCClassType() ||
2499 PropertyTy->isObjCQualifiedClassType());
2500 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2501 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002502 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002503 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002504 else if (propertyInPrimaryClass) {
2505 // Don't issue warning on property with no life time in class
2506 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002507 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002508 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002509 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002510
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002511 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002512 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002513 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002514 }
John McCallb61e14e2015-10-27 04:54:50 +00002515 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002516
2517 // FIXME: Implement warning dependent on NSCopying being
2518 // implemented. See also:
2519 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2520 // (please trim this list while you are at it).
2521 }
2522
Bill Wendling44426052012-12-20 19:22:21 +00002523 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2524 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002525 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002526 && PropertyTy->isBlockPointerType())
2527 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002528 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2529 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2530 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002531 PropertyTy->isBlockPointerType())
2532 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002533
Bill Wendling44426052012-12-20 19:22:21 +00002534 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2535 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002536 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002537}