blob: 4c2f889a438fc77e6058d01b686b7267518e2562 [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"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCall31168b02011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb8982092013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
120 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
121 // Have we seen this protocol before?
122 if (!Known.insert(Proto))
123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
135 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
136 PEnd = Proto->protocol_end();
137 P != PEnd; ++P) {
138 CheckPropertyAgainstProtocol(S, Prop, *P, Known);
139 }
140}
141
John McCall48871652010-08-21 09:40:31 +0000142Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000143 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000144 FieldDeclarator &FD,
145 ObjCDeclSpec &ODS,
146 Selector GetterSel,
147 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000148 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000149 tok::ObjCKeywordKind MethodImplKind,
150 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000151 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000152 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
153 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000154 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000155
Bill Wendling44426052012-12-20 19:22:21 +0000156 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000157 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000158 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000159 // property is defaulted to 'assign' if it is readwrite and is
160 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000161 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000162 (isReadWrite &&
Bill Wendling44426052012-12-20 19:22:21 +0000163 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
165 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
166 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
167 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanianb24b5682011-03-28 23:47:18 +0000168
Douglas Gregor90d34422013-01-21 19:05:22 +0000169 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000170 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregor90d34422013-01-21 19:05:22 +0000171 ObjCPropertyDecl *Res = 0;
172 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000173 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000174 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000175 FD, GetterSel, SetterSel,
176 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000177 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000178 ODS.getPropertyAttributes(),
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000179 isOverridingProperty, TSI,
180 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000181 if (!Res)
182 return 0;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000183 }
Douglas Gregor90d34422013-01-21 19:05:22 +0000184 }
185
186 if (!Res) {
187 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
188 GetterSel, SetterSel, isAssign, isReadWrite,
189 Attributes, ODS.getPropertyAttributes(),
190 TSI, MethodImplKind);
191 if (lexicalDC)
192 Res->setLexicalDeclContext(lexicalDC);
193 }
Ted Kremenekcba58492010-09-23 21:18:05 +0000194
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000195 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000196 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000197 (isa<ObjCInterfaceDecl>(ClassDecl) ||
198 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000199
David Blaikiebbafb8a2012-03-11 07:00:24 +0000200 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000201 checkARCPropertyDecl(*this, Res);
202
Douglas Gregorb8982092013-01-21 19:42:21 +0000203 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000204 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000205 // For a class, compare the property against a property in our superclass.
206 bool FoundInSuper = false;
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000207 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
208 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000209 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000210 for (unsigned I = 0, N = R.size(); I != N; ++I) {
211 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000212 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000213 FoundInSuper = true;
214 break;
215 }
216 }
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000217 if (FoundInSuper)
218 break;
219 else
220 CurrentInterfaceDecl = Super;
Douglas Gregorb8982092013-01-21 19:42:21 +0000221 }
222
223 if (FoundInSuper) {
224 // Also compare the property against a property in our protocols.
Aaron Ballmana49c5062014-03-13 20:29:09 +0000225 for (auto *P : CurrentInterfaceDecl->protocols()) {
226 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000227 }
228 } else {
229 // Slower path: look in all protocols we referenced.
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000230 for (auto *P : IFace->all_referenced_protocols()) {
231 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
Douglas Gregorb8982092013-01-21 19:42:21 +0000232 }
233 }
234 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
235 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
236 PEnd = Cat->protocol_end();
237 P != PEnd; ++P) {
238 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
239 }
240 } else {
241 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
242 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
243 PEnd = Proto->protocol_end();
244 P != PEnd; ++P) {
245 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000246 }
247 }
248
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000249 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000250 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000251}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000252
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000254makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
280
281 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
282}
283
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000284static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000285 SourceLocation LParenLoc, SourceLocation &Loc) {
286 if (LParenLoc.isMacroID())
287 return false;
288
289 SourceManager &SM = Context.getSourceManager();
290 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
291 // Try to load the file buffer.
292 bool invalidTemp = false;
293 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
294 if (invalidTemp)
295 return false;
296 const char *tokenBegin = file.data() + locInfo.second;
297
298 // Lex from the start of the given location.
299 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
300 Context.getLangOpts(),
301 file.begin(), tokenBegin, file.end());
302 Token Tok;
303 do {
304 lexer.LexFromRawLexer(Tok);
305 if (Tok.is(tok::raw_identifier) &&
306 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
307 Loc = Tok.getLocation();
308 return true;
309 }
310 } while (Tok.isNot(tok::r_paren));
311 return false;
312
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000313}
314
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000315static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000316 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
317 ObjCPropertyDecl::OBJC_PR_retain |
318 ObjCPropertyDecl::OBJC_PR_copy |
319 ObjCPropertyDecl::OBJC_PR_weak |
320 ObjCPropertyDecl::OBJC_PR_strong |
321 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
322}
323
Douglas Gregor90d34422013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000346 for (const auto *Ext : CCPrimary->known_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000347 if (ObjCPropertyDecl *prevDecl
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000348 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000349 Diag(AtLoc, diag::err_duplicate_property);
350 Diag(prevDecl->getLocation(), diag::note_property_declare);
351 return 0;
352 }
353 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000354 }
355
Ted Kremenek959e8302010-03-12 02:31:10 +0000356 // Create a new ObjCPropertyDecl with the DeclContext being
357 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000358 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000359 ObjCPropertyDecl *PDecl =
360 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000361 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000362 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000363 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000364 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000365 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000366 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000367 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000368 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
369 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
370 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
371 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000372 // Set setter/getter selector name. Needed later.
373 PDecl->setGetterName(GetterSel);
374 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000375 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000376 DC->addDecl(PDecl);
377
378 // We need to look in the @interface to see if the @property was
379 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000380 if (!CCPrimary) {
381 Diag(CDecl->getLocation(), diag::err_continuation_class);
382 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000383 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000384 }
385
386 // Find the property in continuation class's primary class only.
387 ObjCPropertyDecl *PIDecl =
388 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
389
390 if (!PIDecl) {
391 // No matching property found in the primary class. Just fall thru
392 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000393 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000394 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000395 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000396 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000397
398 // A case of continuation class adding a new property in the class. This
399 // is not what it was meant for. However, gcc supports it and so should we.
400 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000401 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000402 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000403 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
404 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000405 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000406 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
407 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000408 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000409 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
410 bool IncompatibleObjC = false;
411 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000412 // Relax the strict type matching for property type in continuation class.
413 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000414 // as it narrows the object type in its primary class property. Note that
415 // this conversion is safe only because the wider type is for a 'readonly'
416 // property in primary class and 'narrowed' type for a 'readwrite' property
417 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000418 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
419 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
420 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
421 ConvertedType, IncompatibleObjC))
422 || IncompatibleObjC) {
423 Diag(AtLoc,
424 diag::err_type_mismatch_continuation_class) << PDecl->getType();
425 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000426 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000427 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000428 }
429
Ted Kremenek959e8302010-03-12 02:31:10 +0000430 // The property 'PIDecl's readonly attribute will be over-ridden
431 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000432 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000434 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
435 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000436 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000437 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000438 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000439 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
440 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000441 Diag(AtLoc, diag::warn_property_attr_mismatch);
442 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000443 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000444 else if (getLangOpts().ObjCAutoRefCount) {
445 QualType PrimaryPropertyQT =
446 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
447 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000448 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000449 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
450 PrimaryPropertyQT.getObjCLifetime();
451 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000452 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
453 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000454 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
455 Diag(PIDecl->getLocation(), diag::note_property_declare);
456 }
457 }
458 }
459
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000460 DeclContext *DC = cast<DeclContext>(CCPrimary);
461 if (!ObjCPropertyDecl::findPropertyDecl(DC,
462 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000463 // In mrr mode, 'readwrite' property must have an explicit
464 // memory attribute. If none specified, select the default (assign).
465 if (!getLangOpts().ObjCAutoRefCount) {
466 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
467 ObjCDeclSpec::DQ_PR_retain |
468 ObjCDeclSpec::DQ_PR_strong |
469 ObjCDeclSpec::DQ_PR_copy |
470 ObjCDeclSpec::DQ_PR_unsafe_unretained |
471 ObjCDeclSpec::DQ_PR_weak)))
472 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
473 }
474
Ted Kremenek959e8302010-03-12 02:31:10 +0000475 // Protocol is not in the primary class. Must build one for it.
476 ObjCDeclSpec ProtocolPropertyODS;
477 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
478 // and ObjCPropertyDecl::PropertyAttributeKind have identical
479 // values. Should consolidate both into one enum type.
480 ProtocolPropertyODS.
481 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
482 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000483 // Must re-establish the context from class extension to primary
484 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000485 ContextRAII SavedContext(*this, CCPrimary);
486
John McCall48871652010-08-21 09:40:31 +0000487 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000488 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000489 PIDecl->getGetterName(),
490 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000491 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000492 MethodImplKind,
493 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000494 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000495 }
496 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000497 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000498 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000499 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000500 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000501 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000502 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
503 PIDecl->setSetterName(SetterSel);
504 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000505 // Tailor the diagnostics for the common case where a readwrite
506 // property is declared both in the @interface and the continuation.
507 // This is a common error where the user often intended the original
508 // declaration to be readonly.
509 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000510 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000511 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
512 ? diag::err_use_continuation_class_redeclaration_readwrite
513 : diag::err_use_continuation_class;
514 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000515 << CCPrimary->getDeclName();
516 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000517 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000518 }
519 *isOverridingProperty = true;
520 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000521 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000522 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
523 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000524 if (ASTMutationListener *L = Context.getASTMutationListener())
525 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000526 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000527}
528
529ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
530 ObjCContainerDecl *CDecl,
531 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000532 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000533 FieldDeclarator &FD,
534 Selector GetterSel,
535 Selector SetterSel,
536 const bool isAssign,
537 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000538 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000539 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000540 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000541 tok::ObjCKeywordKind MethodImplKind,
542 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000543 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000544 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000545
546 // Issue a warning if property is 'assign' as default and its object, which is
547 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000548 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000549 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000550 if (const ObjCObjectPointerType *ObjPtrTy =
551 T->getAs<ObjCObjectPointerType>()) {
552 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
553 if (IDecl)
554 if (ObjCProtocolDecl* PNSCopying =
555 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
556 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
557 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000558 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000559
560 if (T->isObjCObjectType()) {
561 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
562 StarLoc = PP.getLocForEndOfToken(StarLoc);
563 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
564 << FixItHint::CreateInsertion(StarLoc, "*");
565 T = Context.getObjCObjectPointerType(T);
566 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
567 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
568 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000569
Ted Kremenek959e8302010-03-12 02:31:10 +0000570 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000571 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
572 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000573 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000574
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000575 if (ObjCPropertyDecl *prevDecl =
576 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000577 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000578 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000579 PDecl->setInvalidDecl();
580 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000581 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000582 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000583 if (lexicalDC)
584 PDecl->setLexicalDeclContext(lexicalDC);
585 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000586
587 if (T->isArrayType() || T->isFunctionType()) {
588 Diag(AtLoc, diag::err_property_type) << T;
589 PDecl->setInvalidDecl();
590 }
591
592 ProcessDeclAttributes(S, PDecl, FD.D);
593
594 // Regardless of setter/getter attribute, we save the default getter/setter
595 // selector names in anticipation of declaration of setter/getter methods.
596 PDecl->setGetterName(GetterSel);
597 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000598 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000599 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000600
Bill Wendling44426052012-12-20 19:22:21 +0000601 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
603
Bill Wendling44426052012-12-20 19:22:21 +0000604 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
606
Bill Wendling44426052012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
609
610 if (isReadWrite)
611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
612
Bill Wendling44426052012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
618
Bill Wendling44426052012-12-20 19:22:21 +0000619 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
621
Bill Wendling44426052012-12-20 19:22:21 +0000622 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
624
Bill Wendling44426052012-12-20 19:22:21 +0000625 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
627
Ted Kremenekac597f32010-03-12 00:46:40 +0000628 if (isAssign)
629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
630
John McCall43192862011-09-13 18:31:23 +0000631 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000632 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000634 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000635 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000636
John McCall31168b02011-06-15 23:02:42 +0000637 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000638 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
640 if (isAssign)
641 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
642
Ted Kremenekac597f32010-03-12 00:46:40 +0000643 if (MethodImplKind == tok::objc_required)
644 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
645 else if (MethodImplKind == tok::objc_optional)
646 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000647
Ted Kremenek959e8302010-03-12 02:31:10 +0000648 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000649}
650
John McCall31168b02011-06-15 23:02:42 +0000651static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
652 ObjCPropertyDecl *property,
653 ObjCIvarDecl *ivar) {
654 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
655
John McCall31168b02011-06-15 23:02:42 +0000656 QualType ivarType = ivar->getType();
657 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000658
John McCall43192862011-09-13 18:31:23 +0000659 // The lifetime implied by the property's attributes.
660 Qualifiers::ObjCLifetime propertyLifetime =
661 getImpliedARCOwnership(property->getPropertyAttributes(),
662 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000663
John McCall43192862011-09-13 18:31:23 +0000664 // We're fine if they match.
665 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000666
John McCall43192862011-09-13 18:31:23 +0000667 // These aren't valid lifetimes for object ivars; don't diagnose twice.
668 if (ivarLifetime == Qualifiers::OCL_None ||
669 ivarLifetime == Qualifiers::OCL_Autoreleasing)
670 return;
John McCall31168b02011-06-15 23:02:42 +0000671
John McCalld8561f02012-08-20 23:36:59 +0000672 // If the ivar is private, and it's implicitly __unsafe_unretained
673 // becaues of its type, then pretend it was actually implicitly
674 // __strong. This is only sound because we're processing the
675 // property implementation before parsing any method bodies.
676 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
677 propertyLifetime == Qualifiers::OCL_Strong &&
678 ivar->getAccessControl() == ObjCIvarDecl::Private) {
679 SplitQualType split = ivarType.split();
680 if (split.Quals.hasObjCLifetime()) {
681 assert(ivarType->isObjCARCImplicitlyUnretainedType());
682 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
683 ivarType = S.Context.getQualifiedType(split);
684 ivar->setType(ivarType);
685 return;
686 }
687 }
688
John McCall43192862011-09-13 18:31:23 +0000689 switch (propertyLifetime) {
690 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000691 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000692 << property->getDeclName()
693 << ivar->getDeclName()
694 << ivarLifetime;
695 break;
John McCall31168b02011-06-15 23:02:42 +0000696
John McCall43192862011-09-13 18:31:23 +0000697 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000698 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000699 << property->getDeclName()
700 << ivar->getDeclName();
701 break;
John McCall31168b02011-06-15 23:02:42 +0000702
John McCall43192862011-09-13 18:31:23 +0000703 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000704 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000705 << property->getDeclName()
706 << ivar->getDeclName()
707 << ((property->getPropertyAttributesAsWritten()
708 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
709 break;
John McCall31168b02011-06-15 23:02:42 +0000710
John McCall43192862011-09-13 18:31:23 +0000711 case Qualifiers::OCL_Autoreleasing:
712 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000713
John McCall43192862011-09-13 18:31:23 +0000714 case Qualifiers::OCL_None:
715 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000716 return;
717 }
718
719 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000720 if (propertyImplLoc.isValid())
721 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000722}
723
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000724/// setImpliedPropertyAttributeForReadOnlyProperty -
725/// This routine evaludates life-time attributes for a 'readonly'
726/// property with no known lifetime of its own, using backing
727/// 'ivar's attribute, if any. If no backing 'ivar', property's
728/// life-time is assumed 'strong'.
729static void setImpliedPropertyAttributeForReadOnlyProperty(
730 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
731 Qualifiers::ObjCLifetime propertyLifetime =
732 getImpliedARCOwnership(property->getPropertyAttributes(),
733 property->getType());
734 if (propertyLifetime != Qualifiers::OCL_None)
735 return;
736
737 if (!ivar) {
738 // if no backing ivar, make property 'strong'.
739 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
740 return;
741 }
742 // property assumes owenership of backing ivar.
743 QualType ivarType = ivar->getType();
744 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
745 if (ivarLifetime == Qualifiers::OCL_Strong)
746 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
747 else if (ivarLifetime == Qualifiers::OCL_Weak)
748 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
749 return;
750}
Ted Kremenekac597f32010-03-12 00:46:40 +0000751
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000752/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
753/// in inherited protocols with mismatched types. Since any of them can
754/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000755static void
756DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
757 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000758 ObjCPropertyDecl *Property) {
759 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000760 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
761 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000762 PDecl->collectInheritedProtocolProperties(Property, PropMap);
763 }
764 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
765 while (SDecl) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +0000766 for (const auto *PI : SDecl->all_referenced_protocols()) {
767 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000768 PDecl->collectInheritedProtocolProperties(Property, PropMap);
769 }
770 SDecl = SDecl->getSuperClass();
771 }
772
773 if (PropMap.empty())
774 return;
775
776 QualType RHSType = S.Context.getCanonicalType(Property->getType());
777 bool FirsTime = true;
778 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
779 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
780 ObjCPropertyDecl *Prop = I->second;
781 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
782 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
783 bool IncompatibleObjC = false;
784 QualType ConvertedType;
785 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
786 || IncompatibleObjC) {
787 if (FirsTime) {
788 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
789 << Property->getType();
790 FirsTime = false;
791 }
792 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
793 << Prop->getType();
794 }
795 }
796 }
797 if (!FirsTime && AtLoc.isValid())
798 S.Diag(AtLoc, diag::note_property_synthesize);
799}
800
Ted Kremenekac597f32010-03-12 00:46:40 +0000801/// ActOnPropertyImplDecl - This routine performs semantic checks and
802/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000803/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000804///
John McCall48871652010-08-21 09:40:31 +0000805Decl *Sema::ActOnPropertyImplDecl(Scope *S,
806 SourceLocation AtLoc,
807 SourceLocation PropertyLoc,
808 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000809 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000810 IdentifierInfo *PropertyIvar,
811 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000812 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000813 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000814 // Make sure we have a context for the property implementation declaration.
815 if (!ClassImpDecl) {
816 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000817 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000818 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000819 if (PropertyIvarLoc.isInvalid())
820 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000821 SourceLocation PropertyDiagLoc = PropertyLoc;
822 if (PropertyDiagLoc.isInvalid())
823 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000824 ObjCPropertyDecl *property = 0;
825 ObjCInterfaceDecl* IDecl = 0;
826 // Find the class or category class where this property must have
827 // a declaration.
828 ObjCImplementationDecl *IC = 0;
829 ObjCCategoryImplDecl* CatImplClass = 0;
830 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
831 IDecl = IC->getClassInterface();
832 // We always synthesize an interface for an implementation
833 // without an interface decl. So, IDecl is always non-zero.
834 assert(IDecl &&
835 "ActOnPropertyImplDecl - @implementation without @interface");
836
837 // Look for this property declaration in the @implementation's @interface
838 property = IDecl->FindPropertyDeclaration(PropertyId);
839 if (!property) {
840 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000841 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000842 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000843 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000844 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
845 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000846 if (AtLoc.isValid())
847 Diag(AtLoc, diag::warn_implicit_atomic_property);
848 else
849 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
850 Diag(property->getLocation(), diag::note_property_declare);
851 }
852
Ted Kremenekac597f32010-03-12 00:46:40 +0000853 if (const ObjCCategoryDecl *CD =
854 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
855 if (!CD->IsClassExtension()) {
856 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
857 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000858 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000859 }
860 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000861 if (Synthesize&&
862 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
863 property->hasAttr<IBOutletAttr>() &&
864 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000865 bool ReadWriteProperty = false;
866 // Search into the class extensions and see if 'readonly property is
867 // redeclared 'readwrite', then no warning is to be issued.
Aaron Ballmanb4a53452014-03-13 21:57:01 +0000868 for (auto *Ext : IDecl->known_extensions()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000869 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
870 if (!R.empty())
871 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
872 PIkind = ExtProp->getPropertyAttributesAsWritten();
873 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
874 ReadWriteProperty = true;
875 break;
876 }
877 }
878 }
879
880 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000881 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000882 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000883 SourceLocation readonlyLoc;
884 if (LocPropertyAttribute(Context, "readonly",
885 property->getLParenLoc(), readonlyLoc)) {
886 SourceLocation endLoc =
887 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
888 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
889 Diag(property->getLocation(),
890 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
891 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
892 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000893 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000894 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000895 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
896 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000897
Ted Kremenekac597f32010-03-12 00:46:40 +0000898 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
899 if (Synthesize) {
900 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000901 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000902 }
903 IDecl = CatImplClass->getClassInterface();
904 if (!IDecl) {
905 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000906 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000907 }
908 ObjCCategoryDecl *Category =
909 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
910
911 // If category for this implementation not found, it is an error which
912 // has already been reported eralier.
913 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000914 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000915 // Look for this property declaration in @implementation's category
916 property = Category->FindPropertyDeclaration(PropertyId);
917 if (!property) {
918 Diag(PropertyLoc, diag::error_bad_category_property_decl)
919 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000920 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000921 }
922 } else {
923 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000924 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000925 }
926 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000927 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000928 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000929 // Check that we have a valid, previously declared ivar for @synthesize
930 if (Synthesize) {
931 // @synthesize
932 if (!PropertyIvar)
933 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000934 // Check that this is a previously declared 'ivar' in 'IDecl' interface
935 ObjCInterfaceDecl *ClassDeclared;
936 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
937 QualType PropType = property->getType();
938 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000939
940 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000941 diag::err_incomplete_synthesized_property,
942 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000943 Diag(property->getLocation(), diag::note_property_declare);
944 CompleteTypeErr = true;
945 }
946
David Blaikiebbafb8a2012-03-11 07:00:24 +0000947 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000948 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000949 ObjCPropertyDecl::OBJC_PR_readonly) &&
950 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000951 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
952 }
953
John McCall31168b02011-06-15 23:02:42 +0000954 ObjCPropertyDecl::PropertyAttributeKind kind
955 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000956
957 // Add GC __weak to the ivar type if the property is weak.
958 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000959 getLangOpts().getGC() != LangOptions::NonGC) {
960 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000961 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000962 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000963 Diag(property->getLocation(), diag::note_property_declare);
964 } else {
965 PropertyIvarType =
966 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000967 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000968 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000969 if (AtLoc.isInvalid()) {
970 // Check when default synthesizing a property that there is
971 // an ivar matching property name and issue warning; since this
972 // is the most common case of not using an ivar used for backing
973 // property in non-default synthesis case.
974 ObjCInterfaceDecl *ClassDeclared=0;
975 ObjCIvarDecl *originalIvar =
976 IDecl->lookupInstanceVariable(property->getIdentifier(),
977 ClassDeclared);
978 if (originalIvar) {
979 Diag(PropertyDiagLoc,
980 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000981 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000982 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000983 Diag(property->getLocation(), diag::note_property_declare);
984 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000985 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000986 }
987
988 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000989 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000990 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000991 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000992 !PropertyIvarType.getObjCLifetime() &&
993 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000994
John McCall43192862011-09-13 18:31:23 +0000995 // It's an error if we have to do this and the user didn't
996 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000997 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000998 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000999 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001000 diag::err_arc_objc_property_default_assign_on_object);
1001 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001002 } else {
1003 Qualifiers::ObjCLifetime lifetime =
1004 getImpliedARCOwnership(kind, PropertyIvarType);
1005 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001006 if (lifetime == Qualifiers::OCL_Weak) {
1007 bool err = false;
1008 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001009 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1010 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1011 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001012 Diag(property->getLocation(),
1013 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1014 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1015 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001016 err = true;
1017 }
Richard Smith802c4b72012-08-23 06:16:52 +00001018 }
John McCall3deb1ad2012-08-21 02:47:43 +00001019 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001020 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001021 Diag(property->getLocation(), diag::note_property_declare);
1022 }
John McCall31168b02011-06-15 23:02:42 +00001023 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001024
John McCall31168b02011-06-15 23:02:42 +00001025 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001026 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001027 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1028 }
John McCall31168b02011-06-15 23:02:42 +00001029 }
1030
1031 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001032 !getLangOpts().ObjCAutoRefCount &&
1033 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001034 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001035 Diag(property->getLocation(), diag::note_property_declare);
1036 }
1037
Abramo Bagnaradff19302011-03-08 08:55:46 +00001038 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001039 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001040 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001041 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001042 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001043 if (RequireNonAbstractType(PropertyIvarLoc,
1044 PropertyIvarType,
1045 diag::err_abstract_type_in_decl,
1046 AbstractSynthesizedIvarType)) {
1047 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001048 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001049 } else if (CompleteTypeErr)
1050 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001051 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001052 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001053
John McCall5fb5df92012-06-20 06:18:46 +00001054 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001055 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1056 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001057 // Note! I deliberately want it to fall thru so, we have a
1058 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001059 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001060 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001061 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001062 << property->getDeclName() << Ivar->getDeclName()
1063 << ClassDeclared->getDeclName();
1064 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001065 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001066 // Note! I deliberately want it to fall thru so more errors are caught.
1067 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001068 property->setPropertyIvarDecl(Ivar);
1069
Ted Kremenekac597f32010-03-12 00:46:40 +00001070 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1071
1072 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001073 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001074 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001075 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001076 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001077 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001078 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001079 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001080 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001081 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1082 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001083 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001084 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001085 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001086 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001087 << property->getDeclName() << PropType
1088 << Ivar->getDeclName() << IvarType;
1089 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001090 // Note! I deliberately want it to fall thru so, we have a
1091 // a property implementation and to avoid future warnings.
1092 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001093 else {
1094 // FIXME! Rules for properties are somewhat different that those
1095 // for assignments. Use a new routine to consolidate all cases;
1096 // specifically for property redeclarations as well as for ivars.
1097 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1098 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1099 if (lhsType != rhsType &&
1100 lhsType->isArithmeticType()) {
1101 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1102 << property->getDeclName() << PropType
1103 << Ivar->getDeclName() << IvarType;
1104 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1105 // Fall thru - see previous comment
1106 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001107 }
1108 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001109 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001110 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001111 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001112 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001113 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001114 // Fall thru - see previous comment
1115 }
John McCall31168b02011-06-15 23:02:42 +00001116 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001117 if ((property->getType()->isObjCObjectPointerType() ||
1118 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001119 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001120 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 << property->getDeclName() << Ivar->getDeclName();
1122 // Fall thru - see previous comment
1123 }
1124 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001125 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001126 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001127 } else if (PropertyIvar)
1128 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001129 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001130
Ted Kremenekac597f32010-03-12 00:46:40 +00001131 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1132 ObjCPropertyImplDecl *PIDecl =
1133 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1134 property,
1135 (Synthesize ?
1136 ObjCPropertyImplDecl::Synthesize
1137 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001138 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001139
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001140 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001141 PIDecl->setInvalidDecl();
1142
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001143 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1144 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001145 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001146 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001147 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1148 // returned by the getter as it must conform to C++'s copy-return rules.
1149 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001150 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001151 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1152 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001153 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001154 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001155 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001156 Expr *LoadSelfExpr =
1157 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1158 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001159 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001160 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001161 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001162 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001163 ExprResult Res = PerformCopyInitialization(
1164 InitializedEntity::InitializeResult(PropertyDiagLoc,
1165 getterMethod->getReturnType(),
1166 /*NRVO=*/false),
1167 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001168 if (!Res.isInvalid()) {
1169 Expr *ResExpr = Res.takeAs<Expr>();
1170 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001171 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001172 PIDecl->setGetterCXXConstructor(ResExpr);
1173 }
1174 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001175 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1176 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1177 Diag(getterMethod->getLocation(),
1178 diag::warn_property_getter_owning_mismatch);
1179 Diag(property->getLocation(), diag::note_property_declare);
1180 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001181 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1182 switch (getterMethod->getMethodFamily()) {
1183 case OMF_retain:
1184 case OMF_retainCount:
1185 case OMF_release:
1186 case OMF_autorelease:
1187 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1188 << 1 << getterMethod->getSelector();
1189 break;
1190 default:
1191 break;
1192 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001193 }
1194 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1195 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001196 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1197 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001198 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001199 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001200 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1201 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001202 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001203 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001204 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001205 Expr *LoadSelfExpr =
1206 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1207 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001208 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001209 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001210 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001211 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001212 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1213 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001214 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001215 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1216 VK_LValue, PropertyDiagLoc);
1217 MarkDeclRefReferenced(rhs);
1218 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001219 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001220 if (property->getPropertyAttributes() &
1221 ObjCPropertyDecl::OBJC_PR_atomic) {
1222 Expr *callExpr = Res.takeAs<Expr>();
1223 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001224 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1225 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001226 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001227 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001228 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001229 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001230 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001231 Diag(FuncDecl->getLocStart(),
1232 diag::note_callee_decl) << FuncDecl;
1233 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001234 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001235 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1236 }
1237 }
1238
Ted Kremenekac597f32010-03-12 00:46:40 +00001239 if (IC) {
1240 if (Synthesize)
1241 if (ObjCPropertyImplDecl *PPIDecl =
1242 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1243 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1244 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1245 << PropertyIvar;
1246 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1247 }
1248
1249 if (ObjCPropertyImplDecl *PPIDecl
1250 = IC->FindPropertyImplDecl(PropertyId)) {
1251 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1252 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001253 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001254 }
1255 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001256 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001257 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001258 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001259 // Diagnose if an ivar was lazily synthesdized due to a previous
1260 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001261 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001262 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001263 ObjCIvarDecl *Ivar = 0;
1264 if (!Synthesize)
1265 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1266 else {
1267 if (PropertyIvar && PropertyIvar != PropertyId)
1268 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1269 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001270 // Issue diagnostics only if Ivar belongs to current class.
1271 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001272 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001273 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1274 << PropertyId;
1275 Ivar->setInvalidDecl();
1276 }
1277 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001278 } else {
1279 if (Synthesize)
1280 if (ObjCPropertyImplDecl *PPIDecl =
1281 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001282 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001283 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1284 << PropertyIvar;
1285 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1286 }
1287
1288 if (ObjCPropertyImplDecl *PPIDecl =
1289 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001290 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001291 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001292 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001293 }
1294 CatImplClass->addPropertyImplementation(PIDecl);
1295 }
1296
John McCall48871652010-08-21 09:40:31 +00001297 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001298}
1299
1300//===----------------------------------------------------------------------===//
1301// Helper methods.
1302//===----------------------------------------------------------------------===//
1303
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001304/// DiagnosePropertyMismatch - Compares two properties for their
1305/// attributes and types and warns on a variety of inconsistencies.
1306///
1307void
1308Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1309 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001310 const IdentifierInfo *inheritedName,
1311 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001312 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001313 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001314 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001315 SuperProperty->getPropertyAttributes();
1316
1317 // We allow readonly properties without an explicit ownership
1318 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1319 // to be overridden by a property with any explicit ownership in the subclass.
1320 if (!OverridingProtocolProperty &&
1321 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1322 ;
1323 else {
1324 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1325 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1326 Diag(Property->getLocation(), diag::warn_readonly_property)
1327 << Property->getDeclName() << inheritedName;
1328 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1329 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001330 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001331 << Property->getDeclName() << "copy" << inheritedName;
1332 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1333 unsigned CAttrRetain =
1334 (CAttr &
1335 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1336 unsigned SAttrRetain =
1337 (SAttr &
1338 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1339 bool CStrong = (CAttrRetain != 0);
1340 bool SStrong = (SAttrRetain != 0);
1341 if (CStrong != SStrong)
1342 Diag(Property->getLocation(), diag::warn_property_attribute)
1343 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1344 }
John McCall31168b02011-06-15 23:02:42 +00001345 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001346
1347 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001348 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001349 Diag(Property->getLocation(), diag::warn_property_attribute)
1350 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001351 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1352 }
1353 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001354 Diag(Property->getLocation(), diag::warn_property_attribute)
1355 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001356 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1357 }
1358 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001359 Diag(Property->getLocation(), diag::warn_property_attribute)
1360 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001361 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1362 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001363
1364 QualType LHSType =
1365 Context.getCanonicalType(SuperProperty->getType());
1366 QualType RHSType =
1367 Context.getCanonicalType(Property->getType());
1368
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001369 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001370 // Do cases not handled in above.
1371 // FIXME. For future support of covariant property types, revisit this.
1372 bool IncompatibleObjC = false;
1373 QualType ConvertedType;
1374 if (!isObjCPointerConversion(RHSType, LHSType,
1375 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001376 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001377 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1378 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001379 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1380 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001381 }
1382}
1383
1384bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1385 ObjCMethodDecl *GetterMethod,
1386 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001387 if (!GetterMethod)
1388 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001389 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001390 QualType PropertyIvarType = property->getType().getNonReferenceType();
1391 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1392 if (!compat) {
1393 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1394 isa<ObjCObjectPointerType>(GetterType))
1395 compat =
1396 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001397 GetterType->getAs<ObjCObjectPointerType>(),
1398 PropertyIvarType->getAs<ObjCObjectPointerType>());
1399 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001400 != Compatible) {
1401 Diag(Loc, diag::error_property_accessor_type)
1402 << property->getDeclName() << PropertyIvarType
1403 << GetterMethod->getSelector() << GetterType;
1404 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1405 return true;
1406 } else {
1407 compat = true;
1408 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1409 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1410 if (lhsType != rhsType && lhsType->isArithmeticType())
1411 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001412 }
1413 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001414
1415 if (!compat) {
1416 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1417 << property->getDeclName()
1418 << GetterMethod->getSelector();
1419 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1420 return true;
1421 }
1422
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001423 return false;
1424}
1425
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001426/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001427/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001428static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1429 ObjCContainerDecl::PropertyMap &PropMap,
1430 ObjCContainerDecl::PropertyMap &SuperPropMap,
1431 bool IncludeProtocols = true) {
1432
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001433 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001434 for (auto *Prop : IDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001435 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001436 if (IncludeProtocols) {
1437 // Scan through class's protocols.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001438 for (auto *PI : IDecl->all_referenced_protocols())
1439 CollectImmediateProperties(PI, PropMap, SuperPropMap);
Ted Kremenek204c3c52014-02-22 00:02:03 +00001440 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001441 }
1442 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1443 if (!CATDecl->IsClassExtension())
Aaron Ballmand174edf2014-03-13 19:11:50 +00001444 for (auto *Prop : CATDecl->properties())
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001445 PropMap[Prop->getIdentifier()] = Prop;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001446 if (IncludeProtocols) {
1447 // Scan through class's protocols.
1448 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
1449 E = CATDecl->protocol_end(); PI != E; ++PI)
1450 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
1451 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001452 }
1453 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00001454 for (auto *Prop : PDecl->properties()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001455 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1456 // Exclude property for protocols which conform to class's super-class,
1457 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001458 if (!PropertyFromSuper ||
1459 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001460 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1461 if (!PropEntry)
1462 PropEntry = Prop;
1463 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001464 }
1465 // scan through protocol's protocols.
1466 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1467 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001468 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001469 }
1470}
1471
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001472/// CollectSuperClassPropertyImplementations - This routine collects list of
1473/// properties to be implemented in super class(s) and also coming from their
1474/// conforming protocols.
1475static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001476 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001477 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001478 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001479 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001480 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001481 SDecl = SDecl->getSuperClass();
1482 }
1483 }
1484}
1485
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001486/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1487/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1488/// declared in class 'IFace'.
1489bool
1490Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1491 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1492 if (!IV->getSynthesize())
1493 return false;
1494 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1495 Method->isInstanceMethod());
1496 if (!IMD || !IMD->isPropertyAccessor())
1497 return false;
1498
1499 // look up a property declaration whose one of its accessors is implemented
1500 // by this method.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001501 for (const auto *Property : IFace->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001502 if ((Property->getGetterName() == IMD->getSelector() ||
1503 Property->getSetterName() == IMD->getSelector()) &&
1504 (Property->getPropertyIvarDecl() == IV))
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001505 return true;
1506 }
1507 return false;
1508}
1509
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001510static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1511 ObjCPropertyDecl *Prop) {
1512 bool SuperClassImplementsGetter = false;
1513 bool SuperClassImplementsSetter = false;
1514 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1515 SuperClassImplementsSetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001516
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001517 while (IDecl->getSuperClass()) {
1518 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1519 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1520 SuperClassImplementsGetter = true;
Bob Wilson3ca79042014-03-11 17:17:16 +00001521
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001522 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1523 SuperClassImplementsSetter = true;
1524 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1525 return true;
1526 IDecl = IDecl->getSuperClass();
1527 }
1528 return false;
1529}
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001530
James Dennett2a4d13c2012-06-15 07:13:21 +00001531/// \brief Default synthesizes all properties which must be synthesized
1532/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001533void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1534 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001535
Anna Zaks673d76b2012-10-18 19:17:53 +00001536 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001537 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1538 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001539 if (PropMap.empty())
1540 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001541 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001542 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1543
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001544 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1545 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001546 // Is there a matching property synthesize/dynamic?
1547 if (Prop->isInvalidDecl() ||
1548 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1549 continue;
1550 // Property may have been synthesized by user.
1551 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1552 continue;
1553 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1554 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1555 continue;
1556 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1557 continue;
1558 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001559 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001560 if (SuperPropMap[Prop->getIdentifier()]) {
1561 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1562 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1563 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001564 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001565 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1566 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001567 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001568 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001569 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1570 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001571 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001572 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001573 if (ObjCPropertyImplDecl *PID =
1574 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1575 if (PID->getPropertyDecl() != Prop) {
1576 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001577 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001578 if (!PID->getLocation().isInvalid())
1579 Diag(PID->getLocation(), diag::note_property_synthesize);
1580 }
1581 continue;
1582 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001583 if (ObjCProtocolDecl *Proto =
1584 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001585 // We won't auto-synthesize properties declared in protocols.
Fariborz Jahanian6766f8d2014-03-05 23:44:00 +00001586 // Suppress the warning if class's superclass implements property's
1587 // getter and implements property's setter (if readwrite property).
1588 if (!SuperClassImplementsProperty(IDecl, Prop)) {
1589 Diag(IMPDecl->getLocation(),
1590 diag::warn_auto_synthesizing_protocol_property)
1591 << Prop << Proto;
1592 Diag(Prop->getLocation(), diag::note_property_declare);
1593 }
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001594 continue;
1595 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001596
1597 // We use invalid SourceLocations for the synthesized ivars since they
1598 // aren't really synthesized at a particular location; they just exist.
1599 // Saying that they are located at the @implementation isn't really going
1600 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001601 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1602 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1603 true,
1604 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001605 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001606 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001607 if (PIDecl) {
1608 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001609 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001610 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001611 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001612}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001613
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001614void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001615 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001616 return;
1617 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1618 if (!IC)
1619 return;
1620 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001621 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001622 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001623}
1624
Ted Kremenek7e812952014-02-21 19:41:30 +00001625static void DiagnoseUnimplementedAccessor(Sema &S,
1626 ObjCInterfaceDecl *PrimaryClass,
1627 Selector Method,
1628 ObjCImplDecl* IMPDecl,
1629 ObjCContainerDecl *CDecl,
1630 ObjCCategoryDecl *C,
1631 ObjCPropertyDecl *Prop,
1632 Sema::SelectorSet &SMap) {
1633 // When reporting on missing property setter/getter implementation in
1634 // categories, do not report when they are declared in primary class,
1635 // class's protocol, or one of it super classes. This is because,
1636 // the class is going to implement them.
1637 if (!SMap.count(Method) &&
1638 (PrimaryClass == 0 ||
1639 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1640 S.Diag(IMPDecl->getLocation(),
1641 isa<ObjCCategoryDecl>(CDecl) ?
1642 diag::warn_setter_getter_impl_required_in_category :
1643 diag::warn_setter_getter_impl_required)
1644 << Prop->getDeclName() << Method;
1645 S.Diag(Prop->getLocation(),
1646 diag::note_property_declare);
1647 if (S.LangOpts.ObjCDefaultSynthProperties &&
1648 S.LangOpts.ObjCRuntime.isNonFragile())
1649 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1650 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1651 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1652 }
1653}
1654
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001655void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek348e88c2014-02-21 19:41:34 +00001656 ObjCContainerDecl *CDecl,
1657 bool SynthesizeProperties) {
Anna Zaks673d76b2012-10-18 19:17:53 +00001658 ObjCContainerDecl::PropertyMap PropMap;
Ted Kremenek38882022014-02-21 19:41:39 +00001659 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1660
Ted Kremenek348e88c2014-02-21 19:41:34 +00001661 if (!SynthesizeProperties) {
1662 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
Ted Kremenek348e88c2014-02-21 19:41:34 +00001663 // Gather properties which need not be implemented in this class
1664 // or category.
Ted Kremenek38882022014-02-21 19:41:39 +00001665 if (!IDecl)
Ted Kremenek348e88c2014-02-21 19:41:34 +00001666 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1667 // For categories, no need to implement properties declared in
1668 // its primary class (and its super classes) if property is
1669 // declared in one of those containers.
1670 if ((IDecl = C->getClassInterface())) {
1671 ObjCInterfaceDecl::PropertyDeclOrder PO;
1672 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1673 }
1674 }
1675 if (IDecl)
1676 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1677
1678 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1679 }
1680
Ted Kremenek38882022014-02-21 19:41:39 +00001681 // Scan the @interface to see if any of the protocols it adopts
1682 // require an explicit implementation, via attribute
1683 // 'objc_protocol_requires_explicit_implementation'.
Ted Kremenek204c3c52014-02-22 00:02:03 +00001684 if (IDecl) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001685 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001686
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001687 for (auto *PDecl : IDecl->all_referenced_protocols()) {
Ted Kremenek38882022014-02-21 19:41:39 +00001688 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1689 continue;
Ted Kremenek204c3c52014-02-22 00:02:03 +00001690 // Lazily construct a set of all the properties in the @interface
1691 // of the class, without looking at the superclass. We cannot
1692 // use the call to CollectImmediateProperties() above as that
1693 // utilizes information fromt he super class's properties as well
1694 // as scans the adopted protocols. This work only triggers for protocols
1695 // with the attribute, which is very rare, and only occurs when
1696 // analyzing the @implementation.
1697 if (!LazyMap) {
1698 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1699 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1700 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1701 /* IncludeProtocols */ false);
1702 }
Ted Kremenek38882022014-02-21 19:41:39 +00001703 // Add the properties of 'PDecl' to the list of properties that
1704 // need to be implemented.
Aaron Ballmand174edf2014-03-13 19:11:50 +00001705 for (auto *PropDecl : PDecl->properties()) {
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001706 if ((*LazyMap)[PropDecl->getIdentifier()])
Ted Kremenek204c3c52014-02-22 00:02:03 +00001707 continue;
Aaron Ballmandc4bea42014-03-13 18:47:37 +00001708 PropMap[PropDecl->getIdentifier()] = PropDecl;
Ted Kremenek38882022014-02-21 19:41:39 +00001709 }
1710 }
Ted Kremenek204c3c52014-02-22 00:02:03 +00001711 }
Ted Kremenek38882022014-02-21 19:41:39 +00001712
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001713 if (PropMap.empty())
1714 return;
1715
1716 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1717 for (ObjCImplDecl::propimpl_iterator
1718 I = IMPDecl->propimpl_begin(),
1719 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001720 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001721
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001722 SelectorSet InsMap;
1723 // Collect property accessors implemented in current implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001724 for (const auto *I : IMPDecl->instance_methods())
1725 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001726
1727 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1728 ObjCInterfaceDecl *PrimaryClass = 0;
1729 if (C && !C->IsClassExtension())
1730 if ((PrimaryClass = C->getClassInterface()))
1731 // Report unimplemented properties in the category as well.
1732 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1733 // When reporting on missing setter/getters, do not report when
1734 // setter/getter is implemented in category's primary class
1735 // implementation.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001736 for (const auto *I : IMP->instance_methods())
1737 InsMap.insert(I->getSelector());
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001738 }
1739
Anna Zaks673d76b2012-10-18 19:17:53 +00001740 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001741 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1742 ObjCPropertyDecl *Prop = P->second;
1743 // Is there a matching propery synthesize/dynamic?
1744 if (Prop->isInvalidDecl() ||
1745 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001746 PropImplMap.count(Prop) ||
1747 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001748 continue;
Ted Kremenek7e812952014-02-21 19:41:30 +00001749
1750 // Diagnose unimplemented getters and setters.
1751 DiagnoseUnimplementedAccessor(*this,
1752 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1753 if (!Prop->isReadOnly())
1754 DiagnoseUnimplementedAccessor(*this,
1755 PrimaryClass, Prop->getSetterName(),
1756 IMPDecl, CDecl, C, Prop, InsMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001757 }
1758}
1759
1760void
1761Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1762 ObjCContainerDecl* IDecl) {
1763 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001764 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001765 return;
Aaron Ballmand174edf2014-03-13 19:11:50 +00001766 for (const auto *Property : IDecl->properties()) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001767 ObjCMethodDecl *GetterMethod = 0;
1768 ObjCMethodDecl *SetterMethod = 0;
1769 bool LookedUpGetterSetter = false;
1770
Bill Wendling44426052012-12-20 19:22:21 +00001771 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001772 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001773
John McCall43192862011-09-13 18:31:23 +00001774 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1775 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001776 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1777 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1778 LookedUpGetterSetter = true;
1779 if (GetterMethod) {
1780 Diag(GetterMethod->getLocation(),
1781 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001782 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001783 Diag(Property->getLocation(), diag::note_property_declare);
1784 }
1785 if (SetterMethod) {
1786 Diag(SetterMethod->getLocation(),
1787 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001788 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001789 Diag(Property->getLocation(), diag::note_property_declare);
1790 }
1791 }
1792
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001793 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001794 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1795 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001796 continue;
1797 if (const ObjCPropertyImplDecl *PIDecl
1798 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1799 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1800 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001801 if (!LookedUpGetterSetter) {
1802 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1803 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001804 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001805 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1806 SourceLocation MethodLoc =
1807 (GetterMethod ? GetterMethod->getLocation()
1808 : SetterMethod->getLocation());
1809 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001810 << Property->getIdentifier() << (GetterMethod != 0)
1811 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001812 // fixit stuff.
1813 if (!AttributesAsWritten) {
1814 if (Property->getLParenLoc().isValid()) {
1815 // @property () ... case.
1816 SourceRange PropSourceRange(Property->getAtLoc(),
1817 Property->getLParenLoc());
1818 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1819 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1820 }
1821 else {
1822 //@property id etc.
1823 SourceLocation endLoc =
1824 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1825 endLoc = endLoc.getLocWithOffset(-1);
1826 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1827 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1828 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1829 }
1830 }
1831 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1832 // @property () ... case.
1833 SourceLocation endLoc = Property->getLParenLoc();
1834 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1835 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1836 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1837 }
1838 else
1839 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001840 Diag(Property->getLocation(), diag::note_property_declare);
1841 }
1842 }
1843 }
1844}
1845
John McCall31168b02011-06-15 23:02:42 +00001846void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001847 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001848 return;
1849
1850 for (ObjCImplementationDecl::propimpl_iterator
1851 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001852 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001853 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001854 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1855 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001856 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1857 if (!method)
1858 continue;
1859 ObjCMethodFamily family = method->getMethodFamily();
1860 if (family == OMF_alloc || family == OMF_copy ||
1861 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001862 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001863 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001864 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001865 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001866 }
1867 }
1868 }
1869}
1870
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001871void Sema::DiagnoseMissingDesignatedInitOverrides(
1872 const ObjCImplementationDecl *ImplD,
1873 const ObjCInterfaceDecl *IFD) {
1874 assert(IFD->hasDesignatedInitializers());
1875 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1876 if (!SuperD)
1877 return;
1878
1879 SelectorSet InitSelSet;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001880 for (const auto *I : ImplD->instance_methods())
1881 if (I->getMethodFamily() == OMF_init)
1882 InitSelSet.insert(I->getSelector());
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001883
1884 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1885 SuperD->getDesignatedInitializers(DesignatedInits);
1886 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1887 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1888 const ObjCMethodDecl *MD = *I;
1889 if (!InitSelSet.count(MD->getSelector())) {
1890 Diag(ImplD->getLocation(),
1891 diag::warn_objc_implementation_missing_designated_init_override)
1892 << MD->getSelector();
1893 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1894 }
1895 }
1896}
1897
John McCallad31b5f2010-11-10 07:01:40 +00001898/// AddPropertyAttrs - Propagates attributes from a property to the
1899/// implicitly-declared getter or setter for that property.
1900static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1901 ObjCPropertyDecl *Property) {
1902 // Should we just clone all attributes over?
Aaron Ballmanb97112e2014-03-08 22:19:01 +00001903 for (const auto *A : Property->attrs()) {
1904 if (isa<DeprecatedAttr>(A) ||
1905 isa<UnavailableAttr>(A) ||
1906 isa<AvailabilityAttr>(A))
1907 PropertyMethod->addAttr(A->clone(S.Context));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001908 }
John McCallad31b5f2010-11-10 07:01:40 +00001909}
1910
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001911/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1912/// have the property type and issue diagnostics if they don't.
1913/// Also synthesize a getter/setter method if none exist (and update the
1914/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1915/// methods is the "right" thing to do.
1916void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001917 ObjCContainerDecl *CD,
1918 ObjCPropertyDecl *redeclaredProperty,
1919 ObjCContainerDecl *lexicalDC) {
1920
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001921 ObjCMethodDecl *GetterMethod, *SetterMethod;
1922
1923 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1924 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1925 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1926 property->getLocation());
1927
1928 if (SetterMethod) {
1929 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1930 property->getPropertyAttributes();
1931 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001932 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1933 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001934 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1935 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001936 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001937 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1938 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001939 Diag(property->getLocation(),
1940 diag::warn_accessor_property_type_mismatch)
1941 << property->getDeclName()
1942 << SetterMethod->getSelector();
1943 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1944 }
1945 }
1946
1947 // Synthesize getter/setter methods if none exist.
1948 // Find the default getter and if one not found, add one.
1949 // FIXME: The synthesized property we set here is misleading. We almost always
1950 // synthesize these methods unless the user explicitly provided prototypes
1951 // (which is odd, but allowed). Sema should be typechecking that the
1952 // declarations jive in that situation (which it is not currently).
1953 if (!GetterMethod) {
1954 // No instance method of same name as property getter name was found.
1955 // Declare a getter method and add it to the list of methods
1956 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001957 SourceLocation Loc = redeclaredProperty ?
1958 redeclaredProperty->getLocation() :
1959 property->getLocation();
1960
1961 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1962 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001963 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001964 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001965 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001966 (property->getPropertyImplementation() ==
1967 ObjCPropertyDecl::Optional) ?
1968 ObjCMethodDecl::Optional :
1969 ObjCMethodDecl::Required);
1970 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001971
1972 AddPropertyAttrs(*this, GetterMethod, property);
1973
Ted Kremenek49be9e02010-05-18 21:09:07 +00001974 // FIXME: Eventually this shouldn't be needed, as the lexical context
1975 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001976 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001977 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001978 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001979 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1980 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001981
1982 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1983 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001984 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001985
1986 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001987 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1988 Loc));
John McCalle48f3892013-04-04 01:38:37 +00001989
1990 if (getLangOpts().ObjCAutoRefCount)
1991 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001992 } else
1993 // A user declared getter will be synthesize when @synthesize of
1994 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001995 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001996 property->setGetterMethodDecl(GetterMethod);
1997
1998 // Skip setter if property is read-only.
1999 if (!property->isReadOnly()) {
2000 // Find the default setter and if one not found, add one.
2001 if (!SetterMethod) {
2002 // No instance method of same name as property setter name was found.
2003 // Declare a setter method and add it to the list of methods
2004 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002005 SourceLocation Loc = redeclaredProperty ?
2006 redeclaredProperty->getLocation() :
2007 property->getLocation();
2008
2009 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002010 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002011 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002012 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00002013 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00002014 /*isImplicitlyDeclared=*/true,
2015 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002016 (property->getPropertyImplementation() ==
2017 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002018 ObjCMethodDecl::Optional :
2019 ObjCMethodDecl::Required);
2020
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002021 // Invent the arguments for the setter. We don't bother making a
2022 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00002023 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2024 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002025 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00002026 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002027 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00002028 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002029 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002030 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002031
2032 AddPropertyAttrs(*this, SetterMethod, property);
2033
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002034 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002035 // FIXME: Eventually this shouldn't be needed, as the lexical context
2036 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002037 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002038 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002039 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002040 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2041 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002042 // It's possible for the user to have set a very odd custom
2043 // setter selector that causes it to have a method family.
2044 if (getLangOpts().ObjCAutoRefCount)
2045 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002046 } else
2047 // A user declared setter will be synthesize when @synthesize of
2048 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002049 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002050 property->setSetterMethodDecl(SetterMethod);
2051 }
2052 // Add any synthesized methods to the global pool. This allows us to
2053 // handle the following, which is supported by GCC (and part of the design).
2054 //
2055 // @interface Foo
2056 // @property double bar;
2057 // @end
2058 //
2059 // void thisIsUnfortunate() {
2060 // id foo;
2061 // double bar = [foo bar];
2062 // }
2063 //
2064 if (GetterMethod)
2065 AddInstanceMethodToGlobalPool(GetterMethod);
2066 if (SetterMethod)
2067 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002068
2069 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2070 if (!CurrentClass) {
2071 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2072 CurrentClass = Cat->getClassInterface();
2073 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2074 CurrentClass = Impl->getClassInterface();
2075 }
2076 if (GetterMethod)
2077 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2078 if (SetterMethod)
2079 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002080}
2081
John McCall48871652010-08-21 09:40:31 +00002082void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002083 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002084 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002085 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002086 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002087 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002088 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002089
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002090 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2091 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2092 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2093 << "readonly" << "readwrite";
2094
2095 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2096 QualType PropertyTy = PropertyDecl->getType();
2097 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002098
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002099 // 'readonly' property with no obvious lifetime.
2100 // its life time will be determined by its backing ivar.
2101 if (getLangOpts().ObjCAutoRefCount &&
2102 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2103 PropertyTy->isObjCRetainableType() &&
2104 !PropertyOwnership)
2105 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002106
2107 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002108 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002109 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2110 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002111 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002112 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002113 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2114 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2115 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002116 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002117 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002118 }
2119
2120 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002121 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2122 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002123 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2124 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002125 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002126 }
Bill Wendling44426052012-12-20 19:22:21 +00002127 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002128 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2129 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002130 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002131 }
Bill Wendling44426052012-12-20 19:22:21 +00002132 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002135 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002136 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002137 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002138 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002139 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2140 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002141 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002142 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002143 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002144 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002145 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2146 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002147 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2148 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002149 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002150 }
Bill Wendling44426052012-12-20 19:22:21 +00002151 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002152 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2153 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002154 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002155 }
Bill Wendling44426052012-12-20 19:22:21 +00002156 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002157 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2158 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002159 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002160 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002161 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002162 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002163 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2164 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002165 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002166 }
Bill Wendling44426052012-12-20 19:22:21 +00002167 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2168 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002169 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2170 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002171 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002172 }
Bill Wendling44426052012-12-20 19:22:21 +00002173 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002174 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2175 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002176 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002177 }
Bill Wendling44426052012-12-20 19:22:21 +00002178 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002179 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2180 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002181 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002182 }
2183 }
Bill Wendling44426052012-12-20 19:22:21 +00002184 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2185 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002186 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2187 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002188 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002189 }
Bill Wendling44426052012-12-20 19:22:21 +00002190 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2191 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002192 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2193 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002194 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002195 }
2196
Bill Wendling44426052012-12-20 19:22:21 +00002197 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2198 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002199 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2200 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002201 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002202 }
2203
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002204 // Warn if user supplied no assignment attribute, property is
2205 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002206 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002207 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2208 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2209 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002210 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002211 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002212 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002213 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002214 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002215 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002216 bool isAnyClassTy =
2217 (PropertyTy->isObjCClassType() ||
2218 PropertyTy->isObjCQualifiedClassType());
2219 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2220 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002221 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002222 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002223 else if (propertyInPrimaryClass) {
2224 // Don't issue warning on property with no life time in class
2225 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002226 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002227 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002228 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002229
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002230 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002231 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002232 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002233 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002234 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002235
2236 // FIXME: Implement warning dependent on NSCopying being
2237 // implemented. See also:
2238 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2239 // (please trim this list while you are at it).
2240 }
2241
Bill Wendling44426052012-12-20 19:22:21 +00002242 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2243 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002244 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002245 && PropertyTy->isBlockPointerType())
2246 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002247 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2248 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2249 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002250 PropertyTy->isBlockPointerType())
2251 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002252
Bill Wendling44426052012-12-20 19:22:21 +00002253 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2254 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002255 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2256
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002257}