blob: 60067761333963d4618e8dfb966f32e0af1f9f45 [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
156static unsigned getOwnershipRule(unsigned attr) {
157 return attr & (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}
164
John McCall48871652010-08-21 09:40:31 +0000165Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000166 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000167 FieldDeclarator &FD,
168 ObjCDeclSpec &ODS,
169 Selector GetterSel,
170 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000171 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000172 tok::ObjCKeywordKind MethodImplKind,
173 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000174 unsigned Attributes = ODS.getPropertyAttributes();
Douglas Gregor2a20bd12015-06-19 18:25:57 +0000175 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
John McCall31168b02011-06-15 23:02:42 +0000176 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
177 QualType T = TSI->getType();
John McCallb61e14e2015-10-27 04:54:50 +0000178 if (!getOwnershipRule(Attributes)) {
179 Attributes |= deducePropertyOwnershipFromType(*this, T);
180 }
Bill Wendling44426052012-12-20 19:22:21 +0000181 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000182 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000183 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
John McCallb61e14e2015-10-27 04:54:50 +0000184
185 // Property defaults to 'assign' if it is readwrite, unless this is ARC
186 // and the type is retainable.
187 bool isAssign;
188 if (Attributes & (ObjCDeclSpec::DQ_PR_assign |
189 ObjCDeclSpec::DQ_PR_unsafe_unretained)) {
190 isAssign = true;
191 } else if (getOwnershipRule(Attributes) || !isReadWrite) {
192 isAssign = false;
193 } else {
194 isAssign = (!getLangOpts().ObjCAutoRefCount ||
195 !T->isObjCRetainableType());
196 }
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000197
Douglas Gregor90d34422013-01-21 19:05:22 +0000198 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000199 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000200 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000202 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000203 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000204 FD, GetterSel, SetterSel,
205 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000206 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000207 ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000208 isOverridingProperty, T, TSI,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000209 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000210 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000211 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000212 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000213 }
214
215 if (!Res) {
216 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
217 GetterSel, SetterSel, isAssign, isReadWrite,
218 Attributes, ODS.getPropertyAttributes(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000219 T, TSI, 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;
308
309 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
310}
311
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000312static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000313 SourceLocation LParenLoc, SourceLocation &Loc) {
314 if (LParenLoc.isMacroID())
315 return false;
316
317 SourceManager &SM = Context.getSourceManager();
318 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
319 // Try to load the file buffer.
320 bool invalidTemp = false;
321 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
322 if (invalidTemp)
323 return false;
324 const char *tokenBegin = file.data() + locInfo.second;
325
326 // Lex from the start of the given location.
327 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
328 Context.getLangOpts(),
329 file.begin(), tokenBegin, file.end());
330 Token Tok;
331 do {
332 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000333 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000334 Loc = Tok.getLocation();
335 return true;
336 }
337 } while (Tok.isNot(tok::r_paren));
338 return false;
339
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000340}
341
Douglas Gregor90d34422013-01-21 19:05:22 +0000342ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000343Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000344 SourceLocation AtLoc,
345 SourceLocation LParenLoc,
346 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000347 Selector GetterSel, Selector SetterSel,
348 const bool isAssign,
349 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000350 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000351 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000352 bool *isOverridingProperty,
Douglas Gregor813a0662015-06-19 18:14:38 +0000353 QualType T,
354 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000355 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000356 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000357 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000358 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000359 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000360 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
361
Ted Kremenek959e8302010-03-12 02:31:10 +0000362 // We need to look in the @interface to see if the @property was
363 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000364 if (!CCPrimary) {
365 Diag(CDecl->getLocation(), diag::err_continuation_class);
366 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000367 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000368 }
369
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000370 // Find the property in the extended class's primary class or
371 // extensions.
Ted Kremenek959e8302010-03-12 02:31:10 +0000372 ObjCPropertyDecl *PIDecl =
373 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
374
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000375 // If we found a property in an extension, complain.
376 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) {
377 Diag(AtLoc, diag::err_duplicate_property);
378 Diag(PIDecl->getLocation(), diag::note_property_declare);
379 return nullptr;
380 }
Ted Kremenek959e8302010-03-12 02:31:10 +0000381
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000382 // Create a new ObjCPropertyDecl with the DeclContext being
383 // the class extension.
384 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc,
385 FD, GetterSel, SetterSel,
386 isAssign, isReadWrite,
387 Attributes, AttributesAsWritten,
388 T, TSI, MethodImplKind, DC);
389
390 // If there was no declaration of a property with the same name in
391 // the primary class, we're done.
392 if (!PIDecl) {
Douglas Gregore17765e2015-11-03 17:02:34 +0000393 ProcessPropertyDecl(PDecl);
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000394 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000395 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000396
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000397 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
398 bool IncompatibleObjC = false;
399 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000400 // Relax the strict type matching for property type in continuation class.
401 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000402 // as it narrows the object type in its primary class property. Note that
403 // this conversion is safe only because the wider type is for a 'readonly'
404 // property in primary class and 'narrowed' type for a 'readwrite' property
405 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000406 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
407 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
408 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
409 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
410 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000411 ConvertedType, IncompatibleObjC))
412 || IncompatibleObjC) {
413 Diag(AtLoc,
414 diag::err_type_mismatch_continuation_class) << PDecl->getType();
415 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000416 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000417 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000418 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000419
420 // A readonly property declared in the primary class can be refined
421 // by adding a rewrite property within an extension.
422 // Anything else is an error.
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000423 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000424 if (!(isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly))) {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000425 // Tailor the diagnostics for the common case where a readwrite
426 // property is declared both in the @interface and the continuation.
427 // This is a common error where the user often intended the original
428 // declaration to be readonly.
429 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000430 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000431 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
432 ? diag::err_use_continuation_class_redeclaration_readwrite
433 : diag::err_use_continuation_class;
434 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 << CCPrimary->getDeclName();
436 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000437 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000438 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000439
440 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
441 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
442 PIkind |= deducePropertyOwnershipFromType(*this, PIDecl->getType());
443 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
444 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
445 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
446 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
447 Diag(AtLoc, diag::warn_property_attr_mismatch);
448 Diag(PIDecl->getLocation(), diag::note_property_declare);
449 } else if (getLangOpts().ObjCAutoRefCount) {
450 QualType PrimaryPropertyQT =
451 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
452 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
453 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
454 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
455 PrimaryPropertyQT.getObjCLifetime();
456 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
457 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
458 !PropertyIsWeak) {
459 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
460 Diag(PIDecl->getLocation(), diag::note_property_declare);
461 }
462 }
463 }
464
465 // Check that atomicity of property in class extension matches the previous
466 // declaration.
467 unsigned PDeclAtomicity =
468 PDecl->getPropertyAttributes() & (ObjCDeclSpec::DQ_PR_atomic | ObjCDeclSpec::DQ_PR_nonatomic);
469 unsigned PIDeclAtomicity =
470 PIDecl->getPropertyAttributes() & (ObjCDeclSpec::DQ_PR_atomic | ObjCDeclSpec::DQ_PR_nonatomic);
471 if (PDeclAtomicity != PIDeclAtomicity) {
472 bool PDeclAtomic = (!PDeclAtomicity || PDeclAtomicity & ObjCDeclSpec::DQ_PR_atomic);
473 bool PIDeclAtomic = (!PIDeclAtomicity || PIDeclAtomicity & ObjCDeclSpec::DQ_PR_atomic);
474 if (PDeclAtomic != PIDeclAtomic) {
475 Diag(PDecl->getLocation(), diag::warn_property_attribute)
476 << PDecl->getDeclName() << "atomic"
477 << cast<ObjCContainerDecl>(PIDecl->getDeclContext())->getName();
478 Diag(PIDecl->getLocation(), diag::note_property_declare);
479 }
480 }
481
Ted Kremenek959e8302010-03-12 02:31:10 +0000482 *isOverridingProperty = true;
Douglas Gregoracf4fd32015-11-03 01:15:46 +0000483
Douglas Gregore17765e2015-11-03 17:02:34 +0000484 // Make sure getter/setter are appropriately synthesized.
485 ProcessPropertyDecl(PDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000486 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000487}
488
489ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
490 ObjCContainerDecl *CDecl,
491 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000492 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000493 FieldDeclarator &FD,
494 Selector GetterSel,
495 Selector SetterSel,
496 const bool isAssign,
497 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000498 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000499 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000500 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000501 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000502 tok::ObjCKeywordKind MethodImplKind,
503 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000504 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000505
506 // Issue a warning if property is 'assign' as default and its object, which is
507 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000508 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000509 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000510 if (const ObjCObjectPointerType *ObjPtrTy =
511 T->getAs<ObjCObjectPointerType>()) {
512 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
513 if (IDecl)
514 if (ObjCProtocolDecl* PNSCopying =
515 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
516 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
517 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000518 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000519
520 if (T->isObjCObjectType()) {
521 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000522 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000523 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
524 << FixItHint::CreateInsertion(StarLoc, "*");
525 T = Context.getObjCObjectPointerType(T);
526 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
527 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
528 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000529
Ted Kremenek959e8302010-03-12 02:31:10 +0000530 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000531 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
532 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000533 PropertyId, AtLoc,
534 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000535
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000536 if (ObjCPropertyDecl *prevDecl =
537 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000538 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000539 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000540 PDecl->setInvalidDecl();
541 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000542 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000543 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000544 if (lexicalDC)
545 PDecl->setLexicalDeclContext(lexicalDC);
546 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000547
548 if (T->isArrayType() || T->isFunctionType()) {
549 Diag(AtLoc, diag::err_property_type) << T;
550 PDecl->setInvalidDecl();
551 }
552
553 ProcessDeclAttributes(S, PDecl, FD.D);
554
555 // Regardless of setter/getter attribute, we save the default getter/setter
556 // selector names in anticipation of declaration of setter/getter methods.
557 PDecl->setGetterName(GetterSel);
558 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000559 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000560 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000561
Bill Wendling44426052012-12-20 19:22:21 +0000562 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000563 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
564
Bill Wendling44426052012-12-20 19:22:21 +0000565 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000566 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
567
Bill Wendling44426052012-12-20 19:22:21 +0000568 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000569 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
570
571 if (isReadWrite)
572 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
573
Bill Wendling44426052012-12-20 19:22:21 +0000574 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000575 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
576
Bill Wendling44426052012-12-20 19:22:21 +0000577 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
579
Bill Wendling44426052012-12-20 19:22:21 +0000580 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
582
Bill Wendling44426052012-12-20 19:22:21 +0000583 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
585
Bill Wendling44426052012-12-20 19:22:21 +0000586 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
588
Ted Kremenekac597f32010-03-12 00:46:40 +0000589 if (isAssign)
590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
591
John McCall43192862011-09-13 18:31:23 +0000592 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000593 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000595 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000597
John McCall31168b02011-06-15 23:02:42 +0000598 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
601 if (isAssign)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
603
Ted Kremenekac597f32010-03-12 00:46:40 +0000604 if (MethodImplKind == tok::objc_required)
605 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
606 else if (MethodImplKind == tok::objc_optional)
607 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000608
Douglas Gregor813a0662015-06-19 18:14:38 +0000609 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
611
Douglas Gregor849ebc22015-06-19 18:14:46 +0000612 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
614
Ted Kremenek959e8302010-03-12 02:31:10 +0000615 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000616}
617
John McCall31168b02011-06-15 23:02:42 +0000618static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
619 ObjCPropertyDecl *property,
620 ObjCIvarDecl *ivar) {
621 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
622
John McCall31168b02011-06-15 23:02:42 +0000623 QualType ivarType = ivar->getType();
624 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000625
John McCall43192862011-09-13 18:31:23 +0000626 // The lifetime implied by the property's attributes.
627 Qualifiers::ObjCLifetime propertyLifetime =
628 getImpliedARCOwnership(property->getPropertyAttributes(),
629 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000630
John McCall43192862011-09-13 18:31:23 +0000631 // We're fine if they match.
632 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000633
John McCall460ce582015-10-22 18:38:17 +0000634 // None isn't a valid lifetime for an object ivar in ARC, and
635 // __autoreleasing is never valid; don't diagnose twice.
636 if ((ivarLifetime == Qualifiers::OCL_None &&
637 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000638 ivarLifetime == Qualifiers::OCL_Autoreleasing)
639 return;
John McCall31168b02011-06-15 23:02:42 +0000640
John McCalld8561f02012-08-20 23:36:59 +0000641 // If the ivar is private, and it's implicitly __unsafe_unretained
642 // becaues of its type, then pretend it was actually implicitly
643 // __strong. This is only sound because we're processing the
644 // property implementation before parsing any method bodies.
645 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
646 propertyLifetime == Qualifiers::OCL_Strong &&
647 ivar->getAccessControl() == ObjCIvarDecl::Private) {
648 SplitQualType split = ivarType.split();
649 if (split.Quals.hasObjCLifetime()) {
650 assert(ivarType->isObjCARCImplicitlyUnretainedType());
651 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
652 ivarType = S.Context.getQualifiedType(split);
653 ivar->setType(ivarType);
654 return;
655 }
656 }
657
John McCall43192862011-09-13 18:31:23 +0000658 switch (propertyLifetime) {
659 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000660 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000661 << property->getDeclName()
662 << ivar->getDeclName()
663 << ivarLifetime;
664 break;
John McCall31168b02011-06-15 23:02:42 +0000665
John McCall43192862011-09-13 18:31:23 +0000666 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000667 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000668 << property->getDeclName()
669 << ivar->getDeclName();
670 break;
John McCall31168b02011-06-15 23:02:42 +0000671
John McCall43192862011-09-13 18:31:23 +0000672 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000673 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000674 << property->getDeclName()
675 << ivar->getDeclName()
676 << ((property->getPropertyAttributesAsWritten()
677 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
678 break;
John McCall31168b02011-06-15 23:02:42 +0000679
John McCall43192862011-09-13 18:31:23 +0000680 case Qualifiers::OCL_Autoreleasing:
681 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000682
John McCall43192862011-09-13 18:31:23 +0000683 case Qualifiers::OCL_None:
684 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000685 return;
686 }
687
688 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000689 if (propertyImplLoc.isValid())
690 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000691}
692
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000693/// setImpliedPropertyAttributeForReadOnlyProperty -
694/// This routine evaludates life-time attributes for a 'readonly'
695/// property with no known lifetime of its own, using backing
696/// 'ivar's attribute, if any. If no backing 'ivar', property's
697/// life-time is assumed 'strong'.
698static void setImpliedPropertyAttributeForReadOnlyProperty(
699 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
700 Qualifiers::ObjCLifetime propertyLifetime =
701 getImpliedARCOwnership(property->getPropertyAttributes(),
702 property->getType());
703 if (propertyLifetime != Qualifiers::OCL_None)
704 return;
705
706 if (!ivar) {
707 // if no backing ivar, make property 'strong'.
708 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
709 return;
710 }
711 // property assumes owenership of backing ivar.
712 QualType ivarType = ivar->getType();
713 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
714 if (ivarLifetime == Qualifiers::OCL_Strong)
715 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
716 else if (ivarLifetime == Qualifiers::OCL_Weak)
717 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
718 return;
719}
Ted Kremenekac597f32010-03-12 00:46:40 +0000720
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000721/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
722/// in inherited protocols with mismatched types. Since any of them can
723/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000724static void
725DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
726 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000727 ObjCPropertyDecl *Property) {
728 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000729 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
730 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000731 PDecl->collectInheritedProtocolProperties(Property, PropMap);
732 }
733 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
734 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000735 for (const auto *PI : SDecl->all_referenced_protocols()) {
736 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000737 PDecl->collectInheritedProtocolProperties(Property, PropMap);
738 }
739 SDecl = SDecl->getSuperClass();
740 }
741
742 if (PropMap.empty())
743 return;
744
745 QualType RHSType = S.Context.getCanonicalType(Property->getType());
746 bool FirsTime = true;
747 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
748 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
749 ObjCPropertyDecl *Prop = I->second;
750 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
751 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
752 bool IncompatibleObjC = false;
753 QualType ConvertedType;
754 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
755 || IncompatibleObjC) {
756 if (FirsTime) {
757 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
758 << Property->getType();
759 FirsTime = false;
760 }
761 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
762 << Prop->getType();
763 }
764 }
765 }
766 if (!FirsTime && AtLoc.isValid())
767 S.Diag(AtLoc, diag::note_property_synthesize);
768}
769
Ted Kremenekac597f32010-03-12 00:46:40 +0000770/// ActOnPropertyImplDecl - This routine performs semantic checks and
771/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000772/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000773///
John McCall48871652010-08-21 09:40:31 +0000774Decl *Sema::ActOnPropertyImplDecl(Scope *S,
775 SourceLocation AtLoc,
776 SourceLocation PropertyLoc,
777 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000778 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000779 IdentifierInfo *PropertyIvar,
780 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000781 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000782 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000783 // Make sure we have a context for the property implementation declaration.
784 if (!ClassImpDecl) {
785 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000786 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000787 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000788 if (PropertyIvarLoc.isInvalid())
789 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000790 SourceLocation PropertyDiagLoc = PropertyLoc;
791 if (PropertyDiagLoc.isInvalid())
792 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000793 ObjCPropertyDecl *property = nullptr;
794 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000795 // Find the class or category class where this property must have
796 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000797 ObjCImplementationDecl *IC = nullptr;
798 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000799 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
800 IDecl = IC->getClassInterface();
801 // We always synthesize an interface for an implementation
802 // without an interface decl. So, IDecl is always non-zero.
803 assert(IDecl &&
804 "ActOnPropertyImplDecl - @implementation without @interface");
805
806 // Look for this property declaration in the @implementation's @interface
807 property = IDecl->FindPropertyDeclaration(PropertyId);
808 if (!property) {
809 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000810 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000811 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000812 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000813 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
814 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000815 if (AtLoc.isValid())
816 Diag(AtLoc, diag::warn_implicit_atomic_property);
817 else
818 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
819 Diag(property->getLocation(), diag::note_property_declare);
820 }
821
Ted Kremenekac597f32010-03-12 00:46:40 +0000822 if (const ObjCCategoryDecl *CD =
823 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
824 if (!CD->IsClassExtension()) {
825 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
826 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000827 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000828 }
829 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000830 if (Synthesize&&
831 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
832 property->hasAttr<IBOutletAttr>() &&
833 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000834 bool ReadWriteProperty = false;
835 // Search into the class extensions and see if 'readonly property is
836 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000837 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000838 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
839 if (!R.empty())
840 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
841 PIkind = ExtProp->getPropertyAttributesAsWritten();
842 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
843 ReadWriteProperty = true;
844 break;
845 }
846 }
847 }
848
849 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000850 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000851 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000852 SourceLocation readonlyLoc;
853 if (LocPropertyAttribute(Context, "readonly",
854 property->getLParenLoc(), readonlyLoc)) {
855 SourceLocation endLoc =
856 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
857 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
858 Diag(property->getLocation(),
859 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
860 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
861 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000862 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000863 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000864 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
865 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000866
Ted Kremenekac597f32010-03-12 00:46:40 +0000867 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
868 if (Synthesize) {
869 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000870 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000871 }
872 IDecl = CatImplClass->getClassInterface();
873 if (!IDecl) {
874 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000875 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000876 }
877 ObjCCategoryDecl *Category =
878 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
879
880 // If category for this implementation not found, it is an error which
881 // has already been reported eralier.
882 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000883 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000884 // Look for this property declaration in @implementation's category
885 property = Category->FindPropertyDeclaration(PropertyId);
886 if (!property) {
887 Diag(PropertyLoc, diag::error_bad_category_property_decl)
888 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000889 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000890 }
891 } else {
892 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000893 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000894 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000895 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000896 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000897 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000898 // Check that we have a valid, previously declared ivar for @synthesize
899 if (Synthesize) {
900 // @synthesize
901 if (!PropertyIvar)
902 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000903 // Check that this is a previously declared 'ivar' in 'IDecl' interface
904 ObjCInterfaceDecl *ClassDeclared;
905 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
906 QualType PropType = property->getType();
907 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000908
909 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000910 diag::err_incomplete_synthesized_property,
911 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000912 Diag(property->getLocation(), diag::note_property_declare);
913 CompleteTypeErr = true;
914 }
915
David Blaikiebbafb8a2012-03-11 07:00:24 +0000916 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000917 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000918 ObjCPropertyDecl::OBJC_PR_readonly) &&
919 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000920 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
921 }
922
John McCall31168b02011-06-15 23:02:42 +0000923 ObjCPropertyDecl::PropertyAttributeKind kind
924 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000925
John McCall460ce582015-10-22 18:38:17 +0000926 bool isARCWeak = false;
927 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
928 // Add GC __weak to the ivar type if the property is weak.
929 if (getLangOpts().getGC() != LangOptions::NonGC) {
930 assert(!getLangOpts().ObjCAutoRefCount);
931 if (PropertyIvarType.isObjCGCStrong()) {
932 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
933 Diag(property->getLocation(), diag::note_property_declare);
934 } else {
935 PropertyIvarType =
936 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
937 }
938
939 // Otherwise, check whether ARC __weak is enabled and works with
940 // the property type.
John McCall43192862011-09-13 18:31:23 +0000941 } else {
John McCall460ce582015-10-22 18:38:17 +0000942 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +0000943 // Only complain here when synthesizing an ivar.
944 if (!Ivar) {
945 Diag(PropertyDiagLoc,
946 getLangOpts().ObjCWeakRuntime
947 ? diag::err_synthesizing_arc_weak_property_disabled
948 : diag::err_synthesizing_arc_weak_property_no_runtime);
949 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +0000950 }
John McCallb61e14e2015-10-27 04:54:50 +0000951 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +0000952 } else {
953 isARCWeak = true;
954 if (const ObjCObjectPointerType *ObjT =
955 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
956 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
957 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
958 Diag(property->getLocation(),
959 diag::err_arc_weak_unavailable_property)
960 << PropertyIvarType;
961 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
962 << ClassImpDecl->getName();
963 }
964 }
965 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000966 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000967 }
John McCall460ce582015-10-22 18:38:17 +0000968
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000969 if (AtLoc.isInvalid()) {
970 // Check when default synthesizing a property that there is
971 // an ivar matching property name and issue warning; since this
972 // is the most common case of not using an ivar used for backing
973 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +0000974 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000975 ObjCIvarDecl *originalIvar =
976 IDecl->lookupInstanceVariable(property->getIdentifier(),
977 ClassDeclared);
978 if (originalIvar) {
979 Diag(PropertyDiagLoc,
980 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +0000981 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000982 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000983 Diag(property->getLocation(), diag::note_property_declare);
984 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000985 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000986 }
987
988 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000989 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000990 // property attributes.
John McCall460ce582015-10-22 18:38:17 +0000991 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +0000992 !PropertyIvarType.getObjCLifetime() &&
993 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000994
John McCall43192862011-09-13 18:31:23 +0000995 // It's an error if we have to do this and the user didn't
996 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000997 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000998 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000999 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001000 diag::err_arc_objc_property_default_assign_on_object);
1001 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001002 } else {
1003 Qualifiers::ObjCLifetime lifetime =
1004 getImpliedARCOwnership(kind, PropertyIvarType);
1005 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001006
John McCall31168b02011-06-15 23:02:42 +00001007 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001008 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001009 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1010 }
John McCall31168b02011-06-15 23:02:42 +00001011 }
1012
Abramo Bagnaradff19302011-03-08 08:55:46 +00001013 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001014 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001015 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001016 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001017 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001018 if (RequireNonAbstractType(PropertyIvarLoc,
1019 PropertyIvarType,
1020 diag::err_abstract_type_in_decl,
1021 AbstractSynthesizedIvarType)) {
1022 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001023 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001024 } else if (CompleteTypeErr)
1025 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001026 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001027 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001028
John McCall5fb5df92012-06-20 06:18:46 +00001029 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001030 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1031 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001032 // Note! I deliberately want it to fall thru so, we have a
1033 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001034 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001035 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001036 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001037 << property->getDeclName() << Ivar->getDeclName()
1038 << ClassDeclared->getDeclName();
1039 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001040 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001041 // Note! I deliberately want it to fall thru so more errors are caught.
1042 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001043 property->setPropertyIvarDecl(Ivar);
1044
Ted Kremenekac597f32010-03-12 00:46:40 +00001045 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1046
1047 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001048 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001049 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001050 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001051 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001052 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001053 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001054 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001055 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001056 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1057 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001058 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001059 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001060 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001061 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001062 << property->getDeclName() << PropType
1063 << Ivar->getDeclName() << IvarType;
1064 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001065 // Note! I deliberately want it to fall thru so, we have a
1066 // a property implementation and to avoid future warnings.
1067 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001068 else {
1069 // FIXME! Rules for properties are somewhat different that those
1070 // for assignments. Use a new routine to consolidate all cases;
1071 // specifically for property redeclarations as well as for ivars.
1072 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1073 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1074 if (lhsType != rhsType &&
1075 lhsType->isArithmeticType()) {
1076 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1077 << property->getDeclName() << PropType
1078 << Ivar->getDeclName() << IvarType;
1079 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1080 // Fall thru - see previous comment
1081 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001082 }
1083 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001084 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001085 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001086 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001087 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001088 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001089 // Fall thru - see previous comment
1090 }
John McCall31168b02011-06-15 23:02:42 +00001091 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001092 if ((property->getType()->isObjCObjectPointerType() ||
1093 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001094 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001095 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001096 << property->getDeclName() << Ivar->getDeclName();
1097 // Fall thru - see previous comment
1098 }
1099 }
John McCall460ce582015-10-22 18:38:17 +00001100 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1101 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001102 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001103 } else if (PropertyIvar)
1104 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001105 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001106
Ted Kremenekac597f32010-03-12 00:46:40 +00001107 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1108 ObjCPropertyImplDecl *PIDecl =
1109 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1110 property,
1111 (Synthesize ?
1112 ObjCPropertyImplDecl::Synthesize
1113 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001114 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001115
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001116 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001117 PIDecl->setInvalidDecl();
1118
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001119 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1120 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001121 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001122 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001123 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1124 // returned by the getter as it must conform to C++'s copy-return rules.
1125 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001126 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001127 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1128 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001129 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001130 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001131 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001132 Expr *LoadSelfExpr =
1133 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001134 CK_LValueToRValue, SelfExpr, nullptr,
1135 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001136 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001137 new (Context) ObjCIvarRefExpr(Ivar,
1138 Ivar->getUsageType(SelfDecl->getType()),
1139 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001140 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001141 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001142 ExprResult Res = PerformCopyInitialization(
1143 InitializedEntity::InitializeResult(PropertyDiagLoc,
1144 getterMethod->getReturnType(),
1145 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001146 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001147 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001148 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001149 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001150 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001151 PIDecl->setGetterCXXConstructor(ResExpr);
1152 }
1153 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001154 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1155 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1156 Diag(getterMethod->getLocation(),
1157 diag::warn_property_getter_owning_mismatch);
1158 Diag(property->getLocation(), diag::note_property_declare);
1159 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001160 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1161 switch (getterMethod->getMethodFamily()) {
1162 case OMF_retain:
1163 case OMF_retainCount:
1164 case OMF_release:
1165 case OMF_autorelease:
1166 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1167 << 1 << getterMethod->getSelector();
1168 break;
1169 default:
1170 break;
1171 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001172 }
1173 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1174 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001175 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1176 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001177 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001178 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001179 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1180 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001181 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001182 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001183 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001184 Expr *LoadSelfExpr =
1185 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001186 CK_LValueToRValue, SelfExpr, nullptr,
1187 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001188 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001189 new (Context) ObjCIvarRefExpr(Ivar,
1190 Ivar->getUsageType(SelfDecl->getType()),
1191 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001192 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001193 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001194 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1195 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001196 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001197 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1198 VK_LValue, PropertyDiagLoc);
1199 MarkDeclRefReferenced(rhs);
1200 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001201 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001202 if (property->getPropertyAttributes() &
1203 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001204 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001205 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001206 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1207 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001208 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001209 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001210 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001211 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001212 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001213 Diag(FuncDecl->getLocStart(),
1214 diag::note_callee_decl) << FuncDecl;
1215 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001216 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001217 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001218 }
1219 }
1220
Ted Kremenekac597f32010-03-12 00:46:40 +00001221 if (IC) {
1222 if (Synthesize)
1223 if (ObjCPropertyImplDecl *PPIDecl =
1224 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1225 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1226 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1227 << PropertyIvar;
1228 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1229 }
1230
1231 if (ObjCPropertyImplDecl *PPIDecl
1232 = IC->FindPropertyImplDecl(PropertyId)) {
1233 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1234 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001235 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001236 }
1237 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001238 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001239 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001240 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001241 // Diagnose if an ivar was lazily synthesdized due to a previous
1242 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001243 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001244 ObjCInterfaceDecl *ClassDeclared=nullptr;
1245 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001246 if (!Synthesize)
1247 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1248 else {
1249 if (PropertyIvar && PropertyIvar != PropertyId)
1250 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1251 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001252 // Issue diagnostics only if Ivar belongs to current class.
1253 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001254 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001255 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1256 << PropertyId;
1257 Ivar->setInvalidDecl();
1258 }
1259 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001260 } else {
1261 if (Synthesize)
1262 if (ObjCPropertyImplDecl *PPIDecl =
1263 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001264 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001265 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1266 << PropertyIvar;
1267 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1268 }
1269
1270 if (ObjCPropertyImplDecl *PPIDecl =
1271 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001272 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001273 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001274 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001275 }
1276 CatImplClass->addPropertyImplementation(PIDecl);
1277 }
1278
John McCall48871652010-08-21 09:40:31 +00001279 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001280}
1281
1282//===----------------------------------------------------------------------===//
1283// Helper methods.
1284//===----------------------------------------------------------------------===//
1285
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001286/// DiagnosePropertyMismatch - Compares two properties for their
1287/// attributes and types and warns on a variety of inconsistencies.
1288///
1289void
1290Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1291 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001292 const IdentifierInfo *inheritedName,
1293 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001294 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001295 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001296 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001297 SuperProperty->getPropertyAttributes();
1298
1299 // We allow readonly properties without an explicit ownership
1300 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1301 // to be overridden by a property with any explicit ownership in the subclass.
1302 if (!OverridingProtocolProperty &&
1303 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1304 ;
1305 else {
1306 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1307 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1308 Diag(Property->getLocation(), diag::warn_readonly_property)
1309 << Property->getDeclName() << inheritedName;
1310 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1311 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001312 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001313 << Property->getDeclName() << "copy" << inheritedName;
1314 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1315 unsigned CAttrRetain =
1316 (CAttr &
1317 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1318 unsigned SAttrRetain =
1319 (SAttr &
1320 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1321 bool CStrong = (CAttrRetain != 0);
1322 bool SStrong = (SAttrRetain != 0);
1323 if (CStrong != SStrong)
1324 Diag(Property->getLocation(), diag::warn_property_attribute)
1325 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1326 }
John McCall31168b02011-06-15 23:02:42 +00001327 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001328
1329 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001330 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001331 Diag(Property->getLocation(), diag::warn_property_attribute)
1332 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001333 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1334 }
1335 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001336 Diag(Property->getLocation(), diag::warn_property_attribute)
1337 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001338 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1339 }
1340 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001341 Diag(Property->getLocation(), diag::warn_property_attribute)
1342 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001343 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1344 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001345
1346 QualType LHSType =
1347 Context.getCanonicalType(SuperProperty->getType());
1348 QualType RHSType =
1349 Context.getCanonicalType(Property->getType());
1350
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001351 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001352 // Do cases not handled in above.
1353 // FIXME. For future support of covariant property types, revisit this.
1354 bool IncompatibleObjC = false;
1355 QualType ConvertedType;
1356 if (!isObjCPointerConversion(RHSType, LHSType,
1357 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001358 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001359 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1360 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001361 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1362 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001363 }
1364}
1365
1366bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1367 ObjCMethodDecl *GetterMethod,
1368 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001369 if (!GetterMethod)
1370 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001371 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001372 QualType PropertyIvarType = property->getType().getNonReferenceType();
1373 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1374 if (!compat) {
1375 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1376 isa<ObjCObjectPointerType>(GetterType))
1377 compat =
1378 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001379 GetterType->getAs<ObjCObjectPointerType>(),
1380 PropertyIvarType->getAs<ObjCObjectPointerType>());
1381 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001382 != Compatible) {
1383 Diag(Loc, diag::error_property_accessor_type)
1384 << property->getDeclName() << PropertyIvarType
1385 << GetterMethod->getSelector() << GetterType;
1386 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1387 return true;
1388 } else {
1389 compat = true;
1390 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1391 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1392 if (lhsType != rhsType && lhsType->isArithmeticType())
1393 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001394 }
1395 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001396
1397 if (!compat) {
1398 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1399 << property->getDeclName()
1400 << GetterMethod->getSelector();
1401 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1402 return true;
1403 }
1404
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001405 return false;
1406}
1407
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001408/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001409/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001410static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1411 ObjCContainerDecl::PropertyMap &PropMap,
1412 ObjCContainerDecl::PropertyMap &SuperPropMap,
1413 bool IncludeProtocols = true) {
1414
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001415 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001416 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001417 PropMap[Prop->getIdentifier()] = Prop;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001418
1419 // Collect the properties from visible extensions.
1420 for (auto *Ext : IDecl->visible_extensions())
1421 CollectImmediateProperties(Ext, PropMap, SuperPropMap, IncludeProtocols);
1422
Ted Kremenek204c3c52014-02-22 00:02:03 +00001423 if (IncludeProtocols) {
1424 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001425 for (auto *PI : IDecl->all_referenced_protocols())
1426 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001427 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001428 }
1429 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001430 for (auto *Prop : CATDecl->properties())
1431 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001432 if (IncludeProtocols) {
1433 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001434 for (auto *PI : CATDecl->protocols())
1435 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001436 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001437 }
1438 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001439 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001440 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1441 // Exclude property for protocols which conform to class's super-class,
1442 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001443 if (!PropertyFromSuper ||
1444 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001445 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1446 if (!PropEntry)
1447 PropEntry = Prop;
1448 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001449 }
1450 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001451 for (auto *PI : PDecl->protocols())
1452 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001453 }
1454}
1455
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001456/// CollectSuperClassPropertyImplementations - This routine collects list of
1457/// properties to be implemented in super class(s) and also coming from their
1458/// conforming protocols.
1459static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001460 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001461 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001462 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001463 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001464 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001465 SDecl = SDecl->getSuperClass();
1466 }
1467 }
1468}
1469
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001470/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1471/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1472/// declared in class 'IFace'.
1473bool
1474Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1475 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1476 if (!IV->getSynthesize())
1477 return false;
1478 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1479 Method->isInstanceMethod());
1480 if (!IMD || !IMD->isPropertyAccessor())
1481 return false;
1482
1483 // look up a property declaration whose one of its accessors is implemented
1484 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001485 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001486 if ((Property->getGetterName() == IMD->getSelector() ||
1487 Property->getSetterName() == IMD->getSelector()) &&
1488 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001489 return true;
1490 }
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001491 // Also look up property declaration in class extension whose one of its
1492 // accessors is implemented by this method.
1493 for (const auto *Ext : IFace->known_extensions())
1494 for (const auto *Property : Ext->properties())
1495 if ((Property->getGetterName() == IMD->getSelector() ||
1496 Property->getSetterName() == IMD->getSelector()) &&
1497 (Property->getPropertyIvarDecl() == IV))
1498 return true;
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001499 return false;
1500}
1501
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001502static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1503 ObjCPropertyDecl *Prop) {
1504 bool SuperClassImplementsGetter = false;
1505 bool SuperClassImplementsSetter = false;
1506 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1507 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001508
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001509 while (IDecl->getSuperClass()) {
1510 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1511 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1512 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001513
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001514 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1515 SuperClassImplementsSetter = true;
1516 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1517 return true;
1518 IDecl = IDecl->getSuperClass();
1519 }
1520 return false;
1521}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001522
James Dennett2a4d13c2012-06-15 07:13:21 +00001523/// \brief Default synthesizes all properties which must be synthesized
1524/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001525void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1526 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001527
Anna Zaks673d76b2012-10-18 19:17:53 +00001528 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001529 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1530 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001531 if (PropMap.empty())
1532 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001533 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001534 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1535
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001536 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1537 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001538 // Is there a matching property synthesize/dynamic?
1539 if (Prop->isInvalidDecl() ||
1540 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1541 continue;
1542 // Property may have been synthesized by user.
1543 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1544 continue;
1545 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1546 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1547 continue;
1548 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1549 continue;
1550 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001551 if (ObjCPropertyImplDecl *PID =
1552 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001553 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1554 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001555 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001556 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001557 continue;
1558 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001559 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001560 if (ObjCProtocolDecl *Proto =
1561 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001562 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001563 // Suppress the warning if class's superclass implements property's
1564 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001565 // Or, if property is going to be implemented in its super class.
1566 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001567 Diag(IMPDecl->getLocation(),
1568 diag::warn_auto_synthesizing_protocol_property)
1569 << Prop << Proto;
1570 Diag(Prop->getLocation(), diag::note_property_declare);
1571 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001572 continue;
1573 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001574 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001575 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001576 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1577 (PropInSuperClass->getPropertyAttributes() &
1578 ObjCPropertyDecl::OBJC_PR_readonly) &&
1579 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1580 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1581 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1582 << Prop->getIdentifier();
1583 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1584 }
1585 else {
1586 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1587 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001588 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001589 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1590 }
1591 continue;
1592 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001593 // We use invalid SourceLocations for the synthesized ivars since they
1594 // aren't really synthesized at a particular location; they just exist.
1595 // Saying that they are located at the @implementation isn't really going
1596 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001597 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1598 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1599 true,
1600 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001601 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001602 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001603 if (PIDecl) {
1604 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001605 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001606 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001607 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001608}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001609
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001610void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001611 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001612 return;
1613 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1614 if (!IC)
1615 return;
1616 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001617 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001618 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001619}
1620
Ted Kremenek7e812952014-02-21 19:41:30 +00001621static void DiagnoseUnimplementedAccessor(Sema &S,
1622 ObjCInterfaceDecl *PrimaryClass,
1623 Selector Method,
1624 ObjCImplDecl* IMPDecl,
1625 ObjCContainerDecl *CDecl,
1626 ObjCCategoryDecl *C,
1627 ObjCPropertyDecl *Prop,
1628 Sema::SelectorSet &SMap) {
1629 // When reporting on missing property setter/getter implementation in
1630 // categories, do not report when they are declared in primary class,
1631 // class's protocol, or one of it super classes. This is because,
1632 // the class is going to implement them.
1633 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001634 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001635 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1636 S.Diag(IMPDecl->getLocation(),
1637 isa<ObjCCategoryDecl>(CDecl) ?
1638 diag::warn_setter_getter_impl_required_in_category :
1639 diag::warn_setter_getter_impl_required)
1640 << Prop->getDeclName() << Method;
1641 S.Diag(Prop->getLocation(),
1642 diag::note_property_declare);
1643 if (S.LangOpts.ObjCDefaultSynthProperties &&
1644 S.LangOpts.ObjCRuntime.isNonFragile())
1645 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1646 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1647 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1648 }
1649}
1650
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001651void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001652 ObjCContainerDecl *CDecl,
1653 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001654 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001655 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1656
Ted Kremenek348e88c2014-02-21 19:41:34 +00001657 if (!SynthesizeProperties) {
1658 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001659 // Gather properties which need not be implemented in this class
1660 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001661 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001662 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1663 // For categories, no need to implement properties declared in
1664 // its primary class (and its super classes) if property is
1665 // declared in one of those containers.
1666 if ((IDecl = C->getClassInterface())) {
1667 ObjCInterfaceDecl::PropertyDeclOrder PO;
1668 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1669 }
1670 }
1671 if (IDecl)
1672 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1673
1674 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1675 }
1676
Ted Kremenek38882022014-02-21 19:41:39 +00001677 // Scan the @interface to see if any of the protocols it adopts
1678 // require an explicit implementation, via attribute
1679 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001680 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001681 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001682
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001683 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001684 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1685 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001686 // Lazily construct a set of all the properties in the @interface
1687 // of the class, without looking at the superclass. We cannot
1688 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001689 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001690 // as scans the adopted protocols. This work only triggers for protocols
1691 // with the attribute, which is very rare, and only occurs when
1692 // analyzing the @implementation.
1693 if (!LazyMap) {
1694 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1695 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1696 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1697 /* IncludeProtocols */ false);
1698 }
Ted Kremenek38882022014-02-21 19:41:39 +00001699 // Add the properties of 'PDecl' to the list of properties that
1700 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001701 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001702 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001703 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001704 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001705 }
1706 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001707 }
Ted Kremenek38882022014-02-21 19:41:39 +00001708
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001709 if (PropMap.empty())
1710 return;
1711
1712 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001713 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001714 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001715
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001716 SelectorSet InsMap;
1717 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001718 for (const auto *I : IMPDecl->instance_methods())
1719 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001720
1721 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001722 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001723 if (C && !C->IsClassExtension())
1724 if ((PrimaryClass = C->getClassInterface()))
1725 // Report unimplemented properties in the category as well.
1726 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1727 // When reporting on missing setter/getters, do not report when
1728 // setter/getter is implemented in category's primary class
1729 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001730 for (const auto *I : IMP->instance_methods())
1731 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001732 }
1733
Anna Zaks673d76b2012-10-18 19:17:53 +00001734 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001735 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1736 ObjCPropertyDecl *Prop = P->second;
1737 // Is there a matching propery synthesize/dynamic?
1738 if (Prop->isInvalidDecl() ||
1739 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001740 PropImplMap.count(Prop) ||
1741 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001742 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001743
1744 // Diagnose unimplemented getters and setters.
1745 DiagnoseUnimplementedAccessor(*this,
1746 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1747 if (!Prop->isReadOnly())
1748 DiagnoseUnimplementedAccessor(*this,
1749 PrimaryClass, Prop->getSetterName(),
1750 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001751 }
1752}
1753
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001754void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001755 for (const auto *propertyImpl : impDecl->property_impls()) {
1756 const auto *property = propertyImpl->getPropertyDecl();
1757
1758 // Warn about null_resettable properties with synthesized setters,
1759 // because the setter won't properly handle nil.
1760 if (propertyImpl->getPropertyImplementation()
1761 == ObjCPropertyImplDecl::Synthesize &&
1762 (property->getPropertyAttributes() &
1763 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1764 property->getGetterMethodDecl() &&
1765 property->getSetterMethodDecl()) {
1766 auto *getterMethod = property->getGetterMethodDecl();
1767 auto *setterMethod = property->getSetterMethodDecl();
1768 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1769 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1770 SourceLocation loc = propertyImpl->getLocation();
1771 if (loc.isInvalid())
1772 loc = impDecl->getLocStart();
1773
1774 Diag(loc, diag::warn_null_resettable_setter)
1775 << setterMethod->getSelector() << property->getDeclName();
1776 }
1777 }
1778 }
1779}
1780
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001781void
1782Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001783 ObjCInterfaceDecl* IDecl) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001784 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001785 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001786 return;
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001787 ObjCContainerDecl::PropertyMap PM;
1788 for (auto *Prop : IDecl->properties())
1789 PM[Prop->getIdentifier()] = Prop;
1790 for (const auto *Ext : IDecl->known_extensions())
1791 for (auto *Prop : Ext->properties())
1792 PM[Prop->getIdentifier()] = Prop;
1793
1794 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end();
1795 I != E; ++I) {
1796 const ObjCPropertyDecl *Property = I->second;
Craig Topperc3ec1492014-05-26 06:22:03 +00001797 ObjCMethodDecl *GetterMethod = nullptr;
1798 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001799 bool LookedUpGetterSetter = false;
1800
Bill Wendling44426052012-12-20 19:22:21 +00001801 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001802 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001803
John McCall43192862011-09-13 18:31:23 +00001804 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1805 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001806 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1807 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1808 LookedUpGetterSetter = true;
1809 if (GetterMethod) {
1810 Diag(GetterMethod->getLocation(),
1811 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001812 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001813 Diag(Property->getLocation(), diag::note_property_declare);
1814 }
1815 if (SetterMethod) {
1816 Diag(SetterMethod->getLocation(),
1817 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001818 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001819 Diag(Property->getLocation(), diag::note_property_declare);
1820 }
1821 }
1822
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001823 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001824 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1825 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001826 continue;
1827 if (const ObjCPropertyImplDecl *PIDecl
1828 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1829 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1830 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001831 if (!LookedUpGetterSetter) {
1832 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1833 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001834 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001835 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1836 SourceLocation MethodLoc =
1837 (GetterMethod ? GetterMethod->getLocation()
1838 : SetterMethod->getLocation());
1839 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001840 << Property->getIdentifier() << (GetterMethod != nullptr)
1841 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001842 // fixit stuff.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001843 if (Property->getLParenLoc().isValid() &&
1844 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001845 // @property () ... case.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001846 SourceLocation AfterLParen =
1847 getLocForEndOfToken(Property->getLParenLoc());
1848 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, "
1849 : "nonatomic";
1850 Diag(Property->getLocation(),
1851 diag::note_atomic_property_fixup_suggest)
1852 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr);
1853 } else if (Property->getLParenLoc().isInvalid()) {
1854 //@property id etc.
1855 SourceLocation startLoc =
1856 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1857 Diag(Property->getLocation(),
1858 diag::note_atomic_property_fixup_suggest)
1859 << FixItHint::CreateInsertion(startLoc, "(nonatomic) ");
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001860 }
1861 else
1862 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001863 Diag(Property->getLocation(), diag::note_property_declare);
1864 }
1865 }
1866 }
1867}
1868
John McCall31168b02011-06-15 23:02:42 +00001869void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001870 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001871 return;
1872
Aaron Ballmand85eff42014-03-14 15:02:45 +00001873 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001874 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001875 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1876 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001877 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1878 if (!method)
1879 continue;
1880 ObjCMethodFamily family = method->getMethodFamily();
1881 if (family == OMF_alloc || family == OMF_copy ||
1882 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001883 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001884 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001885 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001886 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001887
1888 // Look for a getter explicitly declared alongside the property.
1889 // If we find one, use its location for the note.
1890 SourceLocation noteLoc = PD->getLocation();
1891 SourceLocation fixItLoc;
1892 for (auto *getterRedecl : method->redecls()) {
1893 if (getterRedecl->isImplicit())
1894 continue;
1895 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1896 continue;
1897 noteLoc = getterRedecl->getLocation();
1898 fixItLoc = getterRedecl->getLocEnd();
1899 }
1900
1901 Preprocessor &PP = getPreprocessor();
1902 TokenValue tokens[] = {
1903 tok::kw___attribute, tok::l_paren, tok::l_paren,
1904 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1905 PP.getIdentifierInfo("none"), tok::r_paren,
1906 tok::r_paren, tok::r_paren
1907 };
1908 StringRef spelling = "__attribute__((objc_method_family(none)))";
1909 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1910 if (!macroName.empty())
1911 spelling = macroName;
1912
1913 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1914 << method->getDeclName() << spelling;
1915 if (fixItLoc.isValid()) {
1916 SmallString<64> fixItText(" ");
1917 fixItText += spelling;
1918 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1919 }
John McCall31168b02011-06-15 23:02:42 +00001920 }
1921 }
1922 }
1923}
1924
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001925void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001926 const ObjCImplementationDecl *ImplD,
1927 const ObjCInterfaceDecl *IFD) {
1928 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001929 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1930 if (!SuperD)
1931 return;
1932
1933 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001934 for (const auto *I : ImplD->instance_methods())
1935 if (I->getMethodFamily() == OMF_init)
1936 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001937
1938 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1939 SuperD->getDesignatedInitializers(DesignatedInits);
1940 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1941 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1942 const ObjCMethodDecl *MD = *I;
1943 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00001944 bool Ignore = false;
1945 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
1946 Ignore = IMD->isUnavailable();
1947 }
1948 if (!Ignore) {
1949 Diag(ImplD->getLocation(),
1950 diag::warn_objc_implementation_missing_designated_init_override)
1951 << MD->getSelector();
1952 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1953 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001954 }
1955 }
1956}
1957
John McCallad31b5f2010-11-10 07:01:40 +00001958/// AddPropertyAttrs - Propagates attributes from a property to the
1959/// implicitly-declared getter or setter for that property.
1960static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1961 ObjCPropertyDecl *Property) {
1962 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001963 for (const auto *A : Property->attrs()) {
1964 if (isa<DeprecatedAttr>(A) ||
1965 isa<UnavailableAttr>(A) ||
1966 isa<AvailabilityAttr>(A))
1967 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001968 }
John McCallad31b5f2010-11-10 07:01:40 +00001969}
1970
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001971/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1972/// have the property type and issue diagnostics if they don't.
1973/// Also synthesize a getter/setter method if none exist (and update the
Douglas Gregore17765e2015-11-03 17:02:34 +00001974/// appropriate lookup tables.
1975void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001976 ObjCMethodDecl *GetterMethod, *SetterMethod;
Douglas Gregore17765e2015-11-03 17:02:34 +00001977 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext());
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00001978 if (CD->isInvalidDecl())
1979 return;
1980
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001981 GetterMethod = CD->getInstanceMethod(property->getGetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001982 // if setter or getter is not found in class extension, it might be
1983 // in the primary class.
1984 if (!GetterMethod)
1985 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
1986 if (CatDecl->IsClassExtension())
1987 GetterMethod = CatDecl->getClassInterface()->
1988 getInstanceMethod(property->getGetterName());
1989
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001990 SetterMethod = CD->getInstanceMethod(property->getSetterName());
Douglas Gregoracf4fd32015-11-03 01:15:46 +00001991 if (!SetterMethod)
1992 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD))
1993 if (CatDecl->IsClassExtension())
1994 SetterMethod = CatDecl->getClassInterface()->
1995 getInstanceMethod(property->getSetterName());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001996 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1997 property->getLocation());
1998
1999 if (SetterMethod) {
2000 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2001 property->getPropertyAttributes();
2002 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002003 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2004 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002005 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2006 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002007 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002008 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2009 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002010 Diag(property->getLocation(),
2011 diag::warn_accessor_property_type_mismatch)
2012 << property->getDeclName()
2013 << SetterMethod->getSelector();
2014 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2015 }
2016 }
2017
2018 // Synthesize getter/setter methods if none exist.
2019 // Find the default getter and if one not found, add one.
2020 // FIXME: The synthesized property we set here is misleading. We almost always
2021 // synthesize these methods unless the user explicitly provided prototypes
2022 // (which is odd, but allowed). Sema should be typechecking that the
2023 // declarations jive in that situation (which it is not currently).
2024 if (!GetterMethod) {
2025 // No instance method of same name as property getter name was found.
2026 // Declare a getter method and add it to the list of methods
2027 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002028 SourceLocation Loc = property->getLocation();
Ted Kremenek2f075632010-09-21 20:52:59 +00002029
Douglas Gregor849ebc22015-06-19 18:14:46 +00002030 // If the property is null_resettable, the getter returns nonnull.
2031 QualType resultTy = property->getType();
2032 if (property->getPropertyAttributes() &
2033 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2034 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002035 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002036 if (*nullability == NullabilityKind::Unspecified)
2037 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2038 modifiedTy, modifiedTy);
2039 }
2040 }
2041
Ted Kremenek2f075632010-09-21 20:52:59 +00002042 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2043 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002044 resultTy, nullptr, CD,
Craig Topperc3ec1492014-05-26 06:22:03 +00002045 /*isInstance=*/true, /*isVariadic=*/false,
2046 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002047 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002048 (property->getPropertyImplementation() ==
2049 ObjCPropertyDecl::Optional) ?
2050 ObjCMethodDecl::Optional :
2051 ObjCMethodDecl::Required);
2052 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002053
2054 AddPropertyAttrs(*this, GetterMethod, property);
2055
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002056 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002057 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2058 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002059
2060 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2061 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002062 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002063
2064 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002065 GetterMethod->addAttr(
2066 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2067 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002068
2069 if (getLangOpts().ObjCAutoRefCount)
2070 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002071 } else
2072 // A user declared getter will be synthesize when @synthesize of
2073 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002074 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002075 property->setGetterMethodDecl(GetterMethod);
2076
2077 // Skip setter if property is read-only.
2078 if (!property->isReadOnly()) {
2079 // Find the default setter and if one not found, add one.
2080 if (!SetterMethod) {
2081 // No instance method of same name as property setter name was found.
2082 // Declare a setter method and add it to the list of methods
2083 // for this class.
Douglas Gregoracf4fd32015-11-03 01:15:46 +00002084 SourceLocation Loc = property->getLocation();
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002085
2086 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002087 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002088 property->getSetterName(), Context.VoidTy,
2089 nullptr, CD, /*isInstance=*/true,
2090 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002091 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002092 /*isImplicitlyDeclared=*/true,
2093 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002094 (property->getPropertyImplementation() ==
2095 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002096 ObjCMethodDecl::Optional :
2097 ObjCMethodDecl::Required);
2098
Douglas Gregor849ebc22015-06-19 18:14:46 +00002099 // If the property is null_resettable, the setter accepts a
2100 // nullable value.
2101 QualType paramTy = property->getType().getUnqualifiedType();
2102 if (property->getPropertyAttributes() &
2103 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2104 QualType modifiedTy = paramTy;
2105 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2106 if (*nullability == NullabilityKind::Unspecified)
2107 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2108 modifiedTy, modifiedTy);
2109 }
2110 }
2111
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002112 // Invent the arguments for the setter. We don't bother making a
2113 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002114 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2115 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002116 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002117 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002118 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002119 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002120 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002121 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002122
2123 AddPropertyAttrs(*this, SetterMethod, property);
2124
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002125 CD->addDecl(SetterMethod);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002126 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002127 SetterMethod->addAttr(
2128 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2129 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002130 // It's possible for the user to have set a very odd custom
2131 // setter selector that causes it to have a method family.
2132 if (getLangOpts().ObjCAutoRefCount)
2133 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002134 } else
2135 // A user declared setter will be synthesize when @synthesize of
2136 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002137 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002138 property->setSetterMethodDecl(SetterMethod);
2139 }
2140 // Add any synthesized methods to the global pool. This allows us to
2141 // handle the following, which is supported by GCC (and part of the design).
2142 //
2143 // @interface Foo
2144 // @property double bar;
2145 // @end
2146 //
2147 // void thisIsUnfortunate() {
2148 // id foo;
2149 // double bar = [foo bar];
2150 // }
2151 //
2152 if (GetterMethod)
2153 AddInstanceMethodToGlobalPool(GetterMethod);
2154 if (SetterMethod)
2155 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002156
2157 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2158 if (!CurrentClass) {
2159 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2160 CurrentClass = Cat->getClassInterface();
2161 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2162 CurrentClass = Impl->getClassInterface();
2163 }
2164 if (GetterMethod)
2165 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2166 if (SetterMethod)
2167 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002168}
2169
John McCall48871652010-08-21 09:40:31 +00002170void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002171 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002172 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002173 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002174 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002175 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002176 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002177
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002178 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2179 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2180 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2181 << "readonly" << "readwrite";
2182
2183 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2184 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002185
2186 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002187 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002188 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2189 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002190 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002191 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002192 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2193 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2194 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002195 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002196 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002197 }
2198
2199 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002200 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2201 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002202 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2203 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002204 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002205 }
Bill Wendling44426052012-12-20 19:22:21 +00002206 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002207 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2208 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002209 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002210 }
Bill Wendling44426052012-12-20 19:22:21 +00002211 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002212 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2213 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002214 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002215 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002216 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002217 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002218 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2219 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002220 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002221 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002222 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002223 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002224 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2225 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002226 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2227 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002228 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002229 }
Bill Wendling44426052012-12-20 19:22:21 +00002230 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002231 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2232 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002233 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002234 }
Bill Wendling44426052012-12-20 19:22:21 +00002235 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002236 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2237 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002238 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002239 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002240 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002241 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002242 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2243 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002244 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002245 }
Bill Wendling44426052012-12-20 19:22:21 +00002246 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2247 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002248 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2249 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002250 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002251 }
Bill Wendling44426052012-12-20 19:22:21 +00002252 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002253 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2254 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002255 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002256 }
Bill Wendling44426052012-12-20 19:22:21 +00002257 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002258 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2259 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002260 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002261 }
2262 }
Bill Wendling44426052012-12-20 19:22:21 +00002263 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2264 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002265 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2266 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002267 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002268 }
Bill Wendling44426052012-12-20 19:22:21 +00002269 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2270 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002271 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2272 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002273 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002274 }
2275
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002276 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002277 // 'weak' and 'nonnull' are mutually exclusive.
2278 if (auto nullability = PropertyTy->getNullability(Context)) {
2279 if (*nullability == NullabilityKind::NonNull)
2280 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2281 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002282 }
2283 }
2284
Bill Wendling44426052012-12-20 19:22:21 +00002285 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2286 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002287 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2288 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002289 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002290 }
2291
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002292 // Warn if user supplied no assignment attribute, property is
2293 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002294 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2295 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2296 // do nothing
2297 } else if (getLangOpts().ObjCAutoRefCount) {
2298 // With arc, @property definitions should default to strong when
2299 // not specified.
2300 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2301 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002302 bool isAnyClassTy =
2303 (PropertyTy->isObjCClassType() ||
2304 PropertyTy->isObjCQualifiedClassType());
2305 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2306 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002307 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002308 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002309 else if (propertyInPrimaryClass) {
2310 // Don't issue warning on property with no life time in class
2311 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002312 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002313 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002314 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002315
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002316 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002317 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002318 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002319 }
John McCallb61e14e2015-10-27 04:54:50 +00002320 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002321
2322 // FIXME: Implement warning dependent on NSCopying being
2323 // implemented. See also:
2324 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2325 // (please trim this list while you are at it).
2326 }
2327
Bill Wendling44426052012-12-20 19:22:21 +00002328 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2329 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002330 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002331 && PropertyTy->isBlockPointerType())
2332 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002333 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2334 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2335 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002336 PropertyTy->isBlockPointerType())
2337 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002338
Bill Wendling44426052012-12-20 19:22:21 +00002339 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2340 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002341 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2342
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002343}