blob: beb58ecdfa39aaccd00f1df828f27e79fe75bf33 [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;
Douglas Gregor90d34422013-01-21 19:05:22 +0000207 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
208 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000209 for (unsigned I = 0, N = R.size(); I != N; ++I) {
210 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000211 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-01-21 19:42:21 +0000212 FoundInSuper = true;
213 break;
214 }
215 }
216 }
217
218 if (FoundInSuper) {
219 // Also compare the property against a property in our protocols.
220 for (ObjCInterfaceDecl::protocol_iterator P = IFace->protocol_begin(),
221 PEnd = IFace->protocol_end();
222 P != PEnd; ++P) {
223 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
227 for (ObjCInterfaceDecl::all_protocol_iterator
228 P = IFace->all_referenced_protocol_begin(),
229 PEnd = IFace->all_referenced_protocol_end();
230 P != PEnd; ++P) {
231 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
232 }
233 }
234 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
235 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
236 PEnd = Cat->protocol_end();
237 P != PEnd; ++P) {
238 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
239 }
240 } else {
241 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
242 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
243 PEnd = Proto->protocol_end();
244 P != PEnd; ++P) {
245 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregor90d34422013-01-21 19:05:22 +0000246 }
247 }
248
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000249 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000250 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000251}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000252
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000254makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
280
281 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
282}
283
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000284static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000285 SourceLocation LParenLoc, SourceLocation &Loc) {
286 if (LParenLoc.isMacroID())
287 return false;
288
289 SourceManager &SM = Context.getSourceManager();
290 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
291 // Try to load the file buffer.
292 bool invalidTemp = false;
293 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
294 if (invalidTemp)
295 return false;
296 const char *tokenBegin = file.data() + locInfo.second;
297
298 // Lex from the start of the given location.
299 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
300 Context.getLangOpts(),
301 file.begin(), tokenBegin, file.end());
302 Token Tok;
303 do {
304 lexer.LexFromRawLexer(Tok);
305 if (Tok.is(tok::raw_identifier) &&
306 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
307 Loc = Tok.getLocation();
308 return true;
309 }
310 } while (Tok.isNot(tok::r_paren));
311 return false;
312
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000313}
314
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000315static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000316 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
317 ObjCPropertyDecl::OBJC_PR_retain |
318 ObjCPropertyDecl::OBJC_PR_copy |
319 ObjCPropertyDecl::OBJC_PR_weak |
320 ObjCPropertyDecl::OBJC_PR_strong |
321 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
322}
323
Douglas Gregor90d34422013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000346 for (ObjCInterfaceDecl::known_extensions_iterator
347 Ext = CCPrimary->known_extensions_begin(),
348 ExtEnd = CCPrimary->known_extensions_end();
349 Ext != ExtEnd; ++Ext) {
350 if (ObjCPropertyDecl *prevDecl
351 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000352 Diag(AtLoc, diag::err_duplicate_property);
353 Diag(prevDecl->getLocation(), diag::note_property_declare);
354 return 0;
355 }
356 }
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000357 }
358
Ted Kremenek959e8302010-03-12 02:31:10 +0000359 // Create a new ObjCPropertyDecl with the DeclContext being
360 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000361 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000362 ObjCPropertyDecl *PDecl =
363 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000364 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000365 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000366 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000367 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-02-10 00:16:04 +0000371 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
373 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000375 // Set setter/getter selector name. Needed later.
376 PDecl->setGetterName(GetterSel);
377 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000378 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-03-12 02:31:10 +0000379 DC->addDecl(PDecl);
380
381 // We need to look in the @interface to see if the @property was
382 // already declared.
Ted Kremenek959e8302010-03-12 02:31:10 +0000383 if (!CCPrimary) {
384 Diag(CDecl->getLocation(), diag::err_continuation_class);
385 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000386 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000387 }
388
389 // Find the property in continuation class's primary class only.
390 ObjCPropertyDecl *PIDecl =
391 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
392
393 if (!PIDecl) {
394 // No matching property found in the primary class. Just fall thru
395 // and add property to continuation class's primary class.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000396 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000397 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000398 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000399 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-03-12 02:31:10 +0000400
401 // A case of continuation class adding a new property in the class. This
402 // is not what it was meant for. However, gcc supports it and so should we.
403 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000404 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000405 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000406 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
407 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000408 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000409 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
410 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000411 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000412 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
413 bool IncompatibleObjC = false;
414 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-02-02 19:34:05 +0000415 // Relax the strict type matching for property type in continuation class.
416 // Allow property object type of continuation class to be different as long
Fariborz Jahanian57539cf2012-02-02 22:37:48 +0000417 // as it narrows the object type in its primary class property. Note that
418 // this conversion is safe only because the wider type is for a 'readonly'
419 // property in primary class and 'narrowed' type for a 'readwrite' property
420 // in continuation class.
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000421 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
422 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
423 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
424 ConvertedType, IncompatibleObjC))
425 || IncompatibleObjC) {
426 Diag(AtLoc,
427 diag::err_type_mismatch_continuation_class) << PDecl->getType();
428 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000429 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000430 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000431 }
432
Ted Kremenek959e8302010-03-12 02:31:10 +0000433 // The property 'PIDecl's readonly attribute will be over-ridden
434 // with continuation class's readwrite property attribute!
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000435 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000436 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000437 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
438 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000439 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000440 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000441 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000442 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
443 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000444 Diag(AtLoc, diag::warn_property_attr_mismatch);
445 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000446 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000447 else if (getLangOpts().ObjCAutoRefCount) {
448 QualType PrimaryPropertyQT =
449 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
450 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000451 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000452 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
453 PrimaryPropertyQT.getObjCLifetime();
454 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000455 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
456 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000457 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
458 Diag(PIDecl->getLocation(), diag::note_property_declare);
459 }
460 }
461 }
462
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000463 DeclContext *DC = cast<DeclContext>(CCPrimary);
464 if (!ObjCPropertyDecl::findPropertyDecl(DC,
465 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000466 // Protocol is not in the primary class. Must build one for it.
467 ObjCDeclSpec ProtocolPropertyODS;
468 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
469 // and ObjCPropertyDecl::PropertyAttributeKind have identical
470 // values. Should consolidate both into one enum type.
471 ProtocolPropertyODS.
472 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
473 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000474 // Must re-establish the context from class extension to primary
475 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000476 ContextRAII SavedContext(*this, CCPrimary);
477
John McCall48871652010-08-21 09:40:31 +0000478 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000479 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000480 PIDecl->getGetterName(),
481 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000482 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000483 MethodImplKind,
484 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000485 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000486 }
487 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000488 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000489 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000490 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000491 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000492 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000493 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
494 PIDecl->setSetterName(SetterSel);
495 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000496 // Tailor the diagnostics for the common case where a readwrite
497 // property is declared both in the @interface and the continuation.
498 // This is a common error where the user often intended the original
499 // declaration to be readonly.
500 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000501 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000502 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
503 ? diag::err_use_continuation_class_redeclaration_readwrite
504 : diag::err_use_continuation_class;
505 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000506 << CCPrimary->getDeclName();
507 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000508 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000509 }
510 *isOverridingProperty = true;
511 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000512 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000513 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
514 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000515 if (ASTMutationListener *L = Context.getASTMutationListener())
516 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000517 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000518}
519
520ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
521 ObjCContainerDecl *CDecl,
522 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000523 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000524 FieldDeclarator &FD,
525 Selector GetterSel,
526 Selector SetterSel,
527 const bool isAssign,
528 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000529 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000530 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000531 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000532 tok::ObjCKeywordKind MethodImplKind,
533 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000534 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000535 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000536
537 // Issue a warning if property is 'assign' as default and its object, which is
538 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000539 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000540 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000541 if (const ObjCObjectPointerType *ObjPtrTy =
542 T->getAs<ObjCObjectPointerType>()) {
543 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
544 if (IDecl)
545 if (ObjCProtocolDecl* PNSCopying =
546 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
547 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
548 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000549 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000550
551 if (T->isObjCObjectType()) {
552 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
553 StarLoc = PP.getLocForEndOfToken(StarLoc);
554 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
555 << FixItHint::CreateInsertion(StarLoc, "*");
556 T = Context.getObjCObjectPointerType(T);
557 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
558 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
559 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000560
Ted Kremenek959e8302010-03-12 02:31:10 +0000561 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000562 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
563 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000564 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000565
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000566 if (ObjCPropertyDecl *prevDecl =
567 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000568 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000569 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000570 PDecl->setInvalidDecl();
571 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000572 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000573 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000574 if (lexicalDC)
575 PDecl->setLexicalDeclContext(lexicalDC);
576 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000577
578 if (T->isArrayType() || T->isFunctionType()) {
579 Diag(AtLoc, diag::err_property_type) << T;
580 PDecl->setInvalidDecl();
581 }
582
583 ProcessDeclAttributes(S, PDecl, FD.D);
584
585 // Regardless of setter/getter attribute, we save the default getter/setter
586 // selector names in anticipation of declaration of setter/getter methods.
587 PDecl->setGetterName(GetterSel);
588 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000589 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000590 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000591
Bill Wendling44426052012-12-20 19:22:21 +0000592 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000593 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
594
Bill Wendling44426052012-12-20 19:22:21 +0000595 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
597
Bill Wendling44426052012-12-20 19:22:21 +0000598 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
600
601 if (isReadWrite)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
603
Bill Wendling44426052012-12-20 19:22:21 +0000604 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
606
Bill Wendling44426052012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
609
Bill Wendling44426052012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
612
Bill Wendling44426052012-12-20 19:22:21 +0000613 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
618
Ted Kremenekac597f32010-03-12 00:46:40 +0000619 if (isAssign)
620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
621
John McCall43192862011-09-13 18:31:23 +0000622 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000623 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000625 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000627
John McCall31168b02011-06-15 23:02:42 +0000628 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000629 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
631 if (isAssign)
632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
633
Ted Kremenekac597f32010-03-12 00:46:40 +0000634 if (MethodImplKind == tok::objc_required)
635 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
636 else if (MethodImplKind == tok::objc_optional)
637 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000638
Ted Kremenek959e8302010-03-12 02:31:10 +0000639 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000640}
641
John McCall31168b02011-06-15 23:02:42 +0000642static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
643 ObjCPropertyDecl *property,
644 ObjCIvarDecl *ivar) {
645 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
646
John McCall31168b02011-06-15 23:02:42 +0000647 QualType ivarType = ivar->getType();
648 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000649
John McCall43192862011-09-13 18:31:23 +0000650 // The lifetime implied by the property's attributes.
651 Qualifiers::ObjCLifetime propertyLifetime =
652 getImpliedARCOwnership(property->getPropertyAttributes(),
653 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000654
John McCall43192862011-09-13 18:31:23 +0000655 // We're fine if they match.
656 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000657
John McCall43192862011-09-13 18:31:23 +0000658 // These aren't valid lifetimes for object ivars; don't diagnose twice.
659 if (ivarLifetime == Qualifiers::OCL_None ||
660 ivarLifetime == Qualifiers::OCL_Autoreleasing)
661 return;
John McCall31168b02011-06-15 23:02:42 +0000662
John McCalld8561f02012-08-20 23:36:59 +0000663 // If the ivar is private, and it's implicitly __unsafe_unretained
664 // becaues of its type, then pretend it was actually implicitly
665 // __strong. This is only sound because we're processing the
666 // property implementation before parsing any method bodies.
667 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
668 propertyLifetime == Qualifiers::OCL_Strong &&
669 ivar->getAccessControl() == ObjCIvarDecl::Private) {
670 SplitQualType split = ivarType.split();
671 if (split.Quals.hasObjCLifetime()) {
672 assert(ivarType->isObjCARCImplicitlyUnretainedType());
673 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
674 ivarType = S.Context.getQualifiedType(split);
675 ivar->setType(ivarType);
676 return;
677 }
678 }
679
John McCall43192862011-09-13 18:31:23 +0000680 switch (propertyLifetime) {
681 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000682 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000683 << property->getDeclName()
684 << ivar->getDeclName()
685 << ivarLifetime;
686 break;
John McCall31168b02011-06-15 23:02:42 +0000687
John McCall43192862011-09-13 18:31:23 +0000688 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000689 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000690 << property->getDeclName()
691 << ivar->getDeclName();
692 break;
John McCall31168b02011-06-15 23:02:42 +0000693
John McCall43192862011-09-13 18:31:23 +0000694 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000695 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000696 << property->getDeclName()
697 << ivar->getDeclName()
698 << ((property->getPropertyAttributesAsWritten()
699 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
700 break;
John McCall31168b02011-06-15 23:02:42 +0000701
John McCall43192862011-09-13 18:31:23 +0000702 case Qualifiers::OCL_Autoreleasing:
703 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000704
John McCall43192862011-09-13 18:31:23 +0000705 case Qualifiers::OCL_None:
706 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000707 return;
708 }
709
710 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000711 if (propertyImplLoc.isValid())
712 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000713}
714
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000715/// setImpliedPropertyAttributeForReadOnlyProperty -
716/// This routine evaludates life-time attributes for a 'readonly'
717/// property with no known lifetime of its own, using backing
718/// 'ivar's attribute, if any. If no backing 'ivar', property's
719/// life-time is assumed 'strong'.
720static void setImpliedPropertyAttributeForReadOnlyProperty(
721 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
722 Qualifiers::ObjCLifetime propertyLifetime =
723 getImpliedARCOwnership(property->getPropertyAttributes(),
724 property->getType());
725 if (propertyLifetime != Qualifiers::OCL_None)
726 return;
727
728 if (!ivar) {
729 // if no backing ivar, make property 'strong'.
730 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
731 return;
732 }
733 // property assumes owenership of backing ivar.
734 QualType ivarType = ivar->getType();
735 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
736 if (ivarLifetime == Qualifiers::OCL_Strong)
737 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
738 else if (ivarLifetime == Qualifiers::OCL_Weak)
739 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
740 return;
741}
Ted Kremenekac597f32010-03-12 00:46:40 +0000742
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000743/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
744/// in inherited protocols with mismatched types. Since any of them can
745/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000746static void
747DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
748 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000749 ObjCPropertyDecl *Property) {
750 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
751 for (ObjCInterfaceDecl::all_protocol_iterator
752 PI = ClassDecl->all_referenced_protocol_begin(),
753 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
754 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
755 PDecl->collectInheritedProtocolProperties(Property, PropMap);
756 }
757 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
758 while (SDecl) {
759 for (ObjCInterfaceDecl::all_protocol_iterator
760 PI = SDecl->all_referenced_protocol_begin(),
761 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
762 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
763 PDecl->collectInheritedProtocolProperties(Property, PropMap);
764 }
765 SDecl = SDecl->getSuperClass();
766 }
767
768 if (PropMap.empty())
769 return;
770
771 QualType RHSType = S.Context.getCanonicalType(Property->getType());
772 bool FirsTime = true;
773 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
774 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
775 ObjCPropertyDecl *Prop = I->second;
776 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
777 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
778 bool IncompatibleObjC = false;
779 QualType ConvertedType;
780 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
781 || IncompatibleObjC) {
782 if (FirsTime) {
783 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
784 << Property->getType();
785 FirsTime = false;
786 }
787 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
788 << Prop->getType();
789 }
790 }
791 }
792 if (!FirsTime && AtLoc.isValid())
793 S.Diag(AtLoc, diag::note_property_synthesize);
794}
795
Ted Kremenekac597f32010-03-12 00:46:40 +0000796/// ActOnPropertyImplDecl - This routine performs semantic checks and
797/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000798/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000799///
John McCall48871652010-08-21 09:40:31 +0000800Decl *Sema::ActOnPropertyImplDecl(Scope *S,
801 SourceLocation AtLoc,
802 SourceLocation PropertyLoc,
803 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000804 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000805 IdentifierInfo *PropertyIvar,
806 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000807 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000808 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000809 // Make sure we have a context for the property implementation declaration.
810 if (!ClassImpDecl) {
811 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000812 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000813 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000814 if (PropertyIvarLoc.isInvalid())
815 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000816 SourceLocation PropertyDiagLoc = PropertyLoc;
817 if (PropertyDiagLoc.isInvalid())
818 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000819 ObjCPropertyDecl *property = 0;
820 ObjCInterfaceDecl* IDecl = 0;
821 // Find the class or category class where this property must have
822 // a declaration.
823 ObjCImplementationDecl *IC = 0;
824 ObjCCategoryImplDecl* CatImplClass = 0;
825 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
826 IDecl = IC->getClassInterface();
827 // We always synthesize an interface for an implementation
828 // without an interface decl. So, IDecl is always non-zero.
829 assert(IDecl &&
830 "ActOnPropertyImplDecl - @implementation without @interface");
831
832 // Look for this property declaration in the @implementation's @interface
833 property = IDecl->FindPropertyDeclaration(PropertyId);
834 if (!property) {
835 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000836 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000837 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000838 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000839 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
840 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000841 if (AtLoc.isValid())
842 Diag(AtLoc, diag::warn_implicit_atomic_property);
843 else
844 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
845 Diag(property->getLocation(), diag::note_property_declare);
846 }
847
Ted Kremenekac597f32010-03-12 00:46:40 +0000848 if (const ObjCCategoryDecl *CD =
849 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
850 if (!CD->IsClassExtension()) {
851 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
852 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000853 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000854 }
855 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000856 if (Synthesize&&
857 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
858 property->hasAttr<IBOutletAttr>() &&
859 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000860 bool ReadWriteProperty = false;
861 // Search into the class extensions and see if 'readonly property is
862 // redeclared 'readwrite', then no warning is to be issued.
863 for (ObjCInterfaceDecl::known_extensions_iterator
864 Ext = IDecl->known_extensions_begin(),
865 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
866 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
867 if (!R.empty())
868 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
869 PIkind = ExtProp->getPropertyAttributesAsWritten();
870 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
871 ReadWriteProperty = true;
872 break;
873 }
874 }
875 }
876
877 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000878 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000879 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000880 SourceLocation readonlyLoc;
881 if (LocPropertyAttribute(Context, "readonly",
882 property->getLParenLoc(), readonlyLoc)) {
883 SourceLocation endLoc =
884 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
885 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
886 Diag(property->getLocation(),
887 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
888 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
889 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000890 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000891 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000892 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
893 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000894
Ted Kremenekac597f32010-03-12 00:46:40 +0000895 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
896 if (Synthesize) {
897 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000898 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000899 }
900 IDecl = CatImplClass->getClassInterface();
901 if (!IDecl) {
902 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000903 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000904 }
905 ObjCCategoryDecl *Category =
906 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
907
908 // If category for this implementation not found, it is an error which
909 // has already been reported eralier.
910 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000911 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000912 // Look for this property declaration in @implementation's category
913 property = Category->FindPropertyDeclaration(PropertyId);
914 if (!property) {
915 Diag(PropertyLoc, diag::error_bad_category_property_decl)
916 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000917 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000918 }
919 } else {
920 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000921 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000922 }
923 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000924 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000925 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000926 // Check that we have a valid, previously declared ivar for @synthesize
927 if (Synthesize) {
928 // @synthesize
929 if (!PropertyIvar)
930 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000931 // Check that this is a previously declared 'ivar' in 'IDecl' interface
932 ObjCInterfaceDecl *ClassDeclared;
933 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
934 QualType PropType = property->getType();
935 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000936
937 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000938 diag::err_incomplete_synthesized_property,
939 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000940 Diag(property->getLocation(), diag::note_property_declare);
941 CompleteTypeErr = true;
942 }
943
David Blaikiebbafb8a2012-03-11 07:00:24 +0000944 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000945 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000946 ObjCPropertyDecl::OBJC_PR_readonly) &&
947 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000948 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
949 }
950
John McCall31168b02011-06-15 23:02:42 +0000951 ObjCPropertyDecl::PropertyAttributeKind kind
952 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000953
954 // Add GC __weak to the ivar type if the property is weak.
955 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000956 getLangOpts().getGC() != LangOptions::NonGC) {
957 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000958 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000959 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000960 Diag(property->getLocation(), diag::note_property_declare);
961 } else {
962 PropertyIvarType =
963 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000964 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000965 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000966 if (AtLoc.isInvalid()) {
967 // Check when default synthesizing a property that there is
968 // an ivar matching property name and issue warning; since this
969 // is the most common case of not using an ivar used for backing
970 // property in non-default synthesis case.
971 ObjCInterfaceDecl *ClassDeclared=0;
972 ObjCIvarDecl *originalIvar =
973 IDecl->lookupInstanceVariable(property->getIdentifier(),
974 ClassDeclared);
975 if (originalIvar) {
976 Diag(PropertyDiagLoc,
977 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000978 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000979 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000980 Diag(property->getLocation(), diag::note_property_declare);
981 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000982 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000983 }
984
985 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000986 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000987 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000988 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +0000989 !PropertyIvarType.getObjCLifetime() &&
990 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +0000991
John McCall43192862011-09-13 18:31:23 +0000992 // It's an error if we have to do this and the user didn't
993 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000994 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +0000995 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +0000996 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +0000997 diag::err_arc_objc_property_default_assign_on_object);
998 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +0000999 } else {
1000 Qualifiers::ObjCLifetime lifetime =
1001 getImpliedARCOwnership(kind, PropertyIvarType);
1002 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001003 if (lifetime == Qualifiers::OCL_Weak) {
1004 bool err = false;
1005 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001006 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1007 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1008 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001009 Diag(property->getLocation(),
1010 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1011 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1012 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001013 err = true;
1014 }
Richard Smith802c4b72012-08-23 06:16:52 +00001015 }
John McCall3deb1ad2012-08-21 02:47:43 +00001016 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001017 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001018 Diag(property->getLocation(), diag::note_property_declare);
1019 }
John McCall31168b02011-06-15 23:02:42 +00001020 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001021
John McCall31168b02011-06-15 23:02:42 +00001022 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001023 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001024 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1025 }
John McCall31168b02011-06-15 23:02:42 +00001026 }
1027
1028 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001029 !getLangOpts().ObjCAutoRefCount &&
1030 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001031 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001032 Diag(property->getLocation(), diag::note_property_declare);
1033 }
1034
Abramo Bagnaradff19302011-03-08 08:55:46 +00001035 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001036 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001037 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001038 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001039 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001040 if (RequireNonAbstractType(PropertyIvarLoc,
1041 PropertyIvarType,
1042 diag::err_abstract_type_in_decl,
1043 AbstractSynthesizedIvarType)) {
1044 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001045 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001046 } else if (CompleteTypeErr)
1047 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001048 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001049 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001050
John McCall5fb5df92012-06-20 06:18:46 +00001051 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001052 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1053 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001054 // Note! I deliberately want it to fall thru so, we have a
1055 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001056 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001057 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001058 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001059 << property->getDeclName() << Ivar->getDeclName()
1060 << ClassDeclared->getDeclName();
1061 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001062 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001063 // Note! I deliberately want it to fall thru so more errors are caught.
1064 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001065 property->setPropertyIvarDecl(Ivar);
1066
Ted Kremenekac597f32010-03-12 00:46:40 +00001067 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1068
1069 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001070 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001071 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001072 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001073 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001074 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001075 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001076 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001077 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001078 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1079 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001080 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001081 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001082 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001083 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001084 << property->getDeclName() << PropType
1085 << Ivar->getDeclName() << IvarType;
1086 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001087 // Note! I deliberately want it to fall thru so, we have a
1088 // a property implementation and to avoid future warnings.
1089 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001090 else {
1091 // FIXME! Rules for properties are somewhat different that those
1092 // for assignments. Use a new routine to consolidate all cases;
1093 // specifically for property redeclarations as well as for ivars.
1094 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1095 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1096 if (lhsType != rhsType &&
1097 lhsType->isArithmeticType()) {
1098 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1099 << property->getDeclName() << PropType
1100 << Ivar->getDeclName() << IvarType;
1101 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1102 // Fall thru - see previous comment
1103 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001104 }
1105 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001106 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001107 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001108 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001109 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001110 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001111 // Fall thru - see previous comment
1112 }
John McCall31168b02011-06-15 23:02:42 +00001113 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001114 if ((property->getType()->isObjCObjectPointerType() ||
1115 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001116 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001117 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001118 << property->getDeclName() << Ivar->getDeclName();
1119 // Fall thru - see previous comment
1120 }
1121 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001122 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001123 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001124 } else if (PropertyIvar)
1125 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001126 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001127
Ted Kremenekac597f32010-03-12 00:46:40 +00001128 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1129 ObjCPropertyImplDecl *PIDecl =
1130 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1131 property,
1132 (Synthesize ?
1133 ObjCPropertyImplDecl::Synthesize
1134 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001135 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001136
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001137 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001138 PIDecl->setInvalidDecl();
1139
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001140 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1141 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001142 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001143 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001144 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1145 // returned by the getter as it must conform to C++'s copy-return rules.
1146 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001147 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001148 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1149 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001150 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001151 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001152 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001153 Expr *LoadSelfExpr =
1154 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1155 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001156 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001157 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001158 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001159 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001160 ExprResult Res = PerformCopyInitialization(
1161 InitializedEntity::InitializeResult(PropertyDiagLoc,
1162 getterMethod->getReturnType(),
1163 /*NRVO=*/false),
1164 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001165 if (!Res.isInvalid()) {
1166 Expr *ResExpr = Res.takeAs<Expr>();
1167 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001168 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001169 PIDecl->setGetterCXXConstructor(ResExpr);
1170 }
1171 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001172 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1173 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1174 Diag(getterMethod->getLocation(),
1175 diag::warn_property_getter_owning_mismatch);
1176 Diag(property->getLocation(), diag::note_property_declare);
1177 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001178 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1179 switch (getterMethod->getMethodFamily()) {
1180 case OMF_retain:
1181 case OMF_retainCount:
1182 case OMF_release:
1183 case OMF_autorelease:
1184 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1185 << 1 << getterMethod->getSelector();
1186 break;
1187 default:
1188 break;
1189 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001190 }
1191 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1192 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001193 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1194 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001195 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001196 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001197 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1198 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001199 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001200 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001201 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001202 Expr *LoadSelfExpr =
1203 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1204 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001205 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001206 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001207 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001208 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001209 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1210 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001211 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001212 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1213 VK_LValue, PropertyDiagLoc);
1214 MarkDeclRefReferenced(rhs);
1215 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001216 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001217 if (property->getPropertyAttributes() &
1218 ObjCPropertyDecl::OBJC_PR_atomic) {
1219 Expr *callExpr = Res.takeAs<Expr>();
1220 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001221 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1222 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001223 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001224 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001225 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001226 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001227 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001228 Diag(FuncDecl->getLocStart(),
1229 diag::note_callee_decl) << FuncDecl;
1230 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001231 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001232 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1233 }
1234 }
1235
Ted Kremenekac597f32010-03-12 00:46:40 +00001236 if (IC) {
1237 if (Synthesize)
1238 if (ObjCPropertyImplDecl *PPIDecl =
1239 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1240 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1241 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1242 << PropertyIvar;
1243 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1244 }
1245
1246 if (ObjCPropertyImplDecl *PPIDecl
1247 = IC->FindPropertyImplDecl(PropertyId)) {
1248 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1249 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001250 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001251 }
1252 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001253 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001254 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001255 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001256 // Diagnose if an ivar was lazily synthesdized due to a previous
1257 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001258 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001259 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001260 ObjCIvarDecl *Ivar = 0;
1261 if (!Synthesize)
1262 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1263 else {
1264 if (PropertyIvar && PropertyIvar != PropertyId)
1265 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1266 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001267 // Issue diagnostics only if Ivar belongs to current class.
1268 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001269 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001270 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1271 << PropertyId;
1272 Ivar->setInvalidDecl();
1273 }
1274 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001275 } else {
1276 if (Synthesize)
1277 if (ObjCPropertyImplDecl *PPIDecl =
1278 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001279 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001280 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1281 << PropertyIvar;
1282 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1283 }
1284
1285 if (ObjCPropertyImplDecl *PPIDecl =
1286 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001287 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001288 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001289 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001290 }
1291 CatImplClass->addPropertyImplementation(PIDecl);
1292 }
1293
John McCall48871652010-08-21 09:40:31 +00001294 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001295}
1296
1297//===----------------------------------------------------------------------===//
1298// Helper methods.
1299//===----------------------------------------------------------------------===//
1300
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001301/// DiagnosePropertyMismatch - Compares two properties for their
1302/// attributes and types and warns on a variety of inconsistencies.
1303///
1304void
1305Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1306 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001307 const IdentifierInfo *inheritedName,
1308 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001309 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001310 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001311 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001312 SuperProperty->getPropertyAttributes();
1313
1314 // We allow readonly properties without an explicit ownership
1315 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1316 // to be overridden by a property with any explicit ownership in the subclass.
1317 if (!OverridingProtocolProperty &&
1318 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1319 ;
1320 else {
1321 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1322 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1323 Diag(Property->getLocation(), diag::warn_readonly_property)
1324 << Property->getDeclName() << inheritedName;
1325 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1326 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001327 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001328 << Property->getDeclName() << "copy" << inheritedName;
1329 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1330 unsigned CAttrRetain =
1331 (CAttr &
1332 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1333 unsigned SAttrRetain =
1334 (SAttr &
1335 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1336 bool CStrong = (CAttrRetain != 0);
1337 bool SStrong = (SAttrRetain != 0);
1338 if (CStrong != SStrong)
1339 Diag(Property->getLocation(), diag::warn_property_attribute)
1340 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1341 }
John McCall31168b02011-06-15 23:02:42 +00001342 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001343
1344 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001345 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001346 Diag(Property->getLocation(), diag::warn_property_attribute)
1347 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001348 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1349 }
1350 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001351 Diag(Property->getLocation(), diag::warn_property_attribute)
1352 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001353 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1354 }
1355 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001356 Diag(Property->getLocation(), diag::warn_property_attribute)
1357 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001358 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1359 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001360
1361 QualType LHSType =
1362 Context.getCanonicalType(SuperProperty->getType());
1363 QualType RHSType =
1364 Context.getCanonicalType(Property->getType());
1365
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001366 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001367 // Do cases not handled in above.
1368 // FIXME. For future support of covariant property types, revisit this.
1369 bool IncompatibleObjC = false;
1370 QualType ConvertedType;
1371 if (!isObjCPointerConversion(RHSType, LHSType,
1372 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001373 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001374 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1375 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001376 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1377 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001378 }
1379}
1380
1381bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1382 ObjCMethodDecl *GetterMethod,
1383 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001384 if (!GetterMethod)
1385 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001386 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001387 QualType PropertyIvarType = property->getType().getNonReferenceType();
1388 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1389 if (!compat) {
1390 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1391 isa<ObjCObjectPointerType>(GetterType))
1392 compat =
1393 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001394 GetterType->getAs<ObjCObjectPointerType>(),
1395 PropertyIvarType->getAs<ObjCObjectPointerType>());
1396 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001397 != Compatible) {
1398 Diag(Loc, diag::error_property_accessor_type)
1399 << property->getDeclName() << PropertyIvarType
1400 << GetterMethod->getSelector() << GetterType;
1401 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1402 return true;
1403 } else {
1404 compat = true;
1405 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1406 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1407 if (lhsType != rhsType && lhsType->isArithmeticType())
1408 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001409 }
1410 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001411
1412 if (!compat) {
1413 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1414 << property->getDeclName()
1415 << GetterMethod->getSelector();
1416 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1417 return true;
1418 }
1419
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001420 return false;
1421}
1422
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001423/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001424/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001425void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001426 ObjCContainerDecl::PropertyMap &PropMap,
1427 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001428 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1429 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1430 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001431 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001432 PropMap[Prop->getIdentifier()] = Prop;
1433 }
1434 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001435 for (ObjCInterfaceDecl::all_protocol_iterator
1436 PI = IDecl->all_referenced_protocol_begin(),
1437 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001438 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001439 }
1440 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1441 if (!CATDecl->IsClassExtension())
1442 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1443 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001444 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001445 PropMap[Prop->getIdentifier()] = Prop;
1446 }
1447 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001448 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001449 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001450 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001451 }
1452 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1453 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1454 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001455 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001456 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1457 // Exclude property for protocols which conform to class's super-class,
1458 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001459 if (!PropertyFromSuper ||
1460 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001461 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1462 if (!PropEntry)
1463 PropEntry = Prop;
1464 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001465 }
1466 // scan through protocol's protocols.
1467 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1468 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001469 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001470 }
1471}
1472
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001473/// CollectSuperClassPropertyImplementations - This routine collects list of
1474/// properties to be implemented in super class(s) and also coming from their
1475/// conforming protocols.
1476static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001477 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001478 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001479 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001480 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001481 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001482 SDecl = SDecl->getSuperClass();
1483 }
1484 }
1485}
1486
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001487/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1488/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1489/// declared in class 'IFace'.
1490bool
1491Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1492 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1493 if (!IV->getSynthesize())
1494 return false;
1495 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1496 Method->isInstanceMethod());
1497 if (!IMD || !IMD->isPropertyAccessor())
1498 return false;
1499
1500 // look up a property declaration whose one of its accessors is implemented
1501 // by this method.
1502 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1503 E = IFace->prop_end(); P != E; ++P) {
1504 ObjCPropertyDecl *property = *P;
1505 if ((property->getGetterName() == IMD->getSelector() ||
1506 property->getSetterName() == IMD->getSelector()) &&
1507 (property->getPropertyIvarDecl() == IV))
1508 return true;
1509 }
1510 return false;
1511}
1512
1513
James Dennett2a4d13c2012-06-15 07:13:21 +00001514/// \brief Default synthesizes all properties which must be synthesized
1515/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001516void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1517 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001518
Anna Zaks673d76b2012-10-18 19:17:53 +00001519 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001520 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1521 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001522 if (PropMap.empty())
1523 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001524 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001525 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1526
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001527 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1528 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001529 // Is there a matching property synthesize/dynamic?
1530 if (Prop->isInvalidDecl() ||
1531 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1532 continue;
1533 // Property may have been synthesized by user.
1534 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1535 continue;
1536 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1537 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1538 continue;
1539 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1540 continue;
1541 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001542 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001543 if (SuperPropMap[Prop->getIdentifier()]) {
1544 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1545 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1546 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001547 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001548 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1549 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001550 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001551 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001552 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1553 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001554 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001555 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001556 if (ObjCPropertyImplDecl *PID =
1557 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1558 if (PID->getPropertyDecl() != Prop) {
1559 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001560 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001561 if (!PID->getLocation().isInvalid())
1562 Diag(PID->getLocation(), diag::note_property_synthesize);
1563 }
1564 continue;
1565 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001566 if (ObjCProtocolDecl *Proto =
1567 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001568 // We won't auto-synthesize properties declared in protocols.
1569 Diag(IMPDecl->getLocation(),
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001570 diag::warn_auto_synthesizing_protocol_property)
1571 << Prop << Proto;
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001572 Diag(Prop->getLocation(), diag::note_property_declare);
1573 continue;
1574 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001575
1576 // We use invalid SourceLocations for the synthesized ivars since they
1577 // aren't really synthesized at a particular location; they just exist.
1578 // Saying that they are located at the @implementation isn't really going
1579 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001580 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1581 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1582 true,
1583 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001584 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001585 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001586 if (PIDecl) {
1587 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001588 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001589 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001590 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001591}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001592
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001593void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001594 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001595 return;
1596 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1597 if (!IC)
1598 return;
1599 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001600 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001601 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001602}
1603
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001604void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001605 ObjCContainerDecl *CDecl) {
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001606 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1607 ObjCInterfaceDecl *IDecl;
1608 // Gather properties which need not be implemented in this class
1609 // or category.
1610 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1611 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1612 // For categories, no need to implement properties declared in
1613 // its primary class (and its super classes) if property is
1614 // declared in one of those containers.
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001615 if ((IDecl = C->getClassInterface())) {
1616 ObjCInterfaceDecl::PropertyDeclOrder PO;
1617 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1618 }
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001619 }
1620 if (IDecl)
1621 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001622
Anna Zaks673d76b2012-10-18 19:17:53 +00001623 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001624 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001625 if (PropMap.empty())
1626 return;
1627
1628 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1629 for (ObjCImplDecl::propimpl_iterator
1630 I = IMPDecl->propimpl_begin(),
1631 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001632 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001633
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001634 SelectorSet InsMap;
1635 // Collect property accessors implemented in current implementation.
1636 for (ObjCImplementationDecl::instmeth_iterator
1637 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1638 InsMap.insert((*I)->getSelector());
1639
1640 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1641 ObjCInterfaceDecl *PrimaryClass = 0;
1642 if (C && !C->IsClassExtension())
1643 if ((PrimaryClass = C->getClassInterface()))
1644 // Report unimplemented properties in the category as well.
1645 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1646 // When reporting on missing setter/getters, do not report when
1647 // setter/getter is implemented in category's primary class
1648 // implementation.
1649 for (ObjCImplementationDecl::instmeth_iterator
1650 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1651 InsMap.insert((*I)->getSelector());
1652 }
1653
Anna Zaks673d76b2012-10-18 19:17:53 +00001654 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001655 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1656 ObjCPropertyDecl *Prop = P->second;
1657 // Is there a matching propery synthesize/dynamic?
1658 if (Prop->isInvalidDecl() ||
1659 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001660 PropImplMap.count(Prop) ||
1661 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001662 continue;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001663 // When reporting on missing property getter implementation in
1664 // categories, do not report when they are declared in primary class,
1665 // class's protocol, or one of it super classes. This is because,
1666 // the class is going to implement them.
1667 if (!InsMap.count(Prop->getGetterName()) &&
1668 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001669 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001670 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001671 isa<ObjCCategoryDecl>(CDecl) ?
1672 diag::warn_setter_getter_impl_required_in_category :
1673 diag::warn_setter_getter_impl_required)
1674 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001675 Diag(Prop->getLocation(),
1676 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001677 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001678 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001679 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001680 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1681
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001682 }
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001683 // When reporting on missing property setter implementation in
1684 // categories, do not report when they are declared in primary class,
1685 // class's protocol, or one of it super classes. This is because,
1686 // the class is going to implement them.
1687 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1688 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001689 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001690 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001691 isa<ObjCCategoryDecl>(CDecl) ?
1692 diag::warn_setter_getter_impl_required_in_category :
1693 diag::warn_setter_getter_impl_required)
1694 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001695 Diag(Prop->getLocation(),
1696 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001697 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001698 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001699 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001700 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001701 }
1702 }
1703}
1704
1705void
1706Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1707 ObjCContainerDecl* IDecl) {
1708 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001709 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001710 return;
1711 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1712 E = IDecl->prop_end();
1713 I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00001714 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001715 ObjCMethodDecl *GetterMethod = 0;
1716 ObjCMethodDecl *SetterMethod = 0;
1717 bool LookedUpGetterSetter = false;
1718
Bill Wendling44426052012-12-20 19:22:21 +00001719 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001720 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001721
John McCall43192862011-09-13 18:31:23 +00001722 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1723 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001724 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1725 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1726 LookedUpGetterSetter = true;
1727 if (GetterMethod) {
1728 Diag(GetterMethod->getLocation(),
1729 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001730 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001731 Diag(Property->getLocation(), diag::note_property_declare);
1732 }
1733 if (SetterMethod) {
1734 Diag(SetterMethod->getLocation(),
1735 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001736 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001737 Diag(Property->getLocation(), diag::note_property_declare);
1738 }
1739 }
1740
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001741 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001742 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1743 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001744 continue;
1745 if (const ObjCPropertyImplDecl *PIDecl
1746 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1747 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1748 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001749 if (!LookedUpGetterSetter) {
1750 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1751 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1752 LookedUpGetterSetter = true;
1753 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001754 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1755 SourceLocation MethodLoc =
1756 (GetterMethod ? GetterMethod->getLocation()
1757 : SetterMethod->getLocation());
1758 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001759 << Property->getIdentifier() << (GetterMethod != 0)
1760 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001761 // fixit stuff.
1762 if (!AttributesAsWritten) {
1763 if (Property->getLParenLoc().isValid()) {
1764 // @property () ... case.
1765 SourceRange PropSourceRange(Property->getAtLoc(),
1766 Property->getLParenLoc());
1767 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1768 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1769 }
1770 else {
1771 //@property id etc.
1772 SourceLocation endLoc =
1773 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1774 endLoc = endLoc.getLocWithOffset(-1);
1775 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1776 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1777 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1778 }
1779 }
1780 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1781 // @property () ... case.
1782 SourceLocation endLoc = Property->getLParenLoc();
1783 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1784 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1785 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1786 }
1787 else
1788 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001789 Diag(Property->getLocation(), diag::note_property_declare);
1790 }
1791 }
1792 }
1793}
1794
John McCall31168b02011-06-15 23:02:42 +00001795void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001796 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001797 return;
1798
1799 for (ObjCImplementationDecl::propimpl_iterator
1800 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001801 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001802 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001803 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1804 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001805 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1806 if (!method)
1807 continue;
1808 ObjCMethodFamily family = method->getMethodFamily();
1809 if (family == OMF_alloc || family == OMF_copy ||
1810 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001811 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001812 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001813 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001814 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001815 }
1816 }
1817 }
1818}
1819
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001820void Sema::DiagnoseMissingDesignatedInitOverrides(
1821 const ObjCImplementationDecl *ImplD,
1822 const ObjCInterfaceDecl *IFD) {
1823 assert(IFD->hasDesignatedInitializers());
1824 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1825 if (!SuperD)
1826 return;
1827
1828 SelectorSet InitSelSet;
1829 for (ObjCImplementationDecl::instmeth_iterator
1830 I = ImplD->instmeth_begin(), E = ImplD->instmeth_end(); I!=E; ++I)
1831 if ((*I)->getMethodFamily() == OMF_init)
1832 InitSelSet.insert((*I)->getSelector());
1833
1834 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1835 SuperD->getDesignatedInitializers(DesignatedInits);
1836 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1837 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1838 const ObjCMethodDecl *MD = *I;
1839 if (!InitSelSet.count(MD->getSelector())) {
1840 Diag(ImplD->getLocation(),
1841 diag::warn_objc_implementation_missing_designated_init_override)
1842 << MD->getSelector();
1843 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1844 }
1845 }
1846}
1847
John McCallad31b5f2010-11-10 07:01:40 +00001848/// AddPropertyAttrs - Propagates attributes from a property to the
1849/// implicitly-declared getter or setter for that property.
1850static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1851 ObjCPropertyDecl *Property) {
1852 // Should we just clone all attributes over?
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001853 for (Decl::attr_iterator A = Property->attr_begin(),
1854 AEnd = Property->attr_end();
1855 A != AEnd; ++A) {
1856 if (isa<DeprecatedAttr>(*A) ||
1857 isa<UnavailableAttr>(*A) ||
1858 isa<AvailabilityAttr>(*A))
1859 PropertyMethod->addAttr((*A)->clone(S.Context));
1860 }
John McCallad31b5f2010-11-10 07:01:40 +00001861}
1862
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001863/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1864/// have the property type and issue diagnostics if they don't.
1865/// Also synthesize a getter/setter method if none exist (and update the
1866/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1867/// methods is the "right" thing to do.
1868void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001869 ObjCContainerDecl *CD,
1870 ObjCPropertyDecl *redeclaredProperty,
1871 ObjCContainerDecl *lexicalDC) {
1872
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001873 ObjCMethodDecl *GetterMethod, *SetterMethod;
1874
1875 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1876 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1877 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1878 property->getLocation());
1879
1880 if (SetterMethod) {
1881 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1882 property->getPropertyAttributes();
1883 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001884 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1885 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001886 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1887 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001888 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001889 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1890 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001891 Diag(property->getLocation(),
1892 diag::warn_accessor_property_type_mismatch)
1893 << property->getDeclName()
1894 << SetterMethod->getSelector();
1895 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1896 }
1897 }
1898
1899 // Synthesize getter/setter methods if none exist.
1900 // Find the default getter and if one not found, add one.
1901 // FIXME: The synthesized property we set here is misleading. We almost always
1902 // synthesize these methods unless the user explicitly provided prototypes
1903 // (which is odd, but allowed). Sema should be typechecking that the
1904 // declarations jive in that situation (which it is not currently).
1905 if (!GetterMethod) {
1906 // No instance method of same name as property getter name was found.
1907 // Declare a getter method and add it to the list of methods
1908 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001909 SourceLocation Loc = redeclaredProperty ?
1910 redeclaredProperty->getLocation() :
1911 property->getLocation();
1912
1913 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1914 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001915 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001916 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001917 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001918 (property->getPropertyImplementation() ==
1919 ObjCPropertyDecl::Optional) ?
1920 ObjCMethodDecl::Optional :
1921 ObjCMethodDecl::Required);
1922 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001923
1924 AddPropertyAttrs(*this, GetterMethod, property);
1925
Ted Kremenek49be9e02010-05-18 21:09:07 +00001926 // FIXME: Eventually this shouldn't be needed, as the lexical context
1927 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001928 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001929 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001930 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001931 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1932 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001933
1934 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1935 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001936 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001937
1938 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001939 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1940 Loc));
John McCalle48f3892013-04-04 01:38:37 +00001941
1942 if (getLangOpts().ObjCAutoRefCount)
1943 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001944 } else
1945 // A user declared getter will be synthesize when @synthesize of
1946 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001947 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001948 property->setGetterMethodDecl(GetterMethod);
1949
1950 // Skip setter if property is read-only.
1951 if (!property->isReadOnly()) {
1952 // Find the default setter and if one not found, add one.
1953 if (!SetterMethod) {
1954 // No instance method of same name as property setter name was found.
1955 // Declare a setter method and add it to the list of methods
1956 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001957 SourceLocation Loc = redeclaredProperty ?
1958 redeclaredProperty->getLocation() :
1959 property->getLocation();
1960
1961 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001962 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001963 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001964 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001965 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001966 /*isImplicitlyDeclared=*/true,
1967 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001968 (property->getPropertyImplementation() ==
1969 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001970 ObjCMethodDecl::Optional :
1971 ObjCMethodDecl::Required);
1972
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001973 // Invent the arguments for the setter. We don't bother making a
1974 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00001975 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1976 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001977 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00001978 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001979 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00001980 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001981 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001982 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00001983
1984 AddPropertyAttrs(*this, SetterMethod, property);
1985
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001986 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00001987 // FIXME: Eventually this shouldn't be needed, as the lexical context
1988 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001989 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001990 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001991 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001992 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
1993 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00001994 // It's possible for the user to have set a very odd custom
1995 // setter selector that causes it to have a method family.
1996 if (getLangOpts().ObjCAutoRefCount)
1997 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001998 } else
1999 // A user declared setter will be synthesize when @synthesize of
2000 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002001 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002002 property->setSetterMethodDecl(SetterMethod);
2003 }
2004 // Add any synthesized methods to the global pool. This allows us to
2005 // handle the following, which is supported by GCC (and part of the design).
2006 //
2007 // @interface Foo
2008 // @property double bar;
2009 // @end
2010 //
2011 // void thisIsUnfortunate() {
2012 // id foo;
2013 // double bar = [foo bar];
2014 // }
2015 //
2016 if (GetterMethod)
2017 AddInstanceMethodToGlobalPool(GetterMethod);
2018 if (SetterMethod)
2019 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002020
2021 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2022 if (!CurrentClass) {
2023 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2024 CurrentClass = Cat->getClassInterface();
2025 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2026 CurrentClass = Impl->getClassInterface();
2027 }
2028 if (GetterMethod)
2029 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2030 if (SetterMethod)
2031 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002032}
2033
John McCall48871652010-08-21 09:40:31 +00002034void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002035 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002036 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002037 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002038 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002039 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002040 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002041
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002042 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2043 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2044 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2045 << "readonly" << "readwrite";
2046
2047 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2048 QualType PropertyTy = PropertyDecl->getType();
2049 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002050
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002051 // 'readonly' property with no obvious lifetime.
2052 // its life time will be determined by its backing ivar.
2053 if (getLangOpts().ObjCAutoRefCount &&
2054 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2055 PropertyTy->isObjCRetainableType() &&
2056 !PropertyOwnership)
2057 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002058
2059 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002060 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002061 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2062 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002063 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002064 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002065 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2066 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2067 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002068 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002069 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002070 }
2071
2072 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002073 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2074 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002075 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2076 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002077 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002078 }
Bill Wendling44426052012-12-20 19:22:21 +00002079 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002080 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2081 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002082 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002083 }
Bill Wendling44426052012-12-20 19:22:21 +00002084 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002085 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2086 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002087 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002088 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002089 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002090 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002091 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2092 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002093 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002094 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002095 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002096 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002097 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2098 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002099 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2100 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002101 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002102 }
Bill Wendling44426052012-12-20 19:22:21 +00002103 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002104 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2105 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002106 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002107 }
Bill Wendling44426052012-12-20 19:22:21 +00002108 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002109 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2110 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002111 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002112 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002113 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002114 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002115 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2116 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002117 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002118 }
Bill Wendling44426052012-12-20 19:22:21 +00002119 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2120 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002121 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2122 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002123 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002124 }
Bill Wendling44426052012-12-20 19:22:21 +00002125 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002126 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2127 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002128 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002129 }
Bill Wendling44426052012-12-20 19:22:21 +00002130 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002131 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2132 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002133 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002134 }
2135 }
Bill Wendling44426052012-12-20 19:22:21 +00002136 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2137 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2139 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002140 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002141 }
Bill Wendling44426052012-12-20 19:22:21 +00002142 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2143 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2145 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002146 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002147 }
2148
Bill Wendling44426052012-12-20 19:22:21 +00002149 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2150 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2152 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002153 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002154 }
2155
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002156 // Warn if user supplied no assignment attribute, property is
2157 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002158 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002159 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2160 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2161 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002162 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002163 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002164 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002165 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002166 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002167 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002168 bool isAnyClassTy =
2169 (PropertyTy->isObjCClassType() ||
2170 PropertyTy->isObjCQualifiedClassType());
2171 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2172 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002173 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002174 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002175 else if (propertyInPrimaryClass) {
2176 // Don't issue warning on property with no life time in class
2177 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002178 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002179 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002180 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002181
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002182 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002183 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002184 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002185 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002186 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002187
2188 // FIXME: Implement warning dependent on NSCopying being
2189 // implemented. See also:
2190 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2191 // (please trim this list while you are at it).
2192 }
2193
Bill Wendling44426052012-12-20 19:22:21 +00002194 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2195 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002196 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002197 && PropertyTy->isBlockPointerType())
2198 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002199 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2200 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2201 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002202 PropertyTy->isBlockPointerType())
2203 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002204
Bill Wendling44426052012-12-20 19:22:21 +00002205 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2206 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002207 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2208
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002209}