blob: 992ac1de909401ef3f0a12432723fdd60b307149 [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.
Fariborz Jahanian92e3aa22014-02-15 00:04:36 +0000225 for (ObjCInterfaceDecl::protocol_iterator
226 P = CurrentInterfaceDecl->protocol_begin(),
227 PEnd = CurrentInterfaceDecl->protocol_end();
Douglas Gregorb8982092013-01-21 19:42:21 +0000228 P != PEnd; ++P) {
229 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
230 }
231 } else {
232 // Slower path: look in all protocols we referenced.
233 for (ObjCInterfaceDecl::all_protocol_iterator
234 P = IFace->all_referenced_protocol_begin(),
235 PEnd = IFace->all_referenced_protocol_end();
236 P != PEnd; ++P) {
237 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
238 }
239 }
240 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
241 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
242 PEnd = Cat->protocol_end();
243 P != PEnd; ++P) {
244 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
245 }
246 } else {
247 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
248 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
249 PEnd = Proto->protocol_end();
250 P != PEnd; ++P) {
251 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000252 }
253 }
254
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000255 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000256 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000257}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000258
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000260makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000280 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000281 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000282 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000284 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
286
287 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
288}
289
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000290static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000291 SourceLocation LParenLoc, SourceLocation &Loc) {
292 if (LParenLoc.isMacroID())
293 return false;
294
295 SourceManager &SM = Context.getSourceManager();
296 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
297 // Try to load the file buffer.
298 bool invalidTemp = false;
299 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
300 if (invalidTemp)
301 return false;
302 const char *tokenBegin = file.data() + locInfo.second;
303
304 // Lex from the start of the given location.
305 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
306 Context.getLangOpts(),
307 file.begin(), tokenBegin, file.end());
308 Token Tok;
309 do {
310 lexer.LexFromRawLexer(Tok);
311 if (Tok.is(tok::raw_identifier) &&
312 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
313 Loc = Tok.getLocation();
314 return true;
315 }
316 } while (Tok.isNot(tok::r_paren));
317 return false;
318
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000319}
320
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000321static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000322 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
323 ObjCPropertyDecl::OBJC_PR_retain |
324 ObjCPropertyDecl::OBJC_PR_copy |
325 ObjCPropertyDecl::OBJC_PR_weak |
326 ObjCPropertyDecl::OBJC_PR_strong |
327 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
328}
329
Douglas Gregor90d34422013-01-21 19:05:22 +0000330ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000331Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000332 SourceLocation AtLoc,
333 SourceLocation LParenLoc,
334 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000335 Selector GetterSel, Selector SetterSel,
336 const bool isAssign,
337 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000338 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000339 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000340 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000341 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000342 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000343 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000344 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000345 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000346 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000347 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
348
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000349 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000350 // Check for duplicate declaration of this property in current and
351 // other class extensions.
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000352 for (ObjCInterfaceDecl::known_extensions_iterator
353 Ext = CCPrimary->known_extensions_begin(),
354 ExtEnd = CCPrimary->known_extensions_end();
355 Ext != ExtEnd; ++Ext) {
356 if (ObjCPropertyDecl *prevDecl
357 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000358 Diag(AtLoc, diag::err_duplicate_property);
359 Diag(prevDecl->getLocation(), diag::note_property_declare);
360 return 0;
361 }
362 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000363 }
364
Ted Kremenek959e8302010-03-12 02:31:10 +0000365 // Create a new ObjCPropertyDecl with the DeclContext being
366 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000367 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000368 ObjCPropertyDecl *PDecl =
369 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000370 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000371 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000372 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000373 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000375 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000376 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000377 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
378 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
379 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
380 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000381 // Set setter/getter selector name. Needed later.
382 PDecl->setGetterName(GetterSel);
383 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000384 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000385 DC->addDecl(PDecl);
386
387 // We need to look in the @interface to see if the @property was
388 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000389 if (!CCPrimary) {
390 Diag(CDecl->getLocation(), diag::err_continuation_class);
391 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000392 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000393 }
394
395 // Find the property in continuation class's primary class only.
396 ObjCPropertyDecl *PIDecl =
397 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
398
399 if (!PIDecl) {
400 // No matching property found in the primary class. Just fall thru
401 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000402 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000403 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000404 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000405 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000406
407 // A case of continuation class adding a new property in the class. This
408 // is not what it was meant for. However, gcc supports it and so should we.
409 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000410 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000411 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000412 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
413 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000414 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000415 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
416 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000417 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000418 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
419 bool IncompatibleObjC = false;
420 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000421 // Relax the strict type matching for property type in continuation class.
422 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000423 // as it narrows the object type in its primary class property. Note that
424 // this conversion is safe only because the wider type is for a 'readonly'
425 // property in primary class and 'narrowed' type for a 'readwrite' property
426 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000427 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
428 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
429 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
430 ConvertedType, IncompatibleObjC))
431 || IncompatibleObjC) {
432 Diag(AtLoc,
433 diag::err_type_mismatch_continuation_class) << PDecl->getType();
434 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000435 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000436 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000437 }
438
Ted Kremenek959e8302010-03-12 02:31:10 +0000439 // The property 'PIDecl's readonly attribute will be over-ridden
440 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000441 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000442 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000443 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
444 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000445 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000446 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000447 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000448 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
449 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000450 Diag(AtLoc, diag::warn_property_attr_mismatch);
451 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000452 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000453 else if (getLangOpts().ObjCAutoRefCount) {
454 QualType PrimaryPropertyQT =
455 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
456 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000457 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000458 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
459 PrimaryPropertyQT.getObjCLifetime();
460 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000461 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
462 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000463 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
464 Diag(PIDecl->getLocation(), diag::note_property_declare);
465 }
466 }
467 }
468
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000469 DeclContext *DC = cast<DeclContext>(CCPrimary);
470 if (!ObjCPropertyDecl::findPropertyDecl(DC,
471 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000472 // In mrr mode, 'readwrite' property must have an explicit
473 // memory attribute. If none specified, select the default (assign).
474 if (!getLangOpts().ObjCAutoRefCount) {
475 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
476 ObjCDeclSpec::DQ_PR_retain |
477 ObjCDeclSpec::DQ_PR_strong |
478 ObjCDeclSpec::DQ_PR_copy |
479 ObjCDeclSpec::DQ_PR_unsafe_unretained |
480 ObjCDeclSpec::DQ_PR_weak)))
481 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
482 }
483
Ted Kremenek959e8302010-03-12 02:31:10 +0000484 // Protocol is not in the primary class. Must build one for it.
485 ObjCDeclSpec ProtocolPropertyODS;
486 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
487 // and ObjCPropertyDecl::PropertyAttributeKind have identical
488 // values. Should consolidate both into one enum type.
489 ProtocolPropertyODS.
490 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
491 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000492 // Must re-establish the context from class extension to primary
493 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000494 ContextRAII SavedContext(*this, CCPrimary);
495
John McCall48871652010-08-21 09:40:31 +0000496 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000497 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000498 PIDecl->getGetterName(),
499 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000500 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000501 MethodImplKind,
502 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000503 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000504 }
505 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000506 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000507 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000508 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000509 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000510 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000511 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
512 PIDecl->setSetterName(SetterSel);
513 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000514 // Tailor the diagnostics for the common case where a readwrite
515 // property is declared both in the @interface and the continuation.
516 // This is a common error where the user often intended the original
517 // declaration to be readonly.
518 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000519 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000520 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
521 ? diag::err_use_continuation_class_redeclaration_readwrite
522 : diag::err_use_continuation_class;
523 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000524 << CCPrimary->getDeclName();
525 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000526 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000527 }
528 *isOverridingProperty = true;
529 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000530 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000531 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
532 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000533 if (ASTMutationListener *L = Context.getASTMutationListener())
534 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000535 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000536}
537
538ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
539 ObjCContainerDecl *CDecl,
540 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000541 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000542 FieldDeclarator &FD,
543 Selector GetterSel,
544 Selector SetterSel,
545 const bool isAssign,
546 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000547 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000548 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000549 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000550 tok::ObjCKeywordKind MethodImplKind,
551 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000552 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000553 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000554
555 // Issue a warning if property is 'assign' as default and its object, which is
556 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000557 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000558 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000559 if (const ObjCObjectPointerType *ObjPtrTy =
560 T->getAs<ObjCObjectPointerType>()) {
561 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
562 if (IDecl)
563 if (ObjCProtocolDecl* PNSCopying =
564 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
565 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
566 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000567 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000568
569 if (T->isObjCObjectType()) {
570 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
571 StarLoc = PP.getLocForEndOfToken(StarLoc);
572 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
573 << FixItHint::CreateInsertion(StarLoc, "*");
574 T = Context.getObjCObjectPointerType(T);
575 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
576 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
577 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000578
Ted Kremenek959e8302010-03-12 02:31:10 +0000579 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000580 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
581 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000582 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000583
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000584 if (ObjCPropertyDecl *prevDecl =
585 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000586 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000587 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000588 PDecl->setInvalidDecl();
589 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000590 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000591 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000592 if (lexicalDC)
593 PDecl->setLexicalDeclContext(lexicalDC);
594 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000595
596 if (T->isArrayType() || T->isFunctionType()) {
597 Diag(AtLoc, diag::err_property_type) << T;
598 PDecl->setInvalidDecl();
599 }
600
601 ProcessDeclAttributes(S, PDecl, FD.D);
602
603 // Regardless of setter/getter attribute, we save the default getter/setter
604 // selector names in anticipation of declaration of setter/getter methods.
605 PDecl->setGetterName(GetterSel);
606 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000607 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000608 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000609
Bill Wendling44426052012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
612
Bill Wendling44426052012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
618
619 if (isReadWrite)
620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
621
Bill Wendling44426052012-12-20 19:22:21 +0000622 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
624
Bill Wendling44426052012-12-20 19:22:21 +0000625 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
627
Bill Wendling44426052012-12-20 19:22:21 +0000628 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
630
Bill Wendling44426052012-12-20 19:22:21 +0000631 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
633
Bill Wendling44426052012-12-20 19:22:21 +0000634 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000635 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
636
Ted Kremenekac597f32010-03-12 00:46:40 +0000637 if (isAssign)
638 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
639
John McCall43192862011-09-13 18:31:23 +0000640 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000641 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000643 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000644 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000645
John McCall31168b02011-06-15 23:02:42 +0000646 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000647 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000648 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
649 if (isAssign)
650 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
651
Ted Kremenekac597f32010-03-12 00:46:40 +0000652 if (MethodImplKind == tok::objc_required)
653 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
654 else if (MethodImplKind == tok::objc_optional)
655 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000656
Ted Kremenek959e8302010-03-12 02:31:10 +0000657 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000658}
659
John McCall31168b02011-06-15 23:02:42 +0000660static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
661 ObjCPropertyDecl *property,
662 ObjCIvarDecl *ivar) {
663 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
664
John McCall31168b02011-06-15 23:02:42 +0000665 QualType ivarType = ivar->getType();
666 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000667
John McCall43192862011-09-13 18:31:23 +0000668 // The lifetime implied by the property's attributes.
669 Qualifiers::ObjCLifetime propertyLifetime =
670 getImpliedARCOwnership(property->getPropertyAttributes(),
671 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000672
John McCall43192862011-09-13 18:31:23 +0000673 // We're fine if they match.
674 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000675
John McCall43192862011-09-13 18:31:23 +0000676 // These aren't valid lifetimes for object ivars; don't diagnose twice.
677 if (ivarLifetime == Qualifiers::OCL_None ||
678 ivarLifetime == Qualifiers::OCL_Autoreleasing)
679 return;
John McCall31168b02011-06-15 23:02:42 +0000680
John McCalld8561f02012-08-20 23:36:59 +0000681 // If the ivar is private, and it's implicitly __unsafe_unretained
682 // becaues of its type, then pretend it was actually implicitly
683 // __strong. This is only sound because we're processing the
684 // property implementation before parsing any method bodies.
685 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
686 propertyLifetime == Qualifiers::OCL_Strong &&
687 ivar->getAccessControl() == ObjCIvarDecl::Private) {
688 SplitQualType split = ivarType.split();
689 if (split.Quals.hasObjCLifetime()) {
690 assert(ivarType->isObjCARCImplicitlyUnretainedType());
691 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
692 ivarType = S.Context.getQualifiedType(split);
693 ivar->setType(ivarType);
694 return;
695 }
696 }
697
John McCall43192862011-09-13 18:31:23 +0000698 switch (propertyLifetime) {
699 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000700 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000701 << property->getDeclName()
702 << ivar->getDeclName()
703 << ivarLifetime;
704 break;
John McCall31168b02011-06-15 23:02:42 +0000705
John McCall43192862011-09-13 18:31:23 +0000706 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000707 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000708 << property->getDeclName()
709 << ivar->getDeclName();
710 break;
John McCall31168b02011-06-15 23:02:42 +0000711
John McCall43192862011-09-13 18:31:23 +0000712 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000713 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000714 << property->getDeclName()
715 << ivar->getDeclName()
716 << ((property->getPropertyAttributesAsWritten()
717 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
718 break;
John McCall31168b02011-06-15 23:02:42 +0000719
John McCall43192862011-09-13 18:31:23 +0000720 case Qualifiers::OCL_Autoreleasing:
721 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000722
John McCall43192862011-09-13 18:31:23 +0000723 case Qualifiers::OCL_None:
724 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000725 return;
726 }
727
728 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000729 if (propertyImplLoc.isValid())
730 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000731}
732
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000733/// setImpliedPropertyAttributeForReadOnlyProperty -
734/// This routine evaludates life-time attributes for a 'readonly'
735/// property with no known lifetime of its own, using backing
736/// 'ivar's attribute, if any. If no backing 'ivar', property's
737/// life-time is assumed 'strong'.
738static void setImpliedPropertyAttributeForReadOnlyProperty(
739 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
740 Qualifiers::ObjCLifetime propertyLifetime =
741 getImpliedARCOwnership(property->getPropertyAttributes(),
742 property->getType());
743 if (propertyLifetime != Qualifiers::OCL_None)
744 return;
745
746 if (!ivar) {
747 // if no backing ivar, make property 'strong'.
748 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
749 return;
750 }
751 // property assumes owenership of backing ivar.
752 QualType ivarType = ivar->getType();
753 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
754 if (ivarLifetime == Qualifiers::OCL_Strong)
755 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
756 else if (ivarLifetime == Qualifiers::OCL_Weak)
757 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
758 return;
759}
Ted Kremenekac597f32010-03-12 00:46:40 +0000760
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000761/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
762/// in inherited protocols with mismatched types. Since any of them can
763/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000764static void
765DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
766 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000767 ObjCPropertyDecl *Property) {
768 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
769 for (ObjCInterfaceDecl::all_protocol_iterator
770 PI = ClassDecl->all_referenced_protocol_begin(),
771 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
772 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
773 PDecl->collectInheritedProtocolProperties(Property, PropMap);
774 }
775 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
776 while (SDecl) {
777 for (ObjCInterfaceDecl::all_protocol_iterator
778 PI = SDecl->all_referenced_protocol_begin(),
779 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
780 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
781 PDecl->collectInheritedProtocolProperties(Property, PropMap);
782 }
783 SDecl = SDecl->getSuperClass();
784 }
785
786 if (PropMap.empty())
787 return;
788
789 QualType RHSType = S.Context.getCanonicalType(Property->getType());
790 bool FirsTime = true;
791 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
792 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
793 ObjCPropertyDecl *Prop = I->second;
794 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
795 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
796 bool IncompatibleObjC = false;
797 QualType ConvertedType;
798 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
799 || IncompatibleObjC) {
800 if (FirsTime) {
801 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
802 << Property->getType();
803 FirsTime = false;
804 }
805 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
806 << Prop->getType();
807 }
808 }
809 }
810 if (!FirsTime && AtLoc.isValid())
811 S.Diag(AtLoc, diag::note_property_synthesize);
812}
813
Ted Kremenekac597f32010-03-12 00:46:40 +0000814/// ActOnPropertyImplDecl - This routine performs semantic checks and
815/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000816/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000817///
John McCall48871652010-08-21 09:40:31 +0000818Decl *Sema::ActOnPropertyImplDecl(Scope *S,
819 SourceLocation AtLoc,
820 SourceLocation PropertyLoc,
821 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000822 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000823 IdentifierInfo *PropertyIvar,
824 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000825 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000826 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000827 // Make sure we have a context for the property implementation declaration.
828 if (!ClassImpDecl) {
829 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000830 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000831 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000832 if (PropertyIvarLoc.isInvalid())
833 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000834 SourceLocation PropertyDiagLoc = PropertyLoc;
835 if (PropertyDiagLoc.isInvalid())
836 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000837 ObjCPropertyDecl *property = 0;
838 ObjCInterfaceDecl* IDecl = 0;
839 // Find the class or category class where this property must have
840 // a declaration.
841 ObjCImplementationDecl *IC = 0;
842 ObjCCategoryImplDecl* CatImplClass = 0;
843 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
844 IDecl = IC->getClassInterface();
845 // We always synthesize an interface for an implementation
846 // without an interface decl. So, IDecl is always non-zero.
847 assert(IDecl &&
848 "ActOnPropertyImplDecl - @implementation without @interface");
849
850 // Look for this property declaration in the @implementation's @interface
851 property = IDecl->FindPropertyDeclaration(PropertyId);
852 if (!property) {
853 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000854 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000855 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000856 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000857 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
858 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000859 if (AtLoc.isValid())
860 Diag(AtLoc, diag::warn_implicit_atomic_property);
861 else
862 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
863 Diag(property->getLocation(), diag::note_property_declare);
864 }
865
Ted Kremenekac597f32010-03-12 00:46:40 +0000866 if (const ObjCCategoryDecl *CD =
867 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
868 if (!CD->IsClassExtension()) {
869 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
870 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000871 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000872 }
873 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000874 if (Synthesize&&
875 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
876 property->hasAttr<IBOutletAttr>() &&
877 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000878 bool ReadWriteProperty = false;
879 // Search into the class extensions and see if 'readonly property is
880 // redeclared 'readwrite', then no warning is to be issued.
881 for (ObjCInterfaceDecl::known_extensions_iterator
882 Ext = IDecl->known_extensions_begin(),
883 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
884 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
885 if (!R.empty())
886 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
887 PIkind = ExtProp->getPropertyAttributesAsWritten();
888 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
889 ReadWriteProperty = true;
890 break;
891 }
892 }
893 }
894
895 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000896 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000897 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000898 SourceLocation readonlyLoc;
899 if (LocPropertyAttribute(Context, "readonly",
900 property->getLParenLoc(), readonlyLoc)) {
901 SourceLocation endLoc =
902 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
903 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
904 Diag(property->getLocation(),
905 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
906 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
907 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000908 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000909 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000910 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
911 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000912
Ted Kremenekac597f32010-03-12 00:46:40 +0000913 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
914 if (Synthesize) {
915 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000916 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000917 }
918 IDecl = CatImplClass->getClassInterface();
919 if (!IDecl) {
920 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000921 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000922 }
923 ObjCCategoryDecl *Category =
924 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
925
926 // If category for this implementation not found, it is an error which
927 // has already been reported eralier.
928 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000929 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000930 // Look for this property declaration in @implementation's category
931 property = Category->FindPropertyDeclaration(PropertyId);
932 if (!property) {
933 Diag(PropertyLoc, diag::error_bad_category_property_decl)
934 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000935 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000936 }
937 } else {
938 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000939 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000940 }
941 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000942 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000943 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000944 // Check that we have a valid, previously declared ivar for @synthesize
945 if (Synthesize) {
946 // @synthesize
947 if (!PropertyIvar)
948 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000949 // Check that this is a previously declared 'ivar' in 'IDecl' interface
950 ObjCInterfaceDecl *ClassDeclared;
951 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
952 QualType PropType = property->getType();
953 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000954
955 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000956 diag::err_incomplete_synthesized_property,
957 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000958 Diag(property->getLocation(), diag::note_property_declare);
959 CompleteTypeErr = true;
960 }
961
David Blaikiebbafb8a2012-03-11 07:00:24 +0000962 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000963 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000964 ObjCPropertyDecl::OBJC_PR_readonly) &&
965 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000966 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
967 }
968
John McCall31168b02011-06-15 23:02:42 +0000969 ObjCPropertyDecl::PropertyAttributeKind kind
970 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000971
972 // Add GC __weak to the ivar type if the property is weak.
973 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000974 getLangOpts().getGC() != LangOptions::NonGC) {
975 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000976 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000977 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000978 Diag(property->getLocation(), diag::note_property_declare);
979 } else {
980 PropertyIvarType =
981 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000982 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000983 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000984 if (AtLoc.isInvalid()) {
985 // Check when default synthesizing a property that there is
986 // an ivar matching property name and issue warning; since this
987 // is the most common case of not using an ivar used for backing
988 // property in non-default synthesis case.
989 ObjCInterfaceDecl *ClassDeclared=0;
990 ObjCIvarDecl *originalIvar =
991 IDecl->lookupInstanceVariable(property->getIdentifier(),
992 ClassDeclared);
993 if (originalIvar) {
994 Diag(PropertyDiagLoc,
995 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000996 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000997 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000998 Diag(property->getLocation(), diag::note_property_declare);
999 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +00001000 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +00001001 }
1002
1003 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +00001004 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +00001005 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001006 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +00001007 !PropertyIvarType.getObjCLifetime() &&
1008 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001009
John McCall43192862011-09-13 18:31:23 +00001010 // It's an error if we have to do this and the user didn't
1011 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001012 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +00001013 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001014 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001015 diag::err_arc_objc_property_default_assign_on_object);
1016 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001017 } else {
1018 Qualifiers::ObjCLifetime lifetime =
1019 getImpliedARCOwnership(kind, PropertyIvarType);
1020 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001021 if (lifetime == Qualifiers::OCL_Weak) {
1022 bool err = false;
1023 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001024 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1025 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1026 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001027 Diag(property->getLocation(),
1028 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1029 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1030 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001031 err = true;
1032 }
Richard Smith802c4b72012-08-23 06:16:52 +00001033 }
John McCall3deb1ad2012-08-21 02:47:43 +00001034 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001035 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001036 Diag(property->getLocation(), diag::note_property_declare);
1037 }
John McCall31168b02011-06-15 23:02:42 +00001038 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001039
John McCall31168b02011-06-15 23:02:42 +00001040 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001041 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001042 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1043 }
John McCall31168b02011-06-15 23:02:42 +00001044 }
1045
1046 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001047 !getLangOpts().ObjCAutoRefCount &&
1048 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001049 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001050 Diag(property->getLocation(), diag::note_property_declare);
1051 }
1052
Abramo Bagnaradff19302011-03-08 08:55:46 +00001053 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001054 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001055 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001056 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001057 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001058 if (RequireNonAbstractType(PropertyIvarLoc,
1059 PropertyIvarType,
1060 diag::err_abstract_type_in_decl,
1061 AbstractSynthesizedIvarType)) {
1062 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001063 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001064 } else if (CompleteTypeErr)
1065 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001066 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001067 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001068
John McCall5fb5df92012-06-20 06:18:46 +00001069 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001070 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1071 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001072 // Note! I deliberately want it to fall thru so, we have a
1073 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001074 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001075 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001076 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001077 << property->getDeclName() << Ivar->getDeclName()
1078 << ClassDeclared->getDeclName();
1079 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001080 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001081 // Note! I deliberately want it to fall thru so more errors are caught.
1082 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001083 property->setPropertyIvarDecl(Ivar);
1084
Ted Kremenekac597f32010-03-12 00:46:40 +00001085 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1086
1087 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001088 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001089 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001090 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001091 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001092 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001093 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001094 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001095 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001096 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1097 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001098 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001099 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001100 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001101 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001102 << property->getDeclName() << PropType
1103 << Ivar->getDeclName() << IvarType;
1104 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001105 // Note! I deliberately want it to fall thru so, we have a
1106 // a property implementation and to avoid future warnings.
1107 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001108 else {
1109 // FIXME! Rules for properties are somewhat different that those
1110 // for assignments. Use a new routine to consolidate all cases;
1111 // specifically for property redeclarations as well as for ivars.
1112 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1113 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1114 if (lhsType != rhsType &&
1115 lhsType->isArithmeticType()) {
1116 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1117 << property->getDeclName() << PropType
1118 << Ivar->getDeclName() << IvarType;
1119 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1120 // Fall thru - see previous comment
1121 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001122 }
1123 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001124 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001125 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001126 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001127 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001128 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001129 // Fall thru - see previous comment
1130 }
John McCall31168b02011-06-15 23:02:42 +00001131 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001132 if ((property->getType()->isObjCObjectPointerType() ||
1133 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001134 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001135 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001136 << property->getDeclName() << Ivar->getDeclName();
1137 // Fall thru - see previous comment
1138 }
1139 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001140 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001141 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001142 } else if (PropertyIvar)
1143 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001144 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001145
Ted Kremenekac597f32010-03-12 00:46:40 +00001146 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1147 ObjCPropertyImplDecl *PIDecl =
1148 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1149 property,
1150 (Synthesize ?
1151 ObjCPropertyImplDecl::Synthesize
1152 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001153 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001154
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001155 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001156 PIDecl->setInvalidDecl();
1157
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001158 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1159 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001160 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001161 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001162 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1163 // returned by the getter as it must conform to C++'s copy-return rules.
1164 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001165 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001166 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1167 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001168 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001169 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001170 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001171 Expr *LoadSelfExpr =
1172 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1173 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001174 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001175 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001176 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001177 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001178 ExprResult Res = PerformCopyInitialization(
1179 InitializedEntity::InitializeResult(PropertyDiagLoc,
1180 getterMethod->getReturnType(),
1181 /*NRVO=*/false),
1182 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001183 if (!Res.isInvalid()) {
1184 Expr *ResExpr = Res.takeAs<Expr>();
1185 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001186 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001187 PIDecl->setGetterCXXConstructor(ResExpr);
1188 }
1189 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001190 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1191 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1192 Diag(getterMethod->getLocation(),
1193 diag::warn_property_getter_owning_mismatch);
1194 Diag(property->getLocation(), diag::note_property_declare);
1195 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001196 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1197 switch (getterMethod->getMethodFamily()) {
1198 case OMF_retain:
1199 case OMF_retainCount:
1200 case OMF_release:
1201 case OMF_autorelease:
1202 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1203 << 1 << getterMethod->getSelector();
1204 break;
1205 default:
1206 break;
1207 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001208 }
1209 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1210 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001211 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1212 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001213 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001214 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001215 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1216 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001217 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001218 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001219 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001220 Expr *LoadSelfExpr =
1221 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1222 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001223 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001224 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001225 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001226 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001227 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1228 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001229 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001230 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1231 VK_LValue, PropertyDiagLoc);
1232 MarkDeclRefReferenced(rhs);
1233 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001234 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001235 if (property->getPropertyAttributes() &
1236 ObjCPropertyDecl::OBJC_PR_atomic) {
1237 Expr *callExpr = Res.takeAs<Expr>();
1238 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001239 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1240 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001241 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001242 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001243 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001244 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001245 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001246 Diag(FuncDecl->getLocStart(),
1247 diag::note_callee_decl) << FuncDecl;
1248 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001249 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001250 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1251 }
1252 }
1253
Ted Kremenekac597f32010-03-12 00:46:40 +00001254 if (IC) {
1255 if (Synthesize)
1256 if (ObjCPropertyImplDecl *PPIDecl =
1257 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1258 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1259 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1260 << PropertyIvar;
1261 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1262 }
1263
1264 if (ObjCPropertyImplDecl *PPIDecl
1265 = IC->FindPropertyImplDecl(PropertyId)) {
1266 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1267 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001268 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001269 }
1270 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001271 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001272 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001273 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001274 // Diagnose if an ivar was lazily synthesdized due to a previous
1275 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001276 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001277 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001278 ObjCIvarDecl *Ivar = 0;
1279 if (!Synthesize)
1280 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1281 else {
1282 if (PropertyIvar && PropertyIvar != PropertyId)
1283 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1284 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001285 // Issue diagnostics only if Ivar belongs to current class.
1286 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001287 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001288 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1289 << PropertyId;
1290 Ivar->setInvalidDecl();
1291 }
1292 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001293 } else {
1294 if (Synthesize)
1295 if (ObjCPropertyImplDecl *PPIDecl =
1296 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001297 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001298 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1299 << PropertyIvar;
1300 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1301 }
1302
1303 if (ObjCPropertyImplDecl *PPIDecl =
1304 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001305 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001306 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001307 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001308 }
1309 CatImplClass->addPropertyImplementation(PIDecl);
1310 }
1311
John McCall48871652010-08-21 09:40:31 +00001312 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001313}
1314
1315//===----------------------------------------------------------------------===//
1316// Helper methods.
1317//===----------------------------------------------------------------------===//
1318
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001319/// DiagnosePropertyMismatch - Compares two properties for their
1320/// attributes and types and warns on a variety of inconsistencies.
1321///
1322void
1323Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1324 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001325 const IdentifierInfo *inheritedName,
1326 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001327 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001328 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001329 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001330 SuperProperty->getPropertyAttributes();
1331
1332 // We allow readonly properties without an explicit ownership
1333 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1334 // to be overridden by a property with any explicit ownership in the subclass.
1335 if (!OverridingProtocolProperty &&
1336 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1337 ;
1338 else {
1339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1340 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1341 Diag(Property->getLocation(), diag::warn_readonly_property)
1342 << Property->getDeclName() << inheritedName;
1343 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1344 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001345 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001346 << Property->getDeclName() << "copy" << inheritedName;
1347 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1348 unsigned CAttrRetain =
1349 (CAttr &
1350 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1351 unsigned SAttrRetain =
1352 (SAttr &
1353 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1354 bool CStrong = (CAttrRetain != 0);
1355 bool SStrong = (SAttrRetain != 0);
1356 if (CStrong != SStrong)
1357 Diag(Property->getLocation(), diag::warn_property_attribute)
1358 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1359 }
John McCall31168b02011-06-15 23:02:42 +00001360 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001361
1362 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001363 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001364 Diag(Property->getLocation(), diag::warn_property_attribute)
1365 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001366 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1367 }
1368 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001369 Diag(Property->getLocation(), diag::warn_property_attribute)
1370 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001371 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1372 }
1373 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001374 Diag(Property->getLocation(), diag::warn_property_attribute)
1375 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001376 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1377 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001378
1379 QualType LHSType =
1380 Context.getCanonicalType(SuperProperty->getType());
1381 QualType RHSType =
1382 Context.getCanonicalType(Property->getType());
1383
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001384 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001385 // Do cases not handled in above.
1386 // FIXME. For future support of covariant property types, revisit this.
1387 bool IncompatibleObjC = false;
1388 QualType ConvertedType;
1389 if (!isObjCPointerConversion(RHSType, LHSType,
1390 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001391 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001392 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1393 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001394 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1395 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001396 }
1397}
1398
1399bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1400 ObjCMethodDecl *GetterMethod,
1401 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001402 if (!GetterMethod)
1403 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001404 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001405 QualType PropertyIvarType = property->getType().getNonReferenceType();
1406 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1407 if (!compat) {
1408 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1409 isa<ObjCObjectPointerType>(GetterType))
1410 compat =
1411 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001412 GetterType->getAs<ObjCObjectPointerType>(),
1413 PropertyIvarType->getAs<ObjCObjectPointerType>());
1414 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001415 != Compatible) {
1416 Diag(Loc, diag::error_property_accessor_type)
1417 << property->getDeclName() << PropertyIvarType
1418 << GetterMethod->getSelector() << GetterType;
1419 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1420 return true;
1421 } else {
1422 compat = true;
1423 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1424 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1425 if (lhsType != rhsType && lhsType->isArithmeticType())
1426 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001427 }
1428 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001429
1430 if (!compat) {
1431 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1432 << property->getDeclName()
1433 << GetterMethod->getSelector();
1434 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1435 return true;
1436 }
1437
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001438 return false;
1439}
1440
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001441/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001442/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001443void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001444 ObjCContainerDecl::PropertyMap &PropMap,
1445 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001446 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1447 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1448 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001449 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001450 PropMap[Prop->getIdentifier()] = Prop;
1451 }
1452 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001453 for (ObjCInterfaceDecl::all_protocol_iterator
1454 PI = IDecl->all_referenced_protocol_begin(),
1455 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001456 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001457 }
1458 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1459 if (!CATDecl->IsClassExtension())
1460 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1461 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001462 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001463 PropMap[Prop->getIdentifier()] = Prop;
1464 }
1465 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001466 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001467 E = CATDecl->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 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1471 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1472 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001473 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001474 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1475 // Exclude property for protocols which conform to class's super-class,
1476 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001477 if (!PropertyFromSuper ||
1478 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001479 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1480 if (!PropEntry)
1481 PropEntry = Prop;
1482 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001483 }
1484 // scan through protocol's protocols.
1485 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1486 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001487 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001488 }
1489}
1490
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001491/// CollectSuperClassPropertyImplementations - This routine collects list of
1492/// properties to be implemented in super class(s) and also coming from their
1493/// conforming protocols.
1494static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001495 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001496 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001497 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001498 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001499 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001500 SDecl = SDecl->getSuperClass();
1501 }
1502 }
1503}
1504
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001505/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1506/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1507/// declared in class 'IFace'.
1508bool
1509Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1510 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1511 if (!IV->getSynthesize())
1512 return false;
1513 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1514 Method->isInstanceMethod());
1515 if (!IMD || !IMD->isPropertyAccessor())
1516 return false;
1517
1518 // look up a property declaration whose one of its accessors is implemented
1519 // by this method.
1520 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1521 E = IFace->prop_end(); P != E; ++P) {
1522 ObjCPropertyDecl *property = *P;
1523 if ((property->getGetterName() == IMD->getSelector() ||
1524 property->getSetterName() == IMD->getSelector()) &&
1525 (property->getPropertyIvarDecl() == IV))
1526 return true;
1527 }
1528 return false;
1529}
1530
1531
James Dennett2a4d13c2012-06-15 07:13:21 +00001532/// \brief Default synthesizes all properties which must be synthesized
1533/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001534void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1535 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001536
Anna Zaks673d76b2012-10-18 19:17:53 +00001537 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001538 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1539 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001540 if (PropMap.empty())
1541 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001542 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001543 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1544
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001545 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1546 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001547 // Is there a matching property synthesize/dynamic?
1548 if (Prop->isInvalidDecl() ||
1549 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1550 continue;
1551 // Property may have been synthesized by user.
1552 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1553 continue;
1554 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1555 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1556 continue;
1557 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1558 continue;
1559 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001560 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001561 if (SuperPropMap[Prop->getIdentifier()]) {
1562 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1563 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1564 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001565 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001566 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1567 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001568 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001569 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001570 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1571 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001572 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001573 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001574 if (ObjCPropertyImplDecl *PID =
1575 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1576 if (PID->getPropertyDecl() != Prop) {
1577 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001578 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001579 if (!PID->getLocation().isInvalid())
1580 Diag(PID->getLocation(), diag::note_property_synthesize);
1581 }
1582 continue;
1583 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001584 if (ObjCProtocolDecl *Proto =
1585 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001586 // We won't auto-synthesize properties declared in protocols.
1587 Diag(IMPDecl->getLocation(),
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001588 diag::warn_auto_synthesizing_protocol_property)
1589 << Prop << Proto;
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001590 Diag(Prop->getLocation(), diag::note_property_declare);
1591 continue;
1592 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001593
1594 // We use invalid SourceLocations for the synthesized ivars since they
1595 // aren't really synthesized at a particular location; they just exist.
1596 // Saying that they are located at the @implementation isn't really going
1597 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001598 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1599 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1600 true,
1601 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001602 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001603 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001604 if (PIDecl) {
1605 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001606 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001607 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001608 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001609}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001610
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001611void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001612 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001613 return;
1614 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1615 if (!IC)
1616 return;
1617 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001618 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001619 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001620}
1621
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001622void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001623 ObjCContainerDecl *CDecl) {
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001624 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1625 ObjCInterfaceDecl *IDecl;
1626 // Gather properties which need not be implemented in this class
1627 // or category.
1628 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1629 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1630 // For categories, no need to implement properties declared in
1631 // its primary class (and its super classes) if property is
1632 // declared in one of those containers.
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001633 if ((IDecl = C->getClassInterface())) {
1634 ObjCInterfaceDecl::PropertyDeclOrder PO;
1635 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1636 }
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001637 }
1638 if (IDecl)
1639 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001640
Anna Zaks673d76b2012-10-18 19:17:53 +00001641 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001642 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001643 if (PropMap.empty())
1644 return;
1645
1646 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1647 for (ObjCImplDecl::propimpl_iterator
1648 I = IMPDecl->propimpl_begin(),
1649 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001650 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001651
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001652 SelectorSet InsMap;
1653 // Collect property accessors implemented in current implementation.
1654 for (ObjCImplementationDecl::instmeth_iterator
1655 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1656 InsMap.insert((*I)->getSelector());
1657
1658 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1659 ObjCInterfaceDecl *PrimaryClass = 0;
1660 if (C && !C->IsClassExtension())
1661 if ((PrimaryClass = C->getClassInterface()))
1662 // Report unimplemented properties in the category as well.
1663 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1664 // When reporting on missing setter/getters, do not report when
1665 // setter/getter is implemented in category's primary class
1666 // implementation.
1667 for (ObjCImplementationDecl::instmeth_iterator
1668 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1669 InsMap.insert((*I)->getSelector());
1670 }
1671
Anna Zaks673d76b2012-10-18 19:17:53 +00001672 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001673 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1674 ObjCPropertyDecl *Prop = P->second;
1675 // Is there a matching propery synthesize/dynamic?
1676 if (Prop->isInvalidDecl() ||
1677 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001678 PropImplMap.count(Prop) ||
1679 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001680 continue;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001681 // When reporting on missing property getter implementation in
1682 // categories, do not report when they are declared in primary class,
1683 // class's protocol, or one of it super classes. This is because,
1684 // the class is going to implement them.
1685 if (!InsMap.count(Prop->getGetterName()) &&
1686 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001687 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001688 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001689 isa<ObjCCategoryDecl>(CDecl) ?
1690 diag::warn_setter_getter_impl_required_in_category :
1691 diag::warn_setter_getter_impl_required)
1692 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001693 Diag(Prop->getLocation(),
1694 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001695 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001696 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001697 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001698 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1699
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001700 }
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001701 // When reporting on missing property setter implementation in
1702 // categories, do not report when they are declared in primary class,
1703 // class's protocol, or one of it super classes. This is because,
1704 // the class is going to implement them.
1705 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1706 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001707 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001708 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001709 isa<ObjCCategoryDecl>(CDecl) ?
1710 diag::warn_setter_getter_impl_required_in_category :
1711 diag::warn_setter_getter_impl_required)
1712 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001713 Diag(Prop->getLocation(),
1714 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001715 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001716 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001717 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001718 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001719 }
1720 }
1721}
1722
1723void
1724Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1725 ObjCContainerDecl* IDecl) {
1726 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001727 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001728 return;
1729 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1730 E = IDecl->prop_end();
1731 I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00001732 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001733 ObjCMethodDecl *GetterMethod = 0;
1734 ObjCMethodDecl *SetterMethod = 0;
1735 bool LookedUpGetterSetter = false;
1736
Bill Wendling44426052012-12-20 19:22:21 +00001737 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001738 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001739
John McCall43192862011-09-13 18:31:23 +00001740 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1741 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001742 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1743 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1744 LookedUpGetterSetter = true;
1745 if (GetterMethod) {
1746 Diag(GetterMethod->getLocation(),
1747 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001748 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001749 Diag(Property->getLocation(), diag::note_property_declare);
1750 }
1751 if (SetterMethod) {
1752 Diag(SetterMethod->getLocation(),
1753 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001754 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001755 Diag(Property->getLocation(), diag::note_property_declare);
1756 }
1757 }
1758
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001759 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001760 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1761 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001762 continue;
1763 if (const ObjCPropertyImplDecl *PIDecl
1764 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1765 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1766 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001767 if (!LookedUpGetterSetter) {
1768 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1769 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1770 LookedUpGetterSetter = true;
1771 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001772 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1773 SourceLocation MethodLoc =
1774 (GetterMethod ? GetterMethod->getLocation()
1775 : SetterMethod->getLocation());
1776 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001777 << Property->getIdentifier() << (GetterMethod != 0)
1778 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001779 // fixit stuff.
1780 if (!AttributesAsWritten) {
1781 if (Property->getLParenLoc().isValid()) {
1782 // @property () ... case.
1783 SourceRange PropSourceRange(Property->getAtLoc(),
1784 Property->getLParenLoc());
1785 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1786 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1787 }
1788 else {
1789 //@property id etc.
1790 SourceLocation endLoc =
1791 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1792 endLoc = endLoc.getLocWithOffset(-1);
1793 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1794 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1795 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1796 }
1797 }
1798 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1799 // @property () ... case.
1800 SourceLocation endLoc = Property->getLParenLoc();
1801 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1802 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1803 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1804 }
1805 else
1806 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001807 Diag(Property->getLocation(), diag::note_property_declare);
1808 }
1809 }
1810 }
1811}
1812
John McCall31168b02011-06-15 23:02:42 +00001813void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001814 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001815 return;
1816
1817 for (ObjCImplementationDecl::propimpl_iterator
1818 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001819 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001820 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001821 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1822 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001823 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1824 if (!method)
1825 continue;
1826 ObjCMethodFamily family = method->getMethodFamily();
1827 if (family == OMF_alloc || family == OMF_copy ||
1828 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001829 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001830 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001831 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001832 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001833 }
1834 }
1835 }
1836}
1837
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001838void Sema::DiagnoseMissingDesignatedInitOverrides(
1839 const ObjCImplementationDecl *ImplD,
1840 const ObjCInterfaceDecl *IFD) {
1841 assert(IFD->hasDesignatedInitializers());
1842 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1843 if (!SuperD)
1844 return;
1845
1846 SelectorSet InitSelSet;
1847 for (ObjCImplementationDecl::instmeth_iterator
1848 I = ImplD->instmeth_begin(), E = ImplD->instmeth_end(); I!=E; ++I)
1849 if ((*I)->getMethodFamily() == OMF_init)
1850 InitSelSet.insert((*I)->getSelector());
1851
1852 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1853 SuperD->getDesignatedInitializers(DesignatedInits);
1854 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1855 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1856 const ObjCMethodDecl *MD = *I;
1857 if (!InitSelSet.count(MD->getSelector())) {
1858 Diag(ImplD->getLocation(),
1859 diag::warn_objc_implementation_missing_designated_init_override)
1860 << MD->getSelector();
1861 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1862 }
1863 }
1864}
1865
John McCallad31b5f2010-11-10 07:01:40 +00001866/// AddPropertyAttrs - Propagates attributes from a property to the
1867/// implicitly-declared getter or setter for that property.
1868static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1869 ObjCPropertyDecl *Property) {
1870 // Should we just clone all attributes over?
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001871 for (Decl::attr_iterator A = Property->attr_begin(),
1872 AEnd = Property->attr_end();
1873 A != AEnd; ++A) {
1874 if (isa<DeprecatedAttr>(*A) ||
1875 isa<UnavailableAttr>(*A) ||
1876 isa<AvailabilityAttr>(*A))
1877 PropertyMethod->addAttr((*A)->clone(S.Context));
1878 }
John McCallad31b5f2010-11-10 07:01:40 +00001879}
1880
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001881/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1882/// have the property type and issue diagnostics if they don't.
1883/// Also synthesize a getter/setter method if none exist (and update the
1884/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1885/// methods is the "right" thing to do.
1886void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001887 ObjCContainerDecl *CD,
1888 ObjCPropertyDecl *redeclaredProperty,
1889 ObjCContainerDecl *lexicalDC) {
1890
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001891 ObjCMethodDecl *GetterMethod, *SetterMethod;
1892
1893 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1894 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1895 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1896 property->getLocation());
1897
1898 if (SetterMethod) {
1899 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1900 property->getPropertyAttributes();
1901 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001902 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1903 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001904 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1905 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001906 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001907 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1908 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001909 Diag(property->getLocation(),
1910 diag::warn_accessor_property_type_mismatch)
1911 << property->getDeclName()
1912 << SetterMethod->getSelector();
1913 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1914 }
1915 }
1916
1917 // Synthesize getter/setter methods if none exist.
1918 // Find the default getter and if one not found, add one.
1919 // FIXME: The synthesized property we set here is misleading. We almost always
1920 // synthesize these methods unless the user explicitly provided prototypes
1921 // (which is odd, but allowed). Sema should be typechecking that the
1922 // declarations jive in that situation (which it is not currently).
1923 if (!GetterMethod) {
1924 // No instance method of same name as property getter name was found.
1925 // Declare a getter method and add it to the list of methods
1926 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001927 SourceLocation Loc = redeclaredProperty ?
1928 redeclaredProperty->getLocation() :
1929 property->getLocation();
1930
1931 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1932 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001933 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001934 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001935 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001936 (property->getPropertyImplementation() ==
1937 ObjCPropertyDecl::Optional) ?
1938 ObjCMethodDecl::Optional :
1939 ObjCMethodDecl::Required);
1940 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001941
1942 AddPropertyAttrs(*this, GetterMethod, property);
1943
Ted Kremenek49be9e02010-05-18 21:09:07 +00001944 // FIXME: Eventually this shouldn't be needed, as the lexical context
1945 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001946 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001947 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001948 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001949 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1950 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001951
1952 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1953 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001954 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001955
1956 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001957 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1958 Loc));
John McCalle48f3892013-04-04 01:38:37 +00001959
1960 if (getLangOpts().ObjCAutoRefCount)
1961 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001962 } else
1963 // A user declared getter will be synthesize when @synthesize of
1964 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001965 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001966 property->setGetterMethodDecl(GetterMethod);
1967
1968 // Skip setter if property is read-only.
1969 if (!property->isReadOnly()) {
1970 // Find the default setter and if one not found, add one.
1971 if (!SetterMethod) {
1972 // No instance method of same name as property setter name was found.
1973 // Declare a setter method and add it to the list of methods
1974 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001975 SourceLocation Loc = redeclaredProperty ?
1976 redeclaredProperty->getLocation() :
1977 property->getLocation();
1978
1979 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001980 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001981 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001982 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001983 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001984 /*isImplicitlyDeclared=*/true,
1985 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001986 (property->getPropertyImplementation() ==
1987 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001988 ObjCMethodDecl::Optional :
1989 ObjCMethodDecl::Required);
1990
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001991 // Invent the arguments for the setter. We don't bother making a
1992 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00001993 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1994 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001995 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00001996 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001997 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00001998 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001999 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00002000 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00002001
2002 AddPropertyAttrs(*this, SetterMethod, property);
2003
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002004 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00002005 // FIXME: Eventually this shouldn't be needed, as the lexical context
2006 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002007 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002008 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002009 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002010 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2011 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002012 // It's possible for the user to have set a very odd custom
2013 // setter selector that causes it to have a method family.
2014 if (getLangOpts().ObjCAutoRefCount)
2015 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002016 } else
2017 // A user declared setter will be synthesize when @synthesize of
2018 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002019 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002020 property->setSetterMethodDecl(SetterMethod);
2021 }
2022 // Add any synthesized methods to the global pool. This allows us to
2023 // handle the following, which is supported by GCC (and part of the design).
2024 //
2025 // @interface Foo
2026 // @property double bar;
2027 // @end
2028 //
2029 // void thisIsUnfortunate() {
2030 // id foo;
2031 // double bar = [foo bar];
2032 // }
2033 //
2034 if (GetterMethod)
2035 AddInstanceMethodToGlobalPool(GetterMethod);
2036 if (SetterMethod)
2037 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002038
2039 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2040 if (!CurrentClass) {
2041 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2042 CurrentClass = Cat->getClassInterface();
2043 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2044 CurrentClass = Impl->getClassInterface();
2045 }
2046 if (GetterMethod)
2047 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2048 if (SetterMethod)
2049 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002050}
2051
John McCall48871652010-08-21 09:40:31 +00002052void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002053 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002054 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002055 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002056 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002057 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002058 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002059
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002060 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2061 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2062 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2063 << "readonly" << "readwrite";
2064
2065 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2066 QualType PropertyTy = PropertyDecl->getType();
2067 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002068
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002069 // 'readonly' property with no obvious lifetime.
2070 // its life time will be determined by its backing ivar.
2071 if (getLangOpts().ObjCAutoRefCount &&
2072 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2073 PropertyTy->isObjCRetainableType() &&
2074 !PropertyOwnership)
2075 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002076
2077 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002078 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002079 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2080 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002081 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002082 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002083 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2084 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2085 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002086 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002087 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002088 }
2089
2090 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002091 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2092 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002093 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2094 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002095 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002096 }
Bill Wendling44426052012-12-20 19:22:21 +00002097 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002098 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2099 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002100 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002101 }
Bill Wendling44426052012-12-20 19:22:21 +00002102 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002103 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2104 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002105 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002106 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002107 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002108 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002109 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2110 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002111 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002112 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002113 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002114 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002115 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2116 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002119 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002120 }
Bill Wendling44426052012-12-20 19:22:21 +00002121 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002124 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002125 }
Bill Wendling44426052012-12-20 19:22:21 +00002126 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002127 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2128 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002129 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002130 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002131 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002132 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002135 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002136 }
Bill Wendling44426052012-12-20 19:22:21 +00002137 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2138 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002139 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2140 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002141 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002142 }
Bill Wendling44426052012-12-20 19:22:21 +00002143 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2145 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002146 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002147 }
Bill Wendling44426052012-12-20 19:22:21 +00002148 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002149 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2150 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002151 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002152 }
2153 }
Bill Wendling44426052012-12-20 19:22:21 +00002154 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2155 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2157 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002158 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002159 }
Bill Wendling44426052012-12-20 19:22:21 +00002160 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2161 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002162 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2163 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002164 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002165 }
2166
Bill Wendling44426052012-12-20 19:22:21 +00002167 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2168 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002169 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2170 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002171 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002172 }
2173
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002174 // Warn if user supplied no assignment attribute, property is
2175 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002176 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002177 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2178 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2179 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002180 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002181 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002182 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002183 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002184 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002185 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002186 bool isAnyClassTy =
2187 (PropertyTy->isObjCClassType() ||
2188 PropertyTy->isObjCQualifiedClassType());
2189 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2190 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002191 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002192 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002193 else if (propertyInPrimaryClass) {
2194 // Don't issue warning on property with no life time in class
2195 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002196 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002197 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002198 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002199
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002200 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002201 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002202 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002203 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002204 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002205
2206 // FIXME: Implement warning dependent on NSCopying being
2207 // implemented. See also:
2208 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2209 // (please trim this list while you are at it).
2210 }
2211
Bill Wendling44426052012-12-20 19:22:21 +00002212 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2213 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002214 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002215 && PropertyTy->isBlockPointerType())
2216 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002217 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2218 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2219 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002220 PropertyTy->isBlockPointerType())
2221 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002222
Bill Wendling44426052012-12-20 19:22:21 +00002223 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2224 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002225 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2226
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002227}