blob: 5e7b4b8b859dff92e060c4ed6871876ca91f10fb [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 McCall31168b02011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb8982092013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
Craig Topper4dd9b432014-08-17 23:49:53 +0000120 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000121 // Have we seen this protocol before?
David Blaikie82e95a32014-11-19 07:49:47 +0000122 if (!Known.insert(Proto).second)
Douglas Gregorb8982092013-01-21 19:42:21 +0000123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000135 for (auto *P : Proto->protocols())
136 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000137}
138
John McCall48871652010-08-21 09:40:31 +0000139Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000140 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000141 FieldDeclarator &FD,
142 ObjCDeclSpec &ODS,
143 Selector GetterSel,
144 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000145 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000146 tok::ObjCKeywordKind MethodImplKind,
147 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000148 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000149 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
150 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000151 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000152
Bill Wendling44426052012-12-20 19:22:21 +0000153 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000154 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000155 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000156 // property is defaulted to 'assign' if it is readwrite and is
157 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000158 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000159 (isReadWrite &&
Bill Wendling44426052012-12-20 19:22:21 +0000160 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
161 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
162 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
163 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000165
Douglas Gregor90d34422013-01-21 19:05:22 +0000166 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000167 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Craig Topperc3ec1492014-05-26 06:22:03 +0000168 ObjCPropertyDecl *Res = nullptr;
Douglas Gregor90d34422013-01-21 19:05:22 +0000169 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000170 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000171 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000172 FD, GetterSel, SetterSel,
173 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000174 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000175 ODS.getPropertyAttributes(),
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000176 isOverridingProperty, TSI,
177 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000178 if (!Res)
Craig Topperc3ec1492014-05-26 06:22:03 +0000179 return nullptr;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000180 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000181 }
182
183 if (!Res) {
184 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
185 GetterSel, SetterSel, isAssign, isReadWrite,
186 Attributes, ODS.getPropertyAttributes(),
187 TSI, MethodImplKind);
188 if (lexicalDC)
189 Res->setLexicalDeclContext(lexicalDC);
190 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000191
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000192 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000193 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000194 (isa<ObjCInterfaceDecl>(ClassDecl) ||
195 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000196
David Blaikiebbafb8a2012-03-11 07:00:24 +0000197 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000198 checkARCPropertyDecl(*this, Res);
199
Douglas Gregorb8982092013-01-21 19:42:21 +0000200 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000201 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000202 // For a class, compare the property against a property in our superclass.
203 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000204 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
205 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000206 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000207 for (unsigned I = 0, N = R.size(); I != N; ++I) {
208 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000209 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000210 FoundInSuper = true;
211 break;
212 }
213 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000214 if (FoundInSuper)
215 break;
216 else
217 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000218 }
219
220 if (FoundInSuper) {
221 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000222 for (auto *P : CurrentInterfaceDecl->protocols()) {
223 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000227 for (auto *P : IFace->all_referenced_protocols()) {
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000229 }
230 }
231 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Aaron Ballman19a41762014-03-14 12:55:57 +0000232 for (auto *P : Cat->protocols())
233 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000234 } else {
235 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000236 for (auto *P : Proto->protocols())
237 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000238 }
239
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000240 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000241 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000242}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000243
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000244static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000245makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000246 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000247 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000248 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000249 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000250 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000251 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000252 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000253 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000254 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000255 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000256 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000257 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000258 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000259 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000260 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000261 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000262 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000263 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000264 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000265 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000266 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000267 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000268 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000269 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000270 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
271
272 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
273}
274
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000275static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000276 SourceLocation LParenLoc, SourceLocation &Loc) {
277 if (LParenLoc.isMacroID())
278 return false;
279
280 SourceManager &SM = Context.getSourceManager();
281 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
282 // Try to load the file buffer.
283 bool invalidTemp = false;
284 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
285 if (invalidTemp)
286 return false;
287 const char *tokenBegin = file.data() + locInfo.second;
288
289 // Lex from the start of the given location.
290 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
291 Context.getLangOpts(),
292 file.begin(), tokenBegin, file.end());
293 Token Tok;
294 do {
295 lexer.LexFromRawLexer(Tok);
Alp Toker2d57cea2014-05-17 04:53:25 +0000296 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000297 Loc = Tok.getLocation();
298 return true;
299 }
300 } while (Tok.isNot(tok::r_paren));
301 return false;
302
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000303}
304
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000305static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000306 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
307 ObjCPropertyDecl::OBJC_PR_retain |
308 ObjCPropertyDecl::OBJC_PR_copy |
309 ObjCPropertyDecl::OBJC_PR_weak |
310 ObjCPropertyDecl::OBJC_PR_strong |
311 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
312}
313
Douglas Gregor90d34422013-01-21 19:05:22 +0000314ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000315Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000316 SourceLocation AtLoc,
317 SourceLocation LParenLoc,
318 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000319 Selector GetterSel, Selector SetterSel,
320 const bool isAssign,
321 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000322 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000323 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000324 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000325 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000326 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000327 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000328 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000329 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000330 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000331 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
332
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000333 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000334 // Check for duplicate declaration of this property in current and
335 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000336 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000337 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000338 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000339 Diag(AtLoc, diag::err_duplicate_property);
340 Diag(prevDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000341 return nullptr;
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000342 }
343 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000344 }
345
Ted Kremenek959e8302010-03-12 02:31:10 +0000346 // Create a new ObjCPropertyDecl with the DeclContext being
347 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000348 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000349 ObjCPropertyDecl *PDecl =
350 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000351 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000352 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000353 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000354 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000356 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000358 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
359 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
360 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
361 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000362 // Set setter/getter selector name. Needed later.
363 PDecl->setGetterName(GetterSel);
364 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000365 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000366 DC->addDecl(PDecl);
367
368 // We need to look in the @interface to see if the @property was
369 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000370 if (!CCPrimary) {
371 Diag(CDecl->getLocation(), diag::err_continuation_class);
372 *isOverridingProperty = true;
Craig Topperc3ec1492014-05-26 06:22:03 +0000373 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000374 }
375
376 // Find the property in continuation class's primary class only.
377 ObjCPropertyDecl *PIDecl =
378 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
379
380 if (!PIDecl) {
381 // No matching property found in the primary class. Just fall thru
382 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000383 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000384 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000385 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000386 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000387
388 // A case of continuation class adding a new property in the class. This
389 // is not what it was meant for. However, gcc supports it and so should we.
390 // Make sure setter/getters are declared here.
Craig Topperc3ec1492014-05-26 06:22:03 +0000391 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
392 /* redeclaredProperty = */ nullptr,
Ted Kremenek2f075632010-09-21 20:52:59 +0000393 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000394 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
395 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000396 if (ASTMutationListener *L = Context.getASTMutationListener())
Craig Topperc3ec1492014-05-26 06:22:03 +0000397 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
398 CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000399 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000400 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000401 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
402 bool IncompatibleObjC = false;
403 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000404 // Relax the strict type matching for property type in continuation class.
405 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000406 // as it narrows the object type in its primary class property. Note that
407 // this conversion is safe only because the wider type is for a 'readonly'
408 // property in primary class and 'narrowed' type for a 'readwrite' property
409 // in continuation class.
Fariborz Jahanian576ff122015-04-08 21:34:04 +0000410 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
411 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
412 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
413 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
414 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000415 ConvertedType, IncompatibleObjC))
416 || IncompatibleObjC) {
417 Diag(AtLoc,
418 diag::err_type_mismatch_continuation_class) << PDecl->getType();
419 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000420 return nullptr;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000421 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000422 }
423
Ted Kremenek959e8302010-03-12 02:31:10 +0000424 // The property 'PIDecl's readonly attribute will be over-ridden
425 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000426 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000427 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000428 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
429 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000430 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000431 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000432 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000433 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
434 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000435 Diag(AtLoc, diag::warn_property_attr_mismatch);
436 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000437 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000438 else if (getLangOpts().ObjCAutoRefCount) {
439 QualType PrimaryPropertyQT =
440 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
441 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000442 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000443 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
444 PrimaryPropertyQT.getObjCLifetime();
445 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000446 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
447 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000448 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
449 Diag(PIDecl->getLocation(), diag::note_property_declare);
450 }
451 }
452 }
453
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000454 DeclContext *DC = cast<DeclContext>(CCPrimary);
455 if (!ObjCPropertyDecl::findPropertyDecl(DC,
456 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000457 // In mrr mode, 'readwrite' property must have an explicit
458 // memory attribute. If none specified, select the default (assign).
459 if (!getLangOpts().ObjCAutoRefCount) {
460 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
461 ObjCDeclSpec::DQ_PR_retain |
462 ObjCDeclSpec::DQ_PR_strong |
463 ObjCDeclSpec::DQ_PR_copy |
464 ObjCDeclSpec::DQ_PR_unsafe_unretained |
465 ObjCDeclSpec::DQ_PR_weak)))
466 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
467 }
468
Ted Kremenek959e8302010-03-12 02:31:10 +0000469 // Protocol is not in the primary class. Must build one for it.
470 ObjCDeclSpec ProtocolPropertyODS;
471 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
472 // and ObjCPropertyDecl::PropertyAttributeKind have identical
473 // values. Should consolidate both into one enum type.
474 ProtocolPropertyODS.
475 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
476 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000477 // Must re-establish the context from class extension to primary
478 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000479 ContextRAII SavedContext(*this, CCPrimary);
480
John McCall48871652010-08-21 09:40:31 +0000481 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000482 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000483 PIDecl->getGetterName(),
484 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000485 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000486 MethodImplKind,
487 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000488 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000489 }
490 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000493 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000494 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000495 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000496 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
497 PIDecl->setSetterName(SetterSel);
498 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000499 // Tailor the diagnostics for the common case where a readwrite
500 // property is declared both in the @interface and the continuation.
501 // This is a common error where the user often intended the original
502 // declaration to be readonly.
503 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000504 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000505 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
506 ? diag::err_use_continuation_class_redeclaration_readwrite
507 : diag::err_use_continuation_class;
508 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000509 << CCPrimary->getDeclName();
510 Diag(PIDecl->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000511 return nullptr;
Ted Kremenek959e8302010-03-12 02:31:10 +0000512 }
513 *isOverridingProperty = true;
514 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000515 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000516 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
517 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000518 if (ASTMutationListener *L = Context.getASTMutationListener())
519 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000520 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000521}
522
523ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
524 ObjCContainerDecl *CDecl,
525 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000526 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000527 FieldDeclarator &FD,
528 Selector GetterSel,
529 Selector SetterSel,
530 const bool isAssign,
531 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000532 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000533 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000534 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000535 tok::ObjCKeywordKind MethodImplKind,
536 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000537 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000538 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000539
540 // Issue a warning if property is 'assign' as default and its object, which is
541 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000542 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000543 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000544 if (const ObjCObjectPointerType *ObjPtrTy =
545 T->getAs<ObjCObjectPointerType>()) {
546 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
547 if (IDecl)
548 if (ObjCProtocolDecl* PNSCopying =
549 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
550 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
551 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000552 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000553
554 if (T->isObjCObjectType()) {
555 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000556 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000557 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
558 << FixItHint::CreateInsertion(StarLoc, "*");
559 T = Context.getObjCObjectPointerType(T);
560 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
561 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
562 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000563
Ted Kremenek959e8302010-03-12 02:31:10 +0000564 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000565 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
566 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000567 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000568
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000569 if (ObjCPropertyDecl *prevDecl =
570 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000571 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000572 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000573 PDecl->setInvalidDecl();
574 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000575 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000576 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000577 if (lexicalDC)
578 PDecl->setLexicalDeclContext(lexicalDC);
579 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000580
581 if (T->isArrayType() || T->isFunctionType()) {
582 Diag(AtLoc, diag::err_property_type) << T;
583 PDecl->setInvalidDecl();
584 }
585
586 ProcessDeclAttributes(S, PDecl, FD.D);
587
588 // Regardless of setter/getter attribute, we save the default getter/setter
589 // selector names in anticipation of declaration of setter/getter methods.
590 PDecl->setGetterName(GetterSel);
591 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000592 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000593 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000594
Bill Wendling44426052012-12-20 19:22:21 +0000595 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
597
Bill Wendling44426052012-12-20 19:22:21 +0000598 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
600
Bill Wendling44426052012-12-20 19:22:21 +0000601 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
603
604 if (isReadWrite)
605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
606
Bill Wendling44426052012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
609
Bill Wendling44426052012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
612
Bill Wendling44426052012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
618
Bill Wendling44426052012-12-20 19:22:21 +0000619 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
621
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 if (isAssign)
623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
624
John McCall43192862011-09-13 18:31:23 +0000625 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000626 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000628 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000630
John McCall31168b02011-06-15 23:02:42 +0000631 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000632 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
634 if (isAssign)
635 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
636
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 if (MethodImplKind == tok::objc_required)
638 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
639 else if (MethodImplKind == tok::objc_optional)
640 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000641
Ted Kremenek959e8302010-03-12 02:31:10 +0000642 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000643}
644
John McCall31168b02011-06-15 23:02:42 +0000645static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
646 ObjCPropertyDecl *property,
647 ObjCIvarDecl *ivar) {
648 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
649
John McCall31168b02011-06-15 23:02:42 +0000650 QualType ivarType = ivar->getType();
651 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000652
John McCall43192862011-09-13 18:31:23 +0000653 // The lifetime implied by the property's attributes.
654 Qualifiers::ObjCLifetime propertyLifetime =
655 getImpliedARCOwnership(property->getPropertyAttributes(),
656 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000657
John McCall43192862011-09-13 18:31:23 +0000658 // We're fine if they match.
659 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000660
John McCall43192862011-09-13 18:31:23 +0000661 // These aren't valid lifetimes for object ivars; don't diagnose twice.
662 if (ivarLifetime == Qualifiers::OCL_None ||
663 ivarLifetime == Qualifiers::OCL_Autoreleasing)
664 return;
John McCall31168b02011-06-15 23:02:42 +0000665
John McCalld8561f02012-08-20 23:36:59 +0000666 // If the ivar is private, and it's implicitly __unsafe_unretained
667 // becaues of its type, then pretend it was actually implicitly
668 // __strong. This is only sound because we're processing the
669 // property implementation before parsing any method bodies.
670 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
671 propertyLifetime == Qualifiers::OCL_Strong &&
672 ivar->getAccessControl() == ObjCIvarDecl::Private) {
673 SplitQualType split = ivarType.split();
674 if (split.Quals.hasObjCLifetime()) {
675 assert(ivarType->isObjCARCImplicitlyUnretainedType());
676 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
677 ivarType = S.Context.getQualifiedType(split);
678 ivar->setType(ivarType);
679 return;
680 }
681 }
682
John McCall43192862011-09-13 18:31:23 +0000683 switch (propertyLifetime) {
684 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000685 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000686 << property->getDeclName()
687 << ivar->getDeclName()
688 << ivarLifetime;
689 break;
John McCall31168b02011-06-15 23:02:42 +0000690
John McCall43192862011-09-13 18:31:23 +0000691 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000692 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000693 << property->getDeclName()
694 << ivar->getDeclName();
695 break;
John McCall31168b02011-06-15 23:02:42 +0000696
John McCall43192862011-09-13 18:31:23 +0000697 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000698 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000699 << property->getDeclName()
700 << ivar->getDeclName()
701 << ((property->getPropertyAttributesAsWritten()
702 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
703 break;
John McCall31168b02011-06-15 23:02:42 +0000704
John McCall43192862011-09-13 18:31:23 +0000705 case Qualifiers::OCL_Autoreleasing:
706 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000707
John McCall43192862011-09-13 18:31:23 +0000708 case Qualifiers::OCL_None:
709 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000710 return;
711 }
712
713 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000714 if (propertyImplLoc.isValid())
715 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000716}
717
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000718/// setImpliedPropertyAttributeForReadOnlyProperty -
719/// This routine evaludates life-time attributes for a 'readonly'
720/// property with no known lifetime of its own, using backing
721/// 'ivar's attribute, if any. If no backing 'ivar', property's
722/// life-time is assumed 'strong'.
723static void setImpliedPropertyAttributeForReadOnlyProperty(
724 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
725 Qualifiers::ObjCLifetime propertyLifetime =
726 getImpliedARCOwnership(property->getPropertyAttributes(),
727 property->getType());
728 if (propertyLifetime != Qualifiers::OCL_None)
729 return;
730
731 if (!ivar) {
732 // if no backing ivar, make property 'strong'.
733 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
734 return;
735 }
736 // property assumes owenership of backing ivar.
737 QualType ivarType = ivar->getType();
738 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
739 if (ivarLifetime == Qualifiers::OCL_Strong)
740 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
741 else if (ivarLifetime == Qualifiers::OCL_Weak)
742 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
743 return;
744}
Ted Kremenekac597f32010-03-12 00:46:40 +0000745
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000746/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
747/// in inherited protocols with mismatched types. Since any of them can
748/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000749static void
750DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
751 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000752 ObjCPropertyDecl *Property) {
753 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000754 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
755 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000756 PDecl->collectInheritedProtocolProperties(Property, PropMap);
757 }
758 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
759 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000760 for (const auto *PI : SDecl->all_referenced_protocols()) {
761 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000762 PDecl->collectInheritedProtocolProperties(Property, PropMap);
763 }
764 SDecl = SDecl->getSuperClass();
765 }
766
767 if (PropMap.empty())
768 return;
769
770 QualType RHSType = S.Context.getCanonicalType(Property->getType());
771 bool FirsTime = true;
772 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
773 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
774 ObjCPropertyDecl *Prop = I->second;
775 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
776 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
777 bool IncompatibleObjC = false;
778 QualType ConvertedType;
779 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
780 || IncompatibleObjC) {
781 if (FirsTime) {
782 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
783 << Property->getType();
784 FirsTime = false;
785 }
786 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
787 << Prop->getType();
788 }
789 }
790 }
791 if (!FirsTime && AtLoc.isValid())
792 S.Diag(AtLoc, diag::note_property_synthesize);
793}
794
Ted Kremenekac597f32010-03-12 00:46:40 +0000795/// ActOnPropertyImplDecl - This routine performs semantic checks and
796/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000797/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000798///
John McCall48871652010-08-21 09:40:31 +0000799Decl *Sema::ActOnPropertyImplDecl(Scope *S,
800 SourceLocation AtLoc,
801 SourceLocation PropertyLoc,
802 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000803 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000804 IdentifierInfo *PropertyIvar,
805 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000806 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000807 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000808 // Make sure we have a context for the property implementation declaration.
809 if (!ClassImpDecl) {
810 Diag(AtLoc, diag::error_missing_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000811 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000812 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000813 if (PropertyIvarLoc.isInvalid())
814 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000815 SourceLocation PropertyDiagLoc = PropertyLoc;
816 if (PropertyDiagLoc.isInvalid())
817 PropertyDiagLoc = ClassImpDecl->getLocStart();
Craig Topperc3ec1492014-05-26 06:22:03 +0000818 ObjCPropertyDecl *property = nullptr;
819 ObjCInterfaceDecl *IDecl = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000820 // Find the class or category class where this property must have
821 // a declaration.
Craig Topperc3ec1492014-05-26 06:22:03 +0000822 ObjCImplementationDecl *IC = nullptr;
823 ObjCCategoryImplDecl *CatImplClass = nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000824 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
825 IDecl = IC->getClassInterface();
826 // We always synthesize an interface for an implementation
827 // without an interface decl. So, IDecl is always non-zero.
828 assert(IDecl &&
829 "ActOnPropertyImplDecl - @implementation without @interface");
830
831 // Look for this property declaration in the @implementation's @interface
832 property = IDecl->FindPropertyDeclaration(PropertyId);
833 if (!property) {
834 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000835 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000836 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000837 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000838 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
839 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000840 if (AtLoc.isValid())
841 Diag(AtLoc, diag::warn_implicit_atomic_property);
842 else
843 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
844 Diag(property->getLocation(), diag::note_property_declare);
845 }
846
Ted Kremenekac597f32010-03-12 00:46:40 +0000847 if (const ObjCCategoryDecl *CD =
848 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
849 if (!CD->IsClassExtension()) {
850 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
851 Diag(property->getLocation(), diag::note_property_declare);
Craig Topperc3ec1492014-05-26 06:22:03 +0000852 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000853 }
854 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000855 if (Synthesize&&
856 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
857 property->hasAttr<IBOutletAttr>() &&
858 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000859 bool ReadWriteProperty = false;
860 // Search into the class extensions and see if 'readonly property is
861 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000862 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000863 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
864 if (!R.empty())
865 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
866 PIkind = ExtProp->getPropertyAttributesAsWritten();
867 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
868 ReadWriteProperty = true;
869 break;
870 }
871 }
872 }
873
874 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000875 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000876 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000877 SourceLocation readonlyLoc;
878 if (LocPropertyAttribute(Context, "readonly",
879 property->getLParenLoc(), readonlyLoc)) {
880 SourceLocation endLoc =
881 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
882 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
883 Diag(property->getLocation(),
884 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
885 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
886 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000887 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000888 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000889 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
890 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000891
Ted Kremenekac597f32010-03-12 00:46:40 +0000892 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
893 if (Synthesize) {
894 Diag(AtLoc, diag::error_synthesize_category_decl);
Craig Topperc3ec1492014-05-26 06:22:03 +0000895 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000896 }
897 IDecl = CatImplClass->getClassInterface();
898 if (!IDecl) {
899 Diag(AtLoc, diag::error_missing_property_interface);
Craig Topperc3ec1492014-05-26 06:22:03 +0000900 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000901 }
902 ObjCCategoryDecl *Category =
903 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
904
905 // If category for this implementation not found, it is an error which
906 // has already been reported eralier.
907 if (!Category)
Craig Topperc3ec1492014-05-26 06:22:03 +0000908 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000909 // Look for this property declaration in @implementation's category
910 property = Category->FindPropertyDeclaration(PropertyId);
911 if (!property) {
912 Diag(PropertyLoc, diag::error_bad_category_property_decl)
913 << Category->getDeclName();
Craig Topperc3ec1492014-05-26 06:22:03 +0000914 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000915 }
916 } else {
917 Diag(AtLoc, diag::error_bad_property_context);
Craig Topperc3ec1492014-05-26 06:22:03 +0000918 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +0000919 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000920 ObjCIvarDecl *Ivar = nullptr;
Eli Friedman169ec352012-05-01 22:26:06 +0000921 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000922 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000923 // Check that we have a valid, previously declared ivar for @synthesize
924 if (Synthesize) {
925 // @synthesize
926 if (!PropertyIvar)
927 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000928 // Check that this is a previously declared 'ivar' in 'IDecl' interface
929 ObjCInterfaceDecl *ClassDeclared;
930 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
931 QualType PropType = property->getType();
932 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000933
934 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000935 diag::err_incomplete_synthesized_property,
936 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000937 Diag(property->getLocation(), diag::note_property_declare);
938 CompleteTypeErr = true;
939 }
940
David Blaikiebbafb8a2012-03-11 07:00:24 +0000941 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000942 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000943 ObjCPropertyDecl::OBJC_PR_readonly) &&
944 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000945 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
946 }
947
John McCall31168b02011-06-15 23:02:42 +0000948 ObjCPropertyDecl::PropertyAttributeKind kind
949 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000950
951 // Add GC __weak to the ivar type if the property is weak.
952 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000953 getLangOpts().getGC() != LangOptions::NonGC) {
954 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000955 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000956 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000957 Diag(property->getLocation(), diag::note_property_declare);
958 } else {
959 PropertyIvarType =
960 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000961 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000962 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000963 if (AtLoc.isInvalid()) {
964 // Check when default synthesizing a property that there is
965 // an ivar matching property name and issue warning; since this
966 // is the most common case of not using an ivar used for backing
967 // property in non-default synthesis case.
Craig Topperc3ec1492014-05-26 06:22:03 +0000968 ObjCInterfaceDecl *ClassDeclared=nullptr;
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000969 ObjCIvarDecl *originalIvar =
970 IDecl->lookupInstanceVariable(property->getIdentifier(),
971 ClassDeclared);
972 if (originalIvar) {
973 Diag(PropertyDiagLoc,
974 diag::warn_autosynthesis_property_ivar_match)
Craig Topperc3ec1492014-05-26 06:22:03 +0000975 << PropertyId << (Ivar == nullptr) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000976 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000977 Diag(property->getLocation(), diag::note_property_declare);
978 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000979 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000980 }
981
982 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000983 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000984 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000985 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000986 !PropertyIvarType.getObjCLifetime() &&
987 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000988
John McCall43192862011-09-13 18:31:23 +0000989 // It's an error if we have to do this and the user didn't
990 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000991 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000992 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000993 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000994 diag::err_arc_objc_property_default_assign_on_object);
995 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +0000996 } else {
997 Qualifiers::ObjCLifetime lifetime =
998 getImpliedARCOwnership(kind, PropertyIvarType);
999 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001000 if (lifetime == Qualifiers::OCL_Weak) {
1001 bool err = false;
1002 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001003 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1004 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1005 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001006 Diag(property->getLocation(),
1007 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1008 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1009 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001010 err = true;
1011 }
Richard Smith802c4b72012-08-23 06:16:52 +00001012 }
John McCall3deb1ad2012-08-21 02:47:43 +00001013 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001014 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001015 Diag(property->getLocation(), diag::note_property_declare);
1016 }
John McCall31168b02011-06-15 23:02:42 +00001017 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001018
John McCall31168b02011-06-15 23:02:42 +00001019 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001020 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001021 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1022 }
John McCall31168b02011-06-15 23:02:42 +00001023 }
1024
1025 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001026 !getLangOpts().ObjCAutoRefCount &&
1027 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001028 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001029 Diag(property->getLocation(), diag::note_property_declare);
1030 }
1031
Abramo Bagnaradff19302011-03-08 08:55:46 +00001032 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001033 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Craig Topperc3ec1492014-05-26 06:22:03 +00001034 PropertyIvarType, /*Dinfo=*/nullptr,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001035 ObjCIvarDecl::Private,
Craig Topperc3ec1492014-05-26 06:22:03 +00001036 (Expr *)nullptr, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001037 if (RequireNonAbstractType(PropertyIvarLoc,
1038 PropertyIvarType,
1039 diag::err_abstract_type_in_decl,
1040 AbstractSynthesizedIvarType)) {
1041 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001042 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001043 } else if (CompleteTypeErr)
1044 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001045 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001046 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001047
John McCall5fb5df92012-06-20 06:18:46 +00001048 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001049 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1050 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001051 // Note! I deliberately want it to fall thru so, we have a
1052 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001053 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001054 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001055 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001056 << property->getDeclName() << Ivar->getDeclName()
1057 << ClassDeclared->getDeclName();
1058 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001059 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001060 // Note! I deliberately want it to fall thru so more errors are caught.
1061 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001062 property->setPropertyIvarDecl(Ivar);
1063
Ted Kremenekac597f32010-03-12 00:46:40 +00001064 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1065
1066 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001067 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001068 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001069 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001070 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001071 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001072 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001073 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001074 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001075 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1076 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001077 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001078 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001079 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001080 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001081 << property->getDeclName() << PropType
1082 << Ivar->getDeclName() << IvarType;
1083 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001084 // Note! I deliberately want it to fall thru so, we have a
1085 // a property implementation and to avoid future warnings.
1086 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001087 else {
1088 // FIXME! Rules for properties are somewhat different that those
1089 // for assignments. Use a new routine to consolidate all cases;
1090 // specifically for property redeclarations as well as for ivars.
1091 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1092 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1093 if (lhsType != rhsType &&
1094 lhsType->isArithmeticType()) {
1095 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1096 << property->getDeclName() << PropType
1097 << Ivar->getDeclName() << IvarType;
1098 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1099 // Fall thru - see previous comment
1100 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001101 }
1102 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001103 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001104 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001105 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001106 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001107 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001108 // Fall thru - see previous comment
1109 }
John McCall31168b02011-06-15 23:02:42 +00001110 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 if ((property->getType()->isObjCObjectPointerType() ||
1112 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001113 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001114 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001115 << property->getDeclName() << Ivar->getDeclName();
1116 // Fall thru - see previous comment
1117 }
1118 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001119 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001120 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 } else if (PropertyIvar)
1122 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001123 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001124
Ted Kremenekac597f32010-03-12 00:46:40 +00001125 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1126 ObjCPropertyImplDecl *PIDecl =
1127 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1128 property,
1129 (Synthesize ?
1130 ObjCPropertyImplDecl::Synthesize
1131 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001132 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001133
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001134 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001135 PIDecl->setInvalidDecl();
1136
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001137 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1138 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001139 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001140 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001141 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1142 // returned by the getter as it must conform to C++'s copy-return rules.
1143 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001144 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001145 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1146 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001147 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001148 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001149 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001150 Expr *LoadSelfExpr =
1151 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001152 CK_LValueToRValue, SelfExpr, nullptr,
1153 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001154 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001155 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001156 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001157 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001158 ExprResult Res = PerformCopyInitialization(
1159 InitializedEntity::InitializeResult(PropertyDiagLoc,
1160 getterMethod->getReturnType(),
1161 /*NRVO=*/false),
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001162 PropertyDiagLoc, IvarRefExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001163 if (!Res.isInvalid()) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001164 Expr *ResExpr = Res.getAs<Expr>();
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001165 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001166 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001167 PIDecl->setGetterCXXConstructor(ResExpr);
1168 }
1169 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001170 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1171 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1172 Diag(getterMethod->getLocation(),
1173 diag::warn_property_getter_owning_mismatch);
1174 Diag(property->getLocation(), diag::note_property_declare);
1175 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001176 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1177 switch (getterMethod->getMethodFamily()) {
1178 case OMF_retain:
1179 case OMF_retainCount:
1180 case OMF_release:
1181 case OMF_autorelease:
1182 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1183 << 1 << getterMethod->getSelector();
1184 break;
1185 default:
1186 break;
1187 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001188 }
1189 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1190 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001191 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1192 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001193 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001194 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001195 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1196 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001197 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001198 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001199 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001200 Expr *LoadSelfExpr =
1201 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001202 CK_LValueToRValue, SelfExpr, nullptr,
1203 VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001204 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001205 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001206 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001207 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001208 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1209 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001210 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001211 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1212 VK_LValue, PropertyDiagLoc);
1213 MarkDeclRefReferenced(rhs);
1214 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001215 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001216 if (property->getPropertyAttributes() &
1217 ObjCPropertyDecl::OBJC_PR_atomic) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001218 Expr *callExpr = Res.getAs<Expr>();
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001219 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001220 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1221 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001222 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001223 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001224 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001225 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001226 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001227 Diag(FuncDecl->getLocStart(),
1228 diag::note_callee_decl) << FuncDecl;
1229 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001230 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001231 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001232 }
1233 }
1234
Ted Kremenekac597f32010-03-12 00:46:40 +00001235 if (IC) {
1236 if (Synthesize)
1237 if (ObjCPropertyImplDecl *PPIDecl =
1238 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1239 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1240 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1241 << PropertyIvar;
1242 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1243 }
1244
1245 if (ObjCPropertyImplDecl *PPIDecl
1246 = IC->FindPropertyImplDecl(PropertyId)) {
1247 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1248 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001249 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001250 }
1251 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001252 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001253 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001254 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001255 // Diagnose if an ivar was lazily synthesdized due to a previous
1256 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001257 // but it requires an ivar of different name.
Craig Topperc3ec1492014-05-26 06:22:03 +00001258 ObjCInterfaceDecl *ClassDeclared=nullptr;
1259 ObjCIvarDecl *Ivar = nullptr;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001260 if (!Synthesize)
1261 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1262 else {
1263 if (PropertyIvar && PropertyIvar != PropertyId)
1264 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1265 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001266 // Issue diagnostics only if Ivar belongs to current class.
1267 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001268 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001269 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1270 << PropertyId;
1271 Ivar->setInvalidDecl();
1272 }
1273 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001274 } else {
1275 if (Synthesize)
1276 if (ObjCPropertyImplDecl *PPIDecl =
1277 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001278 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001279 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1280 << PropertyIvar;
1281 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1282 }
1283
1284 if (ObjCPropertyImplDecl *PPIDecl =
1285 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001286 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001287 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +00001288 return nullptr;
Ted Kremenekac597f32010-03-12 00:46:40 +00001289 }
1290 CatImplClass->addPropertyImplementation(PIDecl);
1291 }
1292
John McCall48871652010-08-21 09:40:31 +00001293 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001294}
1295
1296//===----------------------------------------------------------------------===//
1297// Helper methods.
1298//===----------------------------------------------------------------------===//
1299
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001300/// DiagnosePropertyMismatch - Compares two properties for their
1301/// attributes and types and warns on a variety of inconsistencies.
1302///
1303void
1304Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1305 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001306 const IdentifierInfo *inheritedName,
1307 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001308 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001309 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001310 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001311 SuperProperty->getPropertyAttributes();
1312
1313 // We allow readonly properties without an explicit ownership
1314 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1315 // to be overridden by a property with any explicit ownership in the subclass.
1316 if (!OverridingProtocolProperty &&
1317 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1318 ;
1319 else {
1320 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1321 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1322 Diag(Property->getLocation(), diag::warn_readonly_property)
1323 << Property->getDeclName() << inheritedName;
1324 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1325 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001326 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001327 << Property->getDeclName() << "copy" << inheritedName;
1328 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1329 unsigned CAttrRetain =
1330 (CAttr &
1331 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1332 unsigned SAttrRetain =
1333 (SAttr &
1334 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1335 bool CStrong = (CAttrRetain != 0);
1336 bool SStrong = (SAttrRetain != 0);
1337 if (CStrong != SStrong)
1338 Diag(Property->getLocation(), diag::warn_property_attribute)
1339 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1340 }
John McCall31168b02011-06-15 23:02:42 +00001341 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001342
1343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001344 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001345 Diag(Property->getLocation(), diag::warn_property_attribute)
1346 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001347 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1348 }
1349 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001350 Diag(Property->getLocation(), diag::warn_property_attribute)
1351 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001352 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1353 }
1354 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001355 Diag(Property->getLocation(), diag::warn_property_attribute)
1356 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001357 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1358 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001359
1360 QualType LHSType =
1361 Context.getCanonicalType(SuperProperty->getType());
1362 QualType RHSType =
1363 Context.getCanonicalType(Property->getType());
1364
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001365 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001366 // Do cases not handled in above.
1367 // FIXME. For future support of covariant property types, revisit this.
1368 bool IncompatibleObjC = false;
1369 QualType ConvertedType;
1370 if (!isObjCPointerConversion(RHSType, LHSType,
1371 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001372 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001373 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1374 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001375 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1376 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001377 }
1378}
1379
1380bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1381 ObjCMethodDecl *GetterMethod,
1382 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001383 if (!GetterMethod)
1384 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001385 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001386 QualType PropertyIvarType = property->getType().getNonReferenceType();
1387 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1388 if (!compat) {
1389 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1390 isa<ObjCObjectPointerType>(GetterType))
1391 compat =
1392 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001393 GetterType->getAs<ObjCObjectPointerType>(),
1394 PropertyIvarType->getAs<ObjCObjectPointerType>());
1395 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001396 != Compatible) {
1397 Diag(Loc, diag::error_property_accessor_type)
1398 << property->getDeclName() << PropertyIvarType
1399 << GetterMethod->getSelector() << GetterType;
1400 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1401 return true;
1402 } else {
1403 compat = true;
1404 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1405 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1406 if (lhsType != rhsType && lhsType->isArithmeticType())
1407 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001408 }
1409 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001410
1411 if (!compat) {
1412 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1413 << property->getDeclName()
1414 << GetterMethod->getSelector();
1415 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1416 return true;
1417 }
1418
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001419 return false;
1420}
1421
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001422/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001423/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001424static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1425 ObjCContainerDecl::PropertyMap &PropMap,
1426 ObjCContainerDecl::PropertyMap &SuperPropMap,
1427 bool IncludeProtocols = true) {
1428
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001429 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001430 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001431 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001432 if (IncludeProtocols) {
1433 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001434 for (auto *PI : IDecl->all_referenced_protocols())
1435 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001436 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001437 }
1438 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1439 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001440 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001441 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001442 if (IncludeProtocols) {
1443 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001444 for (auto *PI : CATDecl->protocols())
1445 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001446 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001447 }
1448 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001449 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001450 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1451 // Exclude property for protocols which conform to class's super-class,
1452 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001453 if (!PropertyFromSuper ||
1454 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001455 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1456 if (!PropEntry)
1457 PropEntry = Prop;
1458 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001459 }
1460 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001461 for (auto *PI : PDecl->protocols())
1462 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001463 }
1464}
1465
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001466/// CollectSuperClassPropertyImplementations - This routine collects list of
1467/// properties to be implemented in super class(s) and also coming from their
1468/// conforming protocols.
1469static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001470 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001471 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001472 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001473 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001474 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001475 SDecl = SDecl->getSuperClass();
1476 }
1477 }
1478}
1479
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001480/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1481/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1482/// declared in class 'IFace'.
1483bool
1484Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1485 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1486 if (!IV->getSynthesize())
1487 return false;
1488 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1489 Method->isInstanceMethod());
1490 if (!IMD || !IMD->isPropertyAccessor())
1491 return false;
1492
1493 // look up a property declaration whose one of its accessors is implemented
1494 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001495 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001496 if ((Property->getGetterName() == IMD->getSelector() ||
1497 Property->getSetterName() == IMD->getSelector()) &&
1498 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001499 return true;
1500 }
1501 return false;
1502}
1503
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001504static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1505 ObjCPropertyDecl *Prop) {
1506 bool SuperClassImplementsGetter = false;
1507 bool SuperClassImplementsSetter = false;
1508 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1509 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001510
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001511 while (IDecl->getSuperClass()) {
1512 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1513 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1514 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001515
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001516 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1517 SuperClassImplementsSetter = true;
1518 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1519 return true;
1520 IDecl = IDecl->getSuperClass();
1521 }
1522 return false;
1523}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001524
James Dennett2a4d13c2012-06-15 07:13:21 +00001525/// \brief Default synthesizes all properties which must be synthesized
1526/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001527void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1528 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001529
Anna Zaks673d76b2012-10-18 19:17:53 +00001530 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001531 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1532 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001533 if (PropMap.empty())
1534 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001535 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001536 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1537
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001538 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1539 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001540 // Is there a matching property synthesize/dynamic?
1541 if (Prop->isInvalidDecl() ||
1542 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1543 continue;
1544 // Property may have been synthesized by user.
1545 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1546 continue;
1547 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1548 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1549 continue;
1550 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1551 continue;
1552 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001553 if (ObjCPropertyImplDecl *PID =
1554 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
Fariborz Jahanian6c9ee7b2014-07-26 20:52:26 +00001555 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1556 << Prop->getIdentifier();
1557 if (!PID->getLocation().isInvalid())
1558 Diag(PID->getLocation(), diag::note_property_synthesize);
Fariborz Jahanian46145242013-06-07 18:32:55 +00001559 continue;
1560 }
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001561 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001562 if (ObjCProtocolDecl *Proto =
1563 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001564 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001565 // Suppress the warning if class's superclass implements property's
1566 // getter and implements property's setter (if readwrite property).
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001567 // Or, if property is going to be implemented in its super class.
1568 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001569 Diag(IMPDecl->getLocation(),
1570 diag::warn_auto_synthesizing_protocol_property)
1571 << Prop << Proto;
1572 Diag(Prop->getLocation(), diag::note_property_declare);
1573 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001574 continue;
1575 }
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001576 // If property to be implemented in the super class, ignore.
Fariborz Jahanian3b230082014-08-29 20:29:31 +00001577 if (PropInSuperClass) {
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001578 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1579 (PropInSuperClass->getPropertyAttributes() &
1580 ObjCPropertyDecl::OBJC_PR_readonly) &&
1581 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1582 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1583 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1584 << Prop->getIdentifier();
1585 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1586 }
1587 else {
1588 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1589 << Prop->getIdentifier();
Fariborz Jahanianc985a7f2014-10-10 22:08:23 +00001590 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
Fariborz Jahanianc9b77152014-08-29 18:31:16 +00001591 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1592 }
1593 continue;
1594 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001595 // We use invalid SourceLocations for the synthesized ivars since they
1596 // aren't really synthesized at a particular location; they just exist.
1597 // Saying that they are located at the @implementation isn't really going
1598 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001599 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1600 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1601 true,
1602 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001603 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001604 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001605 if (PIDecl) {
1606 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001607 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001608 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001609 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001610}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001611
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001612void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001613 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001614 return;
1615 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1616 if (!IC)
1617 return;
1618 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001619 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001620 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001621}
1622
Ted Kremenek7e812952014-02-21 19:41:30 +00001623static void DiagnoseUnimplementedAccessor(Sema &S,
1624 ObjCInterfaceDecl *PrimaryClass,
1625 Selector Method,
1626 ObjCImplDecl* IMPDecl,
1627 ObjCContainerDecl *CDecl,
1628 ObjCCategoryDecl *C,
1629 ObjCPropertyDecl *Prop,
1630 Sema::SelectorSet &SMap) {
1631 // When reporting on missing property setter/getter implementation in
1632 // categories, do not report when they are declared in primary class,
1633 // class's protocol, or one of it super classes. This is because,
1634 // the class is going to implement them.
1635 if (!SMap.count(Method) &&
Craig Topperc3ec1492014-05-26 06:22:03 +00001636 (PrimaryClass == nullptr ||
Ted Kremenek7e812952014-02-21 19:41:30 +00001637 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1638 S.Diag(IMPDecl->getLocation(),
1639 isa<ObjCCategoryDecl>(CDecl) ?
1640 diag::warn_setter_getter_impl_required_in_category :
1641 diag::warn_setter_getter_impl_required)
1642 << Prop->getDeclName() << Method;
1643 S.Diag(Prop->getLocation(),
1644 diag::note_property_declare);
1645 if (S.LangOpts.ObjCDefaultSynthProperties &&
1646 S.LangOpts.ObjCRuntime.isNonFragile())
1647 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1648 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1649 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1650 }
1651}
1652
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001653void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001654 ObjCContainerDecl *CDecl,
1655 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001656 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001657 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1658
Ted Kremenek348e88c2014-02-21 19:41:34 +00001659 if (!SynthesizeProperties) {
1660 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001661 // Gather properties which need not be implemented in this class
1662 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001663 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001664 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1665 // For categories, no need to implement properties declared in
1666 // its primary class (and its super classes) if property is
1667 // declared in one of those containers.
1668 if ((IDecl = C->getClassInterface())) {
1669 ObjCInterfaceDecl::PropertyDeclOrder PO;
1670 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1671 }
1672 }
1673 if (IDecl)
1674 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1675
1676 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1677 }
1678
Ted Kremenek38882022014-02-21 19:41:39 +00001679 // Scan the @interface to see if any of the protocols it adopts
1680 // require an explicit implementation, via attribute
1681 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001682 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001683 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001684
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001685 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001686 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1687 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001688 // Lazily construct a set of all the properties in the @interface
1689 // of the class, without looking at the superclass. We cannot
1690 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001691 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001692 // as scans the adopted protocols. This work only triggers for protocols
1693 // with the attribute, which is very rare, and only occurs when
1694 // analyzing the @implementation.
1695 if (!LazyMap) {
1696 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1697 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1698 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1699 /* IncludeProtocols */ false);
1700 }
Ted Kremenek38882022014-02-21 19:41:39 +00001701 // Add the properties of 'PDecl' to the list of properties that
1702 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001703 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001704 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001705 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001706 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001707 }
1708 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001709 }
Ted Kremenek38882022014-02-21 19:41:39 +00001710
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001711 if (PropMap.empty())
1712 return;
1713
1714 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001715 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001716 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001717
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001718 SelectorSet InsMap;
1719 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001720 for (const auto *I : IMPDecl->instance_methods())
1721 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001722
1723 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
Craig Topperc3ec1492014-05-26 06:22:03 +00001724 ObjCInterfaceDecl *PrimaryClass = nullptr;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001725 if (C && !C->IsClassExtension())
1726 if ((PrimaryClass = C->getClassInterface()))
1727 // Report unimplemented properties in the category as well.
1728 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1729 // When reporting on missing setter/getters, do not report when
1730 // setter/getter is implemented in category's primary class
1731 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001732 for (const auto *I : IMP->instance_methods())
1733 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001734 }
1735
Anna Zaks673d76b2012-10-18 19:17:53 +00001736 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001737 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1738 ObjCPropertyDecl *Prop = P->second;
1739 // Is there a matching propery synthesize/dynamic?
1740 if (Prop->isInvalidDecl() ||
1741 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001742 PropImplMap.count(Prop) ||
1743 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001744 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001745
1746 // Diagnose unimplemented getters and setters.
1747 DiagnoseUnimplementedAccessor(*this,
1748 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1749 if (!Prop->isReadOnly())
1750 DiagnoseUnimplementedAccessor(*this,
1751 PrimaryClass, Prop->getSetterName(),
1752 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001753 }
1754}
1755
1756void
1757Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1758 ObjCContainerDecl* IDecl) {
1759 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001760 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001761 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001762 for (const auto *Property : IDecl->properties()) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001763 ObjCMethodDecl *GetterMethod = nullptr;
1764 ObjCMethodDecl *SetterMethod = nullptr;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001765 bool LookedUpGetterSetter = false;
1766
Bill Wendling44426052012-12-20 19:22:21 +00001767 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001768 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001769
John McCall43192862011-09-13 18:31:23 +00001770 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1771 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001772 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1773 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1774 LookedUpGetterSetter = true;
1775 if (GetterMethod) {
1776 Diag(GetterMethod->getLocation(),
1777 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001778 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001779 Diag(Property->getLocation(), diag::note_property_declare);
1780 }
1781 if (SetterMethod) {
1782 Diag(SetterMethod->getLocation(),
1783 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001784 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001785 Diag(Property->getLocation(), diag::note_property_declare);
1786 }
1787 }
1788
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001789 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001790 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1791 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001792 continue;
1793 if (const ObjCPropertyImplDecl *PIDecl
1794 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1795 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1796 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001797 if (!LookedUpGetterSetter) {
1798 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1799 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001800 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001801 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1802 SourceLocation MethodLoc =
1803 (GetterMethod ? GetterMethod->getLocation()
1804 : SetterMethod->getLocation());
1805 Diag(MethodLoc, diag::warn_atomic_property_rule)
Craig Topperc3ec1492014-05-26 06:22:03 +00001806 << Property->getIdentifier() << (GetterMethod != nullptr)
1807 << (SetterMethod != nullptr);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001808 // fixit stuff.
1809 if (!AttributesAsWritten) {
1810 if (Property->getLParenLoc().isValid()) {
1811 // @property () ... case.
1812 SourceRange PropSourceRange(Property->getAtLoc(),
1813 Property->getLParenLoc());
1814 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1815 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1816 }
1817 else {
1818 //@property id etc.
1819 SourceLocation endLoc =
1820 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1821 endLoc = endLoc.getLocWithOffset(-1);
1822 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1823 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1824 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1825 }
1826 }
1827 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1828 // @property () ... case.
1829 SourceLocation endLoc = Property->getLParenLoc();
1830 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1831 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1832 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1833 }
1834 else
1835 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001836 Diag(Property->getLocation(), diag::note_property_declare);
1837 }
1838 }
1839 }
1840}
1841
John McCall31168b02011-06-15 23:02:42 +00001842void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001843 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001844 return;
1845
Aaron Ballmand85eff42014-03-14 15:02:45 +00001846 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001847 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001848 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1849 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001850 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1851 if (!method)
1852 continue;
1853 ObjCMethodFamily family = method->getMethodFamily();
1854 if (family == OMF_alloc || family == OMF_copy ||
1855 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001856 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001857 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001858 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001859 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
Jordan Rosea34d04d2015-01-16 23:04:31 +00001860
1861 // Look for a getter explicitly declared alongside the property.
1862 // If we find one, use its location for the note.
1863 SourceLocation noteLoc = PD->getLocation();
1864 SourceLocation fixItLoc;
1865 for (auto *getterRedecl : method->redecls()) {
1866 if (getterRedecl->isImplicit())
1867 continue;
1868 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1869 continue;
1870 noteLoc = getterRedecl->getLocation();
1871 fixItLoc = getterRedecl->getLocEnd();
1872 }
1873
1874 Preprocessor &PP = getPreprocessor();
1875 TokenValue tokens[] = {
1876 tok::kw___attribute, tok::l_paren, tok::l_paren,
1877 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1878 PP.getIdentifierInfo("none"), tok::r_paren,
1879 tok::r_paren, tok::r_paren
1880 };
1881 StringRef spelling = "__attribute__((objc_method_family(none)))";
1882 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1883 if (!macroName.empty())
1884 spelling = macroName;
1885
1886 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1887 << method->getDeclName() << spelling;
1888 if (fixItLoc.isValid()) {
1889 SmallString<64> fixItText(" ");
1890 fixItText += spelling;
1891 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1892 }
John McCall31168b02011-06-15 23:02:42 +00001893 }
1894 }
1895 }
1896}
1897
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001898void Sema::DiagnoseMissingDesignatedInitOverrides(
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00001899 const ObjCImplementationDecl *ImplD,
1900 const ObjCInterfaceDecl *IFD) {
1901 assert(IFD->hasDesignatedInitializers());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001902 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1903 if (!SuperD)
1904 return;
1905
1906 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001907 for (const auto *I : ImplD->instance_methods())
1908 if (I->getMethodFamily() == OMF_init)
1909 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001910
1911 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1912 SuperD->getDesignatedInitializers(DesignatedInits);
1913 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1914 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1915 const ObjCMethodDecl *MD = *I;
1916 if (!InitSelSet.count(MD->getSelector())) {
1917 Diag(ImplD->getLocation(),
1918 diag::warn_objc_implementation_missing_designated_init_override)
1919 << MD->getSelector();
1920 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1921 }
1922 }
1923}
1924
John McCallad31b5f2010-11-10 07:01:40 +00001925/// AddPropertyAttrs - Propagates attributes from a property to the
1926/// implicitly-declared getter or setter for that property.
1927static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1928 ObjCPropertyDecl *Property) {
1929 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001930 for (const auto *A : Property->attrs()) {
1931 if (isa<DeprecatedAttr>(A) ||
1932 isa<UnavailableAttr>(A) ||
1933 isa<AvailabilityAttr>(A))
1934 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001935 }
John McCallad31b5f2010-11-10 07:01:40 +00001936}
1937
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001938/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1939/// have the property type and issue diagnostics if they don't.
1940/// Also synthesize a getter/setter method if none exist (and update the
1941/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1942/// methods is the "right" thing to do.
1943void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001944 ObjCContainerDecl *CD,
1945 ObjCPropertyDecl *redeclaredProperty,
1946 ObjCContainerDecl *lexicalDC) {
1947
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001948 ObjCMethodDecl *GetterMethod, *SetterMethod;
1949
Fariborz Jahanian0c1c3112014-05-27 18:26:09 +00001950 if (CD->isInvalidDecl())
1951 return;
1952
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001953 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1954 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1955 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1956 property->getLocation());
1957
1958 if (SetterMethod) {
1959 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1960 property->getPropertyAttributes();
1961 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001962 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1963 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001964 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1965 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001966 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001967 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1968 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001969 Diag(property->getLocation(),
1970 diag::warn_accessor_property_type_mismatch)
1971 << property->getDeclName()
1972 << SetterMethod->getSelector();
1973 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1974 }
1975 }
1976
1977 // Synthesize getter/setter methods if none exist.
1978 // Find the default getter and if one not found, add one.
1979 // FIXME: The synthesized property we set here is misleading. We almost always
1980 // synthesize these methods unless the user explicitly provided prototypes
1981 // (which is odd, but allowed). Sema should be typechecking that the
1982 // declarations jive in that situation (which it is not currently).
1983 if (!GetterMethod) {
1984 // No instance method of same name as property getter name was found.
1985 // Declare a getter method and add it to the list of methods
1986 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001987 SourceLocation Loc = redeclaredProperty ?
1988 redeclaredProperty->getLocation() :
1989 property->getLocation();
1990
1991 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1992 property->getGetterName(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001993 property->getType(), nullptr, CD,
1994 /*isInstance=*/true, /*isVariadic=*/false,
1995 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001996 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001997 (property->getPropertyImplementation() ==
1998 ObjCPropertyDecl::Optional) ?
1999 ObjCMethodDecl::Optional :
2000 ObjCMethodDecl::Required);
2001 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00002002
2003 AddPropertyAttrs(*this, GetterMethod, property);
2004
Ted Kremenek49be9e02010-05-18 21:09:07 +00002005 // FIXME: Eventually this shouldn't be needed, as the lexical context
2006 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00002007 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002008 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002009 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002010 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2011 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00002012
2013 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2014 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00002015 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002016
2017 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002018 GetterMethod->addAttr(
2019 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2020 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002021
2022 if (getLangOpts().ObjCAutoRefCount)
2023 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002024 } else
2025 // A user declared getter will be synthesize when @synthesize of
2026 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002027 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002028 property->setGetterMethodDecl(GetterMethod);
2029
2030 // Skip setter if property is read-only.
2031 if (!property->isReadOnly()) {
2032 // Find the default setter and if one not found, add one.
2033 if (!SetterMethod) {
2034 // No instance method of same name as property setter name was found.
2035 // Declare a setter method and add it to the list of methods
2036 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002037 SourceLocation Loc = redeclaredProperty ?
2038 redeclaredProperty->getLocation() :
2039 property->getLocation();
2040
2041 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002042 ObjCMethodDecl::Create(Context, Loc, Loc,
Craig Topperc3ec1492014-05-26 06:22:03 +00002043 property->getSetterName(), Context.VoidTy,
2044 nullptr, CD, /*isInstance=*/true,
2045 /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002046 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002047 /*isImplicitlyDeclared=*/true,
2048 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002049 (property->getPropertyImplementation() ==
2050 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002051 ObjCMethodDecl::Optional :
2052 ObjCMethodDecl::Required);
2053
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002054 // Invent the arguments for the setter. We don't bother making a
2055 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002056 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2057 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002058 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002059 property->getType().getUnqualifiedType(),
Craig Topperc3ec1492014-05-26 06:22:03 +00002060 /*TInfo=*/nullptr,
John McCall8e7d6562010-08-26 03:08:43 +00002061 SC_None,
Craig Topperc3ec1492014-05-26 06:22:03 +00002062 nullptr);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002063 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002064
2065 AddPropertyAttrs(*this, SetterMethod, property);
2066
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002067 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002068 // FIXME: Eventually this shouldn't be needed, as the lexical context
2069 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002070 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002071 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002072 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002073 SetterMethod->addAttr(
2074 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2075 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002076 // It's possible for the user to have set a very odd custom
2077 // setter selector that causes it to have a method family.
2078 if (getLangOpts().ObjCAutoRefCount)
2079 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002080 } else
2081 // A user declared setter will be synthesize when @synthesize of
2082 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002083 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002084 property->setSetterMethodDecl(SetterMethod);
2085 }
2086 // Add any synthesized methods to the global pool. This allows us to
2087 // handle the following, which is supported by GCC (and part of the design).
2088 //
2089 // @interface Foo
2090 // @property double bar;
2091 // @end
2092 //
2093 // void thisIsUnfortunate() {
2094 // id foo;
2095 // double bar = [foo bar];
2096 // }
2097 //
2098 if (GetterMethod)
2099 AddInstanceMethodToGlobalPool(GetterMethod);
2100 if (SetterMethod)
2101 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002102
2103 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2104 if (!CurrentClass) {
2105 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2106 CurrentClass = Cat->getClassInterface();
2107 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2108 CurrentClass = Impl->getClassInterface();
2109 }
2110 if (GetterMethod)
2111 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2112 if (SetterMethod)
2113 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002114}
2115
John McCall48871652010-08-21 09:40:31 +00002116void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002117 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002118 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002119 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002120 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002121 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002122 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002123
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002124 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2125 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2126 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2127 << "readonly" << "readwrite";
2128
2129 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2130 QualType PropertyTy = PropertyDecl->getType();
2131 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002132
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002133 // 'readonly' property with no obvious lifetime.
2134 // its life time will be determined by its backing ivar.
2135 if (getLangOpts().ObjCAutoRefCount &&
2136 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2137 PropertyTy->isObjCRetainableType() &&
2138 !PropertyOwnership)
2139 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002140
2141 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002142 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002143 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2144 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002145 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002146 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002147 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2148 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2149 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002150 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002151 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002152 }
2153
2154 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002155 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2156 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002157 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2158 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002159 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002160 }
Bill Wendling44426052012-12-20 19:22:21 +00002161 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002162 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2163 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002164 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002165 }
Bill Wendling44426052012-12-20 19:22:21 +00002166 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002167 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2168 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002169 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002170 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002171 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002172 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002173 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2174 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002175 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002176 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002177 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002178 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002179 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2180 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002181 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2182 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002183 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002184 }
Bill Wendling44426052012-12-20 19:22:21 +00002185 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002186 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2187 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002188 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002189 }
Bill Wendling44426052012-12-20 19:22:21 +00002190 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002191 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2192 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002193 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002194 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002195 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002196 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002197 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2198 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002199 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002200 }
Bill Wendling44426052012-12-20 19:22:21 +00002201 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2202 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002203 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2204 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002205 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002206 }
Bill Wendling44426052012-12-20 19:22:21 +00002207 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002208 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2209 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002210 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002211 }
Bill Wendling44426052012-12-20 19:22:21 +00002212 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002213 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2214 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002215 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002216 }
2217 }
Bill Wendling44426052012-12-20 19:22:21 +00002218 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2219 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002220 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2221 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002222 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002223 }
Bill Wendling44426052012-12-20 19:22:21 +00002224 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2225 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002226 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2227 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002228 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002229 }
2230
Bill Wendling44426052012-12-20 19:22:21 +00002231 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2232 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002233 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2234 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002235 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002236 }
2237
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002238 // Warn if user supplied no assignment attribute, property is
2239 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002240 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002241 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2242 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2243 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002244 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002245 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002246 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002247 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002248 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002249 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002250 bool isAnyClassTy =
2251 (PropertyTy->isObjCClassType() ||
2252 PropertyTy->isObjCQualifiedClassType());
2253 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2254 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002255 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002256 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002257 else if (propertyInPrimaryClass) {
2258 // Don't issue warning on property with no life time in class
2259 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002260 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002261 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002262 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002263
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002264 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002265 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002266 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002267 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002268 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002269
2270 // FIXME: Implement warning dependent on NSCopying being
2271 // implemented. See also:
2272 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2273 // (please trim this list while you are at it).
2274 }
2275
Bill Wendling44426052012-12-20 19:22:21 +00002276 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2277 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002278 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002279 && PropertyTy->isBlockPointerType())
2280 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002281 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2282 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2283 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002284 PropertyTy->isBlockPointerType())
2285 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002286
Bill Wendling44426052012-12-20 19:22:21 +00002287 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2288 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002289 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2290
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002291}