blob: bd99143d4c9ff54c59eea399590af88a1a1865f6 [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)) {
Aaron Ballman19a41762014-03-14 12:55:57 +0000265 for (auto *P : Cat->protocols())
266 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000267 } else {
268 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000269 for (auto *P : Proto->protocols())
270 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000271 }
272
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000273 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000274 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000275}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000276
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000277static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000278makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000280 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000286 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000288 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000290 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000292 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000294 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000296 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000298 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000300 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000302 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
304
305 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
306}
307
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000308static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000309 SourceLocation LParenLoc, SourceLocation &Loc) {
310 if (LParenLoc.isMacroID())
311 return false;
312
313 SourceManager &SM = Context.getSourceManager();
314 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
315 // Try to load the file buffer.
316 bool invalidTemp = false;
317 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
318 if (invalidTemp)
319 return false;
320 const char *tokenBegin = file.data() + locInfo.second;
321
322 // Lex from the start of the given location.
323 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
324 Context.getLangOpts(),
325 file.begin(), tokenBegin, file.end());
326 Token Tok;
327 do {
328 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000329 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000330 Loc = Tok.getLocation();
331 return true;
332 }
333 } while (Tok.isNot(tok::r_paren));
334 return false;
335
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000336}
337
Douglas Gregor90d34422013-01-21 19:05:22 +0000338ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000339Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000340 SourceLocation AtLoc,
341 SourceLocation LParenLoc,
342 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000343 Selector GetterSel, Selector SetterSel,
344 const bool isAssign,
345 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000346 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000347 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000348 bool *isOverridingProperty,
Douglas Gregor813a0662015-06-19 18:14:38 +0000349 QualType T,
350 TypeSourceInfo *TSI,
Ted Kremenek959e8302010-03-12 02:31:10 +0000351 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000352 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000353 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000354 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000355 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000356 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
357
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000358 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000359 // Check for duplicate declaration of this property in current and
360 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000361 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000362 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000363 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000364 Diag(AtLoc, diag::err_duplicate_property);
365 Diag(prevDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000366 return nullptr;
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000367 }
368 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000369 }
370
Ted Kremenek959e8302010-03-12 02:31:10 +0000371 // Create a new ObjCPropertyDecl with the DeclContext being
372 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000373 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000374 ObjCPropertyDecl *PDecl =
375 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000376 PropertyId, AtLoc, LParenLoc, T, TSI);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000377 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000378 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000379 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000380 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000381 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000382 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000383 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
384 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
385 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
386 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Douglas Gregor813a0662015-06-19 18:14:38 +0000387 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
388 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
Douglas Gregor849ebc22015-06-19 18:14:46 +0000389 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
390 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
391
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000392 // Set setter/getter selector name. Needed later.
393 PDecl->setGetterName(GetterSel);
394 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000395 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000396 DC->addDecl(PDecl);
397
398 // We need to look in the @interface to see if the @property was
399 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000400 if (!CCPrimary) {
401 Diag(CDecl->getLocation(), diag::err_continuation_class);
402 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000403 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000404 }
405
406 // Find the property in continuation class's primary class only.
407 ObjCPropertyDecl *PIDecl =
408 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
409
410 if (!PIDecl) {
411 // No matching property found in the primary class. Just fall thru
412 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000413 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000414 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000415 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Douglas Gregor813a0662015-06-19 18:14:38 +0000416 Attributes,AttributesAsWritten, T, TSI, MethodImplKind,
417 DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000418
419 // A case of continuation class adding a new property in the class. This
420 // is not what it was meant for. However, gcc supports it and so should we.
421 // Make sure setter/getters are declared here.
Craig Topperc3ec1492014-05-26 06:22:03 +0000422 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
423 /* redeclaredProperty = */ nullptr,
Ted Kremenek2f075632010-09-21 20:52:59 +0000424 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000425 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
426 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000427 if (ASTMutationListener *L = Context.getASTMutationListener())
Craig Topperc3ec1492014-05-26 06:22:03 +0000428 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
429 CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000430 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000431 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000432 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
433 bool IncompatibleObjC = false;
434 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000435 // Relax the strict type matching for property type in continuation class.
436 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000437 // as it narrows the object type in its primary class property. Note that
438 // this conversion is safe only because the wider type is for a 'readonly'
439 // property in primary class and 'narrowed' type for a 'readwrite' property
440 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000441 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
442 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
443 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
444 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
445 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000446 ConvertedType, IncompatibleObjC))
447 || IncompatibleObjC) {
448 Diag(AtLoc,
449 diag::err_type_mismatch_continuation_class) << PDecl->getType();
450 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000451 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000452 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000453 }
454
Ted Kremenek959e8302010-03-12 02:31:10 +0000455 // The property 'PIDecl's readonly attribute will be over-ridden
456 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000457 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000458 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000459 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
460 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
John McCallb61e14e2015-10-27 04:54:50 +0000461 PIkind |= deducePropertyOwnershipFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000462 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000463 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000464 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
465 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000466 Diag(AtLoc, diag::warn_property_attr_mismatch);
467 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000468 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000469 else if (getLangOpts().ObjCAutoRefCount) {
470 QualType PrimaryPropertyQT =
471 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
472 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000473 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000474 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
475 PrimaryPropertyQT.getObjCLifetime();
476 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000477 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
478 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000479 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
480 Diag(PIDecl->getLocation(), diag::note_property_declare);
481 }
482 }
483 }
484
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000485 DeclContext *DC = cast<DeclContext>(CCPrimary);
486 if (!ObjCPropertyDecl::findPropertyDecl(DC,
487 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000488 // In mrr mode, 'readwrite' property must have an explicit
489 // memory attribute. If none specified, select the default (assign).
490 if (!getLangOpts().ObjCAutoRefCount) {
491 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
492 ObjCDeclSpec::DQ_PR_retain |
493 ObjCDeclSpec::DQ_PR_strong |
494 ObjCDeclSpec::DQ_PR_copy |
495 ObjCDeclSpec::DQ_PR_unsafe_unretained |
496 ObjCDeclSpec::DQ_PR_weak)))
497 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
498 }
499
Ted Kremenek959e8302010-03-12 02:31:10 +0000500 // Protocol is not in the primary class. Must build one for it.
501 ObjCDeclSpec ProtocolPropertyODS;
502 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
503 // and ObjCPropertyDecl::PropertyAttributeKind have identical
504 // values. Should consolidate both into one enum type.
505 ProtocolPropertyODS.
506 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
507 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000508 // Must re-establish the context from class extension to primary
509 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000510 ContextRAII SavedContext(*this, CCPrimary);
511
John McCall48871652010-08-21 09:40:31 +0000512 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000513 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000514 PIDecl->getGetterName(),
515 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000516 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000517 MethodImplKind,
518 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000519 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000520 }
521 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000522 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000523 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000524 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000525 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000526 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000527 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
528 PIDecl->setSetterName(SetterSel);
529 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000530 // Tailor the diagnostics for the common case where a readwrite
531 // property is declared both in the @interface and the continuation.
532 // This is a common error where the user often intended the original
533 // declaration to be readonly.
534 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000535 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000536 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
537 ? diag::err_use_continuation_class_redeclaration_readwrite
538 : diag::err_use_continuation_class;
539 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000540 << CCPrimary->getDeclName();
541 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000542 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000543 }
544 *isOverridingProperty = true;
545 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000546 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000547 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
548 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000549 if (ASTMutationListener *L = Context.getASTMutationListener())
550 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000551 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000552}
553
554ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
555 ObjCContainerDecl *CDecl,
556 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000557 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000558 FieldDeclarator &FD,
559 Selector GetterSel,
560 Selector SetterSel,
561 const bool isAssign,
562 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000563 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000564 const unsigned AttributesAsWritten,
Douglas Gregor813a0662015-06-19 18:14:38 +0000565 QualType T,
John McCall339bb662010-06-04 20:50:08 +0000566 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000567 tok::ObjCKeywordKind MethodImplKind,
568 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000569 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenekac597f32010-03-12 00:46:40 +0000570
571 // Issue a warning if property is 'assign' as default and its object, which is
572 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000573 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000574 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000575 if (const ObjCObjectPointerType *ObjPtrTy =
576 T->getAs<ObjCObjectPointerType>()) {
577 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
578 if (IDecl)
579 if (ObjCProtocolDecl* PNSCopying =
580 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
581 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
582 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000583 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000584
585 if (T->isObjCObjectType()) {
586 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000587 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000588 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
589 << FixItHint::CreateInsertion(StarLoc, "*");
590 T = Context.getObjCObjectPointerType(T);
591 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
592 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
593 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000594
Ted Kremenek959e8302010-03-12 02:31:10 +0000595 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000596 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
597 FD.D.getIdentifierLoc(),
Douglas Gregor813a0662015-06-19 18:14:38 +0000598 PropertyId, AtLoc,
599 LParenLoc, T, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000600
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000601 if (ObjCPropertyDecl *prevDecl =
602 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000603 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000604 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000605 PDecl->setInvalidDecl();
606 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000607 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000609 if (lexicalDC)
610 PDecl->setLexicalDeclContext(lexicalDC);
611 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000612
613 if (T->isArrayType() || T->isFunctionType()) {
614 Diag(AtLoc, diag::err_property_type) << T;
615 PDecl->setInvalidDecl();
616 }
617
618 ProcessDeclAttributes(S, PDecl, FD.D);
619
620 // Regardless of setter/getter attribute, we save the default getter/setter
621 // selector names in anticipation of declaration of setter/getter methods.
622 PDecl->setGetterName(GetterSel);
623 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000624 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000625 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000626
Bill Wendling44426052012-12-20 19:22:21 +0000627 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
629
Bill Wendling44426052012-12-20 19:22:21 +0000630 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
632
Bill Wendling44426052012-12-20 19:22:21 +0000633 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000634 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
635
636 if (isReadWrite)
637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
638
Bill Wendling44426052012-12-20 19:22:21 +0000639 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000640 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
641
Bill Wendling44426052012-12-20 19:22:21 +0000642 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000643 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
644
Bill Wendling44426052012-12-20 19:22:21 +0000645 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000646 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
647
Bill Wendling44426052012-12-20 19:22:21 +0000648 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000649 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
650
Bill Wendling44426052012-12-20 19:22:21 +0000651 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000652 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
653
Ted Kremenekac597f32010-03-12 00:46:40 +0000654 if (isAssign)
655 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
656
John McCall43192862011-09-13 18:31:23 +0000657 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000658 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000659 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000660 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000662
John McCall31168b02011-06-15 23:02:42 +0000663 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000664 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000665 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
666 if (isAssign)
667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
668
Ted Kremenekac597f32010-03-12 00:46:40 +0000669 if (MethodImplKind == tok::objc_required)
670 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
671 else if (MethodImplKind == tok::objc_optional)
672 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000673
Douglas Gregor813a0662015-06-19 18:14:38 +0000674 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
675 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
676
Douglas Gregor849ebc22015-06-19 18:14:46 +0000677 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
678 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
679
Ted Kremenek959e8302010-03-12 02:31:10 +0000680 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000681}
682
John McCall31168b02011-06-15 23:02:42 +0000683static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
684 ObjCPropertyDecl *property,
685 ObjCIvarDecl *ivar) {
686 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
687
John McCall31168b02011-06-15 23:02:42 +0000688 QualType ivarType = ivar->getType();
689 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000690
John McCall43192862011-09-13 18:31:23 +0000691 // The lifetime implied by the property's attributes.
692 Qualifiers::ObjCLifetime propertyLifetime =
693 getImpliedARCOwnership(property->getPropertyAttributes(),
694 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000695
John McCall43192862011-09-13 18:31:23 +0000696 // We're fine if they match.
697 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000698
John McCall460ce582015-10-22 18:38:17 +0000699 // None isn't a valid lifetime for an object ivar in ARC, and
700 // __autoreleasing is never valid; don't diagnose twice.
701 if ((ivarLifetime == Qualifiers::OCL_None &&
702 S.getLangOpts().ObjCAutoRefCount) ||
John McCall43192862011-09-13 18:31:23 +0000703 ivarLifetime == Qualifiers::OCL_Autoreleasing)
704 return;
John McCall31168b02011-06-15 23:02:42 +0000705
John McCalld8561f02012-08-20 23:36:59 +0000706 // If the ivar is private, and it's implicitly __unsafe_unretained
707 // becaues of its type, then pretend it was actually implicitly
708 // __strong. This is only sound because we're processing the
709 // property implementation before parsing any method bodies.
710 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
711 propertyLifetime == Qualifiers::OCL_Strong &&
712 ivar->getAccessControl() == ObjCIvarDecl::Private) {
713 SplitQualType split = ivarType.split();
714 if (split.Quals.hasObjCLifetime()) {
715 assert(ivarType->isObjCARCImplicitlyUnretainedType());
716 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
717 ivarType = S.Context.getQualifiedType(split);
718 ivar->setType(ivarType);
719 return;
720 }
721 }
722
John McCall43192862011-09-13 18:31:23 +0000723 switch (propertyLifetime) {
724 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000725 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000726 << property->getDeclName()
727 << ivar->getDeclName()
728 << ivarLifetime;
729 break;
John McCall31168b02011-06-15 23:02:42 +0000730
John McCall43192862011-09-13 18:31:23 +0000731 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000732 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000733 << property->getDeclName()
734 << ivar->getDeclName();
735 break;
John McCall31168b02011-06-15 23:02:42 +0000736
John McCall43192862011-09-13 18:31:23 +0000737 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000738 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000739 << property->getDeclName()
740 << ivar->getDeclName()
741 << ((property->getPropertyAttributesAsWritten()
742 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
743 break;
John McCall31168b02011-06-15 23:02:42 +0000744
John McCall43192862011-09-13 18:31:23 +0000745 case Qualifiers::OCL_Autoreleasing:
746 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000747
John McCall43192862011-09-13 18:31:23 +0000748 case Qualifiers::OCL_None:
749 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000750 return;
751 }
752
753 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000754 if (propertyImplLoc.isValid())
755 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000756}
757
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000758/// setImpliedPropertyAttributeForReadOnlyProperty -
759/// This routine evaludates life-time attributes for a 'readonly'
760/// property with no known lifetime of its own, using backing
761/// 'ivar's attribute, if any. If no backing 'ivar', property's
762/// life-time is assumed 'strong'.
763static void setImpliedPropertyAttributeForReadOnlyProperty(
764 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
765 Qualifiers::ObjCLifetime propertyLifetime =
766 getImpliedARCOwnership(property->getPropertyAttributes(),
767 property->getType());
768 if (propertyLifetime != Qualifiers::OCL_None)
769 return;
770
771 if (!ivar) {
772 // if no backing ivar, make property 'strong'.
773 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
774 return;
775 }
776 // property assumes owenership of backing ivar.
777 QualType ivarType = ivar->getType();
778 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
779 if (ivarLifetime == Qualifiers::OCL_Strong)
780 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
781 else if (ivarLifetime == Qualifiers::OCL_Weak)
782 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
783 return;
784}
Ted Kremenekac597f32010-03-12 00:46:40 +0000785
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000786/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
787/// in inherited protocols with mismatched types. Since any of them can
788/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000789static void
790DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
791 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000792 ObjCPropertyDecl *Property) {
793 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000794 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
795 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000796 PDecl->collectInheritedProtocolProperties(Property, PropMap);
797 }
798 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
799 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000800 for (const auto *PI : SDecl->all_referenced_protocols()) {
801 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000802 PDecl->collectInheritedProtocolProperties(Property, PropMap);
803 }
804 SDecl = SDecl->getSuperClass();
805 }
806
807 if (PropMap.empty())
808 return;
809
810 QualType RHSType = S.Context.getCanonicalType(Property->getType());
811 bool FirsTime = true;
812 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
813 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
814 ObjCPropertyDecl *Prop = I->second;
815 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
816 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
817 bool IncompatibleObjC = false;
818 QualType ConvertedType;
819 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
820 || IncompatibleObjC) {
821 if (FirsTime) {
822 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
823 << Property->getType();
824 FirsTime = false;
825 }
826 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
827 << Prop->getType();
828 }
829 }
830 }
831 if (!FirsTime && AtLoc.isValid())
832 S.Diag(AtLoc, diag::note_property_synthesize);
833}
834
Ted Kremenekac597f32010-03-12 00:46:40 +0000835/// ActOnPropertyImplDecl - This routine performs semantic checks and
836/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000837/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000838///
John McCall48871652010-08-21 09:40:31 +0000839Decl *Sema::ActOnPropertyImplDecl(Scope *S,
840 SourceLocation AtLoc,
841 SourceLocation PropertyLoc,
842 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000843 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000844 IdentifierInfo *PropertyIvar,
845 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000846 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000847 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000848 // Make sure we have a context for the property implementation declaration.
849 if (!ClassImpDecl) {
850 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000851 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000852 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000853 if (PropertyIvarLoc.isInvalid())
854 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000855 SourceLocation PropertyDiagLoc = PropertyLoc;
856 if (PropertyDiagLoc.isInvalid())
857 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000858 ObjCPropertyDecl *property = nullptr;
859 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000860 // Find the class or category class where this property must have
861 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000862 ObjCImplementationDecl *IC = nullptr;
863 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000864 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
865 IDecl = IC->getClassInterface();
866 // We always synthesize an interface for an implementation
867 // without an interface decl. So, IDecl is always non-zero.
868 assert(IDecl &&
869 "ActOnPropertyImplDecl - @implementation without @interface");
870
871 // Look for this property declaration in the @implementation's @interface
872 property = IDecl->FindPropertyDeclaration(PropertyId);
873 if (!property) {
874 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000875 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000876 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000877 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000878 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
879 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000880 if (AtLoc.isValid())
881 Diag(AtLoc, diag::warn_implicit_atomic_property);
882 else
883 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
884 Diag(property->getLocation(), diag::note_property_declare);
885 }
886
Ted Kremenekac597f32010-03-12 00:46:40 +0000887 if (const ObjCCategoryDecl *CD =
888 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
889 if (!CD->IsClassExtension()) {
890 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
891 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000892 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000893 }
894 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000895 if (Synthesize&&
896 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
897 property->hasAttr<IBOutletAttr>() &&
898 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000899 bool ReadWriteProperty = false;
900 // Search into the class extensions and see if 'readonly property is
901 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000902 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000903 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
904 if (!R.empty())
905 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
906 PIkind = ExtProp->getPropertyAttributesAsWritten();
907 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
908 ReadWriteProperty = true;
909 break;
910 }
911 }
912 }
913
914 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000915 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000916 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000917 SourceLocation readonlyLoc;
918 if (LocPropertyAttribute(Context, "readonly",
919 property->getLParenLoc(), readonlyLoc)) {
920 SourceLocation endLoc =
921 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
922 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
923 Diag(property->getLocation(),
924 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
925 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
926 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000927 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000928 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000929 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
930 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000931
Ted Kremenekac597f32010-03-12 00:46:40 +0000932 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
933 if (Synthesize) {
934 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000935 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000936 }
937 IDecl = CatImplClass->getClassInterface();
938 if (!IDecl) {
939 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000940 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000941 }
942 ObjCCategoryDecl *Category =
943 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
944
945 // If category for this implementation not found, it is an error which
946 // has already been reported eralier.
947 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000948 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000949 // Look for this property declaration in @implementation's category
950 property = Category->FindPropertyDeclaration(PropertyId);
951 if (!property) {
952 Diag(PropertyLoc, diag::error_bad_category_property_decl)
953 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000954 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000955 }
956 } else {
957 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000958 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000959 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000960 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000961 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000962 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000963 // Check that we have a valid, previously declared ivar for @synthesize
964 if (Synthesize) {
965 // @synthesize
966 if (!PropertyIvar)
967 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000968 // Check that this is a previously declared 'ivar' in 'IDecl' interface
969 ObjCInterfaceDecl *ClassDeclared;
970 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
971 QualType PropType = property->getType();
972 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000973
974 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000975 diag::err_incomplete_synthesized_property,
976 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000977 Diag(property->getLocation(), diag::note_property_declare);
978 CompleteTypeErr = true;
979 }
980
David Blaikiebbafb8a2012-03-11 07:00:24 +0000981 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000982 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000983 ObjCPropertyDecl::OBJC_PR_readonly) &&
984 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000985 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
986 }
987
John McCall31168b02011-06-15 23:02:42 +0000988 ObjCPropertyDecl::PropertyAttributeKind kind
989 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000990
John McCall460ce582015-10-22 18:38:17 +0000991 bool isARCWeak = false;
992 if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
993 // Add GC __weak to the ivar type if the property is weak.
994 if (getLangOpts().getGC() != LangOptions::NonGC) {
995 assert(!getLangOpts().ObjCAutoRefCount);
996 if (PropertyIvarType.isObjCGCStrong()) {
997 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
998 Diag(property->getLocation(), diag::note_property_declare);
999 } else {
1000 PropertyIvarType =
1001 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
1002 }
1003
1004 // Otherwise, check whether ARC __weak is enabled and works with
1005 // the property type.
John McCall43192862011-09-13 18:31:23 +00001006 } else {
John McCall460ce582015-10-22 18:38:17 +00001007 if (!getLangOpts().ObjCWeak) {
John McCallb61e14e2015-10-27 04:54:50 +00001008 // Only complain here when synthesizing an ivar.
1009 if (!Ivar) {
1010 Diag(PropertyDiagLoc,
1011 getLangOpts().ObjCWeakRuntime
1012 ? diag::err_synthesizing_arc_weak_property_disabled
1013 : diag::err_synthesizing_arc_weak_property_no_runtime);
1014 Diag(property->getLocation(), diag::note_property_declare);
John McCall460ce582015-10-22 18:38:17 +00001015 }
John McCallb61e14e2015-10-27 04:54:50 +00001016 CompleteTypeErr = true; // suppress later diagnostics about the ivar
John McCall460ce582015-10-22 18:38:17 +00001017 } else {
1018 isARCWeak = true;
1019 if (const ObjCObjectPointerType *ObjT =
1020 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1021 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1022 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1023 Diag(property->getLocation(),
1024 diag::err_arc_weak_unavailable_property)
1025 << PropertyIvarType;
1026 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1027 << ClassImpDecl->getName();
1028 }
1029 }
1030 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001031 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001032 }
John McCall460ce582015-10-22 18:38:17 +00001033
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001034 if (AtLoc.isInvalid()) {
1035 // Check when default synthesizing a property that there is
1036 // an ivar matching property name and issue warning; since this
1037 // is the most common case of not using an ivar used for backing
1038 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +00001039 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001040 ObjCIvarDecl *originalIvar =
1041 IDecl->lookupInstanceVariable(property->getIdentifier(),
1042 ClassDeclared);
1043 if (originalIvar) {
1044 Diag(PropertyDiagLoc,
1045 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +00001046 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +00001047 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001048 Diag(property->getLocation(), diag::note_property_declare);
1049 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001050 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001051 }
1052
1053 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001054 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001055 // property attributes.
John McCall460ce582015-10-22 18:38:17 +00001056 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) &&
John McCall43192862011-09-13 18:31:23 +00001057 !PropertyIvarType.getObjCLifetime() &&
1058 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001059
John McCall43192862011-09-13 18:31:23 +00001060 // It's an error if we have to do this and the user didn't
1061 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001062 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +00001063 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001064 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001065 diag::err_arc_objc_property_default_assign_on_object);
1066 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001067 } else {
1068 Qualifiers::ObjCLifetime lifetime =
1069 getImpliedARCOwnership(kind, PropertyIvarType);
1070 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001071
John McCall31168b02011-06-15 23:02:42 +00001072 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001073 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001074 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1075 }
John McCall31168b02011-06-15 23:02:42 +00001076 }
1077
Abramo Bagnaradff19302011-03-08 08:55:46 +00001078 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001079 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001080 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001081 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001082 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001083 if (RequireNonAbstractType(PropertyIvarLoc,
1084 PropertyIvarType,
1085 diag::err_abstract_type_in_decl,
1086 AbstractSynthesizedIvarType)) {
1087 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001088 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001089 } else if (CompleteTypeErr)
1090 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001091 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001092 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001093
John McCall5fb5df92012-06-20 06:18:46 +00001094 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001095 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1096 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001097 // Note! I deliberately want it to fall thru so, we have a
1098 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001099 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001100 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001101 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001102 << property->getDeclName() << Ivar->getDeclName()
1103 << ClassDeclared->getDeclName();
1104 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001105 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001106 // Note! I deliberately want it to fall thru so more errors are caught.
1107 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001108 property->setPropertyIvarDecl(Ivar);
1109
Ted Kremenekac597f32010-03-12 00:46:40 +00001110 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1111
1112 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001113 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001114 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001115 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001116 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001117 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001118 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001119 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001120 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001121 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1122 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001123 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001124 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001125 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001126 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001127 << property->getDeclName() << PropType
1128 << Ivar->getDeclName() << IvarType;
1129 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001130 // Note! I deliberately want it to fall thru so, we have a
1131 // a property implementation and to avoid future warnings.
1132 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001133 else {
1134 // FIXME! Rules for properties are somewhat different that those
1135 // for assignments. Use a new routine to consolidate all cases;
1136 // specifically for property redeclarations as well as for ivars.
1137 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1138 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1139 if (lhsType != rhsType &&
1140 lhsType->isArithmeticType()) {
1141 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1142 << property->getDeclName() << PropType
1143 << Ivar->getDeclName() << IvarType;
1144 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1145 // Fall thru - see previous comment
1146 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001147 }
1148 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001149 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001150 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001151 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001152 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001153 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001154 // Fall thru - see previous comment
1155 }
John McCall31168b02011-06-15 23:02:42 +00001156 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001157 if ((property->getType()->isObjCObjectPointerType() ||
1158 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001159 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001160 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001161 << property->getDeclName() << Ivar->getDeclName();
1162 // Fall thru - see previous comment
1163 }
1164 }
John McCall460ce582015-10-22 18:38:17 +00001165 if (getLangOpts().ObjCAutoRefCount || isARCWeak ||
1166 Ivar->getType().getObjCLifetime())
John McCall31168b02011-06-15 23:02:42 +00001167 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001168 } else if (PropertyIvar)
1169 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001170 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001171
Ted Kremenekac597f32010-03-12 00:46:40 +00001172 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1173 ObjCPropertyImplDecl *PIDecl =
1174 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1175 property,
1176 (Synthesize ?
1177 ObjCPropertyImplDecl::Synthesize
1178 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001179 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001180
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001181 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001182 PIDecl->setInvalidDecl();
1183
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001184 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1185 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001186 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001187 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001188 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1189 // returned by the getter as it must conform to C++'s copy-return rules.
1190 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001191 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001192 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1193 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001194 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001195 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001196 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001197 Expr *LoadSelfExpr =
1198 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001199 CK_LValueToRValue, SelfExpr, nullptr,
1200 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001201 Expr *IvarRefExpr =
Douglas Gregore83b9562015-07-07 03:57:53 +00001202 new (Context) ObjCIvarRefExpr(Ivar,
1203 Ivar->getUsageType(SelfDecl->getType()),
1204 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001205 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001206 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001207 ExprResult Res = PerformCopyInitialization(
1208 InitializedEntity::InitializeResult(PropertyDiagLoc,
1209 getterMethod->getReturnType(),
1210 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001211 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001212 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001213 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001214 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001215 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001216 PIDecl->setGetterCXXConstructor(ResExpr);
1217 }
1218 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001219 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1220 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1221 Diag(getterMethod->getLocation(),
1222 diag::warn_property_getter_owning_mismatch);
1223 Diag(property->getLocation(), diag::note_property_declare);
1224 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001225 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1226 switch (getterMethod->getMethodFamily()) {
1227 case OMF_retain:
1228 case OMF_retainCount:
1229 case OMF_release:
1230 case OMF_autorelease:
1231 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1232 << 1 << getterMethod->getSelector();
1233 break;
1234 default:
1235 break;
1236 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001237 }
1238 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1239 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001240 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1241 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001242 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001243 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001244 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1245 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001246 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001247 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001248 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001249 Expr *LoadSelfExpr =
1250 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001251 CK_LValueToRValue, SelfExpr, nullptr,
1252 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001253 Expr *lhs =
Douglas Gregore83b9562015-07-07 03:57:53 +00001254 new (Context) ObjCIvarRefExpr(Ivar,
1255 Ivar->getUsageType(SelfDecl->getType()),
1256 PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001257 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001258 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001259 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1260 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001261 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001262 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1263 VK_LValue, PropertyDiagLoc);
1264 MarkDeclRefReferenced(rhs);
1265 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001266 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001267 if (property->getPropertyAttributes() &
1268 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001269 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001270 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001271 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1272 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001273 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001274 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001275 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001276 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001277 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001278 Diag(FuncDecl->getLocStart(),
1279 diag::note_callee_decl) << FuncDecl;
1280 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001281 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001282 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001283 }
1284 }
1285
Ted Kremenekac597f32010-03-12 00:46:40 +00001286 if (IC) {
1287 if (Synthesize)
1288 if (ObjCPropertyImplDecl *PPIDecl =
1289 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1290 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1291 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1292 << PropertyIvar;
1293 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1294 }
1295
1296 if (ObjCPropertyImplDecl *PPIDecl
1297 = IC->FindPropertyImplDecl(PropertyId)) {
1298 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1299 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001300 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001301 }
1302 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001303 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001304 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001305 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001306 // Diagnose if an ivar was lazily synthesdized due to a previous
1307 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001308 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001309 ObjCInterfaceDecl *ClassDeclared=nullptr;
1310 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001311 if (!Synthesize)
1312 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1313 else {
1314 if (PropertyIvar && PropertyIvar != PropertyId)
1315 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1316 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001317 // Issue diagnostics only if Ivar belongs to current class.
1318 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001319 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001320 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1321 << PropertyId;
1322 Ivar->setInvalidDecl();
1323 }
1324 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001325 } else {
1326 if (Synthesize)
1327 if (ObjCPropertyImplDecl *PPIDecl =
1328 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001329 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001330 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1331 << PropertyIvar;
1332 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1333 }
1334
1335 if (ObjCPropertyImplDecl *PPIDecl =
1336 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001337 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001338 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001339 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001340 }
1341 CatImplClass->addPropertyImplementation(PIDecl);
1342 }
1343
John McCall48871652010-08-21 09:40:31 +00001344 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001345}
1346
1347//===----------------------------------------------------------------------===//
1348// Helper methods.
1349//===----------------------------------------------------------------------===//
1350
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001351/// DiagnosePropertyMismatch - Compares two properties for their
1352/// attributes and types and warns on a variety of inconsistencies.
1353///
1354void
1355Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1356 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001357 const IdentifierInfo *inheritedName,
1358 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001359 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001360 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001361 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001362 SuperProperty->getPropertyAttributes();
1363
1364 // We allow readonly properties without an explicit ownership
1365 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1366 // to be overridden by a property with any explicit ownership in the subclass.
1367 if (!OverridingProtocolProperty &&
1368 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1369 ;
1370 else {
1371 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1372 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1373 Diag(Property->getLocation(), diag::warn_readonly_property)
1374 << Property->getDeclName() << inheritedName;
1375 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1376 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001377 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001378 << Property->getDeclName() << "copy" << inheritedName;
1379 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1380 unsigned CAttrRetain =
1381 (CAttr &
1382 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1383 unsigned SAttrRetain =
1384 (SAttr &
1385 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1386 bool CStrong = (CAttrRetain != 0);
1387 bool SStrong = (SAttrRetain != 0);
1388 if (CStrong != SStrong)
1389 Diag(Property->getLocation(), diag::warn_property_attribute)
1390 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1391 }
John McCall31168b02011-06-15 23:02:42 +00001392 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001393
1394 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001395 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001396 Diag(Property->getLocation(), diag::warn_property_attribute)
1397 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001398 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1399 }
1400 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001401 Diag(Property->getLocation(), diag::warn_property_attribute)
1402 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001403 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1404 }
1405 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001406 Diag(Property->getLocation(), diag::warn_property_attribute)
1407 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001408 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1409 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001410
1411 QualType LHSType =
1412 Context.getCanonicalType(SuperProperty->getType());
1413 QualType RHSType =
1414 Context.getCanonicalType(Property->getType());
1415
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001416 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001417 // Do cases not handled in above.
1418 // FIXME. For future support of covariant property types, revisit this.
1419 bool IncompatibleObjC = false;
1420 QualType ConvertedType;
1421 if (!isObjCPointerConversion(RHSType, LHSType,
1422 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001423 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001424 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1425 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001426 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1427 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001428 }
1429}
1430
1431bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1432 ObjCMethodDecl *GetterMethod,
1433 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001434 if (!GetterMethod)
1435 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001436 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001437 QualType PropertyIvarType = property->getType().getNonReferenceType();
1438 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1439 if (!compat) {
1440 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1441 isa<ObjCObjectPointerType>(GetterType))
1442 compat =
1443 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001444 GetterType->getAs<ObjCObjectPointerType>(),
1445 PropertyIvarType->getAs<ObjCObjectPointerType>());
1446 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001447 != Compatible) {
1448 Diag(Loc, diag::error_property_accessor_type)
1449 << property->getDeclName() << PropertyIvarType
1450 << GetterMethod->getSelector() << GetterType;
1451 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1452 return true;
1453 } else {
1454 compat = true;
1455 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1456 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1457 if (lhsType != rhsType && lhsType->isArithmeticType())
1458 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001459 }
1460 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001461
1462 if (!compat) {
1463 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1464 << property->getDeclName()
1465 << GetterMethod->getSelector();
1466 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1467 return true;
1468 }
1469
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001470 return false;
1471}
1472
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001473/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001474/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001475static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1476 ObjCContainerDecl::PropertyMap &PropMap,
1477 ObjCContainerDecl::PropertyMap &SuperPropMap,
1478 bool IncludeProtocols = true) {
1479
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001480 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001481 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001482 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001483 if (IncludeProtocols) {
1484 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001485 for (auto *PI : IDecl->all_referenced_protocols())
1486 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001487 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001488 }
1489 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1490 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001491 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001492 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001493 if (IncludeProtocols) {
1494 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001495 for (auto *PI : CATDecl->protocols())
1496 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001497 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001498 }
1499 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001500 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001501 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1502 // Exclude property for protocols which conform to class's super-class,
1503 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001504 if (!PropertyFromSuper ||
1505 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001506 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1507 if (!PropEntry)
1508 PropEntry = Prop;
1509 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001510 }
1511 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001512 for (auto *PI : PDecl->protocols())
1513 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001514 }
1515}
1516
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001517/// CollectSuperClassPropertyImplementations - This routine collects list of
1518/// properties to be implemented in super class(s) and also coming from their
1519/// conforming protocols.
1520static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001521 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001522 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001523 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001524 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001525 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001526 SDecl = SDecl->getSuperClass();
1527 }
1528 }
1529}
1530
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001531/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1532/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1533/// declared in class 'IFace'.
1534bool
1535Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1536 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1537 if (!IV->getSynthesize())
1538 return false;
1539 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1540 Method->isInstanceMethod());
1541 if (!IMD || !IMD->isPropertyAccessor())
1542 return false;
1543
1544 // look up a property declaration whose one of its accessors is implemented
1545 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001546 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001547 if ((Property->getGetterName() == IMD->getSelector() ||
1548 Property->getSetterName() == IMD->getSelector()) &&
1549 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001550 return true;
1551 }
1552 return false;
1553}
1554
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001555static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1556 ObjCPropertyDecl *Prop) {
1557 bool SuperClassImplementsGetter = false;
1558 bool SuperClassImplementsSetter = false;
1559 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1560 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001561
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001562 while (IDecl->getSuperClass()) {
1563 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1564 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1565 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001566
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001567 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1568 SuperClassImplementsSetter = true;
1569 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1570 return true;
1571 IDecl = IDecl->getSuperClass();
1572 }
1573 return false;
1574}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001575
James Dennett2a4d13c2012-06-15 07:13:21 +00001576/// \brief Default synthesizes all properties which must be synthesized
1577/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001578void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1579 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001580
Anna Zaks673d76b2012-10-18 19:17:53 +00001581 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001582 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1583 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001584 if (PropMap.empty())
1585 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001586 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001587 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1588
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001589 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1590 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001591 // Is there a matching property synthesize/dynamic?
1592 if (Prop->isInvalidDecl() ||
1593 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1594 continue;
1595 // Property may have been synthesized by user.
1596 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1597 continue;
1598 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1599 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1600 continue;
1601 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1602 continue;
1603 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001604 if (ObjCPropertyImplDecl *PID =
1605 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001606 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1607 << Prop->getIdentifier();
Yaron Keren8b563662015-10-03 10:46:20 +00001608 if (PID->getLocation().isValid())
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001609 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001610 continue;
1611 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001612 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001613 if (ObjCProtocolDecl *Proto =
1614 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001615 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001616 // Suppress the warning if class's superclass implements property's
1617 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001618 // Or, if property is going to be implemented in its super class.
1619 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001620 Diag(IMPDecl->getLocation(),
1621 diag::warn_auto_synthesizing_protocol_property)
1622 << Prop << Proto;
1623 Diag(Prop->getLocation(), diag::note_property_declare);
1624 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001625 continue;
1626 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001627 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001628 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001629 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1630 (PropInSuperClass->getPropertyAttributes() &
1631 ObjCPropertyDecl::OBJC_PR_readonly) &&
1632 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1633 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1634 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1635 << Prop->getIdentifier();
1636 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1637 }
1638 else {
1639 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1640 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001641 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001642 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1643 }
1644 continue;
1645 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001646 // We use invalid SourceLocations for the synthesized ivars since they
1647 // aren't really synthesized at a particular location; they just exist.
1648 // Saying that they are located at the @implementation isn't really going
1649 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001650 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1651 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1652 true,
1653 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001654 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001655 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001656 if (PIDecl) {
1657 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001658 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001659 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001660 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001661}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001662
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001663void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001664 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001665 return;
1666 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1667 if (!IC)
1668 return;
1669 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001670 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001671 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001672}
1673
Ted Kremenek7e812952014-02-21 19:41:30 +00001674static void DiagnoseUnimplementedAccessor(Sema &S,
1675 ObjCInterfaceDecl *PrimaryClass,
1676 Selector Method,
1677 ObjCImplDecl* IMPDecl,
1678 ObjCContainerDecl *CDecl,
1679 ObjCCategoryDecl *C,
1680 ObjCPropertyDecl *Prop,
1681 Sema::SelectorSet &SMap) {
1682 // When reporting on missing property setter/getter implementation in
1683 // categories, do not report when they are declared in primary class,
1684 // class's protocol, or one of it super classes. This is because,
1685 // the class is going to implement them.
1686 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001687 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001688 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1689 S.Diag(IMPDecl->getLocation(),
1690 isa<ObjCCategoryDecl>(CDecl) ?
1691 diag::warn_setter_getter_impl_required_in_category :
1692 diag::warn_setter_getter_impl_required)
1693 << Prop->getDeclName() << Method;
1694 S.Diag(Prop->getLocation(),
1695 diag::note_property_declare);
1696 if (S.LangOpts.ObjCDefaultSynthProperties &&
1697 S.LangOpts.ObjCRuntime.isNonFragile())
1698 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1699 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1700 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1701 }
1702}
1703
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001704void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001705 ObjCContainerDecl *CDecl,
1706 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001707 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001708 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1709
Ted Kremenek348e88c2014-02-21 19:41:34 +00001710 if (!SynthesizeProperties) {
1711 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001712 // Gather properties which need not be implemented in this class
1713 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001714 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001715 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1716 // For categories, no need to implement properties declared in
1717 // its primary class (and its super classes) if property is
1718 // declared in one of those containers.
1719 if ((IDecl = C->getClassInterface())) {
1720 ObjCInterfaceDecl::PropertyDeclOrder PO;
1721 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1722 }
1723 }
1724 if (IDecl)
1725 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1726
1727 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1728 }
1729
Ted Kremenek38882022014-02-21 19:41:39 +00001730 // Scan the @interface to see if any of the protocols it adopts
1731 // require an explicit implementation, via attribute
1732 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001733 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001734 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001735
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001736 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001737 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1738 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001739 // Lazily construct a set of all the properties in the @interface
1740 // of the class, without looking at the superclass. We cannot
1741 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001742 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001743 // as scans the adopted protocols. This work only triggers for protocols
1744 // with the attribute, which is very rare, and only occurs when
1745 // analyzing the @implementation.
1746 if (!LazyMap) {
1747 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1748 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1749 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1750 /* IncludeProtocols */ false);
1751 }
Ted Kremenek38882022014-02-21 19:41:39 +00001752 // Add the properties of 'PDecl' to the list of properties that
1753 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001754 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001755 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001756 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001757 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001758 }
1759 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001760 }
Ted Kremenek38882022014-02-21 19:41:39 +00001761
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001762 if (PropMap.empty())
1763 return;
1764
1765 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001766 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001767 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001768
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001769 SelectorSet InsMap;
1770 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001771 for (const auto *I : IMPDecl->instance_methods())
1772 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001773
1774 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001775 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001776 if (C && !C->IsClassExtension())
1777 if ((PrimaryClass = C->getClassInterface()))
1778 // Report unimplemented properties in the category as well.
1779 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1780 // When reporting on missing setter/getters, do not report when
1781 // setter/getter is implemented in category's primary class
1782 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001783 for (const auto *I : IMP->instance_methods())
1784 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001785 }
1786
Anna Zaks673d76b2012-10-18 19:17:53 +00001787 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001788 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1789 ObjCPropertyDecl *Prop = P->second;
1790 // Is there a matching propery synthesize/dynamic?
1791 if (Prop->isInvalidDecl() ||
1792 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001793 PropImplMap.count(Prop) ||
1794 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001795 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001796
1797 // Diagnose unimplemented getters and setters.
1798 DiagnoseUnimplementedAccessor(*this,
1799 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1800 if (!Prop->isReadOnly())
1801 DiagnoseUnimplementedAccessor(*this,
1802 PrimaryClass, Prop->getSetterName(),
1803 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001804 }
1805}
1806
Douglas Gregoraea7afd2015-06-24 22:02:08 +00001807void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00001808 for (const auto *propertyImpl : impDecl->property_impls()) {
1809 const auto *property = propertyImpl->getPropertyDecl();
1810
1811 // Warn about null_resettable properties with synthesized setters,
1812 // because the setter won't properly handle nil.
1813 if (propertyImpl->getPropertyImplementation()
1814 == ObjCPropertyImplDecl::Synthesize &&
1815 (property->getPropertyAttributes() &
1816 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1817 property->getGetterMethodDecl() &&
1818 property->getSetterMethodDecl()) {
1819 auto *getterMethod = property->getGetterMethodDecl();
1820 auto *setterMethod = property->getSetterMethodDecl();
1821 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1822 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1823 SourceLocation loc = propertyImpl->getLocation();
1824 if (loc.isInvalid())
1825 loc = impDecl->getLocStart();
1826
1827 Diag(loc, diag::warn_null_resettable_setter)
1828 << setterMethod->getSelector() << property->getDeclName();
1829 }
1830 }
1831 }
1832}
1833
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001834void
1835Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1836 ObjCContainerDecl* IDecl) {
1837 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001838 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001839 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001840 for (const auto *Property : IDecl->properties()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001841 ObjCMethodDecl *GetterMethod = nullptr;
1842 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001843 bool LookedUpGetterSetter = false;
1844
Bill Wendling44426052012-12-20 19:22:21 +00001845 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001846 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001847
John McCall43192862011-09-13 18:31:23 +00001848 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1849 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001850 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1851 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1852 LookedUpGetterSetter = true;
1853 if (GetterMethod) {
1854 Diag(GetterMethod->getLocation(),
1855 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001856 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001857 Diag(Property->getLocation(), diag::note_property_declare);
1858 }
1859 if (SetterMethod) {
1860 Diag(SetterMethod->getLocation(),
1861 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001862 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001863 Diag(Property->getLocation(), diag::note_property_declare);
1864 }
1865 }
1866
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001867 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001868 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1869 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001870 continue;
1871 if (const ObjCPropertyImplDecl *PIDecl
1872 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1873 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1874 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001875 if (!LookedUpGetterSetter) {
1876 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1877 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001878 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001879 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1880 SourceLocation MethodLoc =
1881 (GetterMethod ? GetterMethod->getLocation()
1882 : SetterMethod->getLocation());
1883 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001884 << Property->getIdentifier() << (GetterMethod != nullptr)
1885 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001886 // fixit stuff.
1887 if (!AttributesAsWritten) {
1888 if (Property->getLParenLoc().isValid()) {
1889 // @property () ... case.
1890 SourceRange PropSourceRange(Property->getAtLoc(),
1891 Property->getLParenLoc());
1892 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1893 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1894 }
1895 else {
1896 //@property id etc.
1897 SourceLocation endLoc =
1898 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1899 endLoc = endLoc.getLocWithOffset(-1);
1900 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1901 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1902 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1903 }
1904 }
1905 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1906 // @property () ... case.
1907 SourceLocation endLoc = Property->getLParenLoc();
1908 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1909 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1910 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1911 }
1912 else
1913 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001914 Diag(Property->getLocation(), diag::note_property_declare);
1915 }
1916 }
1917 }
1918}
1919
John McCall31168b02011-06-15 23:02:42 +00001920void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001921 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001922 return;
1923
Aaron Ballmand85eff42014-03-14 15:02:45 +00001924 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001925 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001926 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1927 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001928 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1929 if (!method)
1930 continue;
1931 ObjCMethodFamily family = method->getMethodFamily();
1932 if (family == OMF_alloc || family == OMF_copy ||
1933 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001934 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001935 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001936 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001937 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001938
1939 // Look for a getter explicitly declared alongside the property.
1940 // If we find one, use its location for the note.
1941 SourceLocation noteLoc = PD->getLocation();
1942 SourceLocation fixItLoc;
1943 for (auto *getterRedecl : method->redecls()) {
1944 if (getterRedecl->isImplicit())
1945 continue;
1946 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1947 continue;
1948 noteLoc = getterRedecl->getLocation();
1949 fixItLoc = getterRedecl->getLocEnd();
1950 }
1951
1952 Preprocessor &PP = getPreprocessor();
1953 TokenValue tokens[] = {
1954 tok::kw___attribute, tok::l_paren, tok::l_paren,
1955 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1956 PP.getIdentifierInfo("none"), tok::r_paren,
1957 tok::r_paren, tok::r_paren
1958 };
1959 StringRef spelling = "__attribute__((objc_method_family(none)))";
1960 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1961 if (!macroName.empty())
1962 spelling = macroName;
1963
1964 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1965 << method->getDeclName() << spelling;
1966 if (fixItLoc.isValid()) {
1967 SmallString<64> fixItText(" ");
1968 fixItText += spelling;
1969 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1970 }
John McCall31168b02011-06-15 23:02:42 +00001971 }
1972 }
1973 }
1974}
1975
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001976void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001977 const ObjCImplementationDecl *ImplD,
1978 const ObjCInterfaceDecl *IFD) {
1979 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001980 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1981 if (!SuperD)
1982 return;
1983
1984 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001985 for (const auto *I : ImplD->instance_methods())
1986 if (I->getMethodFamily() == OMF_init)
1987 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001988
1989 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1990 SuperD->getDesignatedInitializers(DesignatedInits);
1991 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1992 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1993 const ObjCMethodDecl *MD = *I;
1994 if (!InitSelSet.count(MD->getSelector())) {
Argyrios Kyrtzidisc0d4b00f2015-07-30 19:06:04 +00001995 bool Ignore = false;
1996 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) {
1997 Ignore = IMD->isUnavailable();
1998 }
1999 if (!Ignore) {
2000 Diag(ImplD->getLocation(),
2001 diag::warn_objc_implementation_missing_designated_init_override)
2002 << MD->getSelector();
2003 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
2004 }
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002005 }
2006 }
2007}
2008
John McCallad31b5f2010-11-10 07:01:40 +00002009/// AddPropertyAttrs - Propagates attributes from a property to the
2010/// implicitly-declared getter or setter for that property.
2011static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
2012 ObjCPropertyDecl *Property) {
2013 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00002014 for (const auto *A : Property->attrs()) {
2015 if (isa<DeprecatedAttr>(A) ||
2016 isa<UnavailableAttr>(A) ||
2017 isa<AvailabilityAttr>(A))
2018 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002019 }
John McCallad31b5f2010-11-10 07:01:40 +00002020}
2021
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002022/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
2023/// have the property type and issue diagnostics if they don't.
2024/// Also synthesize a getter/setter method if none exist (and update the
2025/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
2026/// methods is the "right" thing to do.
2027void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002028 ObjCContainerDecl *CD,
2029 ObjCPropertyDecl *redeclaredProperty,
2030 ObjCContainerDecl *lexicalDC) {
2031
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002032 ObjCMethodDecl *GetterMethod, *SetterMethod;
2033
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00002034 if (CD->isInvalidDecl())
2035 return;
2036
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002037 GetterMethod = CD->getInstanceMethod(property->getGetterName());
2038 SetterMethod = CD->getInstanceMethod(property->getSetterName());
2039 DiagnosePropertyAccessorMismatch(property, GetterMethod,
2040 property->getLocation());
2041
2042 if (SetterMethod) {
2043 ObjCPropertyDecl::PropertyAttributeKind CAttr =
2044 property->getPropertyAttributes();
2045 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00002046 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2047 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002048 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2049 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00002050 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00002051 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2052 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002053 Diag(property->getLocation(),
2054 diag::warn_accessor_property_type_mismatch)
2055 << property->getDeclName()
2056 << SetterMethod->getSelector();
2057 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2058 }
2059 }
2060
2061 // Synthesize getter/setter methods if none exist.
2062 // Find the default getter and if one not found, add one.
2063 // FIXME: The synthesized property we set here is misleading. We almost always
2064 // synthesize these methods unless the user explicitly provided prototypes
2065 // (which is odd, but allowed). Sema should be typechecking that the
2066 // declarations jive in that situation (which it is not currently).
2067 if (!GetterMethod) {
2068 // No instance method of same name as property getter name was found.
2069 // Declare a getter method and add it to the list of methods
2070 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00002071 SourceLocation Loc = redeclaredProperty ?
2072 redeclaredProperty->getLocation() :
2073 property->getLocation();
2074
Douglas Gregor849ebc22015-06-19 18:14:46 +00002075 // If the property is null_resettable, the getter returns nonnull.
2076 QualType resultTy = property->getType();
2077 if (property->getPropertyAttributes() &
2078 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2079 QualType modifiedTy = resultTy;
Douglas Gregoraea7afd2015-06-24 22:02:08 +00002080 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
Douglas Gregor849ebc22015-06-19 18:14:46 +00002081 if (*nullability == NullabilityKind::Unspecified)
2082 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2083 modifiedTy, modifiedTy);
2084 }
2085 }
2086
Ted Kremenek2f075632010-09-21 20:52:59 +00002087 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2088 property->getGetterName(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002089 resultTy, nullptr, CD,
Craig Topperc3ec1492014-05-26 06:22:03 +00002090 /*isInstance=*/true, /*isVariadic=*/false,
2091 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002092 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002093 (property->getPropertyImplementation() ==
2094 ObjCPropertyDecl::Optional) ?
2095 ObjCMethodDecl::Optional :
2096 ObjCMethodDecl::Required);
2097 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002098
2099 AddPropertyAttrs(*this, GetterMethod, property);
2100
Ted Kremenek49be9e02010-05-18 21:09:07 +00002101 // FIXME: Eventually this shouldn't be needed, as the lexical context
2102 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00002103 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002104 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002105 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002106 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2107 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002108
2109 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2110 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002111 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002112
2113 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002114 GetterMethod->addAttr(
2115 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2116 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002117
2118 if (getLangOpts().ObjCAutoRefCount)
2119 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002120 } else
2121 // A user declared getter will be synthesize when @synthesize of
2122 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002123 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002124 property->setGetterMethodDecl(GetterMethod);
2125
2126 // Skip setter if property is read-only.
2127 if (!property->isReadOnly()) {
2128 // Find the default setter and if one not found, add one.
2129 if (!SetterMethod) {
2130 // No instance method of same name as property setter name was found.
2131 // Declare a setter method and add it to the list of methods
2132 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002133 SourceLocation Loc = redeclaredProperty ?
2134 redeclaredProperty->getLocation() :
2135 property->getLocation();
2136
2137 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002138 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002139 property->getSetterName(), Context.VoidTy,
2140 nullptr, CD, /*isInstance=*/true,
2141 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002142 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002143 /*isImplicitlyDeclared=*/true,
2144 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002145 (property->getPropertyImplementation() ==
2146 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002147 ObjCMethodDecl::Optional :
2148 ObjCMethodDecl::Required);
2149
Douglas Gregor849ebc22015-06-19 18:14:46 +00002150 // If the property is null_resettable, the setter accepts a
2151 // nullable value.
2152 QualType paramTy = property->getType().getUnqualifiedType();
2153 if (property->getPropertyAttributes() &
2154 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2155 QualType modifiedTy = paramTy;
2156 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2157 if (*nullability == NullabilityKind::Unspecified)
2158 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2159 modifiedTy, modifiedTy);
2160 }
2161 }
2162
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002163 // Invent the arguments for the setter. We don't bother making a
2164 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002165 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2166 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002167 property->getIdentifier(),
Douglas Gregor849ebc22015-06-19 18:14:46 +00002168 paramTy,
Craig Topperc3ec1492014-05-26 06:22:03 +00002169 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002170 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002171 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002172 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002173
2174 AddPropertyAttrs(*this, SetterMethod, property);
2175
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002176 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002177 // FIXME: Eventually this shouldn't be needed, as the lexical context
2178 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002179 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002180 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002181 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002182 SetterMethod->addAttr(
2183 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2184 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002185 // It's possible for the user to have set a very odd custom
2186 // setter selector that causes it to have a method family.
2187 if (getLangOpts().ObjCAutoRefCount)
2188 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002189 } else
2190 // A user declared setter will be synthesize when @synthesize of
2191 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002192 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002193 property->setSetterMethodDecl(SetterMethod);
2194 }
2195 // Add any synthesized methods to the global pool. This allows us to
2196 // handle the following, which is supported by GCC (and part of the design).
2197 //
2198 // @interface Foo
2199 // @property double bar;
2200 // @end
2201 //
2202 // void thisIsUnfortunate() {
2203 // id foo;
2204 // double bar = [foo bar];
2205 // }
2206 //
2207 if (GetterMethod)
2208 AddInstanceMethodToGlobalPool(GetterMethod);
2209 if (SetterMethod)
2210 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002211
2212 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2213 if (!CurrentClass) {
2214 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2215 CurrentClass = Cat->getClassInterface();
2216 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2217 CurrentClass = Impl->getClassInterface();
2218 }
2219 if (GetterMethod)
2220 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2221 if (SetterMethod)
2222 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002223}
2224
John McCall48871652010-08-21 09:40:31 +00002225void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002226 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002227 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002228 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002229 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002230 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002231 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002232
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002233 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2234 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2235 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2236 << "readonly" << "readwrite";
2237
2238 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2239 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002240
2241 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002242 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002243 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2244 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002245 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002246 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002247 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2248 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2249 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002250 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002251 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002252 }
2253
2254 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002255 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2256 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002257 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2258 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002259 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002260 }
Bill Wendling44426052012-12-20 19:22:21 +00002261 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002262 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2263 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002264 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002265 }
Bill Wendling44426052012-12-20 19:22:21 +00002266 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002267 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2268 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002269 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002270 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002271 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002272 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002273 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2274 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002275 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002276 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002277 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002278 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002279 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2280 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002281 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2282 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002283 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002284 }
Bill Wendling44426052012-12-20 19:22:21 +00002285 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002286 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2287 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002288 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002289 }
Bill Wendling44426052012-12-20 19:22:21 +00002290 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002291 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2292 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002293 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002294 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002295 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002296 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002297 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2298 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002299 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002300 }
Bill Wendling44426052012-12-20 19:22:21 +00002301 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2302 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002303 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2304 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002305 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002306 }
Bill Wendling44426052012-12-20 19:22:21 +00002307 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002308 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2309 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002310 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002311 }
Bill Wendling44426052012-12-20 19:22:21 +00002312 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002313 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2314 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002315 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002316 }
2317 }
Bill Wendling44426052012-12-20 19:22:21 +00002318 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2319 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002320 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2321 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002322 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002323 }
Bill Wendling44426052012-12-20 19:22:21 +00002324 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2325 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002326 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2327 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002328 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002329 }
2330
Douglas Gregor2a20bd12015-06-19 18:25:57 +00002331 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
Douglas Gregor813a0662015-06-19 18:14:38 +00002332 // 'weak' and 'nonnull' are mutually exclusive.
2333 if (auto nullability = PropertyTy->getNullability(Context)) {
2334 if (*nullability == NullabilityKind::NonNull)
2335 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2336 << "nonnull" << "weak";
Douglas Gregor813a0662015-06-19 18:14:38 +00002337 }
2338 }
2339
Bill Wendling44426052012-12-20 19:22:21 +00002340 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2341 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002342 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2343 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002344 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002345 }
2346
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002347 // Warn if user supplied no assignment attribute, property is
2348 // readwrite, and this is an object type.
John McCallb61e14e2015-10-27 04:54:50 +00002349 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) {
2350 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2351 // do nothing
2352 } else if (getLangOpts().ObjCAutoRefCount) {
2353 // With arc, @property definitions should default to strong when
2354 // not specified.
2355 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2356 } else if (PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002357 bool isAnyClassTy =
2358 (PropertyTy->isObjCClassType() ||
2359 PropertyTy->isObjCQualifiedClassType());
2360 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2361 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002362 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002363 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002364 else if (propertyInPrimaryClass) {
2365 // Don't issue warning on property with no life time in class
2366 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002367 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002368 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002369 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002370
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002371 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002372 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002373 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002374 }
John McCallb61e14e2015-10-27 04:54:50 +00002375 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002376
2377 // FIXME: Implement warning dependent on NSCopying being
2378 // implemented. See also:
2379 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2380 // (please trim this list while you are at it).
2381 }
2382
Bill Wendling44426052012-12-20 19:22:21 +00002383 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2384 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002385 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002386 && PropertyTy->isBlockPointerType())
2387 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002388 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2389 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2390 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002391 PropertyTy->isBlockPointerType())
2392 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002393
Bill Wendling44426052012-12-20 19:22:21 +00002394 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2395 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002396 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2397
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002398}