blob: b950615210412415bf475eeececc859e86b1f423 [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,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000203 FD,
204 GetterSel, ODS.getGetterNameLoc(),
205 SetterSel, ODS.getSetterNameLoc(),
206 isReadWrite, Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000207 ODS.getPropertyAttributes(),
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000208 T, TSI, MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000209 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000210 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000211 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000212 }
213
214 if (!Res) {
215 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000216 GetterSel, ODS.getGetterNameLoc(), SetterSel,
217 ODS.getSetterNameLoc(), isReadWrite, Attributes,
218 ODS.getPropertyAttributes(), T, TSI,
219 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000220 if (lexicalDC)
221 Res->setLexicalDeclContext(lexicalDC);
222 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000223
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000224 // Validate the attributes on the @property.
Douglas Gregord4f2afa2015-10-09 20:36:17 +0000225 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000226 (isa<ObjCInterfaceDecl>(ClassDecl) ||
227 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000228
John McCallb61e14e2015-10-27 04:54:50 +0000229 // Check consistency if the type has explicit ownership qualification.
230 if (Res->getType().getObjCLifetime())
231 checkPropertyDeclWithOwnership(*this, Res);
John McCall31168b02011-06-15 23:02:42 +0000232
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000234 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000235 // For a class, compare the property against a property in our superclass.
236 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000237 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
238 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000239 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000240 for (unsigned I = 0, N = R.size(); I != N; ++I) {
241 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000242 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000243 FoundInSuper = true;
244 break;
245 }
246 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000247 if (FoundInSuper)
248 break;
249 else
250 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000251 }
252
253 if (FoundInSuper) {
254 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000255 for (auto *P : CurrentInterfaceDecl->protocols()) {
256 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000257 }
258 } else {
259 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000260 for (auto *P : IFace->all_referenced_protocols()) {
261 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000262 }
263 }
264 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000265 // We don't check if class extension. Because properties in class extension
266 // are meant to override some of the attributes and checking has already done
267 // when property in class extension is constructed.
268 if (!Cat->IsClassExtension())
269 for (auto *P : Cat->protocols())
270 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000271 } else {
272 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000273 for (auto *P : Proto->protocols())
274 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000275 }
276
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000277 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000278 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000279}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000280
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000282makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
Manman Ren387ff7f2016-01-26 18:52:43 +0000308 if (Attributes & ObjCDeclSpec::DQ_PR_class)
309 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class;
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000310
311 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
312}
313
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000314static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000315 SourceLocation LParenLoc, SourceLocation &Loc) {
316 if (LParenLoc.isMacroID())
317 return false;
318
319 SourceManager &SM = Context.getSourceManager();
320 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
321 // Try to load the file buffer.
322 bool invalidTemp = false;
323 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
324 if (invalidTemp)
325 return false;
326 const char *tokenBegin = file.data() + locInfo.second;
327
328 // Lex from the start of the given location.
329 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
330 Context.getLangOpts(),
331 file.begin(), tokenBegin, file.end());
332 Token Tok;
333 do {
334 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000335 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000336 Loc = Tok.getLocation();
337 return true;
338 }
339 } while (Tok.isNot(tok::r_paren));
340 return false;
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000341}
342
Douglas Gregor429183e2015-12-09 22:57:32 +0000343/// Check for a mismatch in the atomicity of the given properties.
344static void checkAtomicPropertyMismatch(Sema &S,
345 ObjCPropertyDecl *OldProperty,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000346 ObjCPropertyDecl *NewProperty,
347 bool PropagateAtomicity) {
Douglas Gregor429183e2015-12-09 22:57:32 +0000348 // If the atomicity of both matches, we're done.
349 bool OldIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000350 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
351 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000352 bool NewIsAtomic =
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000353 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
354 == 0;
Douglas Gregor429183e2015-12-09 22:57:32 +0000355 if (OldIsAtomic == NewIsAtomic) return;
356
357 // Determine whether the given property is readonly and implicitly
358 // atomic.
359 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool {
360 // Is it readonly?
361 auto Attrs = Property->getPropertyAttributes();
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000362 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000363
364 // Is it nonatomic?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000365 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false;
Douglas Gregor429183e2015-12-09 22:57:32 +0000366
367 // Was 'atomic' specified directly?
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000368 if (Property->getPropertyAttributesAsWritten() &
369 ObjCPropertyDecl::OBJC_PR_atomic)
Douglas Gregor429183e2015-12-09 22:57:32 +0000370 return false;
371
372 return true;
373 };
374
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000375 // If we're allowed to propagate atomicity, and the new property did
376 // not specify atomicity at all, propagate.
377 const unsigned AtomicityMask =
378 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic);
379 if (PropagateAtomicity &&
380 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) {
381 unsigned Attrs = NewProperty->getPropertyAttributes();
382 Attrs = Attrs & ~AtomicityMask;
383 if (OldIsAtomic)
384 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic;
385 else
386 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic;
387
388 NewProperty->overwritePropertyAttributes(Attrs);
389 return;
390 }
391
Douglas Gregor429183e2015-12-09 22:57:32 +0000392 // One of the properties is atomic; if it's a readonly property, and
393 // 'atomic' wasn't explicitly specified, we're okay.
394 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) ||
395 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty)))
396 return;
397
398 // Diagnose the conflict.
399 const IdentifierInfo *OldContextName;
400 auto *OldDC = OldProperty->getDeclContext();
401 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC))
402 OldContextName = Category->getClassInterface()->getIdentifier();
403 else
404 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier();
405
406 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute)
407 << NewProperty->getDeclName() << "atomic"
408 << OldContextName;
409 S.Diag(OldProperty->getLocation(), diag::note_property_declare);
410}
411
Douglas Gregor90d34422013-01-21 19:05:22 +0000412ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000413Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000414 SourceLocation AtLoc,
415 SourceLocation LParenLoc,
416 FieldDeclarator &FD,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000417 Selector GetterSel,
418 SourceLocation GetterNameLoc,
419 Selector SetterSel,
420 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000421 const bool isReadWrite,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000422 unsigned &Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000423 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000424 QualType T,
425 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000426 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000427 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000428 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000429 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000431 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
432
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 // We need to look in the @interface to see if the @property was
434 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 if (!CCPrimary) {
436 Diag(CDecl->getLocation(), diag::err_continuation_class);
Craig Topperc3ec1492014-05-26 06:22:03 +0000437 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000438 }
439
Manman Ren5b786402016-01-28 18:49:28 +0000440 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
441 (Attributes & ObjCDeclSpec::DQ_PR_class);
442
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000443 // Find the property in the extended class's primary class or
444 // extensions.
Manman Ren5b786402016-01-28 18:49:28 +0000445 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass(
446 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty));
Ted Kremenek959e8302010-03-12 02:31:10 +0000447
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000448 // If we found a property in an extension, complain.
449 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
450 Diag(AtLoc, diag::err_duplicate_property);
451 Diag(PIDecl->getLocation(), diag::note_property_declare);
452 return nullptr;
453 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000454
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000455 // Check for consistency with the previous declaration, if there is one.
456 if (PIDecl) {
457 // A readonly property declared in the primary class can be refined
458 // by adding a readwrite property within an extension.
459 // Anything else is an error.
460 if (!(PIDecl->isReadOnly() && isReadWrite)) {
461 // Tailor the diagnostics for the common case where a readwrite
462 // property is declared both in the @interface and the continuation.
463 // This is a common error where the user often intended the original
464 // declaration to be readonly.
465 unsigned diag =
466 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
467 (PIDecl->getPropertyAttributesAsWritten() &
468 ObjCPropertyDecl::OBJC_PR_readwrite)
469 ? diag::err_use_continuation_class_redeclaration_readwrite
470 : diag::err_use_continuation_class;
471 Diag(AtLoc, diag)
472 << CCPrimary->getDeclName();
473 Diag(PIDecl->getLocation(), diag::note_property_declare);
474 return nullptr;
475 }
476
477 // Check for consistency of getters.
478 if (PIDecl->getGetterName() != GetterSel) {
479 // If the getter was written explicitly, complain.
480 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) {
481 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch)
482 << PIDecl->getGetterName() << GetterSel;
483 Diag(PIDecl->getLocation(), diag::note_property_declare);
484 }
485
486 // Always adopt the getter from the original declaration.
487 GetterSel = PIDecl->getGetterName();
488 Attributes |= ObjCDeclSpec::DQ_PR_getter;
489 }
490
491 // Check consistency of ownership.
492 unsigned ExistingOwnership
493 = getOwnershipRule(PIDecl->getPropertyAttributes());
494 unsigned NewOwnership = getOwnershipRule(Attributes);
495 if (ExistingOwnership && NewOwnership != ExistingOwnership) {
496 // If the ownership was written explicitly, complain.
497 if (getOwnershipRule(AttributesAsWritten)) {
498 Diag(AtLoc, diag::warn_property_attr_mismatch);
499 Diag(PIDecl->getLocation(), diag::note_property_declare);
500 }
501
502 // Take the ownership from the original property.
503 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership;
504 }
505
506 // If the redeclaration is 'weak' but the original property is not,
507 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) &&
508 !(PIDecl->getPropertyAttributesAsWritten()
509 & ObjCPropertyDecl::OBJC_PR_weak) &&
510 PIDecl->getType()->getAs<ObjCObjectPointerType>() &&
511 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) {
512 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
513 Diag(PIDecl->getLocation(), diag::note_property_declare);
514 }
515 }
516
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000517 // Create a new ObjCPropertyDecl with the DeclContext being
518 // the class extension.
519 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000520 FD, GetterSel, GetterNameLoc,
521 SetterSel, SetterNameLoc,
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000522 isReadWrite,
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000523 Attributes, AttributesAsWritten,
524 T, TSI, MethodImplKind, DC);
525
526 // If there was no declaration of a property with the same name in
527 // the primary class, we're done.
528 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000529 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000530 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000531 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000532
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000533 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
534 bool IncompatibleObjC = false;
535 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000536 // Relax the strict type matching for property type in continuation class.
537 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000538 // as it narrows the object type in its primary class property. Note that
539 // this conversion is safe only because the wider type is for a 'readonly'
540 // property in primary class and 'narrowed' type for a 'readwrite' property
541 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000542 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
543 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
544 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
545 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
546 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000547 ConvertedType, IncompatibleObjC))
548 || IncompatibleObjC) {
549 Diag(AtLoc,
550 diag::err_type_mismatch_continuation_class) << PDecl->getType();
551 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000552 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000553 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000554 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000555
556 // Check that atomicity of property in class extension matches the previous
557 // declaration.
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000558 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000559
Douglas Gregore17765e2015-11-03 17:02:34 +0000560 // Make sure getter/setter are appropriately synthesized.
561 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000562 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000563}
564
565ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
566 ObjCContainerDecl *CDecl,
567 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000568 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000569 FieldDeclarator &FD,
570 Selector GetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000571 SourceLocation GetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000572 Selector SetterSel,
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000573 SourceLocation SetterNameLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000574 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000575 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000576 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000577 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000578 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000579 tok::ObjCKeywordKind MethodImplKind,
580 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000581 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000582
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000583 // Property defaults to 'assign' if it is readwrite, unless this is ARC
584 // and the type is retainable.
585 bool isAssign;
586 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
587 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
588 isAssign = true;
589 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
590 isAssign = false;
591 } else {
592 isAssign = (!getLangOpts().ObjCAutoRefCount ||
593 !T->isObjCRetainableType());
594 }
595
596 // Issue a warning if property is 'assign' as default and its
597 // object, which is gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000598 if (getLangOpts().getGC() != LangOptions::NonGC &&
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000599 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) {
John McCall8b07ec22010-05-15 11:32:37 +0000600 if (const ObjCObjectPointerType *ObjPtrTy =
601 T->getAs<ObjCObjectPointerType>()) {
602 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
603 if (IDecl)
604 if (ObjCProtocolDecl* PNSCopying =
605 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
606 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
607 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 }
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000609 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000610
611 if (T->isObjCObjectType()) {
612 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000613 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000614 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
615 << FixItHint::CreateInsertion(StarLoc, "*");
616 T = Context.getObjCObjectPointerType(T);
617 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
618 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
619 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000620
Ted Kremenek959e8302010-03-12 02:31:10 +0000621 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
623 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000624 PropertyId, AtLoc,
625 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000626
Manman Ren5b786402016-01-28 18:49:28 +0000627 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) ||
628 (Attributes & ObjCDeclSpec::DQ_PR_class);
629 // Class property and instance property can have the same name.
630 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl(
631 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000632 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000633 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000634 PDecl->setInvalidDecl();
635 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000636 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000638 if (lexicalDC)
639 PDecl->setLexicalDeclContext(lexicalDC);
640 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000641
642 if (T->isArrayType() || T->isFunctionType()) {
643 Diag(AtLoc, diag::err_property_type) << T;
644 PDecl->setInvalidDecl();
645 }
646
647 ProcessDeclAttributes(S, PDecl, FD.D);
648
649 // Regardless of setter/getter attribute, we save the default getter/setter
650 // selector names in anticipation of declaration of setter/getter methods.
Argyrios Kyrtzidis194b28e2017-03-16 18:25:40 +0000651 PDecl->setGetterName(GetterSel, GetterNameLoc);
652 PDecl->setSetterName(SetterSel, SetterNameLoc);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000653 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000654 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000655
Bill Wendling44426052012-12-20 19:22:21 +0000656 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000657 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
658
Bill Wendling44426052012-12-20 19:22:21 +0000659 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000660 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
661
Bill Wendling44426052012-12-20 19:22:21 +0000662 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000663 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
664
665 if (isReadWrite)
666 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
667
Bill Wendling44426052012-12-20 19:22:21 +0000668 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000669 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
670
Bill Wendling44426052012-12-20 19:22:21 +0000671 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000672 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
673
Bill Wendling44426052012-12-20 19:22:21 +0000674 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
676
Bill Wendling44426052012-12-20 19:22:21 +0000677 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000678 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
679
Bill Wendling44426052012-12-20 19:22:21 +0000680 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000681 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
682
Ted Kremenekac597f32010-03-12 00:46:40 +0000683 if (isAssign)
684 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
685
John McCall43192862011-09-13 18:31:23 +0000686 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000687 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000689 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000690 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000691
John McCall31168b02011-06-15 23:02:42 +0000692 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000693 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000694 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
695 if (isAssign)
696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
697
Ted Kremenekac597f32010-03-12 00:46:40 +0000698 if (MethodImplKind == tok::objc_required)
699 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
700 else if (MethodImplKind == tok::objc_optional)
701 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000702
Douglas Gregor813a0662015-06-19 18:14:38 +0000703 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
704 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
705
Douglas Gregor849ebc22015-06-19 18:14:46 +0000706 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
707 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
708
Manman Ren387ff7f2016-01-26 18:52:43 +0000709 if (Attributes & ObjCDeclSpec::DQ_PR_class)
710 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class);
711
Ted Kremenek959e8302010-03-12 02:31:10 +0000712 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000713}
714
John McCall31168b02011-06-15 23:02:42 +0000715static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
716 ObjCPropertyDecl *property,
717 ObjCIvarDecl *ivar) {
718 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
719
John McCall31168b02011-06-15 23:02:42 +0000720 QualType ivarType = ivar->getType();
721 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall43192862011-09-13 18:31:23 +0000723 // The lifetime implied by the property's attributes.
724 Qualifiers::ObjCLifetime propertyLifetime =
725 getImpliedARCOwnership(property->getPropertyAttributes(),
726 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000727
John McCall43192862011-09-13 18:31:23 +0000728 // We're fine if they match.
729 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000730
John McCall460ce582015-10-22 18:38:17 +0000731 // None isn't a valid lifetime for an object ivar in ARC, and
732 // __autoreleasing is never valid; don't diagnose twice.
733 if ((ivarLifetime == Qualifiers::OCL_None &&
734 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000735 ivarLifetime == Qualifiers::OCL_Autoreleasing)
736 return;
John McCall31168b02011-06-15 23:02:42 +0000737
John McCalld8561f02012-08-20 23:36:59 +0000738 // If the ivar is private, and it's implicitly __unsafe_unretained
739 // becaues of its type, then pretend it was actually implicitly
740 // __strong. This is only sound because we're processing the
741 // property implementation before parsing any method bodies.
742 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
743 propertyLifetime == Qualifiers::OCL_Strong &&
744 ivar->getAccessControl() == ObjCIvarDecl::Private) {
745 SplitQualType split = ivarType.split();
746 if (split.Quals.hasObjCLifetime()) {
747 assert(ivarType->isObjCARCImplicitlyUnretainedType());
748 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
749 ivarType = S.Context.getQualifiedType(split);
750 ivar->setType(ivarType);
751 return;
752 }
753 }
754
John McCall43192862011-09-13 18:31:23 +0000755 switch (propertyLifetime) {
756 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000757 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000758 << property->getDeclName()
759 << ivar->getDeclName()
760 << ivarLifetime;
761 break;
John McCall31168b02011-06-15 23:02:42 +0000762
John McCall43192862011-09-13 18:31:23 +0000763 case Qualifiers::OCL_Weak:
Richard Smithf8812672016-12-02 22:38:31 +0000764 S.Diag(ivar->getLocation(), diag::err_weak_property)
John McCall43192862011-09-13 18:31:23 +0000765 << property->getDeclName()
766 << ivar->getDeclName();
767 break;
John McCall31168b02011-06-15 23:02:42 +0000768
John McCall43192862011-09-13 18:31:23 +0000769 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000770 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000771 << property->getDeclName()
772 << ivar->getDeclName()
773 << ((property->getPropertyAttributesAsWritten()
774 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
775 break;
John McCall31168b02011-06-15 23:02:42 +0000776
John McCall43192862011-09-13 18:31:23 +0000777 case Qualifiers::OCL_Autoreleasing:
778 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000779
John McCall43192862011-09-13 18:31:23 +0000780 case Qualifiers::OCL_None:
781 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000782 return;
783 }
784
785 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000786 if (propertyImplLoc.isValid())
787 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000788}
789
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000790/// setImpliedPropertyAttributeForReadOnlyProperty -
791/// This routine evaludates life-time attributes for a 'readonly'
792/// property with no known lifetime of its own, using backing
793/// 'ivar's attribute, if any. If no backing 'ivar', property's
794/// life-time is assumed 'strong'.
795static void setImpliedPropertyAttributeForReadOnlyProperty(
796 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
797 Qualifiers::ObjCLifetime propertyLifetime =
798 getImpliedARCOwnership(property->getPropertyAttributes(),
799 property->getType());
800 if (propertyLifetime != Qualifiers::OCL_None)
801 return;
802
803 if (!ivar) {
804 // if no backing ivar, make property 'strong'.
805 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
806 return;
807 }
808 // property assumes owenership of backing ivar.
809 QualType ivarType = ivar->getType();
810 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
811 if (ivarLifetime == Qualifiers::OCL_Strong)
812 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
813 else if (ivarLifetime == Qualifiers::OCL_Weak)
814 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000815}
Ted Kremenekac597f32010-03-12 00:46:40 +0000816
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000817/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
818/// in inherited protocols with mismatched types. Since any of them can
819/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000820static void
821DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
822 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000823 ObjCPropertyDecl *Property) {
824 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000825 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
826 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000827 PDecl->collectInheritedProtocolProperties(Property, PropMap);
828 }
829 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
830 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000831 for (const auto *PI : SDecl->all_referenced_protocols()) {
832 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000833 PDecl->collectInheritedProtocolProperties(Property, PropMap);
834 }
835 SDecl = SDecl->getSuperClass();
836 }
837
838 if (PropMap.empty())
839 return;
840
841 QualType RHSType = S.Context.getCanonicalType(Property->getType());
842 bool FirsTime = true;
843 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
844 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
845 ObjCPropertyDecl *Prop = I->second;
846 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
847 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
848 bool IncompatibleObjC = false;
849 QualType ConvertedType;
850 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
851 || IncompatibleObjC) {
852 if (FirsTime) {
853 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
854 << Property->getType();
855 FirsTime = false;
856 }
857 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
858 << Prop->getType();
859 }
860 }
861 }
862 if (!FirsTime && AtLoc.isValid())
863 S.Diag(AtLoc, diag::note_property_synthesize);
864}
865
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000866/// Determine whether any storage attributes were written on the property.
Manman Ren5b786402016-01-28 18:49:28 +0000867static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop,
868 ObjCPropertyQueryKind QueryKind) {
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000869 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true;
870
871 // If this is a readwrite property in a class extension that refines
872 // a readonly property in the original class definition, check it as
873 // well.
874
875 // If it's a readonly property, we're not interested.
876 if (Prop->isReadOnly()) return false;
877
878 // Is it declared in an extension?
879 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext());
880 if (!Category || !Category->IsClassExtension()) return false;
881
882 // Find the corresponding property in the primary class definition.
883 auto OrigClass = Category->getClassInterface();
884 for (auto Found : OrigClass->lookup(Prop->getDeclName())) {
885 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found))
886 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
887 }
888
Douglas Gregor02535432015-12-18 00:52:31 +0000889 // Look through all of the protocols.
890 for (const auto *Proto : OrigClass->all_referenced_protocols()) {
Manman Ren5b786402016-01-28 18:49:28 +0000891 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration(
892 Prop->getIdentifier(), QueryKind))
Douglas Gregor02535432015-12-18 00:52:31 +0000893 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask;
894 }
895
Douglas Gregor9dd25b72015-12-10 23:02:09 +0000896 return false;
897}
898
Ted Kremenekac597f32010-03-12 00:46:40 +0000899/// ActOnPropertyImplDecl - This routine performs semantic checks and
900/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000901/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000902///
John McCall48871652010-08-21 09:40:31 +0000903Decl *Sema::ActOnPropertyImplDecl(Scope *S,
904 SourceLocation AtLoc,
905 SourceLocation PropertyLoc,
906 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000907 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000908 IdentifierInfo *PropertyIvar,
Manman Ren5b786402016-01-28 18:49:28 +0000909 SourceLocation PropertyIvarLoc,
910 ObjCPropertyQueryKind QueryKind) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000911 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000912 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000913 // Make sure we have a context for the property implementation declaration.
914 if (!ClassImpDecl) {
Richard Smithf8812672016-12-02 22:38:31 +0000915 Diag(AtLoc, diag::err_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000916 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000917 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000918 if (PropertyIvarLoc.isInvalid())
919 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000920 SourceLocation PropertyDiagLoc = PropertyLoc;
921 if (PropertyDiagLoc.isInvalid())
922 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000923 ObjCPropertyDecl *property = nullptr;
924 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000925 // Find the class or category class where this property must have
926 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000927 ObjCImplementationDecl *IC = nullptr;
928 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000929 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
930 IDecl = IC->getClassInterface();
931 // We always synthesize an interface for an implementation
932 // without an interface decl. So, IDecl is always non-zero.
933 assert(IDecl &&
934 "ActOnPropertyImplDecl - @implementation without @interface");
935
936 // Look for this property declaration in the @implementation's @interface
Manman Ren5b786402016-01-28 18:49:28 +0000937 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +0000938 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +0000939 Diag(PropertyLoc, diag::err_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000940 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000941 }
Manman Rendfef4062016-01-29 19:16:39 +0000942 if (property->isClassProperty() && Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +0000943 Diag(PropertyLoc, diag::err_synthesize_on_class_property) << PropertyId;
Manman Rendfef4062016-01-29 19:16:39 +0000944 return nullptr;
945 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000946 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000947 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
948 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000949 if (AtLoc.isValid())
950 Diag(AtLoc, diag::warn_implicit_atomic_property);
951 else
952 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
953 Diag(property->getLocation(), diag::note_property_declare);
954 }
955
Ted Kremenekac597f32010-03-12 00:46:40 +0000956 if (const ObjCCategoryDecl *CD =
957 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
958 if (!CD->IsClassExtension()) {
Richard Smithf8812672016-12-02 22:38:31 +0000959 Diag(PropertyLoc, diag::err_category_property) << CD->getDeclName();
Ted Kremenekac597f32010-03-12 00:46:40 +0000960 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000961 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000962 }
963 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000964 if (Synthesize&&
965 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
966 property->hasAttr<IBOutletAttr>() &&
967 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000968 bool ReadWriteProperty = false;
969 // Search into the class extensions and see if 'readonly property is
970 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000971 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000972 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
973 if (!R.empty())
974 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
975 PIkind = ExtProp->getPropertyAttributesAsWritten();
976 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
977 ReadWriteProperty = true;
978 break;
979 }
980 }
981 }
982
983 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000984 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000985 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000986 SourceLocation readonlyLoc;
987 if (LocPropertyAttribute(Context, "readonly",
988 property->getLParenLoc(), readonlyLoc)) {
989 SourceLocation endLoc =
990 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
991 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
992 Diag(property->getLocation(),
993 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
994 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
995 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000996 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000997 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000998 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
999 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +00001000
Ted Kremenekac597f32010-03-12 00:46:40 +00001001 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
1002 if (Synthesize) {
Richard Smithf8812672016-12-02 22:38:31 +00001003 Diag(AtLoc, diag::err_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001004 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001005 }
1006 IDecl = CatImplClass->getClassInterface();
1007 if (!IDecl) {
Richard Smithf8812672016-12-02 22:38:31 +00001008 Diag(AtLoc, diag::err_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +00001009 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001010 }
1011 ObjCCategoryDecl *Category =
1012 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
1013
1014 // If category for this implementation not found, it is an error which
1015 // has already been reported eralier.
1016 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +00001017 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001018 // Look for this property declaration in @implementation's category
Manman Ren5b786402016-01-28 18:49:28 +00001019 property = Category->FindPropertyDeclaration(PropertyId, QueryKind);
Ted Kremenekac597f32010-03-12 00:46:40 +00001020 if (!property) {
Richard Smithf8812672016-12-02 22:38:31 +00001021 Diag(PropertyLoc, diag::err_bad_category_property_decl)
Ted Kremenekac597f32010-03-12 00:46:40 +00001022 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +00001023 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001024 }
1025 } else {
Richard Smithf8812672016-12-02 22:38:31 +00001026 Diag(AtLoc, diag::err_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00001027 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001028 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001029 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +00001030 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001031 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +00001032 // Check that we have a valid, previously declared ivar for @synthesize
1033 if (Synthesize) {
1034 // @synthesize
1035 if (!PropertyIvar)
1036 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001037 // Check that this is a previously declared 'ivar' in 'IDecl' interface
1038 ObjCInterfaceDecl *ClassDeclared;
1039 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
1040 QualType PropType = property->getType();
1041 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +00001042
1043 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001044 diag::err_incomplete_synthesized_property,
1045 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +00001046 Diag(property->getLocation(), diag::note_property_declare);
1047 CompleteTypeErr = true;
1048 }
1049
David Blaikiebbafb8a2012-03-11 07:00:24 +00001050 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001051 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +00001052 ObjCPropertyDecl::OBJC_PR_readonly) &&
1053 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00001054 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1055 }
1056
John McCall31168b02011-06-15 23:02:42 +00001057 ObjCPropertyDecl::PropertyAttributeKind kind
1058 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001059
John McCall460ce582015-10-22 18:38:17 +00001060 bool isARCWeak = false;
1061 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
1062 // Add GC __weak to the ivar type if the property is weak.
1063 if (getLangOpts().getGC() != LangOptions::NonGC) {
1064 assert(!getLangOpts().ObjCAutoRefCount);
1065 if (PropertyIvarType.isObjCGCStrong()) {
1066 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
1067 Diag(property->getLocation(), diag::note_property_declare);
1068 } else {
1069 PropertyIvarType =
1070 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1071 }
1072
1073 // Otherwise, check whether ARC __weak is enabled and works with
1074 // the property type.
John McCall43192862011-09-13 18:31:23 +00001075 } else {
John McCall460ce582015-10-22 18:38:17 +00001076 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001077 // Only complain here when synthesizing an ivar.
1078 if (!Ivar) {
1079 Diag(PropertyDiagLoc,
1080 getLangOpts().ObjCWeakRuntime
1081 ? diag::err_synthesizing_arc_weak_property_disabled
1082 : diag::err_synthesizing_arc_weak_property_no_runtime);
1083 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001084 }
John McCallb61e14e2015-10-27 04:54:50 +00001085 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001086 } else {
1087 isARCWeak = true;
1088 if (const ObjCObjectPointerType *ObjT =
1089 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1090 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1091 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1092 Diag(property->getLocation(),
1093 diag::err_arc_weak_unavailable_property)
1094 << PropertyIvarType;
1095 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1096 << ClassImpDecl->getName();
1097 }
1098 }
1099 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001100 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001101 }
John McCall460ce582015-10-22 18:38:17 +00001102
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001103 if (AtLoc.isInvalid()) {
1104 // Check when default synthesizing a property that there is
1105 // an ivar matching property name and issue warning; since this
1106 // is the most common case of not using an ivar used for backing
1107 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001108 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001109 ObjCIvarDecl *originalIvar =
1110 IDecl->lookupInstanceVariable(property->getIdentifier(),
1111 ClassDeclared);
1112 if (originalIvar) {
1113 Diag(PropertyDiagLoc,
1114 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001115 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001116 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001117 Diag(property->getLocation(), diag::note_property_declare);
1118 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001119 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001120 }
1121
1122 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001123 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001124 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001125 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001126 !PropertyIvarType.getObjCLifetime() &&
1127 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001128
John McCall43192862011-09-13 18:31:23 +00001129 // It's an error if we have to do this and the user didn't
1130 // explicitly write an ownership attribute on the property.
Manman Ren5b786402016-01-28 18:49:28 +00001131 if (!hasWrittenStorageAttribute(property, QueryKind) &&
John McCall43192862011-09-13 18:31:23 +00001132 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001133 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001134 diag::err_arc_objc_property_default_assign_on_object);
1135 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001136 } else {
1137 Qualifiers::ObjCLifetime lifetime =
1138 getImpliedARCOwnership(kind, PropertyIvarType);
1139 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001140
John McCall31168b02011-06-15 23:02:42 +00001141 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001142 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001143 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1144 }
John McCall31168b02011-06-15 23:02:42 +00001145 }
1146
Abramo Bagnaradff19302011-03-08 08:55:46 +00001147 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001148 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001149 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001150 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001151 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001152 if (RequireNonAbstractType(PropertyIvarLoc,
1153 PropertyIvarType,
1154 diag::err_abstract_type_in_decl,
1155 AbstractSynthesizedIvarType)) {
1156 Diag(property->getLocation(), diag::note_property_declare);
Richard Smith81f5ade2016-12-15 02:28:18 +00001157 // An abstract type is as bad as an incomplete type.
1158 CompleteTypeErr = true;
1159 }
1160 if (CompleteTypeErr)
Eli Friedman169ec352012-05-01 22:26:06 +00001161 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001162 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001163 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001164
John McCall5fb5df92012-06-20 06:18:46 +00001165 if (getLangOpts().ObjCRuntime.isFragile())
Richard Smithf8812672016-12-02 22:38:31 +00001166 Diag(PropertyDiagLoc, diag::err_missing_property_ivar_decl)
Eli Friedman169ec352012-05-01 22:26:06 +00001167 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001168 // Note! I deliberately want it to fall thru so, we have a
1169 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001170 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001171 !declaresSameEntity(ClassDeclared, IDecl)) {
Richard Smithf8812672016-12-02 22:38:31 +00001172 Diag(PropertyDiagLoc, diag::err_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001173 << property->getDeclName() << Ivar->getDeclName()
1174 << ClassDeclared->getDeclName();
1175 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001176 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001177 // Note! I deliberately want it to fall thru so more errors are caught.
1178 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001179 property->setPropertyIvarDecl(Ivar);
1180
Ted Kremenekac597f32010-03-12 00:46:40 +00001181 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1182
1183 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001184 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001185 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001186 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001187 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001188 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001189 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001190 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001191 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001192 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1193 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001194 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001195 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001196 if (!compat) {
Richard Smithf8812672016-12-02 22:38:31 +00001197 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001198 << property->getDeclName() << PropType
1199 << Ivar->getDeclName() << IvarType;
1200 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001201 // Note! I deliberately want it to fall thru so, we have a
1202 // a property implementation and to avoid future warnings.
1203 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001204 else {
1205 // FIXME! Rules for properties are somewhat different that those
1206 // for assignments. Use a new routine to consolidate all cases;
1207 // specifically for property redeclarations as well as for ivars.
1208 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1209 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1210 if (lhsType != rhsType &&
1211 lhsType->isArithmeticType()) {
Richard Smithf8812672016-12-02 22:38:31 +00001212 Diag(PropertyDiagLoc, diag::err_property_ivar_type)
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001213 << property->getDeclName() << PropType
1214 << Ivar->getDeclName() << IvarType;
1215 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1216 // Fall thru - see previous comment
1217 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001218 }
1219 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001220 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001221 getLangOpts().getGC() != LangOptions::NonGC)) {
Richard Smithf8812672016-12-02 22:38:31 +00001222 Diag(PropertyDiagLoc, diag::err_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001223 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001224 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001225 // Fall thru - see previous comment
1226 }
John McCall31168b02011-06-15 23:02:42 +00001227 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001228 if ((property->getType()->isObjCObjectPointerType() ||
1229 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001230 getLangOpts().getGC() != LangOptions::NonGC) {
Richard Smithf8812672016-12-02 22:38:31 +00001231 Diag(PropertyDiagLoc, diag::err_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001232 << property->getDeclName() << Ivar->getDeclName();
1233 // Fall thru - see previous comment
1234 }
1235 }
John McCall460ce582015-10-22 18:38:17 +00001236 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1237 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001238 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001239 } else if (PropertyIvar)
1240 // @dynamic
Richard Smithf8812672016-12-02 22:38:31 +00001241 Diag(PropertyDiagLoc, diag::err_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001242
Ted Kremenekac597f32010-03-12 00:46:40 +00001243 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1244 ObjCPropertyImplDecl *PIDecl =
1245 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1246 property,
1247 (Synthesize ?
1248 ObjCPropertyImplDecl::Synthesize
1249 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001250 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001251
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001252 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001253 PIDecl->setInvalidDecl();
1254
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001255 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1256 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001257 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001258 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001259 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1260 // returned by the getter as it must conform to C++'s copy-return rules.
1261 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001262 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001263 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1264 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001265 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001266 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001267 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001268 Expr *LoadSelfExpr =
1269 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001270 CK_LValueToRValue, SelfExpr, nullptr,
1271 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001272 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001273 new (Context) ObjCIvarRefExpr(Ivar,
1274 Ivar->getUsageType(SelfDecl->getType()),
1275 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001276 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001277 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001278 ExprResult Res = PerformCopyInitialization(
1279 InitializedEntity::InitializeResult(PropertyDiagLoc,
1280 getterMethod->getReturnType(),
1281 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001282 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001283 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001284 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001285 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001286 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001287 PIDecl->setGetterCXXConstructor(ResExpr);
1288 }
1289 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001290 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1291 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1292 Diag(getterMethod->getLocation(),
1293 diag::warn_property_getter_owning_mismatch);
1294 Diag(property->getLocation(), diag::note_property_declare);
1295 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001296 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1297 switch (getterMethod->getMethodFamily()) {
1298 case OMF_retain:
1299 case OMF_retainCount:
1300 case OMF_release:
1301 case OMF_autorelease:
1302 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1303 << 1 << getterMethod->getSelector();
1304 break;
1305 default:
1306 break;
1307 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001308 }
1309 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1310 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001311 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1312 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001313 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001314 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001315 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1316 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001317 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001318 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001319 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001320 Expr *LoadSelfExpr =
1321 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001322 CK_LValueToRValue, SelfExpr, nullptr,
1323 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001324 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001325 new (Context) ObjCIvarRefExpr(Ivar,
1326 Ivar->getUsageType(SelfDecl->getType()),
1327 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001328 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001329 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001330 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1331 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001332 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001333 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1334 VK_LValue, PropertyDiagLoc);
1335 MarkDeclRefReferenced(rhs);
1336 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001337 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001338 if (property->getPropertyAttributes() &
1339 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001340 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001341 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001342 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1343 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001344 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001345 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001346 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001347 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001348 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001349 Diag(FuncDecl->getLocStart(),
1350 diag::note_callee_decl) << FuncDecl;
1351 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001352 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001353 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001354 }
1355 }
1356
Ted Kremenekac597f32010-03-12 00:46:40 +00001357 if (IC) {
1358 if (Synthesize)
1359 if (ObjCPropertyImplDecl *PPIDecl =
1360 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001361 Diag(PropertyLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001362 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1363 << PropertyIvar;
1364 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1365 }
1366
1367 if (ObjCPropertyImplDecl *PPIDecl
Manman Ren5b786402016-01-28 18:49:28 +00001368 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001369 Diag(PropertyLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001370 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001371 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001372 }
1373 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001374 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001375 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001376 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001377 // Diagnose if an ivar was lazily synthesdized due to a previous
1378 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001379 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001380 ObjCInterfaceDecl *ClassDeclared=nullptr;
1381 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001382 if (!Synthesize)
1383 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1384 else {
1385 if (PropertyIvar && PropertyIvar != PropertyId)
1386 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1387 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001388 // Issue diagnostics only if Ivar belongs to current class.
1389 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001390 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001391 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1392 << PropertyId;
1393 Ivar->setInvalidDecl();
1394 }
1395 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001396 } else {
1397 if (Synthesize)
1398 if (ObjCPropertyImplDecl *PPIDecl =
1399 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Richard Smithf8812672016-12-02 22:38:31 +00001400 Diag(PropertyDiagLoc, diag::err_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001401 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1402 << PropertyIvar;
1403 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1404 }
1405
1406 if (ObjCPropertyImplDecl *PPIDecl =
Manman Ren5b786402016-01-28 18:49:28 +00001407 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) {
Richard Smithf8812672016-12-02 22:38:31 +00001408 Diag(PropertyDiagLoc, diag::err_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001409 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001410 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001411 }
1412 CatImplClass->addPropertyImplementation(PIDecl);
1413 }
1414
John McCall48871652010-08-21 09:40:31 +00001415 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001416}
1417
1418//===----------------------------------------------------------------------===//
1419// Helper methods.
1420//===----------------------------------------------------------------------===//
1421
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001422/// DiagnosePropertyMismatch - Compares two properties for their
1423/// attributes and types and warns on a variety of inconsistencies.
1424///
1425void
1426Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1427 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001428 const IdentifierInfo *inheritedName,
1429 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001430 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001431 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001432 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001433 SuperProperty->getPropertyAttributes();
1434
1435 // We allow readonly properties without an explicit ownership
1436 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1437 // to be overridden by a property with any explicit ownership in the subclass.
1438 if (!OverridingProtocolProperty &&
1439 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1440 ;
1441 else {
1442 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1443 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1444 Diag(Property->getLocation(), diag::warn_readonly_property)
1445 << Property->getDeclName() << inheritedName;
1446 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1447 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001448 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001449 << Property->getDeclName() << "copy" << inheritedName;
1450 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1451 unsigned CAttrRetain =
1452 (CAttr &
1453 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1454 unsigned SAttrRetain =
1455 (SAttr &
1456 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1457 bool CStrong = (CAttrRetain != 0);
1458 bool SStrong = (SAttrRetain != 0);
1459 if (CStrong != SStrong)
1460 Diag(Property->getLocation(), diag::warn_property_attribute)
1461 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1462 }
John McCall31168b02011-06-15 23:02:42 +00001463 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001464
Douglas Gregor429183e2015-12-09 22:57:32 +00001465 // Check for nonatomic; note that nonatomic is effectively
1466 // meaningless for readonly properties, so don't diagnose if the
1467 // atomic property is 'readonly'.
Douglas Gregor9dd25b72015-12-10 23:02:09 +00001468 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001469 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001470 Diag(Property->getLocation(), diag::warn_property_attribute)
1471 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001472 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1473 }
1474 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001475 Diag(Property->getLocation(), diag::warn_property_attribute)
1476 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001477 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1478 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001479
1480 QualType LHSType =
1481 Context.getCanonicalType(SuperProperty->getType());
1482 QualType RHSType =
1483 Context.getCanonicalType(Property->getType());
1484
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001485 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001486 // Do cases not handled in above.
1487 // FIXME. For future support of covariant property types, revisit this.
1488 bool IncompatibleObjC = false;
1489 QualType ConvertedType;
1490 if (!isObjCPointerConversion(RHSType, LHSType,
1491 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001492 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001493 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1494 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001495 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1496 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001497 }
1498}
1499
1500bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1501 ObjCMethodDecl *GetterMethod,
1502 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001503 if (!GetterMethod)
1504 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001505 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001506 QualType PropertyRValueType =
1507 property->getType().getNonReferenceType().getAtomicUnqualifiedType();
1508 bool compat = Context.hasSameType(PropertyRValueType, GetterType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001509 if (!compat) {
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001510 const ObjCObjectPointerType *propertyObjCPtr = nullptr;
1511 const ObjCObjectPointerType *getterObjCPtr = nullptr;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001512 if ((propertyObjCPtr =
1513 PropertyRValueType->getAs<ObjCObjectPointerType>()) &&
Douglas Gregor1cbb2892015-12-08 22:45:17 +00001514 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>()))
1515 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr);
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001516 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyRValueType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001517 != Compatible) {
Richard Smithf8812672016-12-02 22:38:31 +00001518 Diag(Loc, diag::err_property_accessor_type)
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001519 << property->getDeclName() << PropertyRValueType
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001520 << GetterMethod->getSelector() << GetterType;
1521 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1522 return true;
1523 } else {
1524 compat = true;
Akira Hatanakade6f25f2016-05-26 00:37:30 +00001525 QualType lhsType = Context.getCanonicalType(PropertyRValueType);
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001526 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1527 if (lhsType != rhsType && lhsType->isArithmeticType())
1528 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001529 }
1530 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001531
1532 if (!compat) {
1533 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1534 << property->getDeclName()
1535 << GetterMethod->getSelector();
1536 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1537 return true;
1538 }
1539
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001540 return false;
1541}
1542
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001543/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001544/// the class and its conforming protocols; but not those in its super class.
Manman Ren16a7d632016-04-12 23:01:55 +00001545static void
1546CollectImmediateProperties(ObjCContainerDecl *CDecl,
1547 ObjCContainerDecl::PropertyMap &PropMap,
1548 ObjCContainerDecl::PropertyMap &SuperPropMap,
1549 bool CollectClassPropsOnly = false,
1550 bool IncludeProtocols = true) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001551 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001552 for (auto *Prop : IDecl->properties()) {
1553 if (CollectClassPropsOnly && !Prop->isClassProperty())
1554 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001555 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1556 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001557 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001558
1559 // Collect the properties from visible extensions.
1560 for (auto *Ext : IDecl->visible_extensions())
Manman Ren16a7d632016-04-12 23:01:55 +00001561 CollectImmediateProperties(Ext, PropMap, SuperPropMap,
1562 CollectClassPropsOnly, IncludeProtocols);
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001563
Ted Kremenek204c3c52014-02-22 00:02:03 +00001564 if (IncludeProtocols) {
1565 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001566 for (auto *PI : IDecl->all_referenced_protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001567 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1568 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001569 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001570 }
1571 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Manman Ren16a7d632016-04-12 23:01:55 +00001572 for (auto *Prop : CATDecl->properties()) {
1573 if (CollectClassPropsOnly && !Prop->isClassProperty())
1574 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001575 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] =
1576 Prop;
Manman Ren16a7d632016-04-12 23:01:55 +00001577 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001578 if (IncludeProtocols) {
1579 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001580 for (auto *PI : CATDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001581 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1582 CollectClassPropsOnly);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001583 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001584 }
1585 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001586 for (auto *Prop : PDecl->properties()) {
Manman Ren16a7d632016-04-12 23:01:55 +00001587 if (CollectClassPropsOnly && !Prop->isClassProperty())
1588 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001589 ObjCPropertyDecl *PropertyFromSuper =
1590 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1591 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001592 // Exclude property for protocols which conform to class's super-class,
1593 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001594 if (!PropertyFromSuper ||
1595 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Manman Ren494ee5b2016-01-28 23:36:05 +00001596 ObjCPropertyDecl *&PropEntry =
1597 PropMap[std::make_pair(Prop->getIdentifier(),
1598 Prop->isClassProperty())];
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001599 if (!PropEntry)
1600 PropEntry = Prop;
1601 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001602 }
Manman Ren16a7d632016-04-12 23:01:55 +00001603 // Scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001604 for (auto *PI : PDecl->protocols())
Manman Ren16a7d632016-04-12 23:01:55 +00001605 CollectImmediateProperties(PI, PropMap, SuperPropMap,
1606 CollectClassPropsOnly);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001607 }
1608}
1609
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001610/// CollectSuperClassPropertyImplementations - This routine collects list of
1611/// properties to be implemented in super class(s) and also coming from their
1612/// conforming protocols.
1613static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001614 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001615 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001616 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001617 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001618 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001619 SDecl = SDecl->getSuperClass();
1620 }
1621 }
1622}
1623
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001624/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1625/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1626/// declared in class 'IFace'.
1627bool
1628Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1629 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1630 if (!IV->getSynthesize())
1631 return false;
1632 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1633 Method->isInstanceMethod());
1634 if (!IMD || !IMD->isPropertyAccessor())
1635 return false;
1636
1637 // look up a property declaration whose one of its accessors is implemented
1638 // by this method.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001639 for (const auto *Property : IFace->instance_properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001640 if ((Property->getGetterName() == IMD->getSelector() ||
1641 Property->getSetterName() == IMD->getSelector()) &&
1642 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001643 return true;
1644 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001645 // Also look up property declaration in class extension whose one of its
1646 // accessors is implemented by this method.
1647 for (const auto *Ext : IFace->known_extensions())
Manman Rena7a8b1f2016-01-26 18:05:23 +00001648 for (const auto *Property : Ext->instance_properties())
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001649 if ((Property->getGetterName() == IMD->getSelector() ||
1650 Property->getSetterName() == IMD->getSelector()) &&
1651 (Property->getPropertyIvarDecl() == IV))
1652 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001653 return false;
1654}
1655
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001656static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1657 ObjCPropertyDecl *Prop) {
1658 bool SuperClassImplementsGetter = false;
1659 bool SuperClassImplementsSetter = false;
1660 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1661 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001662
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001663 while (IDecl->getSuperClass()) {
1664 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1665 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1666 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001667
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001668 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1669 SuperClassImplementsSetter = true;
1670 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1671 return true;
1672 IDecl = IDecl->getSuperClass();
1673 }
1674 return false;
1675}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001676
James Dennett2a4d13c2012-06-15 07:13:21 +00001677/// \brief Default synthesizes all properties which must be synthesized
1678/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001679void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1680 ObjCInterfaceDecl *IDecl) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001681 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001682 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1683 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001684 if (PropMap.empty())
1685 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001686 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001687 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1688
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001689 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1690 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001691 // Is there a matching property synthesize/dynamic?
1692 if (Prop->isInvalidDecl() ||
Manman Ren494ee5b2016-01-28 23:36:05 +00001693 Prop->isClassProperty() ||
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001694 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1695 continue;
1696 // Property may have been synthesized by user.
Manman Ren5b786402016-01-28 18:49:28 +00001697 if (IMPDecl->FindPropertyImplDecl(
1698 Prop->getIdentifier(), Prop->getQueryKind()))
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001699 continue;
1700 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1701 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1702 continue;
1703 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1704 continue;
1705 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001706 if (ObjCPropertyImplDecl *PID =
1707 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001708 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1709 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001710 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001711 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001712 continue;
1713 }
Manman Ren494ee5b2016-01-28 23:36:05 +00001714 ObjCPropertyDecl *PropInSuperClass =
1715 SuperPropMap[std::make_pair(Prop->getIdentifier(),
1716 Prop->isClassProperty())];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001717 if (ObjCProtocolDecl *Proto =
1718 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001719 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001720 // Suppress the warning if class's superclass implements property's
1721 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001722 // Or, if property is going to be implemented in its super class.
1723 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001724 Diag(IMPDecl->getLocation(),
1725 diag::warn_auto_synthesizing_protocol_property)
1726 << Prop << Proto;
1727 Diag(Prop->getLocation(), diag::note_property_declare);
1728 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001729 continue;
1730 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001731 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001732 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001733 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1734 (PropInSuperClass->getPropertyAttributes() &
1735 ObjCPropertyDecl::OBJC_PR_readonly) &&
1736 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1737 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1738 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1739 << Prop->getIdentifier();
1740 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1741 }
1742 else {
1743 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1744 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001745 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001746 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1747 }
1748 continue;
1749 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001750 // We use invalid SourceLocations for the synthesized ivars since they
1751 // aren't really synthesized at a particular location; they just exist.
1752 // Saying that they are located at the @implementation isn't really going
1753 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001754 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1755 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1756 true,
1757 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001758 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Manman Ren5b786402016-01-28 18:49:28 +00001759 Prop->getLocation(), Prop->getQueryKind()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001760 if (PIDecl) {
1761 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001762 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001763 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001764 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001765}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001766
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001767void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001768 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001769 return;
1770 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1771 if (!IC)
1772 return;
1773 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001774 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001775 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001776}
1777
Manman Ren08ce7342016-05-18 18:12:34 +00001778static void DiagnoseUnimplementedAccessor(
1779 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method,
1780 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C,
1781 ObjCPropertyDecl *Prop,
1782 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) {
1783 // Check to see if we have a corresponding selector in SMap and with the
1784 // right method type.
1785 auto I = std::find_if(SMap.begin(), SMap.end(),
1786 [&](const ObjCMethodDecl *x) {
1787 return x->getSelector() == Method &&
1788 x->isClassMethod() == Prop->isClassProperty();
1789 });
Ted Kremenek7e812952014-02-21 19:41:30 +00001790 // When reporting on missing property setter/getter implementation in
1791 // categories, do not report when they are declared in primary class,
1792 // class's protocol, or one of it super classes. This is because,
1793 // the class is going to implement them.
Manman Ren08ce7342016-05-18 18:12:34 +00001794 if (I == SMap.end() &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001795 (PrimaryClass == nullptr ||
Manman Rend36f7d52016-01-27 20:10:32 +00001796 !PrimaryClass->lookupPropertyAccessor(Method, C,
1797 Prop->isClassProperty()))) {
Manman Ren16a7d632016-04-12 23:01:55 +00001798 unsigned diag =
1799 isa<ObjCCategoryDecl>(CDecl)
1800 ? (Prop->isClassProperty()
1801 ? diag::warn_impl_required_in_category_for_class_property
1802 : diag::warn_setter_getter_impl_required_in_category)
1803 : (Prop->isClassProperty()
1804 ? diag::warn_impl_required_for_class_property
1805 : diag::warn_setter_getter_impl_required);
1806 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method;
1807 S.Diag(Prop->getLocation(), diag::note_property_declare);
1808 if (S.LangOpts.ObjCDefaultSynthProperties &&
1809 S.LangOpts.ObjCRuntime.isNonFragile())
1810 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1811 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1812 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1813 }
Ted Kremenek7e812952014-02-21 19:41:30 +00001814}
1815
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001816void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001817 ObjCContainerDecl *CDecl,
1818 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001819 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001820 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1821
Manman Ren16a7d632016-04-12 23:01:55 +00001822 // Since we don't synthesize class properties, we should emit diagnose even
1823 // if SynthesizeProperties is true.
1824 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1825 // Gather properties which need not be implemented in this class
1826 // or category.
1827 if (!IDecl)
1828 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1829 // For categories, no need to implement properties declared in
1830 // its primary class (and its super classes) if property is
1831 // declared in one of those containers.
1832 if ((IDecl = C->getClassInterface())) {
1833 ObjCInterfaceDecl::PropertyDeclOrder PO;
1834 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001835 }
Manman Ren16a7d632016-04-12 23:01:55 +00001836 }
1837 if (IDecl)
1838 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001839
Manman Ren16a7d632016-04-12 23:01:55 +00001840 // When SynthesizeProperties is true, we only check class properties.
1841 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap,
1842 SynthesizeProperties/*CollectClassPropsOnly*/);
Ted Kremenek348e88c2014-02-21 19:41:34 +00001843
Ted Kremenek38882022014-02-21 19:41:39 +00001844 // Scan the @interface to see if any of the protocols it adopts
1845 // require an explicit implementation, via attribute
1846 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001847 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001848 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001849
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001850 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001851 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1852 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001853 // Lazily construct a set of all the properties in the @interface
1854 // of the class, without looking at the superclass. We cannot
1855 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001856 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001857 // as scans the adopted protocols. This work only triggers for protocols
1858 // with the attribute, which is very rare, and only occurs when
1859 // analyzing the @implementation.
1860 if (!LazyMap) {
1861 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1862 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1863 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
Manman Ren16a7d632016-04-12 23:01:55 +00001864 /* CollectClassPropsOnly */ false,
Ted Kremenek204c3c52014-02-22 00:02:03 +00001865 /* IncludeProtocols */ false);
1866 }
Ted Kremenek38882022014-02-21 19:41:39 +00001867 // Add the properties of 'PDecl' to the list of properties that
1868 // need to be implemented.
Manman Ren494ee5b2016-01-28 23:36:05 +00001869 for (auto *PropDecl : PDecl->properties()) {
1870 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(),
1871 PropDecl->isClassProperty())])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001872 continue;
Manman Ren494ee5b2016-01-28 23:36:05 +00001873 PropMap[std::make_pair(PropDecl->getIdentifier(),
1874 PropDecl->isClassProperty())] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001875 }
1876 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001877 }
Ted Kremenek38882022014-02-21 19:41:39 +00001878
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001879 if (PropMap.empty())
1880 return;
1881
1882 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001883 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001884 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001885
Manman Ren08ce7342016-05-18 18:12:34 +00001886 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001887 // Collect property accessors implemented in current implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001888 for (const auto *I : IMPDecl->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00001889 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001890
1891 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001892 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001893 if (C && !C->IsClassExtension())
1894 if ((PrimaryClass = C->getClassInterface()))
1895 // Report unimplemented properties in the category as well.
1896 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1897 // When reporting on missing setter/getters, do not report when
1898 // setter/getter is implemented in category's primary class
1899 // implementation.
Manman Ren494ee5b2016-01-28 23:36:05 +00001900 for (const auto *I : IMP->methods())
Manman Ren08ce7342016-05-18 18:12:34 +00001901 InsMap.insert(I);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001902 }
1903
Anna Zaks673d76b2012-10-18 19:17:53 +00001904 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001905 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1906 ObjCPropertyDecl *Prop = P->second;
Manman Ren16a7d632016-04-12 23:01:55 +00001907 // Is there a matching property synthesize/dynamic?
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001908 if (Prop->isInvalidDecl() ||
1909 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001910 PropImplMap.count(Prop) ||
1911 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001912 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001913
1914 // Diagnose unimplemented getters and setters.
1915 DiagnoseUnimplementedAccessor(*this,
1916 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1917 if (!Prop->isReadOnly())
1918 DiagnoseUnimplementedAccessor(*this,
1919 PrimaryClass, Prop->getSetterName(),
1920 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001921 }
1922}
1923
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001924void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001925 for (const auto *propertyImpl : impDecl->property_impls()) {
1926 const auto *property = propertyImpl->getPropertyDecl();
1927
1928 // Warn about null_resettable properties with synthesized setters,
1929 // because the setter won't properly handle nil.
1930 if (propertyImpl->getPropertyImplementation()
1931 == ObjCPropertyImplDecl::Synthesize &&
1932 (property->getPropertyAttributes() &
1933 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1934 property->getGetterMethodDecl() &&
1935 property->getSetterMethodDecl()) {
1936 auto *getterMethod = property->getGetterMethodDecl();
1937 auto *setterMethod = property->getSetterMethodDecl();
1938 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1939 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1940 SourceLocation loc = propertyImpl->getLocation();
1941 if (loc.isInvalid())
1942 loc = impDecl->getLocStart();
1943
1944 Diag(loc, diag::warn_null_resettable_setter)
1945 << setterMethod->getSelector() << property->getDeclName();
1946 }
1947 }
1948 }
1949}
1950
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001951void
1952Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001953 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001954 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001955 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001956 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001957 ObjCContainerDecl::PropertyMap PM;
Manman Ren494ee5b2016-01-28 23:36:05 +00001958 for (auto *Prop : IDecl->properties())
1959 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001960 for (const auto *Ext : IDecl->known_extensions())
Manman Ren494ee5b2016-01-28 23:36:05 +00001961 for (auto *Prop : Ext->properties())
1962 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001963
Manman Renefe1bac2016-01-27 20:00:32 +00001964 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1965 I != E; ++I) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001966 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001967 ObjCMethodDecl *GetterMethod = nullptr;
1968 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001969 bool LookedUpGetterSetter = false;
1970
Bill Wendling44426052012-12-20 19:22:21 +00001971 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001972 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001973
John McCall43192862011-09-13 18:31:23 +00001974 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1975 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Manman Rend36f7d52016-01-27 20:10:32 +00001976 GetterMethod = Property->isClassProperty() ?
1977 IMPDecl->getClassMethod(Property->getGetterName()) :
1978 IMPDecl->getInstanceMethod(Property->getGetterName());
1979 SetterMethod = Property->isClassProperty() ?
1980 IMPDecl->getClassMethod(Property->getSetterName()) :
1981 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001982 LookedUpGetterSetter = true;
1983 if (GetterMethod) {
1984 Diag(GetterMethod->getLocation(),
1985 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001986 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001987 Diag(Property->getLocation(), diag::note_property_declare);
1988 }
1989 if (SetterMethod) {
1990 Diag(SetterMethod->getLocation(),
1991 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001992 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001993 Diag(Property->getLocation(), diag::note_property_declare);
1994 }
1995 }
1996
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001997 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001998 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1999 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002000 continue;
Manman Ren5b786402016-01-28 18:49:28 +00002001 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl(
2002 Property->getIdentifier(), Property->getQueryKind())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002003 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
2004 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002005 if (!LookedUpGetterSetter) {
Manman Rend36f7d52016-01-27 20:10:32 +00002006 GetterMethod = Property->isClassProperty() ?
2007 IMPDecl->getClassMethod(Property->getGetterName()) :
2008 IMPDecl->getInstanceMethod(Property->getGetterName());
2009 SetterMethod = Property->isClassProperty() ?
2010 IMPDecl->getClassMethod(Property->getSetterName()) :
2011 IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00002012 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002013 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
2014 SourceLocation MethodLoc =
2015 (GetterMethod ? GetterMethod->getLocation()
2016 : SetterMethod->getLocation());
2017 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00002018 << Property->getIdentifier() << (GetterMethod != nullptr)
2019 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002020 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002021 if (Property->getLParenLoc().isValid() &&
2022 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002023 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002024 SourceLocation AfterLParen =
2025 getLocForEndOfToken(Property->getLParenLoc());
2026 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
2027 : "nonatomic";
2028 Diag(Property->getLocation(),
2029 diag::note_atomic_property_fixup_suggest)
2030 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
2031 } else if (Property->getLParenLoc().isInvalid()) {
2032 //@property id etc.
2033 SourceLocation startLoc =
2034 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
2035 Diag(Property->getLocation(),
2036 diag::note_atomic_property_fixup_suggest)
2037 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00002038 }
2039 else
2040 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002041 Diag(Property->getLocation(), diag::note_property_declare);
2042 }
2043 }
2044 }
2045}
2046
John McCall31168b02011-06-15 23:02:42 +00002047void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002048 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00002049 return;
2050
Aaron Ballmand85eff42014-03-14 15:02:45 +00002051 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00002052 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002053 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
Manman Rend36f7d52016-01-27 20:10:32 +00002054 !PD->isClassProperty() &&
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002055 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00002056 ObjCMethodDecl *method = PD->getGetterMethodDecl();
2057 if (!method)
2058 continue;
2059 ObjCMethodFamily family = method->getMethodFamily();
2060 if (family == OMF_alloc || family == OMF_copy ||
2061 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002062 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002063 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00002064 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00002065 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00002066
2067 // Look for a getter explicitly declared alongside the property.
2068 // If we find one, use its location for the note.
2069 SourceLocation noteLoc = PD->getLocation();
2070 SourceLocation fixItLoc;
2071 for (auto *getterRedecl : method->redecls()) {
2072 if (getterRedecl->isImplicit())
2073 continue;
2074 if (getterRedecl->getDeclContext() != PD->getDeclContext())
2075 continue;
2076 noteLoc = getterRedecl->getLocation();
2077 fixItLoc = getterRedecl->getLocEnd();
2078 }
2079
2080 Preprocessor &PP = getPreprocessor();
2081 TokenValue tokens[] = {
2082 tok::kw___attribute, tok::l_paren, tok::l_paren,
2083 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
2084 PP.getIdentifierInfo("none"), tok::r_paren,
2085 tok::r_paren, tok::r_paren
2086 };
2087 StringRef spelling = "__attribute__((objc_method_family(none)))";
2088 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
2089 if (!macroName.empty())
2090 spelling = macroName;
2091
2092 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
2093 << method->getDeclName() << spelling;
2094 if (fixItLoc.isValid()) {
2095 SmallString<64> fixItText(" ");
2096 fixItText += spelling;
2097 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
2098 }
John McCall31168b02011-06-15 23:02:42 +00002099 }
2100 }
2101 }
2102}
2103
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002104void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002105 const ObjCImplementationDecl *ImplD,
2106 const ObjCInterfaceDecl *IFD) {
2107 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002108 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
2109 if (!SuperD)
2110 return;
2111
2112 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002113 for (const auto *I : ImplD->instance_methods())
2114 if (I->getMethodFamily() == OMF_init)
2115 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002116
2117 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
2118 SuperD->getDesignatedInitializers(DesignatedInits);
2119 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
2120 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
2121 const ObjCMethodDecl *MD = *I;
2122 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00002123 bool Ignore = false;
2124 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
2125 Ignore = IMD->isUnavailable();
2126 }
2127 if (!Ignore) {
2128 Diag(ImplD->getLocation(),
2129 diag::warn_objc_implementation_missing_designated_init_override)
2130 << MD->getSelector();
2131 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2132 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002133 }
2134 }
2135}
2136
John McCallad31b5f2010-11-10 07:01:40 +00002137/// AddPropertyAttrs - Propagates attributes from a property to the
2138/// implicitly-declared getter or setter for that property.
2139static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2140 ObjCPropertyDecl *Property) {
2141 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002142 for (const auto *A : Property->attrs()) {
2143 if (isa<DeprecatedAttr>(A) ||
2144 isa<UnavailableAttr>(A) ||
2145 isa<AvailabilityAttr>(A))
2146 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002147 }
John McCallad31b5f2010-11-10 07:01:40 +00002148}
2149
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002150/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2151/// have the property type and issue diagnostics if they don't.
2152/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00002153/// appropriate lookup tables.
2154void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002155 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00002156 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002157 if (CD->isInvalidDecl())
2158 return;
2159
Manman Rend36f7d52016-01-27 20:10:32 +00002160 bool IsClassProperty = property->isClassProperty();
2161 GetterMethod = IsClassProperty ?
2162 CD->getClassMethod(property->getGetterName()) :
2163 CD->getInstanceMethod(property->getGetterName());
2164
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002165 // if setter or getter is not found in class extension, it might be
2166 // in the primary class.
2167 if (!GetterMethod)
2168 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2169 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002170 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2171 getClassMethod(property->getGetterName()) :
2172 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002173 getInstanceMethod(property->getGetterName());
2174
Manman Rend36f7d52016-01-27 20:10:32 +00002175 SetterMethod = IsClassProperty ?
2176 CD->getClassMethod(property->getSetterName()) :
2177 CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002178 if (!SetterMethod)
2179 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
2180 if (CatDecl->IsClassExtension())
Manman Rend36f7d52016-01-27 20:10:32 +00002181 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()->
2182 getClassMethod(property->getSetterName()) :
2183 CatDecl->getClassInterface()->
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002184 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002185 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2186 property->getLocation());
2187
2188 if (SetterMethod) {
2189 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2190 property->getPropertyAttributes();
2191 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002192 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2193 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002194 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2195 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002196 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002197 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2198 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002199 Diag(property->getLocation(),
2200 diag::warn_accessor_property_type_mismatch)
2201 << property->getDeclName()
2202 << SetterMethod->getSelector();
2203 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2204 }
2205 }
2206
2207 // Synthesize getter/setter methods if none exist.
2208 // Find the default getter and if one not found, add one.
2209 // FIXME: The synthesized property we set here is misleading. We almost always
2210 // synthesize these methods unless the user explicitly provided prototypes
2211 // (which is odd, but allowed). Sema should be typechecking that the
2212 // declarations jive in that situation (which it is not currently).
2213 if (!GetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002214 // No instance/class method of same name as property getter name was found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002215 // Declare a getter method and add it to the list of methods
2216 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002217 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002218
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002219 // The getter returns the declared property type with all qualifiers
2220 // removed.
2221 QualType resultTy = property->getType().getAtomicUnqualifiedType();
2222
Douglas Gregor849ebc22015-06-19 18:14:46 +00002223 // If the property is null_resettable, the getter returns nonnull.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002224 if (property->getPropertyAttributes() &
2225 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2226 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002227 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002228 if (*nullability == NullabilityKind::Unspecified)
2229 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2230 modifiedTy, modifiedTy);
2231 }
2232 }
2233
Ted Kremenek2f075632010-09-21 20:52:59 +00002234 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2235 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002236 resultTy, nullptr, CD,
Manman Rend36f7d52016-01-27 20:10:32 +00002237 !IsClassProperty, /*isVariadic=*/false,
Craig Topperc3ec1492014-05-26 06:22:03 +00002238 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002239 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002240 (property->getPropertyImplementation() ==
2241 ObjCPropertyDecl::Optional) ?
2242 ObjCMethodDecl::Optional :
2243 ObjCMethodDecl::Required);
2244 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002245
2246 AddPropertyAttrs(*this, GetterMethod, property);
2247
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002248 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002249 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2250 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002251
2252 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2253 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002254 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002255
2256 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002257 GetterMethod->addAttr(
2258 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2259 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002260
2261 if (getLangOpts().ObjCAutoRefCount)
2262 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002263 } else
2264 // A user declared getter will be synthesize when @synthesize of
2265 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002266 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002267 property->setGetterMethodDecl(GetterMethod);
2268
2269 // Skip setter if property is read-only.
2270 if (!property->isReadOnly()) {
2271 // Find the default setter and if one not found, add one.
2272 if (!SetterMethod) {
Manman Rend36f7d52016-01-27 20:10:32 +00002273 // No instance/class method of same name as property setter name was
2274 // found.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002275 // Declare a setter method and add it to the list of methods
2276 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002277 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002278
2279 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002280 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002281 property->getSetterName(), Context.VoidTy,
Manman Rend36f7d52016-01-27 20:10:32 +00002282 nullptr, CD, !IsClassProperty,
Craig Topperc3ec1492014-05-26 06:22:03 +00002283 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002284 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002285 /*isImplicitlyDeclared=*/true,
2286 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002287 (property->getPropertyImplementation() ==
2288 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002289 ObjCMethodDecl::Optional :
2290 ObjCMethodDecl::Required);
2291
Akira Hatanakade6f25f2016-05-26 00:37:30 +00002292 // Remove all qualifiers from the setter's parameter type.
2293 QualType paramTy =
2294 property->getType().getUnqualifiedType().getAtomicUnqualifiedType();
2295
Douglas Gregor849ebc22015-06-19 18:14:46 +00002296 // If the property is null_resettable, the setter accepts a
2297 // nullable value.
Douglas Gregor849ebc22015-06-19 18:14:46 +00002298 if (property->getPropertyAttributes() &
2299 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2300 QualType modifiedTy = paramTy;
2301 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2302 if (*nullability == NullabilityKind::Unspecified)
2303 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2304 modifiedTy, modifiedTy);
2305 }
2306 }
2307
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002308 // Invent the arguments for the setter. We don't bother making a
2309 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002310 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2311 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002312 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002313 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002314 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002315 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002316 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002317 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002318
2319 AddPropertyAttrs(*this, SetterMethod, property);
2320
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002321 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002322 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002323 SetterMethod->addAttr(
2324 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2325 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002326 // It's possible for the user to have set a very odd custom
2327 // setter selector that causes it to have a method family.
2328 if (getLangOpts().ObjCAutoRefCount)
2329 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002330 } else
2331 // A user declared setter will be synthesize when @synthesize of
2332 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002333 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002334 property->setSetterMethodDecl(SetterMethod);
2335 }
2336 // Add any synthesized methods to the global pool. This allows us to
2337 // handle the following, which is supported by GCC (and part of the design).
2338 //
2339 // @interface Foo
2340 // @property double bar;
2341 // @end
2342 //
2343 // void thisIsUnfortunate() {
2344 // id foo;
2345 // double bar = [foo bar];
2346 // }
2347 //
Manman Rend36f7d52016-01-27 20:10:32 +00002348 if (!IsClassProperty) {
2349 if (GetterMethod)
2350 AddInstanceMethodToGlobalPool(GetterMethod);
2351 if (SetterMethod)
2352 AddInstanceMethodToGlobalPool(SetterMethod);
Manman Ren15325f82016-03-23 21:39:31 +00002353 } else {
2354 if (GetterMethod)
2355 AddFactoryMethodToGlobalPool(GetterMethod);
2356 if (SetterMethod)
2357 AddFactoryMethodToGlobalPool(SetterMethod);
Manman Rend36f7d52016-01-27 20:10:32 +00002358 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002359
2360 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2361 if (!CurrentClass) {
2362 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2363 CurrentClass = Cat->getClassInterface();
2364 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2365 CurrentClass = Impl->getClassInterface();
2366 }
2367 if (GetterMethod)
2368 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2369 if (SetterMethod)
2370 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002371}
2372
John McCall48871652010-08-21 09:40:31 +00002373void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002374 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002375 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002376 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002377 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002378 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002379 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002380
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002381 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2382 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2383 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2384 << "readonly" << "readwrite";
2385
2386 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2387 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002388
2389 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002390 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002391 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2392 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002393 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002394 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002395 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2396 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2397 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002398 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002399 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002400 }
2401
2402 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002403 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2404 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002405 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2406 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002407 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002408 }
Bill Wendling44426052012-12-20 19:22:21 +00002409 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002410 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2411 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002412 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002413 }
Bill Wendling44426052012-12-20 19:22:21 +00002414 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002415 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2416 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002417 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002418 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002419 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002420 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002421 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2422 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002423 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002424 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002425 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002426 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002427 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2428 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002429 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2430 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002431 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002432 }
Bill Wendling44426052012-12-20 19:22:21 +00002433 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002434 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2435 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002436 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002437 }
Bill Wendling44426052012-12-20 19:22:21 +00002438 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002439 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2440 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002441 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002442 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002443 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002444 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002445 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2446 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002447 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002448 }
Bill Wendling44426052012-12-20 19:22:21 +00002449 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2450 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002451 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2452 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002453 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002454 }
Bill Wendling44426052012-12-20 19:22:21 +00002455 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002456 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2457 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002458 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002459 }
Bill Wendling44426052012-12-20 19:22:21 +00002460 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002461 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2462 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002463 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002464 }
2465 }
Bill Wendling44426052012-12-20 19:22:21 +00002466 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2467 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002468 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2469 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002470 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002471 }
Bill Wendling44426052012-12-20 19:22:21 +00002472 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2473 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002474 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2475 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002476 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002477 }
2478
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002479 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002480 // 'weak' and 'nonnull' are mutually exclusive.
2481 if (auto nullability = PropertyTy->getNullability(Context)) {
2482 if (*nullability == NullabilityKind::NonNull)
2483 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2484 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002485 }
2486 }
2487
Bill Wendling44426052012-12-20 19:22:21 +00002488 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2489 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002490 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2491 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002492 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002493 }
2494
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002495 // Warn if user supplied no assignment attribute, property is
2496 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002497 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2498 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2499 // do nothing
2500 } else if (getLangOpts().ObjCAutoRefCount) {
2501 // With arc, @property definitions should default to strong when
2502 // not specified.
2503 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2504 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002505 bool isAnyClassTy =
2506 (PropertyTy->isObjCClassType() ||
2507 PropertyTy->isObjCQualifiedClassType());
2508 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2509 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002510 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002511 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002512 else if (propertyInPrimaryClass) {
2513 // Don't issue warning on property with no life time in class
2514 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002515 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002516 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002517 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002518
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002519 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002520 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002521 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002522 }
John McCallb61e14e2015-10-27 04:54:50 +00002523 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002524
2525 // FIXME: Implement warning dependent on NSCopying being
2526 // implemented. See also:
2527 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2528 // (please trim this list while you are at it).
2529 }
2530
Bill Wendling44426052012-12-20 19:22:21 +00002531 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2532 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002533 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002534 && PropertyTy->isBlockPointerType())
2535 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002536 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2537 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2538 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002539 PropertyTy->isBlockPointerType())
2540 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002541
Bill Wendling44426052012-12-20 19:22:21 +00002542 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2543 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002544 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002545}