blob: dd0721ad4b3bcdeefd3058cd2b85607b6f483234 [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);
Alp Toker2d57cea2014-05-17 04:53:25 +0000295 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000296 Loc = Tok.getLocation();
297 return true;
298 }
299 } while (Tok.isNot(tok::r_paren));
300 return false;
301
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000302}
303
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000304static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000305 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
306 ObjCPropertyDecl::OBJC_PR_retain |
307 ObjCPropertyDecl::OBJC_PR_copy |
308 ObjCPropertyDecl::OBJC_PR_weak |
309 ObjCPropertyDecl::OBJC_PR_strong |
310 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
311}
312
Douglas Gregor90d34422013-01-21 19:05:22 +0000313ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000314Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000315 SourceLocation AtLoc,
316 SourceLocation LParenLoc,
317 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000318 Selector GetterSel, Selector SetterSel,
319 const bool isAssign,
320 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000321 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000322 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000323 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000324 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000325 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000326 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000327 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000328 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000329 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000330 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
331
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000332 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000333 // Check for duplicate declaration of this property in current and
334 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000335 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000336 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000337 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000338 Diag(AtLoc, diag::err_duplicate_property);
339 Diag(prevDecl->getLocation(), diag::note_property_declare);
340 return 0;
341 }
342 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000343 }
344
Ted Kremenek959e8302010-03-12 02:31:10 +0000345 // Create a new ObjCPropertyDecl with the DeclContext being
346 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000347 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000348 ObjCPropertyDecl *PDecl =
349 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000350 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000351 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000352 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000353 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000354 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000355 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000356 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000357 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
358 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
359 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
360 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000361 // Set setter/getter selector name. Needed later.
362 PDecl->setGetterName(GetterSel);
363 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000364 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000365 DC->addDecl(PDecl);
366
367 // We need to look in the @interface to see if the @property was
368 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000369 if (!CCPrimary) {
370 Diag(CDecl->getLocation(), diag::err_continuation_class);
371 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000372 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000373 }
374
375 // Find the property in continuation class's primary class only.
376 ObjCPropertyDecl *PIDecl =
377 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
378
379 if (!PIDecl) {
380 // No matching property found in the primary class. Just fall thru
381 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000382 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000383 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000384 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000385 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000386
387 // A case of continuation class adding a new property in the class. This
388 // is not what it was meant for. However, gcc supports it and so should we.
389 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000390 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000391 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000392 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
393 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000394 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000395 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
396 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000397 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000398 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
399 bool IncompatibleObjC = false;
400 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000401 // Relax the strict type matching for property type in continuation class.
402 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000403 // as it narrows the object type in its primary class property. Note that
404 // this conversion is safe only because the wider type is for a 'readonly'
405 // property in primary class and 'narrowed' type for a 'readwrite' property
406 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000407 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
408 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
409 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
410 ConvertedType, IncompatibleObjC))
411 || IncompatibleObjC) {
412 Diag(AtLoc,
413 diag::err_type_mismatch_continuation_class) << PDecl->getType();
414 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000415 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000416 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000417 }
418
Ted Kremenek959e8302010-03-12 02:31:10 +0000419 // The property 'PIDecl's readonly attribute will be over-ridden
420 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000421 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000422 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000423 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
424 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000425 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000426 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000427 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000428 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
429 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 Diag(AtLoc, diag::warn_property_attr_mismatch);
431 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000432 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000433 else if (getLangOpts().ObjCAutoRefCount) {
434 QualType PrimaryPropertyQT =
435 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
436 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000437 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000438 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
439 PrimaryPropertyQT.getObjCLifetime();
440 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000441 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
442 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000443 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
444 Diag(PIDecl->getLocation(), diag::note_property_declare);
445 }
446 }
447 }
448
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000449 DeclContext *DC = cast<DeclContext>(CCPrimary);
450 if (!ObjCPropertyDecl::findPropertyDecl(DC,
451 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000452 // In mrr mode, 'readwrite' property must have an explicit
453 // memory attribute. If none specified, select the default (assign).
454 if (!getLangOpts().ObjCAutoRefCount) {
455 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
456 ObjCDeclSpec::DQ_PR_retain |
457 ObjCDeclSpec::DQ_PR_strong |
458 ObjCDeclSpec::DQ_PR_copy |
459 ObjCDeclSpec::DQ_PR_unsafe_unretained |
460 ObjCDeclSpec::DQ_PR_weak)))
461 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
462 }
463
Ted Kremenek959e8302010-03-12 02:31:10 +0000464 // Protocol is not in the primary class. Must build one for it.
465 ObjCDeclSpec ProtocolPropertyODS;
466 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
467 // and ObjCPropertyDecl::PropertyAttributeKind have identical
468 // values. Should consolidate both into one enum type.
469 ProtocolPropertyODS.
470 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
471 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000472 // Must re-establish the context from class extension to primary
473 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000474 ContextRAII SavedContext(*this, CCPrimary);
475
John McCall48871652010-08-21 09:40:31 +0000476 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000477 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000478 PIDecl->getGetterName(),
479 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000480 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000481 MethodImplKind,
482 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000483 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000484 }
485 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000486 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000487 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000488 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000489 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000491 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
492 PIDecl->setSetterName(SetterSel);
493 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000494 // Tailor the diagnostics for the common case where a readwrite
495 // property is declared both in the @interface and the continuation.
496 // This is a common error where the user often intended the original
497 // declaration to be readonly.
498 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000499 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000500 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
501 ? diag::err_use_continuation_class_redeclaration_readwrite
502 : diag::err_use_continuation_class;
503 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000504 << CCPrimary->getDeclName();
505 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000506 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000507 }
508 *isOverridingProperty = true;
509 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000510 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000511 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
512 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000513 if (ASTMutationListener *L = Context.getASTMutationListener())
514 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000515 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000516}
517
518ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
519 ObjCContainerDecl *CDecl,
520 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000521 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000522 FieldDeclarator &FD,
523 Selector GetterSel,
524 Selector SetterSel,
525 const bool isAssign,
526 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000527 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000528 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000529 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000530 tok::ObjCKeywordKind MethodImplKind,
531 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000532 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000533 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000534
535 // Issue a warning if property is 'assign' as default and its object, which is
536 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000537 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000538 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000539 if (const ObjCObjectPointerType *ObjPtrTy =
540 T->getAs<ObjCObjectPointerType>()) {
541 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
542 if (IDecl)
543 if (ObjCProtocolDecl* PNSCopying =
544 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
545 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
546 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000547 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000548
549 if (T->isObjCObjectType()) {
550 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
Alp Tokerb6cc5922014-05-03 03:45:55 +0000551 StarLoc = getLocForEndOfToken(StarLoc);
Eli Friedman999af7b2013-07-09 01:38:07 +0000552 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
553 << FixItHint::CreateInsertion(StarLoc, "*");
554 T = Context.getObjCObjectPointerType(T);
555 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
556 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
557 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000558
Ted Kremenek959e8302010-03-12 02:31:10 +0000559 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000560 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
561 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000562 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000563
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000564 if (ObjCPropertyDecl *prevDecl =
565 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000566 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000567 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000568 PDecl->setInvalidDecl();
569 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000570 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000571 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000572 if (lexicalDC)
573 PDecl->setLexicalDeclContext(lexicalDC);
574 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000575
576 if (T->isArrayType() || T->isFunctionType()) {
577 Diag(AtLoc, diag::err_property_type) << T;
578 PDecl->setInvalidDecl();
579 }
580
581 ProcessDeclAttributes(S, PDecl, FD.D);
582
583 // Regardless of setter/getter attribute, we save the default getter/setter
584 // selector names in anticipation of declaration of setter/getter methods.
585 PDecl->setGetterName(GetterSel);
586 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000587 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000588 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000589
Bill Wendling44426052012-12-20 19:22:21 +0000590 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000591 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
592
Bill Wendling44426052012-12-20 19:22:21 +0000593 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
595
Bill Wendling44426052012-12-20 19:22:21 +0000596 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000597 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
598
599 if (isReadWrite)
600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
601
Bill Wendling44426052012-12-20 19:22:21 +0000602 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000603 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
604
Bill Wendling44426052012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
607
Bill Wendling44426052012-12-20 19:22:21 +0000608 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
610
Bill Wendling44426052012-12-20 19:22:21 +0000611 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
613
Bill Wendling44426052012-12-20 19:22:21 +0000614 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
616
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 if (isAssign)
618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
619
John McCall43192862011-09-13 18:31:23 +0000620 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000621 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000623 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000625
John McCall31168b02011-06-15 23:02:42 +0000626 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000627 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
629 if (isAssign)
630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
631
Ted Kremenekac597f32010-03-12 00:46:40 +0000632 if (MethodImplKind == tok::objc_required)
633 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
634 else if (MethodImplKind == tok::objc_optional)
635 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000636
Ted Kremenek959e8302010-03-12 02:31:10 +0000637 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000638}
639
John McCall31168b02011-06-15 23:02:42 +0000640static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
641 ObjCPropertyDecl *property,
642 ObjCIvarDecl *ivar) {
643 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
644
John McCall31168b02011-06-15 23:02:42 +0000645 QualType ivarType = ivar->getType();
646 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000647
John McCall43192862011-09-13 18:31:23 +0000648 // The lifetime implied by the property's attributes.
649 Qualifiers::ObjCLifetime propertyLifetime =
650 getImpliedARCOwnership(property->getPropertyAttributes(),
651 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000652
John McCall43192862011-09-13 18:31:23 +0000653 // We're fine if they match.
654 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000655
John McCall43192862011-09-13 18:31:23 +0000656 // These aren't valid lifetimes for object ivars; don't diagnose twice.
657 if (ivarLifetime == Qualifiers::OCL_None ||
658 ivarLifetime == Qualifiers::OCL_Autoreleasing)
659 return;
John McCall31168b02011-06-15 23:02:42 +0000660
John McCalld8561f02012-08-20 23:36:59 +0000661 // If the ivar is private, and it's implicitly __unsafe_unretained
662 // becaues of its type, then pretend it was actually implicitly
663 // __strong. This is only sound because we're processing the
664 // property implementation before parsing any method bodies.
665 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
666 propertyLifetime == Qualifiers::OCL_Strong &&
667 ivar->getAccessControl() == ObjCIvarDecl::Private) {
668 SplitQualType split = ivarType.split();
669 if (split.Quals.hasObjCLifetime()) {
670 assert(ivarType->isObjCARCImplicitlyUnretainedType());
671 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
672 ivarType = S.Context.getQualifiedType(split);
673 ivar->setType(ivarType);
674 return;
675 }
676 }
677
John McCall43192862011-09-13 18:31:23 +0000678 switch (propertyLifetime) {
679 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000680 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000681 << property->getDeclName()
682 << ivar->getDeclName()
683 << ivarLifetime;
684 break;
John McCall31168b02011-06-15 23:02:42 +0000685
John McCall43192862011-09-13 18:31:23 +0000686 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000687 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000688 << property->getDeclName()
689 << ivar->getDeclName();
690 break;
John McCall31168b02011-06-15 23:02:42 +0000691
John McCall43192862011-09-13 18:31:23 +0000692 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000693 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000694 << property->getDeclName()
695 << ivar->getDeclName()
696 << ((property->getPropertyAttributesAsWritten()
697 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
698 break;
John McCall31168b02011-06-15 23:02:42 +0000699
John McCall43192862011-09-13 18:31:23 +0000700 case Qualifiers::OCL_Autoreleasing:
701 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000702
John McCall43192862011-09-13 18:31:23 +0000703 case Qualifiers::OCL_None:
704 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000705 return;
706 }
707
708 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000709 if (propertyImplLoc.isValid())
710 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000711}
712
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000713/// setImpliedPropertyAttributeForReadOnlyProperty -
714/// This routine evaludates life-time attributes for a 'readonly'
715/// property with no known lifetime of its own, using backing
716/// 'ivar's attribute, if any. If no backing 'ivar', property's
717/// life-time is assumed 'strong'.
718static void setImpliedPropertyAttributeForReadOnlyProperty(
719 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
720 Qualifiers::ObjCLifetime propertyLifetime =
721 getImpliedARCOwnership(property->getPropertyAttributes(),
722 property->getType());
723 if (propertyLifetime != Qualifiers::OCL_None)
724 return;
725
726 if (!ivar) {
727 // if no backing ivar, make property 'strong'.
728 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
729 return;
730 }
731 // property assumes owenership of backing ivar.
732 QualType ivarType = ivar->getType();
733 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
734 if (ivarLifetime == Qualifiers::OCL_Strong)
735 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
736 else if (ivarLifetime == Qualifiers::OCL_Weak)
737 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
738 return;
739}
Ted Kremenekac597f32010-03-12 00:46:40 +0000740
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000741/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
742/// in inherited protocols with mismatched types. Since any of them can
743/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000744static void
745DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
746 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000747 ObjCPropertyDecl *Property) {
748 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000749 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
750 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000751 PDecl->collectInheritedProtocolProperties(Property, PropMap);
752 }
753 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
754 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000755 for (const auto *PI : SDecl->all_referenced_protocols()) {
756 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000757 PDecl->collectInheritedProtocolProperties(Property, PropMap);
758 }
759 SDecl = SDecl->getSuperClass();
760 }
761
762 if (PropMap.empty())
763 return;
764
765 QualType RHSType = S.Context.getCanonicalType(Property->getType());
766 bool FirsTime = true;
767 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
768 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
769 ObjCPropertyDecl *Prop = I->second;
770 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
771 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
772 bool IncompatibleObjC = false;
773 QualType ConvertedType;
774 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
775 || IncompatibleObjC) {
776 if (FirsTime) {
777 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
778 << Property->getType();
779 FirsTime = false;
780 }
781 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
782 << Prop->getType();
783 }
784 }
785 }
786 if (!FirsTime && AtLoc.isValid())
787 S.Diag(AtLoc, diag::note_property_synthesize);
788}
789
Ted Kremenekac597f32010-03-12 00:46:40 +0000790/// ActOnPropertyImplDecl - This routine performs semantic checks and
791/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000792/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000793///
John McCall48871652010-08-21 09:40:31 +0000794Decl *Sema::ActOnPropertyImplDecl(Scope *S,
795 SourceLocation AtLoc,
796 SourceLocation PropertyLoc,
797 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000798 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000799 IdentifierInfo *PropertyIvar,
800 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000801 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000802 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000803 // Make sure we have a context for the property implementation declaration.
804 if (!ClassImpDecl) {
805 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000806 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000807 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000808 if (PropertyIvarLoc.isInvalid())
809 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000810 SourceLocation PropertyDiagLoc = PropertyLoc;
811 if (PropertyDiagLoc.isInvalid())
812 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000813 ObjCPropertyDecl *property = 0;
814 ObjCInterfaceDecl* IDecl = 0;
815 // Find the class or category class where this property must have
816 // a declaration.
817 ObjCImplementationDecl *IC = 0;
818 ObjCCategoryImplDecl* CatImplClass = 0;
819 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
820 IDecl = IC->getClassInterface();
821 // We always synthesize an interface for an implementation
822 // without an interface decl. So, IDecl is always non-zero.
823 assert(IDecl &&
824 "ActOnPropertyImplDecl - @implementation without @interface");
825
826 // Look for this property declaration in the @implementation's @interface
827 property = IDecl->FindPropertyDeclaration(PropertyId);
828 if (!property) {
829 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000830 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000831 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000832 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000833 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
834 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000835 if (AtLoc.isValid())
836 Diag(AtLoc, diag::warn_implicit_atomic_property);
837 else
838 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
839 Diag(property->getLocation(), diag::note_property_declare);
840 }
841
Ted Kremenekac597f32010-03-12 00:46:40 +0000842 if (const ObjCCategoryDecl *CD =
843 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
844 if (!CD->IsClassExtension()) {
845 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
846 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000847 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000848 }
849 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000850 if (Synthesize&&
851 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
852 property->hasAttr<IBOutletAttr>() &&
853 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000854 bool ReadWriteProperty = false;
855 // Search into the class extensions and see if 'readonly property is
856 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000857 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000858 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
859 if (!R.empty())
860 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
861 PIkind = ExtProp->getPropertyAttributesAsWritten();
862 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
863 ReadWriteProperty = true;
864 break;
865 }
866 }
867 }
868
869 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000870 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000871 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000872 SourceLocation readonlyLoc;
873 if (LocPropertyAttribute(Context, "readonly",
874 property->getLParenLoc(), readonlyLoc)) {
875 SourceLocation endLoc =
876 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
877 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
878 Diag(property->getLocation(),
879 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
880 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
881 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000882 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000883 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000884 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
885 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000886
Ted Kremenekac597f32010-03-12 00:46:40 +0000887 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
888 if (Synthesize) {
889 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000890 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000891 }
892 IDecl = CatImplClass->getClassInterface();
893 if (!IDecl) {
894 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000895 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000896 }
897 ObjCCategoryDecl *Category =
898 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
899
900 // If category for this implementation not found, it is an error which
901 // has already been reported eralier.
902 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000903 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000904 // Look for this property declaration in @implementation's category
905 property = Category->FindPropertyDeclaration(PropertyId);
906 if (!property) {
907 Diag(PropertyLoc, diag::error_bad_category_property_decl)
908 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000909 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000910 }
911 } else {
912 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000913 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000914 }
915 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000916 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000917 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000918 // Check that we have a valid, previously declared ivar for @synthesize
919 if (Synthesize) {
920 // @synthesize
921 if (!PropertyIvar)
922 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000923 // Check that this is a previously declared 'ivar' in 'IDecl' interface
924 ObjCInterfaceDecl *ClassDeclared;
925 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
926 QualType PropType = property->getType();
927 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000928
929 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000930 diag::err_incomplete_synthesized_property,
931 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000932 Diag(property->getLocation(), diag::note_property_declare);
933 CompleteTypeErr = true;
934 }
935
David Blaikiebbafb8a2012-03-11 07:00:24 +0000936 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000937 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000938 ObjCPropertyDecl::OBJC_PR_readonly) &&
939 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000940 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
941 }
942
John McCall31168b02011-06-15 23:02:42 +0000943 ObjCPropertyDecl::PropertyAttributeKind kind
944 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000945
946 // Add GC __weak to the ivar type if the property is weak.
947 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000948 getLangOpts().getGC() != LangOptions::NonGC) {
949 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000950 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000951 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000952 Diag(property->getLocation(), diag::note_property_declare);
953 } else {
954 PropertyIvarType =
955 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000956 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000957 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000958 if (AtLoc.isInvalid()) {
959 // Check when default synthesizing a property that there is
960 // an ivar matching property name and issue warning; since this
961 // is the most common case of not using an ivar used for backing
962 // property in non-default synthesis case.
963 ObjCInterfaceDecl *ClassDeclared=0;
964 ObjCIvarDecl *originalIvar =
965 IDecl->lookupInstanceVariable(property->getIdentifier(),
966 ClassDeclared);
967 if (originalIvar) {
968 Diag(PropertyDiagLoc,
969 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000970 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000971 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000972 Diag(property->getLocation(), diag::note_property_declare);
973 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000974 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000975 }
976
977 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000978 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000979 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000980 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000981 !PropertyIvarType.getObjCLifetime() &&
982 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000983
John McCall43192862011-09-13 18:31:23 +0000984 // It's an error if we have to do this and the user didn't
985 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000986 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000987 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000988 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000989 diag::err_arc_objc_property_default_assign_on_object);
990 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +0000991 } else {
992 Qualifiers::ObjCLifetime lifetime =
993 getImpliedARCOwnership(kind, PropertyIvarType);
994 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +0000995 if (lifetime == Qualifiers::OCL_Weak) {
996 bool err = false;
997 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +0000998 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
999 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1000 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001001 Diag(property->getLocation(),
1002 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1003 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1004 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001005 err = true;
1006 }
Richard Smith802c4b72012-08-23 06:16:52 +00001007 }
John McCall3deb1ad2012-08-21 02:47:43 +00001008 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001009 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001010 Diag(property->getLocation(), diag::note_property_declare);
1011 }
John McCall31168b02011-06-15 23:02:42 +00001012 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001013
John McCall31168b02011-06-15 23:02:42 +00001014 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001015 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001016 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1017 }
John McCall31168b02011-06-15 23:02:42 +00001018 }
1019
1020 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001021 !getLangOpts().ObjCAutoRefCount &&
1022 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001023 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001024 Diag(property->getLocation(), diag::note_property_declare);
1025 }
1026
Abramo Bagnaradff19302011-03-08 08:55:46 +00001027 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001028 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001029 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001030 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001031 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001032 if (RequireNonAbstractType(PropertyIvarLoc,
1033 PropertyIvarType,
1034 diag::err_abstract_type_in_decl,
1035 AbstractSynthesizedIvarType)) {
1036 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001037 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001038 } else if (CompleteTypeErr)
1039 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001040 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001041 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001042
John McCall5fb5df92012-06-20 06:18:46 +00001043 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001044 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1045 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001046 // Note! I deliberately want it to fall thru so, we have a
1047 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001048 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001049 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001050 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001051 << property->getDeclName() << Ivar->getDeclName()
1052 << ClassDeclared->getDeclName();
1053 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001054 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001055 // Note! I deliberately want it to fall thru so more errors are caught.
1056 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001057 property->setPropertyIvarDecl(Ivar);
1058
Ted Kremenekac597f32010-03-12 00:46:40 +00001059 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1060
1061 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001062 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001063 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001064 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001065 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001066 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001067 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001068 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001069 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001070 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1071 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001072 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001073 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001074 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001075 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001076 << property->getDeclName() << PropType
1077 << Ivar->getDeclName() << IvarType;
1078 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001079 // Note! I deliberately want it to fall thru so, we have a
1080 // a property implementation and to avoid future warnings.
1081 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001082 else {
1083 // FIXME! Rules for properties are somewhat different that those
1084 // for assignments. Use a new routine to consolidate all cases;
1085 // specifically for property redeclarations as well as for ivars.
1086 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1087 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1088 if (lhsType != rhsType &&
1089 lhsType->isArithmeticType()) {
1090 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1091 << property->getDeclName() << PropType
1092 << Ivar->getDeclName() << IvarType;
1093 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1094 // Fall thru - see previous comment
1095 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001096 }
1097 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001098 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001099 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001100 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001101 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001102 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001103 // Fall thru - see previous comment
1104 }
John McCall31168b02011-06-15 23:02:42 +00001105 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001106 if ((property->getType()->isObjCObjectPointerType() ||
1107 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001108 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001109 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001110 << property->getDeclName() << Ivar->getDeclName();
1111 // Fall thru - see previous comment
1112 }
1113 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001114 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001115 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001116 } else if (PropertyIvar)
1117 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001118 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001119
Ted Kremenekac597f32010-03-12 00:46:40 +00001120 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1121 ObjCPropertyImplDecl *PIDecl =
1122 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1123 property,
1124 (Synthesize ?
1125 ObjCPropertyImplDecl::Synthesize
1126 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001127 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001128
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001129 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001130 PIDecl->setInvalidDecl();
1131
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001132 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1133 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001134 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001135 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001136 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1137 // returned by the getter as it must conform to C++'s copy-return rules.
1138 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001139 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001140 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1141 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001142 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001143 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001144 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001145 Expr *LoadSelfExpr =
1146 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1147 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001148 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001149 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001150 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001151 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001152 ExprResult Res = PerformCopyInitialization(
1153 InitializedEntity::InitializeResult(PropertyDiagLoc,
1154 getterMethod->getReturnType(),
1155 /*NRVO=*/false),
1156 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001157 if (!Res.isInvalid()) {
1158 Expr *ResExpr = Res.takeAs<Expr>();
1159 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001160 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001161 PIDecl->setGetterCXXConstructor(ResExpr);
1162 }
1163 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001164 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1165 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1166 Diag(getterMethod->getLocation(),
1167 diag::warn_property_getter_owning_mismatch);
1168 Diag(property->getLocation(), diag::note_property_declare);
1169 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001170 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1171 switch (getterMethod->getMethodFamily()) {
1172 case OMF_retain:
1173 case OMF_retainCount:
1174 case OMF_release:
1175 case OMF_autorelease:
1176 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1177 << 1 << getterMethod->getSelector();
1178 break;
1179 default:
1180 break;
1181 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001182 }
1183 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1184 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001185 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1186 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001187 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001188 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001189 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1190 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001191 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001192 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001193 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001194 Expr *LoadSelfExpr =
1195 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1196 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001197 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001198 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001199 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001200 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001201 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1202 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001203 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001204 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1205 VK_LValue, PropertyDiagLoc);
1206 MarkDeclRefReferenced(rhs);
1207 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001208 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001209 if (property->getPropertyAttributes() &
1210 ObjCPropertyDecl::OBJC_PR_atomic) {
1211 Expr *callExpr = Res.takeAs<Expr>();
1212 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001213 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1214 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001215 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001216 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001217 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001218 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001219 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001220 Diag(FuncDecl->getLocStart(),
1221 diag::note_callee_decl) << FuncDecl;
1222 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001223 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001224 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1225 }
1226 }
1227
Ted Kremenekac597f32010-03-12 00:46:40 +00001228 if (IC) {
1229 if (Synthesize)
1230 if (ObjCPropertyImplDecl *PPIDecl =
1231 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1232 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1233 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1234 << PropertyIvar;
1235 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1236 }
1237
1238 if (ObjCPropertyImplDecl *PPIDecl
1239 = IC->FindPropertyImplDecl(PropertyId)) {
1240 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1241 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001242 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001243 }
1244 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001245 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001246 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001247 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001248 // Diagnose if an ivar was lazily synthesdized due to a previous
1249 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001250 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001251 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001252 ObjCIvarDecl *Ivar = 0;
1253 if (!Synthesize)
1254 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1255 else {
1256 if (PropertyIvar && PropertyIvar != PropertyId)
1257 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1258 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001259 // Issue diagnostics only if Ivar belongs to current class.
1260 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001261 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001262 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1263 << PropertyId;
1264 Ivar->setInvalidDecl();
1265 }
1266 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001267 } else {
1268 if (Synthesize)
1269 if (ObjCPropertyImplDecl *PPIDecl =
1270 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001271 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001272 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1273 << PropertyIvar;
1274 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1275 }
1276
1277 if (ObjCPropertyImplDecl *PPIDecl =
1278 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001279 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001280 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001281 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001282 }
1283 CatImplClass->addPropertyImplementation(PIDecl);
1284 }
1285
John McCall48871652010-08-21 09:40:31 +00001286 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001287}
1288
1289//===----------------------------------------------------------------------===//
1290// Helper methods.
1291//===----------------------------------------------------------------------===//
1292
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001293/// DiagnosePropertyMismatch - Compares two properties for their
1294/// attributes and types and warns on a variety of inconsistencies.
1295///
1296void
1297Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1298 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001299 const IdentifierInfo *inheritedName,
1300 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001301 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001302 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001303 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001304 SuperProperty->getPropertyAttributes();
1305
1306 // We allow readonly properties without an explicit ownership
1307 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1308 // to be overridden by a property with any explicit ownership in the subclass.
1309 if (!OverridingProtocolProperty &&
1310 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1311 ;
1312 else {
1313 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1314 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1315 Diag(Property->getLocation(), diag::warn_readonly_property)
1316 << Property->getDeclName() << inheritedName;
1317 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1318 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001319 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001320 << Property->getDeclName() << "copy" << inheritedName;
1321 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1322 unsigned CAttrRetain =
1323 (CAttr &
1324 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1325 unsigned SAttrRetain =
1326 (SAttr &
1327 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1328 bool CStrong = (CAttrRetain != 0);
1329 bool SStrong = (SAttrRetain != 0);
1330 if (CStrong != SStrong)
1331 Diag(Property->getLocation(), diag::warn_property_attribute)
1332 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1333 }
John McCall31168b02011-06-15 23:02:42 +00001334 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001335
1336 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001337 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001338 Diag(Property->getLocation(), diag::warn_property_attribute)
1339 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001340 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1341 }
1342 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001343 Diag(Property->getLocation(), diag::warn_property_attribute)
1344 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001345 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1346 }
1347 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001348 Diag(Property->getLocation(), diag::warn_property_attribute)
1349 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001350 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1351 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001352
1353 QualType LHSType =
1354 Context.getCanonicalType(SuperProperty->getType());
1355 QualType RHSType =
1356 Context.getCanonicalType(Property->getType());
1357
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001358 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001359 // Do cases not handled in above.
1360 // FIXME. For future support of covariant property types, revisit this.
1361 bool IncompatibleObjC = false;
1362 QualType ConvertedType;
1363 if (!isObjCPointerConversion(RHSType, LHSType,
1364 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001365 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001366 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1367 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001368 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1369 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001370 }
1371}
1372
1373bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1374 ObjCMethodDecl *GetterMethod,
1375 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001376 if (!GetterMethod)
1377 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001378 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001379 QualType PropertyIvarType = property->getType().getNonReferenceType();
1380 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1381 if (!compat) {
1382 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1383 isa<ObjCObjectPointerType>(GetterType))
1384 compat =
1385 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001386 GetterType->getAs<ObjCObjectPointerType>(),
1387 PropertyIvarType->getAs<ObjCObjectPointerType>());
1388 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001389 != Compatible) {
1390 Diag(Loc, diag::error_property_accessor_type)
1391 << property->getDeclName() << PropertyIvarType
1392 << GetterMethod->getSelector() << GetterType;
1393 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1394 return true;
1395 } else {
1396 compat = true;
1397 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1398 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1399 if (lhsType != rhsType && lhsType->isArithmeticType())
1400 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001401 }
1402 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001403
1404 if (!compat) {
1405 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1406 << property->getDeclName()
1407 << GetterMethod->getSelector();
1408 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1409 return true;
1410 }
1411
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001412 return false;
1413}
1414
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001415/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001416/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001417static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1418 ObjCContainerDecl::PropertyMap &PropMap,
1419 ObjCContainerDecl::PropertyMap &SuperPropMap,
1420 bool IncludeProtocols = true) {
1421
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001422 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001423 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001424 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001425 if (IncludeProtocols) {
1426 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001427 for (auto *PI : IDecl->all_referenced_protocols())
1428 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001429 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001430 }
1431 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1432 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001433 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001434 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001435 if (IncludeProtocols) {
1436 // Scan through class's protocols.
Aaron Ballman19a41762014-03-14 12:55:57 +00001437 for (auto *PI : CATDecl->protocols())
1438 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001439 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001440 }
1441 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001442 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001443 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1444 // Exclude property for protocols which conform to class's super-class,
1445 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001446 if (!PropertyFromSuper ||
1447 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001448 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1449 if (!PropEntry)
1450 PropEntry = Prop;
1451 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001452 }
1453 // scan through protocol's protocols.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001454 for (auto *PI : PDecl->protocols())
1455 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001456 }
1457}
1458
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001459/// CollectSuperClassPropertyImplementations - This routine collects list of
1460/// properties to be implemented in super class(s) and also coming from their
1461/// conforming protocols.
1462static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001463 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001464 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001465 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001466 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001467 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001468 SDecl = SDecl->getSuperClass();
1469 }
1470 }
1471}
1472
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001473/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1474/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1475/// declared in class 'IFace'.
1476bool
1477Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1478 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1479 if (!IV->getSynthesize())
1480 return false;
1481 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1482 Method->isInstanceMethod());
1483 if (!IMD || !IMD->isPropertyAccessor())
1484 return false;
1485
1486 // look up a property declaration whose one of its accessors is implemented
1487 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001488 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001489 if ((Property->getGetterName() == IMD->getSelector() ||
1490 Property->getSetterName() == IMD->getSelector()) &&
1491 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001492 return true;
1493 }
1494 return false;
1495}
1496
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001497static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1498 ObjCPropertyDecl *Prop) {
1499 bool SuperClassImplementsGetter = false;
1500 bool SuperClassImplementsSetter = false;
1501 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1502 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001503
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001504 while (IDecl->getSuperClass()) {
1505 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1506 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1507 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001508
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001509 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1510 SuperClassImplementsSetter = true;
1511 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1512 return true;
1513 IDecl = IDecl->getSuperClass();
1514 }
1515 return false;
1516}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001517
James Dennett2a4d13c2012-06-15 07:13:21 +00001518/// \brief Default synthesizes all properties which must be synthesized
1519/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001520void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1521 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001522
Anna Zaks673d76b2012-10-18 19:17:53 +00001523 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001524 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1525 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001526 if (PropMap.empty())
1527 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001528 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001529 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1530
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001531 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1532 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001533 // Is there a matching property synthesize/dynamic?
1534 if (Prop->isInvalidDecl() ||
1535 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1536 continue;
1537 // Property may have been synthesized by user.
1538 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1539 continue;
1540 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1541 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1542 continue;
1543 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1544 continue;
1545 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001546 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001547 if (SuperPropMap[Prop->getIdentifier()]) {
1548 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1549 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1550 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001551 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001552 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1553 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001554 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001555 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001556 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1557 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001558 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001559 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001560 if (ObjCPropertyImplDecl *PID =
1561 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1562 if (PID->getPropertyDecl() != Prop) {
1563 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001564 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001565 if (!PID->getLocation().isInvalid())
1566 Diag(PID->getLocation(), diag::note_property_synthesize);
1567 }
1568 continue;
1569 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001570 if (ObjCProtocolDecl *Proto =
1571 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001572 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001573 // Suppress the warning if class's superclass implements property's
1574 // getter and implements property's setter (if readwrite property).
1575 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1576 Diag(IMPDecl->getLocation(),
1577 diag::warn_auto_synthesizing_protocol_property)
1578 << Prop << Proto;
1579 Diag(Prop->getLocation(), diag::note_property_declare);
1580 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001581 continue;
1582 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001583
1584 // We use invalid SourceLocations for the synthesized ivars since they
1585 // aren't really synthesized at a particular location; they just exist.
1586 // Saying that they are located at the @implementation isn't really going
1587 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001588 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1589 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1590 true,
1591 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001592 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001593 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001594 if (PIDecl) {
1595 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001596 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001597 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001598 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001599}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001600
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001601void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001602 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001603 return;
1604 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1605 if (!IC)
1606 return;
1607 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001608 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001609 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001610}
1611
Ted Kremenek7e812952014-02-21 19:41:30 +00001612static void DiagnoseUnimplementedAccessor(Sema &S,
1613 ObjCInterfaceDecl *PrimaryClass,
1614 Selector Method,
1615 ObjCImplDecl* IMPDecl,
1616 ObjCContainerDecl *CDecl,
1617 ObjCCategoryDecl *C,
1618 ObjCPropertyDecl *Prop,
1619 Sema::SelectorSet &SMap) {
1620 // When reporting on missing property setter/getter implementation in
1621 // categories, do not report when they are declared in primary class,
1622 // class's protocol, or one of it super classes. This is because,
1623 // the class is going to implement them.
1624 if (!SMap.count(Method) &&
1625 (PrimaryClass == 0 ||
1626 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1627 S.Diag(IMPDecl->getLocation(),
1628 isa<ObjCCategoryDecl>(CDecl) ?
1629 diag::warn_setter_getter_impl_required_in_category :
1630 diag::warn_setter_getter_impl_required)
1631 << Prop->getDeclName() << Method;
1632 S.Diag(Prop->getLocation(),
1633 diag::note_property_declare);
1634 if (S.LangOpts.ObjCDefaultSynthProperties &&
1635 S.LangOpts.ObjCRuntime.isNonFragile())
1636 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1637 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1638 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1639 }
1640}
1641
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001642void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001643 ObjCContainerDecl *CDecl,
1644 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001645 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001646 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1647
Ted Kremenek348e88c2014-02-21 19:41:34 +00001648 if (!SynthesizeProperties) {
1649 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001650 // Gather properties which need not be implemented in this class
1651 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001652 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001653 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1654 // For categories, no need to implement properties declared in
1655 // its primary class (and its super classes) if property is
1656 // declared in one of those containers.
1657 if ((IDecl = C->getClassInterface())) {
1658 ObjCInterfaceDecl::PropertyDeclOrder PO;
1659 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1660 }
1661 }
1662 if (IDecl)
1663 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1664
1665 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1666 }
1667
Ted Kremenek38882022014-02-21 19:41:39 +00001668 // Scan the @interface to see if any of the protocols it adopts
1669 // require an explicit implementation, via attribute
1670 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001671 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001672 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001673
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001674 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001675 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1676 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001677 // Lazily construct a set of all the properties in the @interface
1678 // of the class, without looking at the superclass. We cannot
1679 // use the call to CollectImmediateProperties() above as that
Eric Christopherc9e2a682014-05-20 17:10:39 +00001680 // utilizes information from the super class's properties as well
Ted Kremenek204c3c52014-02-22 00:02:03 +00001681 // as scans the adopted protocols. This work only triggers for protocols
1682 // with the attribute, which is very rare, and only occurs when
1683 // analyzing the @implementation.
1684 if (!LazyMap) {
1685 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1686 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1687 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1688 /* IncludeProtocols */ false);
1689 }
Ted Kremenek38882022014-02-21 19:41:39 +00001690 // Add the properties of 'PDecl' to the list of properties that
1691 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001692 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001693 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001694 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001695 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001696 }
1697 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001698 }
Ted Kremenek38882022014-02-21 19:41:39 +00001699
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001700 if (PropMap.empty())
1701 return;
1702
1703 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
Aaron Ballmand85eff42014-03-14 15:02:45 +00001704 for (const auto *I : IMPDecl->property_impls())
David Blaikie2d7c57e2012-04-30 02:36:29 +00001705 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001706
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001707 SelectorSet InsMap;
1708 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001709 for (const auto *I : IMPDecl->instance_methods())
1710 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001711
1712 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1713 ObjCInterfaceDecl *PrimaryClass = 0;
1714 if (C && !C->IsClassExtension())
1715 if ((PrimaryClass = C->getClassInterface()))
1716 // Report unimplemented properties in the category as well.
1717 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1718 // When reporting on missing setter/getters, do not report when
1719 // setter/getter is implemented in category's primary class
1720 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001721 for (const auto *I : IMP->instance_methods())
1722 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001723 }
1724
Anna Zaks673d76b2012-10-18 19:17:53 +00001725 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001726 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1727 ObjCPropertyDecl *Prop = P->second;
1728 // Is there a matching propery synthesize/dynamic?
1729 if (Prop->isInvalidDecl() ||
1730 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001731 PropImplMap.count(Prop) ||
1732 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001733 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001734
1735 // Diagnose unimplemented getters and setters.
1736 DiagnoseUnimplementedAccessor(*this,
1737 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1738 if (!Prop->isReadOnly())
1739 DiagnoseUnimplementedAccessor(*this,
1740 PrimaryClass, Prop->getSetterName(),
1741 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001742 }
1743}
1744
1745void
1746Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1747 ObjCContainerDecl* IDecl) {
1748 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001749 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001750 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001751 for (const auto *Property : IDecl->properties()) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001752 ObjCMethodDecl *GetterMethod = 0;
1753 ObjCMethodDecl *SetterMethod = 0;
1754 bool LookedUpGetterSetter = false;
1755
Bill Wendling44426052012-12-20 19:22:21 +00001756 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001757 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001758
John McCall43192862011-09-13 18:31:23 +00001759 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1760 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001761 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1762 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1763 LookedUpGetterSetter = true;
1764 if (GetterMethod) {
1765 Diag(GetterMethod->getLocation(),
1766 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001767 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001768 Diag(Property->getLocation(), diag::note_property_declare);
1769 }
1770 if (SetterMethod) {
1771 Diag(SetterMethod->getLocation(),
1772 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001773 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001774 Diag(Property->getLocation(), diag::note_property_declare);
1775 }
1776 }
1777
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001778 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001779 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1780 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001781 continue;
1782 if (const ObjCPropertyImplDecl *PIDecl
1783 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1784 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1785 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001786 if (!LookedUpGetterSetter) {
1787 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1788 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001789 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001790 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1791 SourceLocation MethodLoc =
1792 (GetterMethod ? GetterMethod->getLocation()
1793 : SetterMethod->getLocation());
1794 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001795 << Property->getIdentifier() << (GetterMethod != 0)
1796 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001797 // fixit stuff.
1798 if (!AttributesAsWritten) {
1799 if (Property->getLParenLoc().isValid()) {
1800 // @property () ... case.
1801 SourceRange PropSourceRange(Property->getAtLoc(),
1802 Property->getLParenLoc());
1803 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1804 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1805 }
1806 else {
1807 //@property id etc.
1808 SourceLocation endLoc =
1809 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1810 endLoc = endLoc.getLocWithOffset(-1);
1811 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1812 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1813 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1814 }
1815 }
1816 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1817 // @property () ... case.
1818 SourceLocation endLoc = Property->getLParenLoc();
1819 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1820 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1821 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1822 }
1823 else
1824 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001825 Diag(Property->getLocation(), diag::note_property_declare);
1826 }
1827 }
1828 }
1829}
1830
John McCall31168b02011-06-15 23:02:42 +00001831void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001832 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001833 return;
1834
Aaron Ballmand85eff42014-03-14 15:02:45 +00001835 for (const auto *PID : D->property_impls()) {
John McCall31168b02011-06-15 23:02:42 +00001836 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001837 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1838 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001839 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1840 if (!method)
1841 continue;
1842 ObjCMethodFamily family = method->getMethodFamily();
1843 if (family == OMF_alloc || family == OMF_copy ||
1844 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001845 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001846 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001847 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001848 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001849 }
1850 }
1851 }
1852}
1853
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001854void Sema::DiagnoseMissingDesignatedInitOverrides(
1855 const ObjCImplementationDecl *ImplD,
1856 const ObjCInterfaceDecl *IFD) {
1857 assert(IFD->hasDesignatedInitializers());
1858 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1859 if (!SuperD)
1860 return;
1861
1862 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001863 for (const auto *I : ImplD->instance_methods())
1864 if (I->getMethodFamily() == OMF_init)
1865 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001866
1867 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1868 SuperD->getDesignatedInitializers(DesignatedInits);
1869 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1870 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1871 const ObjCMethodDecl *MD = *I;
1872 if (!InitSelSet.count(MD->getSelector())) {
1873 Diag(ImplD->getLocation(),
1874 diag::warn_objc_implementation_missing_designated_init_override)
1875 << MD->getSelector();
1876 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1877 }
1878 }
1879}
1880
John McCallad31b5f2010-11-10 07:01:40 +00001881/// AddPropertyAttrs - Propagates attributes from a property to the
1882/// implicitly-declared getter or setter for that property.
1883static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1884 ObjCPropertyDecl *Property) {
1885 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001886 for (const auto *A : Property->attrs()) {
1887 if (isa<DeprecatedAttr>(A) ||
1888 isa<UnavailableAttr>(A) ||
1889 isa<AvailabilityAttr>(A))
1890 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001891 }
John McCallad31b5f2010-11-10 07:01:40 +00001892}
1893
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001894/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1895/// have the property type and issue diagnostics if they don't.
1896/// Also synthesize a getter/setter method if none exist (and update the
1897/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1898/// methods is the "right" thing to do.
1899void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001900 ObjCContainerDecl *CD,
1901 ObjCPropertyDecl *redeclaredProperty,
1902 ObjCContainerDecl *lexicalDC) {
1903
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904 ObjCMethodDecl *GetterMethod, *SetterMethod;
1905
1906 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1907 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1908 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1909 property->getLocation());
1910
1911 if (SetterMethod) {
1912 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1913 property->getPropertyAttributes();
1914 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001915 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1916 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001917 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1918 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001919 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001920 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1921 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001922 Diag(property->getLocation(),
1923 diag::warn_accessor_property_type_mismatch)
1924 << property->getDeclName()
1925 << SetterMethod->getSelector();
1926 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1927 }
1928 }
1929
1930 // Synthesize getter/setter methods if none exist.
1931 // Find the default getter and if one not found, add one.
1932 // FIXME: The synthesized property we set here is misleading. We almost always
1933 // synthesize these methods unless the user explicitly provided prototypes
1934 // (which is odd, but allowed). Sema should be typechecking that the
1935 // declarations jive in that situation (which it is not currently).
1936 if (!GetterMethod) {
1937 // No instance method of same name as property getter name was found.
1938 // Declare a getter method and add it to the list of methods
1939 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001940 SourceLocation Loc = redeclaredProperty ?
1941 redeclaredProperty->getLocation() :
1942 property->getLocation();
1943
1944 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1945 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001946 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001947 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001948 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001949 (property->getPropertyImplementation() ==
1950 ObjCPropertyDecl::Optional) ?
1951 ObjCMethodDecl::Optional :
1952 ObjCMethodDecl::Required);
1953 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001954
1955 AddPropertyAttrs(*this, GetterMethod, property);
1956
Ted Kremenek49be9e02010-05-18 21:09:07 +00001957 // FIXME: Eventually this shouldn't be needed, as the lexical context
1958 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001959 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001960 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001961 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001962 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1963 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001964
1965 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1966 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001967 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001968
1969 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00001970 GetterMethod->addAttr(
1971 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
1972 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00001973
1974 if (getLangOpts().ObjCAutoRefCount)
1975 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001976 } else
1977 // A user declared getter will be synthesize when @synthesize of
1978 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001979 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001980 property->setGetterMethodDecl(GetterMethod);
1981
1982 // Skip setter if property is read-only.
1983 if (!property->isReadOnly()) {
1984 // Find the default setter and if one not found, add one.
1985 if (!SetterMethod) {
1986 // No instance method of same name as property setter name was found.
1987 // Declare a setter method and add it to the list of methods
1988 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001989 SourceLocation Loc = redeclaredProperty ?
1990 redeclaredProperty->getLocation() :
1991 property->getLocation();
1992
1993 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001994 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001995 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001996 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001997 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001998 /*isImplicitlyDeclared=*/true,
1999 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002000 (property->getPropertyImplementation() ==
2001 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002002 ObjCMethodDecl::Optional :
2003 ObjCMethodDecl::Required);
2004
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002005 // Invent the arguments for the setter. We don't bother making a
2006 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002007 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2008 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002009 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002010 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002011 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00002012 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002013 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002014 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002015
2016 AddPropertyAttrs(*this, SetterMethod, property);
2017
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002018 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002019 // FIXME: Eventually this shouldn't be needed, as the lexical context
2020 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002021 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002022 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002023 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Warren Huntc3b18962014-04-08 22:30:47 +00002024 SetterMethod->addAttr(
2025 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2026 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002027 // It's possible for the user to have set a very odd custom
2028 // setter selector that causes it to have a method family.
2029 if (getLangOpts().ObjCAutoRefCount)
2030 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002031 } else
2032 // A user declared setter will be synthesize when @synthesize of
2033 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002034 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002035 property->setSetterMethodDecl(SetterMethod);
2036 }
2037 // Add any synthesized methods to the global pool. This allows us to
2038 // handle the following, which is supported by GCC (and part of the design).
2039 //
2040 // @interface Foo
2041 // @property double bar;
2042 // @end
2043 //
2044 // void thisIsUnfortunate() {
2045 // id foo;
2046 // double bar = [foo bar];
2047 // }
2048 //
2049 if (GetterMethod)
2050 AddInstanceMethodToGlobalPool(GetterMethod);
2051 if (SetterMethod)
2052 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002053
2054 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2055 if (!CurrentClass) {
2056 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2057 CurrentClass = Cat->getClassInterface();
2058 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2059 CurrentClass = Impl->getClassInterface();
2060 }
2061 if (GetterMethod)
2062 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2063 if (SetterMethod)
2064 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002065}
2066
John McCall48871652010-08-21 09:40:31 +00002067void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002068 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002069 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002070 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002071 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002072 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002073 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002074
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002075 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2076 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "readonly" << "readwrite";
2079
2080 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2081 QualType PropertyTy = PropertyDecl->getType();
2082 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002083
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002084 // 'readonly' property with no obvious lifetime.
2085 // its life time will be determined by its backing ivar.
2086 if (getLangOpts().ObjCAutoRefCount &&
2087 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2088 PropertyTy->isObjCRetainableType() &&
2089 !PropertyOwnership)
2090 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002091
2092 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002093 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002094 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2095 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002096 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002097 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002098 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2099 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2100 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002101 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002102 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002103 }
2104
2105 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002106 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2107 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002108 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2109 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002110 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002111 }
Bill Wendling44426052012-12-20 19:22:21 +00002112 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002113 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2114 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002115 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002116 }
Bill Wendling44426052012-12-20 19:22:21 +00002117 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002118 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2119 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002120 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002121 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002122 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002123 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002126 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002127 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002128 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002129 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002130 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2131 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002132 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2133 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002134 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002135 }
Bill Wendling44426052012-12-20 19:22:21 +00002136 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002137 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2138 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002139 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002140 }
Bill Wendling44426052012-12-20 19:22:21 +00002141 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002142 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2143 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002144 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002145 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002146 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002147 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002148 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2149 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002150 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002151 }
Bill Wendling44426052012-12-20 19:22:21 +00002152 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2153 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002154 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2155 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002156 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002157 }
Bill Wendling44426052012-12-20 19:22:21 +00002158 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002159 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2160 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002161 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002162 }
Bill Wendling44426052012-12-20 19:22:21 +00002163 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002164 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2165 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002166 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002167 }
2168 }
Bill Wendling44426052012-12-20 19:22:21 +00002169 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2170 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002173 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002174 }
Bill Wendling44426052012-12-20 19:22:21 +00002175 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2176 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002177 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2178 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002179 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002180 }
2181
Bill Wendling44426052012-12-20 19:22:21 +00002182 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2183 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002184 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2185 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002186 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002187 }
2188
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002189 // Warn if user supplied no assignment attribute, property is
2190 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002191 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002192 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2193 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2194 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002195 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002196 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002197 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002198 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002199 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002200 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002201 bool isAnyClassTy =
2202 (PropertyTy->isObjCClassType() ||
2203 PropertyTy->isObjCQualifiedClassType());
2204 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2205 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002206 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002207 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002208 else if (propertyInPrimaryClass) {
2209 // Don't issue warning on property with no life time in class
2210 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002211 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002212 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002213 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002214
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002215 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002216 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002217 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002218 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002219 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002220
2221 // FIXME: Implement warning dependent on NSCopying being
2222 // implemented. See also:
2223 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2224 // (please trim this list while you are at it).
2225 }
2226
Bill Wendling44426052012-12-20 19:22:21 +00002227 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2228 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002229 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002230 && PropertyTy->isBlockPointerType())
2231 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002232 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2233 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2234 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002235 PropertyTy->isBlockPointerType())
2236 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002237
Bill Wendling44426052012-12-20 19:22:21 +00002238 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2239 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002240 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2241
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002242}