blob: 1b49c2abdc9571e768332ce26a02e70e672cc269 [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"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000025
26using namespace clang;
27
Ted Kremenekac597f32010-03-12 00:46:40 +000028//===----------------------------------------------------------------------===//
29// Grammar actions.
30//===----------------------------------------------------------------------===//
31
John McCall43192862011-09-13 18:31:23 +000032/// getImpliedARCOwnership - Given a set of property attributes and a
33/// type, infer an expected lifetime. The type's ownership qualification
34/// is not considered.
35///
36/// Returns OCL_None if the attributes as stated do not imply an ownership.
37/// Never returns OCL_Autoreleasing.
38static Qualifiers::ObjCLifetime getImpliedARCOwnership(
39 ObjCPropertyDecl::PropertyAttributeKind attrs,
40 QualType type) {
41 // retain, strong, copy, weak, and unsafe_unretained are only legal
42 // on properties of retainable pointer type.
43 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000046 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000047 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
48 return Qualifiers::OCL_Weak;
49 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
50 return Qualifiers::OCL_ExplicitNone;
51 }
52
53 // assign can appear on other types, so we have to check the
54 // property type.
55 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
56 type->isObjCRetainableType()) {
57 return Qualifiers::OCL_ExplicitNone;
58 }
59
60 return Qualifiers::OCL_None;
61}
62
John McCall31168b02011-06-15 23:02:42 +000063/// Check the internal consistency of a property declaration.
64static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
65 if (property->isInvalidDecl()) return;
66
67 ObjCPropertyDecl::PropertyAttributeKind propertyKind
68 = property->getPropertyAttributes();
69 Qualifiers::ObjCLifetime propertyLifetime
70 = property->getType().getObjCLifetime();
71
72 // Nothing to do if we don't have a lifetime.
73 if (propertyLifetime == Qualifiers::OCL_None) return;
74
John McCall43192862011-09-13 18:31:23 +000075 Qualifiers::ObjCLifetime expectedLifetime
76 = getImpliedARCOwnership(propertyKind, property->getType());
77 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000078 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000079 // attribute. That's okay, but restore reasonable invariants by
80 // setting the property attribute according to the lifetime
81 // qualifier.
82 ObjCPropertyDecl::PropertyAttributeKind attr;
83 if (propertyLifetime == Qualifiers::OCL_Strong) {
84 attr = ObjCPropertyDecl::OBJC_PR_strong;
85 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
86 attr = ObjCPropertyDecl::OBJC_PR_weak;
87 } else {
88 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
89 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
90 }
91 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000092 return;
93 }
94
95 if (propertyLifetime == expectedLifetime) return;
96
97 property->setInvalidDecl();
98 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +000099 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000100 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000101 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000102 << propertyLifetime;
103}
104
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000105static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
106 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
107 T.isObjCGCWeak()) ||
108 (S.getLangOpts().ObjCAutoRefCount &&
109 T.getObjCLifetime() == Qualifiers::OCL_Weak))
110 return ObjCDeclSpec::DQ_PR_weak;
111 return 0;
112}
113
Douglas Gregorb8982092013-01-21 19:42:21 +0000114/// \brief Check this Objective-C property against a property declared in the
115/// given protocol.
116static void
117CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
118 ObjCProtocolDecl *Proto,
119 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
120 // Have we seen this protocol before?
121 if (!Known.insert(Proto))
122 return;
123
124 // Look for a property with the same name.
125 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
126 for (unsigned I = 0, N = R.size(); I != N; ++I) {
127 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000128 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000129 return;
130 }
131 }
132
133 // Check this property against any protocols we inherit.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000134 for (auto *P : Proto->protocols())
135 CheckPropertyAgainstProtocol(S, Prop, P, Known);
Douglas Gregorb8982092013-01-21 19:42:21 +0000136}
137
John McCall48871652010-08-21 09:40:31 +0000138Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000139 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000140 FieldDeclarator &FD,
141 ObjCDeclSpec &ODS,
142 Selector GetterSel,
143 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000144 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000145 tok::ObjCKeywordKind MethodImplKind,
146 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000147 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000148 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
149 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000150 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000151
Bill Wendling44426052012-12-20 19:22:21 +0000152 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000153 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000154 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000155 // property is defaulted to 'assign' if it is readwrite and is
156 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000157 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000158 (isReadWrite &&
Bill Wendling44426052012-12-20 19:22:21 +0000159 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
160 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
161 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
162 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
163 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000164
Douglas Gregor90d34422013-01-21 19:05:22 +0000165 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000166 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregor90d34422013-01-21 19:05:22 +0000167 ObjCPropertyDecl *Res = 0;
168 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000169 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000170 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000171 FD, GetterSel, SetterSel,
172 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000173 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000174 ODS.getPropertyAttributes(),
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000175 isOverridingProperty, TSI,
176 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000177 if (!Res)
178 return 0;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000179 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000180 }
181
182 if (!Res) {
183 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
184 GetterSel, SetterSel, isAssign, isReadWrite,
185 Attributes, ODS.getPropertyAttributes(),
186 TSI, MethodImplKind);
187 if (lexicalDC)
188 Res->setLexicalDeclContext(lexicalDC);
189 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000190
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000191 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000192 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000193 (isa<ObjCInterfaceDecl>(ClassDecl) ||
194 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000195
David Blaikiebbafb8a2012-03-11 07:00:24 +0000196 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000197 checkARCPropertyDecl(*this, Res);
198
Douglas Gregorb8982092013-01-21 19:42:21 +0000199 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000200 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000201 // For a class, compare the property against a property in our superclass.
202 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000203 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
204 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000205 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000206 for (unsigned I = 0, N = R.size(); I != N; ++I) {
207 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000208 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000209 FoundInSuper = true;
210 break;
211 }
212 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000213 if (FoundInSuper)
214 break;
215 else
216 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000217 }
218
219 if (FoundInSuper) {
220 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000221 for (auto *P : CurrentInterfaceDecl->protocols()) {
222 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000223 }
224 } else {
225 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000226 for (auto *P : IFace->all_referenced_protocols()) {
227 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000228 }
229 }
230 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Aaron Ballman19a41762014-03-14 12:55:57 +0000231 for (auto *P : Cat->protocols())
232 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000233 } else {
234 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000235 for (auto *P : Proto->protocols())
236 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000237 }
238
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000239 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000240 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000241}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000242
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000243static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000244makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000245 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000246 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000247 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000248 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000249 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000254 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
270
271 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
272}
273
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000274static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000275 SourceLocation LParenLoc, SourceLocation &Loc) {
276 if (LParenLoc.isMacroID())
277 return false;
278
279 SourceManager &SM = Context.getSourceManager();
280 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
281 // Try to load the file buffer.
282 bool invalidTemp = false;
283 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
284 if (invalidTemp)
285 return false;
286 const char *tokenBegin = file.data() + locInfo.second;
287
288 // Lex from the start of the given location.
289 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
290 Context.getLangOpts(),
291 file.begin(), tokenBegin, file.end());
292 Token Tok;
293 do {
294 lexer.LexFromRawLexer(Tok);
295 if (Tok.is(tok::raw_identifier) &&
296 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
297 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);
341 return 0;
342 }
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;
John McCall48871652010-08-21 09:40:31 +0000373 return 0;
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.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000391 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000392 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000393 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
394 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000395 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000396 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
397 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000398 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000399 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
400 bool IncompatibleObjC = false;
401 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000402 // Relax the strict type matching for property type in continuation class.
403 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000404 // as it narrows the object type in its primary class property. Note that
405 // this conversion is safe only because the wider type is for a 'readonly'
406 // property in primary class and 'narrowed' type for a 'readwrite' property
407 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000408 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
409 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
410 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
411 ConvertedType, IncompatibleObjC))
412 || IncompatibleObjC) {
413 Diag(AtLoc,
414 diag::err_type_mismatch_continuation_class) << PDecl->getType();
415 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000416 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000417 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000418 }
419
Ted Kremenek959e8302010-03-12 02:31:10 +0000420 // The property 'PIDecl's readonly attribute will be over-ridden
421 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000422 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000423 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000424 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
425 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000426 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000427 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000428 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000429 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
430 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000431 Diag(AtLoc, diag::warn_property_attr_mismatch);
432 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000433 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000434 else if (getLangOpts().ObjCAutoRefCount) {
435 QualType PrimaryPropertyQT =
436 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
437 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000438 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000439 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
440 PrimaryPropertyQT.getObjCLifetime();
441 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000442 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
443 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000444 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
445 Diag(PIDecl->getLocation(), diag::note_property_declare);
446 }
447 }
448 }
449
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000450 DeclContext *DC = cast<DeclContext>(CCPrimary);
451 if (!ObjCPropertyDecl::findPropertyDecl(DC,
452 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000453 // In mrr mode, 'readwrite' property must have an explicit
454 // memory attribute. If none specified, select the default (assign).
455 if (!getLangOpts().ObjCAutoRefCount) {
456 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
457 ObjCDeclSpec::DQ_PR_retain |
458 ObjCDeclSpec::DQ_PR_strong |
459 ObjCDeclSpec::DQ_PR_copy |
460 ObjCDeclSpec::DQ_PR_unsafe_unretained |
461 ObjCDeclSpec::DQ_PR_weak)))
462 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
463 }
464
Ted Kremenek959e8302010-03-12 02:31:10 +0000465 // Protocol is not in the primary class. Must build one for it.
466 ObjCDeclSpec ProtocolPropertyODS;
467 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
468 // and ObjCPropertyDecl::PropertyAttributeKind have identical
469 // values. Should consolidate both into one enum type.
470 ProtocolPropertyODS.
471 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
472 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000473 // Must re-establish the context from class extension to primary
474 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000475 ContextRAII SavedContext(*this, CCPrimary);
476
John McCall48871652010-08-21 09:40:31 +0000477 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000478 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000479 PIDecl->getGetterName(),
480 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000481 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000482 MethodImplKind,
483 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000484 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000485 }
486 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000487 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000488 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000489 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000490 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
493 PIDecl->setSetterName(SetterSel);
494 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000495 // Tailor the diagnostics for the common case where a readwrite
496 // property is declared both in the @interface and the continuation.
497 // This is a common error where the user often intended the original
498 // declaration to be readonly.
499 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000500 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000501 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
502 ? diag::err_use_continuation_class_redeclaration_readwrite
503 : diag::err_use_continuation_class;
504 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000505 << CCPrimary->getDeclName();
506 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000507 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000508 }
509 *isOverridingProperty = true;
510 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000511 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000512 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
513 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000514 if (ASTMutationListener *L = Context.getASTMutationListener())
515 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000516 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000517}
518
519ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
520 ObjCContainerDecl *CDecl,
521 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000522 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000523 FieldDeclarator &FD,
524 Selector GetterSel,
525 Selector SetterSel,
526 const bool isAssign,
527 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000528 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000529 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000530 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000531 tok::ObjCKeywordKind MethodImplKind,
532 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000533 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000534 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000535
536 // Issue a warning if property is 'assign' as default and its object, which is
537 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000538 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000539 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000540 if (const ObjCObjectPointerType *ObjPtrTy =
541 T->getAs<ObjCObjectPointerType>()) {
542 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
543 if (IDecl)
544 if (ObjCProtocolDecl* PNSCopying =
545 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
546 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
547 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000548 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000549
550 if (T->isObjCObjectType()) {
551 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000552 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000553 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
554 << FixItHint::CreateInsertion(StarLoc, "*");
555 T = Context.getObjCObjectPointerType(T);
556 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
557 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
558 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000559
Ted Kremenek959e8302010-03-12 02:31:10 +0000560 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000561 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
562 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000563 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000564
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000565 if (ObjCPropertyDecl *prevDecl =
566 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000567 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000568 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000569 PDecl->setInvalidDecl();
570 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000571 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000572 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000573 if (lexicalDC)
574 PDecl->setLexicalDeclContext(lexicalDC);
575 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000576
577 if (T->isArrayType() || T->isFunctionType()) {
578 Diag(AtLoc, diag::err_property_type) << T;
579 PDecl->setInvalidDecl();
580 }
581
582 ProcessDeclAttributes(S, PDecl, FD.D);
583
584 // Regardless of setter/getter attribute, we save the default getter/setter
585 // selector names in anticipation of declaration of setter/getter methods.
586 PDecl->setGetterName(GetterSel);
587 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000588 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000589 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000590
Bill Wendling44426052012-12-20 19:22:21 +0000591 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000592 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
593
Bill Wendling44426052012-12-20 19:22:21 +0000594 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
596
Bill Wendling44426052012-12-20 19:22:21 +0000597 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
599
600 if (isReadWrite)
601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
602
Bill Wendling44426052012-12-20 19:22:21 +0000603 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
605
Bill Wendling44426052012-12-20 19:22:21 +0000606 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
608
Bill Wendling44426052012-12-20 19:22:21 +0000609 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
611
Bill Wendling44426052012-12-20 19:22:21 +0000612 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
614
Bill Wendling44426052012-12-20 19:22:21 +0000615 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
617
Ted Kremenekac597f32010-03-12 00:46:40 +0000618 if (isAssign)
619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
620
John McCall43192862011-09-13 18:31:23 +0000621 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000622 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000624 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000626
John McCall31168b02011-06-15 23:02:42 +0000627 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000628 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
630 if (isAssign)
631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
632
Ted Kremenekac597f32010-03-12 00:46:40 +0000633 if (MethodImplKind == tok::objc_required)
634 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
635 else if (MethodImplKind == tok::objc_optional)
636 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000637
Ted Kremenek959e8302010-03-12 02:31:10 +0000638 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000639}
640
John McCall31168b02011-06-15 23:02:42 +0000641static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
642 ObjCPropertyDecl *property,
643 ObjCIvarDecl *ivar) {
644 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
645
John McCall31168b02011-06-15 23:02:42 +0000646 QualType ivarType = ivar->getType();
647 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000648
John McCall43192862011-09-13 18:31:23 +0000649 // The lifetime implied by the property's attributes.
650 Qualifiers::ObjCLifetime propertyLifetime =
651 getImpliedARCOwnership(property->getPropertyAttributes(),
652 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000653
John McCall43192862011-09-13 18:31:23 +0000654 // We're fine if they match.
655 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000656
John McCall43192862011-09-13 18:31:23 +0000657 // These aren't valid lifetimes for object ivars; don't diagnose twice.
658 if (ivarLifetime == Qualifiers::OCL_None ||
659 ivarLifetime == Qualifiers::OCL_Autoreleasing)
660 return;
John McCall31168b02011-06-15 23:02:42 +0000661
John McCalld8561f02012-08-20 23:36:59 +0000662 // If the ivar is private, and it's implicitly __unsafe_unretained
663 // becaues of its type, then pretend it was actually implicitly
664 // __strong. This is only sound because we're processing the
665 // property implementation before parsing any method bodies.
666 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
667 propertyLifetime == Qualifiers::OCL_Strong &&
668 ivar->getAccessControl() == ObjCIvarDecl::Private) {
669 SplitQualType split = ivarType.split();
670 if (split.Quals.hasObjCLifetime()) {
671 assert(ivarType->isObjCARCImplicitlyUnretainedType());
672 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
673 ivarType = S.Context.getQualifiedType(split);
674 ivar->setType(ivarType);
675 return;
676 }
677 }
678
John McCall43192862011-09-13 18:31:23 +0000679 switch (propertyLifetime) {
680 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000681 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000682 << property->getDeclName()
683 << ivar->getDeclName()
684 << ivarLifetime;
685 break;
John McCall31168b02011-06-15 23:02:42 +0000686
John McCall43192862011-09-13 18:31:23 +0000687 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000688 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000689 << property->getDeclName()
690 << ivar->getDeclName();
691 break;
John McCall31168b02011-06-15 23:02:42 +0000692
John McCall43192862011-09-13 18:31:23 +0000693 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000694 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000695 << property->getDeclName()
696 << ivar->getDeclName()
697 << ((property->getPropertyAttributesAsWritten()
698 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
699 break;
John McCall31168b02011-06-15 23:02:42 +0000700
John McCall43192862011-09-13 18:31:23 +0000701 case Qualifiers::OCL_Autoreleasing:
702 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000703
John McCall43192862011-09-13 18:31:23 +0000704 case Qualifiers::OCL_None:
705 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000706 return;
707 }
708
709 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000710 if (propertyImplLoc.isValid())
711 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000712}
713
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000714/// setImpliedPropertyAttributeForReadOnlyProperty -
715/// This routine evaludates life-time attributes for a 'readonly'
716/// property with no known lifetime of its own, using backing
717/// 'ivar's attribute, if any. If no backing 'ivar', property's
718/// life-time is assumed 'strong'.
719static void setImpliedPropertyAttributeForReadOnlyProperty(
720 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
721 Qualifiers::ObjCLifetime propertyLifetime =
722 getImpliedARCOwnership(property->getPropertyAttributes(),
723 property->getType());
724 if (propertyLifetime != Qualifiers::OCL_None)
725 return;
726
727 if (!ivar) {
728 // if no backing ivar, make property 'strong'.
729 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
730 return;
731 }
732 // property assumes owenership of backing ivar.
733 QualType ivarType = ivar->getType();
734 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
735 if (ivarLifetime == Qualifiers::OCL_Strong)
736 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
737 else if (ivarLifetime == Qualifiers::OCL_Weak)
738 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
739 return;
740}
Ted Kremenekac597f32010-03-12 00:46:40 +0000741
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000742/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
743/// in inherited protocols with mismatched types. Since any of them can
744/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000745static void
746DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
747 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000748 ObjCPropertyDecl *Property) {
749 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000750 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
751 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000752 PDecl->collectInheritedProtocolProperties(Property, PropMap);
753 }
754 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
755 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000756 for (const auto *PI : SDecl->all_referenced_protocols()) {
757 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000758 PDecl->collectInheritedProtocolProperties(Property, PropMap);
759 }
760 SDecl = SDecl->getSuperClass();
761 }
762
763 if (PropMap.empty())
764 return;
765
766 QualType RHSType = S.Context.getCanonicalType(Property->getType());
767 bool FirsTime = true;
768 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
769 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
770 ObjCPropertyDecl *Prop = I->second;
771 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
772 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
773 bool IncompatibleObjC = false;
774 QualType ConvertedType;
775 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
776 || IncompatibleObjC) {
777 if (FirsTime) {
778 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
779 << Property->getType();
780 FirsTime = false;
781 }
782 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
783 << Prop->getType();
784 }
785 }
786 }
787 if (!FirsTime && AtLoc.isValid())
788 S.Diag(AtLoc, diag::note_property_synthesize);
789}
790
Ted Kremenekac597f32010-03-12 00:46:40 +0000791/// ActOnPropertyImplDecl - This routine performs semantic checks and
792/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000793/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000794///
John McCall48871652010-08-21 09:40:31 +0000795Decl *Sema::ActOnPropertyImplDecl(Scope *S,
796 SourceLocation AtLoc,
797 SourceLocation PropertyLoc,
798 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000799 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000800 IdentifierInfo *PropertyIvar,
801 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000802 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000803 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000804 // Make sure we have a context for the property implementation declaration.
805 if (!ClassImpDecl) {
806 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000807 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000808 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000809 if (PropertyIvarLoc.isInvalid())
810 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000811 SourceLocation PropertyDiagLoc = PropertyLoc;
812 if (PropertyDiagLoc.isInvalid())
813 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000814 ObjCPropertyDecl *property = 0;
815 ObjCInterfaceDecl* IDecl = 0;
816 // Find the class or category class where this property must have
817 // a declaration.
818 ObjCImplementationDecl *IC = 0;
819 ObjCCategoryImplDecl* CatImplClass = 0;
820 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
821 IDecl = IC->getClassInterface();
822 // We always synthesize an interface for an implementation
823 // without an interface decl. So, IDecl is always non-zero.
824 assert(IDecl &&
825 "ActOnPropertyImplDecl - @implementation without @interface");
826
827 // Look for this property declaration in the @implementation's @interface
828 property = IDecl->FindPropertyDeclaration(PropertyId);
829 if (!property) {
830 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000831 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000832 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000833 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000834 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
835 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000836 if (AtLoc.isValid())
837 Diag(AtLoc, diag::warn_implicit_atomic_property);
838 else
839 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
840 Diag(property->getLocation(), diag::note_property_declare);
841 }
842
Ted Kremenekac597f32010-03-12 00:46:40 +0000843 if (const ObjCCategoryDecl *CD =
844 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
845 if (!CD->IsClassExtension()) {
846 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
847 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000848 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000849 }
850 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000851 if (Synthesize&&
852 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
853 property->hasAttr<IBOutletAttr>() &&
854 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000855 bool ReadWriteProperty = false;
856 // Search into the class extensions and see if 'readonly property is
857 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000858 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000859 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
860 if (!R.empty())
861 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
862 PIkind = ExtProp->getPropertyAttributesAsWritten();
863 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
864 ReadWriteProperty = true;
865 break;
866 }
867 }
868 }
869
870 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000871 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000872 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000873 SourceLocation readonlyLoc;
874 if (LocPropertyAttribute(Context, "readonly",
875 property->getLParenLoc(), readonlyLoc)) {
876 SourceLocation endLoc =
877 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
878 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
879 Diag(property->getLocation(),
880 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
881 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
882 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000883 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000884 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000885 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
886 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000887
Ted Kremenekac597f32010-03-12 00:46:40 +0000888 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
889 if (Synthesize) {
890 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000891 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000892 }
893 IDecl = CatImplClass->getClassInterface();
894 if (!IDecl) {
895 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000896 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000897 }
898 ObjCCategoryDecl *Category =
899 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
900
901 // If category for this implementation not found, it is an error which
902 // has already been reported eralier.
903 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000904 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000905 // Look for this property declaration in @implementation's category
906 property = Category->FindPropertyDeclaration(PropertyId);
907 if (!property) {
908 Diag(PropertyLoc, diag::error_bad_category_property_decl)
909 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000910 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000911 }
912 } else {
913 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000914 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000915 }
916 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000917 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000918 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000919 // Check that we have a valid, previously declared ivar for @synthesize
920 if (Synthesize) {
921 // @synthesize
922 if (!PropertyIvar)
923 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000924 // Check that this is a previously declared 'ivar' in 'IDecl' interface
925 ObjCInterfaceDecl *ClassDeclared;
926 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
927 QualType PropType = property->getType();
928 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000929
930 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000931 diag::err_incomplete_synthesized_property,
932 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000933 Diag(property->getLocation(), diag::note_property_declare);
934 CompleteTypeErr = true;
935 }
936
David Blaikiebbafb8a2012-03-11 07:00:24 +0000937 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000938 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000939 ObjCPropertyDecl::OBJC_PR_readonly) &&
940 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000941 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
942 }
943
John McCall31168b02011-06-15 23:02:42 +0000944 ObjCPropertyDecl::PropertyAttributeKind kind
945 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000946
947 // Add GC __weak to the ivar type if the property is weak.
948 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000949 getLangOpts().getGC() != LangOptions::NonGC) {
950 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000951 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000952 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000953 Diag(property->getLocation(), diag::note_property_declare);
954 } else {
955 PropertyIvarType =
956 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000957 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000958 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000959 if (AtLoc.isInvalid()) {
960 // Check when default synthesizing a property that there is
961 // an ivar matching property name and issue warning; since this
962 // is the most common case of not using an ivar used for backing
963 // property in non-default synthesis case.
964 ObjCInterfaceDecl *ClassDeclared=0;
965 ObjCIvarDecl *originalIvar =
966 IDecl->lookupInstanceVariable(property->getIdentifier(),
967 ClassDeclared);
968 if (originalIvar) {
969 Diag(PropertyDiagLoc,
970 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000971 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000972 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000973 Diag(property->getLocation(), diag::note_property_declare);
974 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000975 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000976 }
977
978 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000979 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000980 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000981 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000982 !PropertyIvarType.getObjCLifetime() &&
983 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000984
John McCall43192862011-09-13 18:31:23 +0000985 // It's an error if we have to do this and the user didn't
986 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000987 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000988 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000989 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000990 diag::err_arc_objc_property_default_assign_on_object);
991 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +0000992 } else {
993 Qualifiers::ObjCLifetime lifetime =
994 getImpliedARCOwnership(kind, PropertyIvarType);
995 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +0000996 if (lifetime == Qualifiers::OCL_Weak) {
997 bool err = false;
998 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +0000999 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1000 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1001 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001002 Diag(property->getLocation(),
1003 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1004 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1005 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001006 err = true;
1007 }
Richard Smith802c4b72012-08-23 06:16:52 +00001008 }
John McCall3deb1ad2012-08-21 02:47:43 +00001009 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001010 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001011 Diag(property->getLocation(), diag::note_property_declare);
1012 }
John McCall31168b02011-06-15 23:02:42 +00001013 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001014
John McCall31168b02011-06-15 23:02:42 +00001015 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001016 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001017 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1018 }
John McCall31168b02011-06-15 23:02:42 +00001019 }
1020
1021 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001022 !getLangOpts().ObjCAutoRefCount &&
1023 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001024 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001025 Diag(property->getLocation(), diag::note_property_declare);
1026 }
1027
Abramo Bagnaradff19302011-03-08 08:55:46 +00001028 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001029 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001030 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001031 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001032 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001033 if (RequireNonAbstractType(PropertyIvarLoc,
1034 PropertyIvarType,
1035 diag::err_abstract_type_in_decl,
1036 AbstractSynthesizedIvarType)) {
1037 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001038 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001039 } else if (CompleteTypeErr)
1040 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001041 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001042 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001043
John McCall5fb5df92012-06-20 06:18:46 +00001044 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001045 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1046 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001047 // Note! I deliberately want it to fall thru so, we have a
1048 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001049 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001050 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001051 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001052 << property->getDeclName() << Ivar->getDeclName()
1053 << ClassDeclared->getDeclName();
1054 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001055 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001056 // Note! I deliberately want it to fall thru so more errors are caught.
1057 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001058 property->setPropertyIvarDecl(Ivar);
1059
Ted Kremenekac597f32010-03-12 00:46:40 +00001060 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1061
1062 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001063 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001064 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001065 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001066 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001067 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001068 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001069 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001070 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001071 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1072 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001073 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001074 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001075 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001076 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001077 << property->getDeclName() << PropType
1078 << Ivar->getDeclName() << IvarType;
1079 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001080 // Note! I deliberately want it to fall thru so, we have a
1081 // a property implementation and to avoid future warnings.
1082 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001083 else {
1084 // FIXME! Rules for properties are somewhat different that those
1085 // for assignments. Use a new routine to consolidate all cases;
1086 // specifically for property redeclarations as well as for ivars.
1087 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1088 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1089 if (lhsType != rhsType &&
1090 lhsType->isArithmeticType()) {
1091 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1092 << property->getDeclName() << PropType
1093 << Ivar->getDeclName() << IvarType;
1094 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1095 // Fall thru - see previous comment
1096 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001097 }
1098 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001099 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001100 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001101 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001102 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001103 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001104 // Fall thru - see previous comment
1105 }
John McCall31168b02011-06-15 23:02:42 +00001106 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001107 if ((property->getType()->isObjCObjectPointerType() ||
1108 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001109 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001110 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 << property->getDeclName() << Ivar->getDeclName();
1112 // Fall thru - see previous comment
1113 }
1114 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001115 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001116 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001117 } else if (PropertyIvar)
1118 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001119 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001120
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1122 ObjCPropertyImplDecl *PIDecl =
1123 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1124 property,
1125 (Synthesize ?
1126 ObjCPropertyImplDecl::Synthesize
1127 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001128 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001129
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001130 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001131 PIDecl->setInvalidDecl();
1132
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001133 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1134 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001135 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001136 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001137 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1138 // returned by the getter as it must conform to C++'s copy-return rules.
1139 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001140 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001141 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1142 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001143 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001144 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001145 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001146 Expr *LoadSelfExpr =
1147 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1148 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001149 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001150 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001151 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001152 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001153 ExprResult Res = PerformCopyInitialization(
1154 InitializedEntity::InitializeResult(PropertyDiagLoc,
1155 getterMethod->getReturnType(),
1156 /*NRVO=*/false),
1157 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001158 if (!Res.isInvalid()) {
1159 Expr *ResExpr = Res.takeAs<Expr>();
1160 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001161 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001162 PIDecl->setGetterCXXConstructor(ResExpr);
1163 }
1164 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001165 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1166 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1167 Diag(getterMethod->getLocation(),
1168 diag::warn_property_getter_owning_mismatch);
1169 Diag(property->getLocation(), diag::note_property_declare);
1170 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001171 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1172 switch (getterMethod->getMethodFamily()) {
1173 case OMF_retain:
1174 case OMF_retainCount:
1175 case OMF_release:
1176 case OMF_autorelease:
1177 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1178 << 1 << getterMethod->getSelector();
1179 break;
1180 default:
1181 break;
1182 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001183 }
1184 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1185 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001186 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1187 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001188 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001189 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001190 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1191 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001192 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001193 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001194 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001195 Expr *LoadSelfExpr =
1196 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1197 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001198 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001199 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001200 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001201 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001202 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1203 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001204 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001205 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1206 VK_LValue, PropertyDiagLoc);
1207 MarkDeclRefReferenced(rhs);
1208 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001209 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001210 if (property->getPropertyAttributes() &
1211 ObjCPropertyDecl::OBJC_PR_atomic) {
1212 Expr *callExpr = Res.takeAs<Expr>();
1213 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001214 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1215 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001216 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001217 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001218 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001219 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001220 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001221 Diag(FuncDecl->getLocStart(),
1222 diag::note_callee_decl) << FuncDecl;
1223 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001224 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001225 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1226 }
1227 }
1228
Ted Kremenekac597f32010-03-12 00:46:40 +00001229 if (IC) {
1230 if (Synthesize)
1231 if (ObjCPropertyImplDecl *PPIDecl =
1232 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1233 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1234 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1235 << PropertyIvar;
1236 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1237 }
1238
1239 if (ObjCPropertyImplDecl *PPIDecl
1240 = IC->FindPropertyImplDecl(PropertyId)) {
1241 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1242 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001243 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001244 }
1245 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001246 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001247 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001248 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001249 // Diagnose if an ivar was lazily synthesdized due to a previous
1250 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001251 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001252 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001253 ObjCIvarDecl *Ivar = 0;
1254 if (!Synthesize)
1255 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1256 else {
1257 if (PropertyIvar && PropertyIvar != PropertyId)
1258 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1259 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001260 // Issue diagnostics only if Ivar belongs to current class.
1261 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001262 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001263 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1264 << PropertyId;
1265 Ivar->setInvalidDecl();
1266 }
1267 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001268 } else {
1269 if (Synthesize)
1270 if (ObjCPropertyImplDecl *PPIDecl =
1271 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001272 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001273 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1274 << PropertyIvar;
1275 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1276 }
1277
1278 if (ObjCPropertyImplDecl *PPIDecl =
1279 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001280 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001281 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001282 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001283 }
1284 CatImplClass->addPropertyImplementation(PIDecl);
1285 }
1286
John McCall48871652010-08-21 09:40:31 +00001287 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001288}
1289
1290//===----------------------------------------------------------------------===//
1291// Helper methods.
1292//===----------------------------------------------------------------------===//
1293
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001294/// DiagnosePropertyMismatch - Compares two properties for their
1295/// attributes and types and warns on a variety of inconsistencies.
1296///
1297void
1298Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1299 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001300 const IdentifierInfo *inheritedName,
1301 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001302 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001303 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001304 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001305 SuperProperty->getPropertyAttributes();
1306
1307 // We allow readonly properties without an explicit ownership
1308 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1309 // to be overridden by a property with any explicit ownership in the subclass.
1310 if (!OverridingProtocolProperty &&
1311 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1312 ;
1313 else {
1314 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1315 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1316 Diag(Property->getLocation(), diag::warn_readonly_property)
1317 << Property->getDeclName() << inheritedName;
1318 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1319 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001320 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001321 << Property->getDeclName() << "copy" << inheritedName;
1322 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1323 unsigned CAttrRetain =
1324 (CAttr &
1325 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1326 unsigned SAttrRetain =
1327 (SAttr &
1328 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1329 bool CStrong = (CAttrRetain != 0);
1330 bool SStrong = (SAttrRetain != 0);
1331 if (CStrong != SStrong)
1332 Diag(Property->getLocation(), diag::warn_property_attribute)
1333 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1334 }
John McCall31168b02011-06-15 23:02:42 +00001335 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001336
1337 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001338 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001339 Diag(Property->getLocation(), diag::warn_property_attribute)
1340 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001341 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1342 }
1343 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001344 Diag(Property->getLocation(), diag::warn_property_attribute)
1345 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001346 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1347 }
1348 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001349 Diag(Property->getLocation(), diag::warn_property_attribute)
1350 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001351 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1352 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001353
1354 QualType LHSType =
1355 Context.getCanonicalType(SuperProperty->getType());
1356 QualType RHSType =
1357 Context.getCanonicalType(Property->getType());
1358
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001359 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001360 // Do cases not handled in above.
1361 // FIXME. For future support of covariant property types, revisit this.
1362 bool IncompatibleObjC = false;
1363 QualType ConvertedType;
1364 if (!isObjCPointerConversion(RHSType, LHSType,
1365 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001366 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001367 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1368 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001369 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1370 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001371 }
1372}
1373
1374bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1375 ObjCMethodDecl *GetterMethod,
1376 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001377 if (!GetterMethod)
1378 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001379 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001380 QualType PropertyIvarType = property->getType().getNonReferenceType();
1381 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1382 if (!compat) {
1383 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1384 isa<ObjCObjectPointerType>(GetterType))
1385 compat =
1386 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001387 GetterType->getAs<ObjCObjectPointerType>(),
1388 PropertyIvarType->getAs<ObjCObjectPointerType>());
1389 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001390 != Compatible) {
1391 Diag(Loc, diag::error_property_accessor_type)
1392 << property->getDeclName() << PropertyIvarType
1393 << GetterMethod->getSelector() << GetterType;
1394 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1395 return true;
1396 } else {
1397 compat = true;
1398 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1399 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1400 if (lhsType != rhsType && lhsType->isArithmeticType())
1401 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001402 }
1403 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001404
1405 if (!compat) {
1406 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1407 << property->getDeclName()
1408 << GetterMethod->getSelector();
1409 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1410 return true;
1411 }
1412
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001413 return false;
1414}
1415
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001416/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001417/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001418static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1419 ObjCContainerDecl::PropertyMap &PropMap,
1420 ObjCContainerDecl::PropertyMap &SuperPropMap,
1421 bool IncludeProtocols = true) {
1422
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001423 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001424 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001425 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001426 if (IncludeProtocols) {
1427 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001428 for (auto *PI : IDecl->all_referenced_protocols())
1429 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001430 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001431 }
1432 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1433 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001434 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001435 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001436 if (IncludeProtocols) {
1437 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001438 for (auto *PI : CATDecl->protocols())
1439 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001440 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001441 }
1442 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001443 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001444 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1445 // Exclude property for protocols which conform to class's super-class,
1446 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001447 if (!PropertyFromSuper ||
1448 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001449 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1450 if (!PropEntry)
1451 PropEntry = Prop;
1452 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001453 }
1454 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001455 for (auto *PI : PDecl->protocols())
1456 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001457 }
1458}
1459
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001460/// CollectSuperClassPropertyImplementations - This routine collects list of
1461/// properties to be implemented in super class(s) and also coming from their
1462/// conforming protocols.
1463static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001464 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001465 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001466 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001467 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001468 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001469 SDecl = SDecl->getSuperClass();
1470 }
1471 }
1472}
1473
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001474/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1475/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1476/// declared in class 'IFace'.
1477bool
1478Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1479 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1480 if (!IV->getSynthesize())
1481 return false;
1482 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1483 Method->isInstanceMethod());
1484 if (!IMD || !IMD->isPropertyAccessor())
1485 return false;
1486
1487 // look up a property declaration whose one of its accessors is implemented
1488 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001489 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001490 if ((Property->getGetterName() == IMD->getSelector() ||
1491 Property->getSetterName() == IMD->getSelector()) &&
1492 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001493 return true;
1494 }
1495 return false;
1496}
1497
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001498static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1499 ObjCPropertyDecl *Prop) {
1500 bool SuperClassImplementsGetter = false;
1501 bool SuperClassImplementsSetter = false;
1502 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1503 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001504
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001505 while (IDecl->getSuperClass()) {
1506 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1507 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1508 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001509
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001510 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1511 SuperClassImplementsSetter = true;
1512 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1513 return true;
1514 IDecl = IDecl->getSuperClass();
1515 }
1516 return false;
1517}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001518
James Dennett2a4d13c2012-06-15 07:13:21 +00001519/// \brief Default synthesizes all properties which must be synthesized
1520/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001521void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1522 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001523
Anna Zaks673d76b2012-10-18 19:17:53 +00001524 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001525 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1526 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001527 if (PropMap.empty())
1528 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001529 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001530 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1531
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001532 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1533 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001534 // Is there a matching property synthesize/dynamic?
1535 if (Prop->isInvalidDecl() ||
1536 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1537 continue;
1538 // Property may have been synthesized by user.
1539 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1540 continue;
1541 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1542 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1543 continue;
1544 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1545 continue;
1546 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001547 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001548 if (SuperPropMap[Prop->getIdentifier()]) {
1549 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1550 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1551 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001552 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001553 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1554 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001555 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001556 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001557 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1558 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001559 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001560 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001561 if (ObjCPropertyImplDecl *PID =
1562 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1563 if (PID->getPropertyDecl() != Prop) {
1564 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001565 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001566 if (!PID->getLocation().isInvalid())
1567 Diag(PID->getLocation(), diag::note_property_synthesize);
1568 }
1569 continue;
1570 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001571 if (ObjCProtocolDecl *Proto =
1572 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001573 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001574 // Suppress the warning if class's superclass implements property's
1575 // getter and implements property's setter (if readwrite property).
1576 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1577 Diag(IMPDecl->getLocation(),
1578 diag::warn_auto_synthesizing_protocol_property)
1579 << Prop << Proto;
1580 Diag(Prop->getLocation(), diag::note_property_declare);
1581 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001582 continue;
1583 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001584
1585 // We use invalid SourceLocations for the synthesized ivars since they
1586 // aren't really synthesized at a particular location; they just exist.
1587 // Saying that they are located at the @implementation isn't really going
1588 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001589 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1590 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1591 true,
1592 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001593 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001594 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001595 if (PIDecl) {
1596 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001597 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001598 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001599 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001600}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001601
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001602void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001603 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001604 return;
1605 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1606 if (!IC)
1607 return;
1608 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001609 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001610 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001611}
1612
Ted Kremenek7e812952014-02-21 19:41:30 +00001613static void DiagnoseUnimplementedAccessor(Sema &S,
1614 ObjCInterfaceDecl *PrimaryClass,
1615 Selector Method,
1616 ObjCImplDecl* IMPDecl,
1617 ObjCContainerDecl *CDecl,
1618 ObjCCategoryDecl *C,
1619 ObjCPropertyDecl *Prop,
1620 Sema::SelectorSet &SMap) {
1621 // When reporting on missing property setter/getter implementation in
1622 // categories, do not report when they are declared in primary class,
1623 // class's protocol, or one of it super classes. This is because,
1624 // the class is going to implement them.
1625 if (!SMap.count(Method) &&
1626 (PrimaryClass == 0 ||
1627 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1628 S.Diag(IMPDecl->getLocation(),
1629 isa<ObjCCategoryDecl>(CDecl) ?
1630 diag::warn_setter_getter_impl_required_in_category :
1631 diag::warn_setter_getter_impl_required)
1632 << Prop->getDeclName() << Method;
1633 S.Diag(Prop->getLocation(),
1634 diag::note_property_declare);
1635 if (S.LangOpts.ObjCDefaultSynthProperties &&
1636 S.LangOpts.ObjCRuntime.isNonFragile())
1637 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1638 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1639 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1640 }
1641}
1642
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001643void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001644 ObjCContainerDecl *CDecl,
1645 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001646 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001647 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1648
Ted Kremenek348e88c2014-02-21 19:41:34 +00001649 if (!SynthesizeProperties) {
1650 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001651 // Gather properties which need not be implemented in this class
1652 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001653 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001654 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1655 // For categories, no need to implement properties declared in
1656 // its primary class (and its super classes) if property is
1657 // declared in one of those containers.
1658 if ((IDecl = C->getClassInterface())) {
1659 ObjCInterfaceDecl::PropertyDeclOrder PO;
1660 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1661 }
1662 }
1663 if (IDecl)
1664 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1665
1666 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1667 }
1668
Ted Kremenek38882022014-02-21 19:41:39 +00001669 // Scan the @interface to see if any of the protocols it adopts
1670 // require an explicit implementation, via attribute
1671 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001672 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001673 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001674
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001675 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001676 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1677 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001678 // Lazily construct a set of all the properties in the @interface
1679 // of the class, without looking at the superclass. We cannot
1680 // use the call to CollectImmediateProperties() above as that
1681 // utilizes information fromt he super class's properties as well
1682 // as scans the adopted protocols. This work only triggers for protocols
1683 // with the attribute, which is very rare, and only occurs when
1684 // analyzing the @implementation.
1685 if (!LazyMap) {
1686 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1687 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1688 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1689 /* IncludeProtocols */ false);
1690 }
Ted Kremenek38882022014-02-21 19:41:39 +00001691 // Add the properties of 'PDecl' to the list of properties that
1692 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001693 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001694 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001695 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001696 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001697 }
1698 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001699 }
Ted Kremenek38882022014-02-21 19:41:39 +00001700
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001701 if (PropMap.empty())
1702 return;
1703
1704 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001705 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001706 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001707
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001708 SelectorSet InsMap;
1709 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001710 for (const auto *I : IMPDecl->instance_methods())
1711 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001712
1713 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1714 ObjCInterfaceDecl *PrimaryClass = 0;
1715 if (C && !C->IsClassExtension())
1716 if ((PrimaryClass = C->getClassInterface()))
1717 // Report unimplemented properties in the category as well.
1718 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1719 // When reporting on missing setter/getters, do not report when
1720 // setter/getter is implemented in category's primary class
1721 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001722 for (const auto *I : IMP->instance_methods())
1723 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001724 }
1725
Anna Zaks673d76b2012-10-18 19:17:53 +00001726 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001727 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1728 ObjCPropertyDecl *Prop = P->second;
1729 // Is there a matching propery synthesize/dynamic?
1730 if (Prop->isInvalidDecl() ||
1731 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001732 PropImplMap.count(Prop) ||
1733 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001734 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001735
1736 // Diagnose unimplemented getters and setters.
1737 DiagnoseUnimplementedAccessor(*this,
1738 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1739 if (!Prop->isReadOnly())
1740 DiagnoseUnimplementedAccessor(*this,
1741 PrimaryClass, Prop->getSetterName(),
1742 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001743 }
1744}
1745
1746void
1747Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1748 ObjCContainerDecl* IDecl) {
1749 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001750 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001751 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001752 for (const auto *Property : IDecl->properties()) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001753 ObjCMethodDecl *GetterMethod = 0;
1754 ObjCMethodDecl *SetterMethod = 0;
1755 bool LookedUpGetterSetter = false;
1756
Bill Wendling44426052012-12-20 19:22:21 +00001757 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001758 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001759
John McCall43192862011-09-13 18:31:23 +00001760 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1761 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001762 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1763 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1764 LookedUpGetterSetter = true;
1765 if (GetterMethod) {
1766 Diag(GetterMethod->getLocation(),
1767 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001768 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001769 Diag(Property->getLocation(), diag::note_property_declare);
1770 }
1771 if (SetterMethod) {
1772 Diag(SetterMethod->getLocation(),
1773 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001774 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001775 Diag(Property->getLocation(), diag::note_property_declare);
1776 }
1777 }
1778
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001779 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001780 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1781 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001782 continue;
1783 if (const ObjCPropertyImplDecl *PIDecl
1784 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1785 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1786 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001787 if (!LookedUpGetterSetter) {
1788 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1789 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001790 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001791 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1792 SourceLocation MethodLoc =
1793 (GetterMethod ? GetterMethod->getLocation()
1794 : SetterMethod->getLocation());
1795 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001796 << Property->getIdentifier() << (GetterMethod != 0)
1797 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001798 // fixit stuff.
1799 if (!AttributesAsWritten) {
1800 if (Property->getLParenLoc().isValid()) {
1801 // @property () ... case.
1802 SourceRange PropSourceRange(Property->getAtLoc(),
1803 Property->getLParenLoc());
1804 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1805 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1806 }
1807 else {
1808 //@property id etc.
1809 SourceLocation endLoc =
1810 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1811 endLoc = endLoc.getLocWithOffset(-1);
1812 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1813 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1814 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1815 }
1816 }
1817 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1818 // @property () ... case.
1819 SourceLocation endLoc = Property->getLParenLoc();
1820 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1821 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1822 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1823 }
1824 else
1825 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001826 Diag(Property->getLocation(), diag::note_property_declare);
1827 }
1828 }
1829 }
1830}
1831
John McCall31168b02011-06-15 23:02:42 +00001832void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001833 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001834 return;
1835
Aaron Ballmand85eff42014-03-14 15:02:45 +00001836 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001837 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001838 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1839 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001840 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1841 if (!method)
1842 continue;
1843 ObjCMethodFamily family = method->getMethodFamily();
1844 if (family == OMF_alloc || family == OMF_copy ||
1845 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001846 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001847 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001848 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001849 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001850 }
1851 }
1852 }
1853}
1854
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001855void Sema::DiagnoseMissingDesignatedInitOverrides(
1856 const ObjCImplementationDecl *ImplD,
1857 const ObjCInterfaceDecl *IFD) {
1858 assert(IFD->hasDesignatedInitializers());
1859 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1860 if (!SuperD)
1861 return;
1862
1863 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001864 for (const auto *I : ImplD->instance_methods())
1865 if (I->getMethodFamily() == OMF_init)
1866 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001867
1868 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1869 SuperD->getDesignatedInitializers(DesignatedInits);
1870 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1871 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1872 const ObjCMethodDecl *MD = *I;
1873 if (!InitSelSet.count(MD->getSelector())) {
1874 Diag(ImplD->getLocation(),
1875 diag::warn_objc_implementation_missing_designated_init_override)
1876 << MD->getSelector();
1877 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1878 }
1879 }
1880}
1881
John McCallad31b5f2010-11-10 07:01:40 +00001882/// AddPropertyAttrs - Propagates attributes from a property to the
1883/// implicitly-declared getter or setter for that property.
1884static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1885 ObjCPropertyDecl *Property) {
1886 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001887 for (const auto *A : Property->attrs()) {
1888 if (isa<DeprecatedAttr>(A) ||
1889 isa<UnavailableAttr>(A) ||
1890 isa<AvailabilityAttr>(A))
1891 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001892 }
John McCallad31b5f2010-11-10 07:01:40 +00001893}
1894
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001895/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1896/// have the property type and issue diagnostics if they don't.
1897/// Also synthesize a getter/setter method if none exist (and update the
1898/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1899/// methods is the "right" thing to do.
1900void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001901 ObjCContainerDecl *CD,
1902 ObjCPropertyDecl *redeclaredProperty,
1903 ObjCContainerDecl *lexicalDC) {
1904
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001905 ObjCMethodDecl *GetterMethod, *SetterMethod;
1906
1907 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1908 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1909 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1910 property->getLocation());
1911
1912 if (SetterMethod) {
1913 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1914 property->getPropertyAttributes();
1915 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001916 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1917 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001918 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1919 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001920 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001921 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1922 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001923 Diag(property->getLocation(),
1924 diag::warn_accessor_property_type_mismatch)
1925 << property->getDeclName()
1926 << SetterMethod->getSelector();
1927 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1928 }
1929 }
1930
1931 // Synthesize getter/setter methods if none exist.
1932 // Find the default getter and if one not found, add one.
1933 // FIXME: The synthesized property we set here is misleading. We almost always
1934 // synthesize these methods unless the user explicitly provided prototypes
1935 // (which is odd, but allowed). Sema should be typechecking that the
1936 // declarations jive in that situation (which it is not currently).
1937 if (!GetterMethod) {
1938 // No instance method of same name as property getter name was found.
1939 // Declare a getter method and add it to the list of methods
1940 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001941 SourceLocation Loc = redeclaredProperty ?
1942 redeclaredProperty->getLocation() :
1943 property->getLocation();
1944
1945 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1946 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001947 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001948 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001949 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001950 (property->getPropertyImplementation() ==
1951 ObjCPropertyDecl::Optional) ?
1952 ObjCMethodDecl::Optional :
1953 ObjCMethodDecl::Required);
1954 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001955
1956 AddPropertyAttrs(*this, GetterMethod, property);
1957
Ted Kremenek49be9e02010-05-18 21:09:07 +00001958 // FIXME: Eventually this shouldn't be needed, as the lexical context
1959 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001960 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001961 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001962 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001963 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1964 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001965
1966 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1967 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001968 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001969
1970 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00001971 GetterMethod->addAttr(
1972 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
1973 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00001974
1975 if (getLangOpts().ObjCAutoRefCount)
1976 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001977 } else
1978 // A user declared getter will be synthesize when @synthesize of
1979 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001980 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001981 property->setGetterMethodDecl(GetterMethod);
1982
1983 // Skip setter if property is read-only.
1984 if (!property->isReadOnly()) {
1985 // Find the default setter and if one not found, add one.
1986 if (!SetterMethod) {
1987 // No instance method of same name as property setter name was found.
1988 // Declare a setter method and add it to the list of methods
1989 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001990 SourceLocation Loc = redeclaredProperty ?
1991 redeclaredProperty->getLocation() :
1992 property->getLocation();
1993
1994 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001995 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001996 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001997 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001998 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001999 /*isImplicitlyDeclared=*/true,
2000 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002001 (property->getPropertyImplementation() ==
2002 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002003 ObjCMethodDecl::Optional :
2004 ObjCMethodDecl::Required);
2005
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002006 // Invent the arguments for the setter. We don't bother making a
2007 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002008 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2009 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002010 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002011 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002012 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00002013 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002014 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002015 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002016
2017 AddPropertyAttrs(*this, SetterMethod, property);
2018
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002019 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002020 // FIXME: Eventually this shouldn't be needed, as the lexical context
2021 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002022 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002023 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002024 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002025 SetterMethod->addAttr(
2026 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2027 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002028 // It's possible for the user to have set a very odd custom
2029 // setter selector that causes it to have a method family.
2030 if (getLangOpts().ObjCAutoRefCount)
2031 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002032 } else
2033 // A user declared setter will be synthesize when @synthesize of
2034 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002035 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002036 property->setSetterMethodDecl(SetterMethod);
2037 }
2038 // Add any synthesized methods to the global pool. This allows us to
2039 // handle the following, which is supported by GCC (and part of the design).
2040 //
2041 // @interface Foo
2042 // @property double bar;
2043 // @end
2044 //
2045 // void thisIsUnfortunate() {
2046 // id foo;
2047 // double bar = [foo bar];
2048 // }
2049 //
2050 if (GetterMethod)
2051 AddInstanceMethodToGlobalPool(GetterMethod);
2052 if (SetterMethod)
2053 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002054
2055 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2056 if (!CurrentClass) {
2057 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2058 CurrentClass = Cat->getClassInterface();
2059 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2060 CurrentClass = Impl->getClassInterface();
2061 }
2062 if (GetterMethod)
2063 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2064 if (SetterMethod)
2065 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002066}
2067
John McCall48871652010-08-21 09:40:31 +00002068void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002069 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002070 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002071 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002072 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002073 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002074 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002075
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002076 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2077 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2078 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2079 << "readonly" << "readwrite";
2080
2081 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2082 QualType PropertyTy = PropertyDecl->getType();
2083 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002084
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002085 // 'readonly' property with no obvious lifetime.
2086 // its life time will be determined by its backing ivar.
2087 if (getLangOpts().ObjCAutoRefCount &&
2088 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2089 PropertyTy->isObjCRetainableType() &&
2090 !PropertyOwnership)
2091 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002092
2093 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002094 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002095 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2096 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002097 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002098 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002099 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2100 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2101 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002102 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002103 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002104 }
2105
2106 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002107 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2108 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002109 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2110 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002111 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002112 }
Bill Wendling44426052012-12-20 19:22:21 +00002113 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002114 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2115 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002116 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002117 }
Bill Wendling44426052012-12-20 19:22:21 +00002118 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002119 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2120 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002121 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002122 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002123 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002124 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002125 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2126 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002127 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002128 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002129 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002130 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002131 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2132 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002135 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002136 }
Bill Wendling44426052012-12-20 19:22:21 +00002137 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2139 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002140 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002141 }
Bill Wendling44426052012-12-20 19:22:21 +00002142 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002143 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2144 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002145 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002146 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002147 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002148 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002149 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2150 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002151 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002152 }
Bill Wendling44426052012-12-20 19:22:21 +00002153 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2154 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002155 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2156 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002157 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002158 }
Bill Wendling44426052012-12-20 19:22:21 +00002159 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002160 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2161 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002162 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002163 }
Bill Wendling44426052012-12-20 19:22:21 +00002164 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2166 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002167 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002168 }
2169 }
Bill Wendling44426052012-12-20 19:22:21 +00002170 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2171 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002172 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2173 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002174 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002175 }
Bill Wendling44426052012-12-20 19:22:21 +00002176 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2177 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002178 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2179 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002180 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002181 }
2182
Bill Wendling44426052012-12-20 19:22:21 +00002183 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2184 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002185 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2186 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002187 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002188 }
2189
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002190 // Warn if user supplied no assignment attribute, property is
2191 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002192 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002193 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2194 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2195 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002196 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002197 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002198 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002199 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002200 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002201 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002202 bool isAnyClassTy =
2203 (PropertyTy->isObjCClassType() ||
2204 PropertyTy->isObjCQualifiedClassType());
2205 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2206 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002207 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002208 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002209 else if (propertyInPrimaryClass) {
2210 // Don't issue warning on property with no life time in class
2211 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002212 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002213 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002214 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002215
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002216 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002217 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002218 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002219 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002220 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002221
2222 // FIXME: Implement warning dependent on NSCopying being
2223 // implemented. See also:
2224 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2225 // (please trim this list while you are at it).
2226 }
2227
Bill Wendling44426052012-12-20 19:22:21 +00002228 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2229 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002230 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002231 && PropertyTy->isBlockPointerType())
2232 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002233 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2234 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2235 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002236 PropertyTy->isBlockPointerType())
2237 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002238
Bill Wendling44426052012-12-20 19:22:21 +00002239 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2240 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002241 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2242
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002243}