blob: f613b643ca040ee5337253bb4d4f54b5ca8bcd42 [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(),
Eli Friedmaneaf34142012-10-18 20:14:08 +00001151 VK_RValue, PropertyDiagLoc);
1152 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001153 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001154 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001155 Ivar->getLocation(),
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001156 SelfExpr, true, true);
John McCalldadc5752010-08-24 06:29:42 +00001157 ExprResult Res =
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001158 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedmaneaf34142012-10-18 20:14:08 +00001159 PropertyDiagLoc,
Douglas Gregor222cf0e2010-05-15 00:13:29 +00001160 getterMethod->getResultType(),
1161 /*NRVO=*/false),
Eli Friedmaneaf34142012-10-18 20:14:08 +00001162 PropertyDiagLoc,
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001163 Owned(IvarRefExpr));
1164 if (!Res.isInvalid()) {
1165 Expr *ResExpr = Res.takeAs<Expr>();
1166 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001167 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001168 PIDecl->setGetterCXXConstructor(ResExpr);
1169 }
1170 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001171 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1172 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1173 Diag(getterMethod->getLocation(),
1174 diag::warn_property_getter_owning_mismatch);
1175 Diag(property->getLocation(), diag::note_property_declare);
1176 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001177 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1178 switch (getterMethod->getMethodFamily()) {
1179 case OMF_retain:
1180 case OMF_retainCount:
1181 case OMF_release:
1182 case OMF_autorelease:
1183 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1184 << 1 << getterMethod->getSelector();
1185 break;
1186 default:
1187 break;
1188 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001189 }
1190 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1191 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001192 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1193 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001194 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001195 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001196 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1197 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001198 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedmaneaf34142012-10-18 20:14:08 +00001199 VK_RValue, PropertyDiagLoc);
1200 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001201 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001202 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001203 Ivar->getLocation(),
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001204 SelfExpr, true, true);
1205 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1206 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001207 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001208 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1209 VK_LValue, PropertyDiagLoc);
1210 MarkDeclRefReferenced(rhs);
1211 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001212 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001213 if (property->getPropertyAttributes() &
1214 ObjCPropertyDecl::OBJC_PR_atomic) {
1215 Expr *callExpr = Res.takeAs<Expr>();
1216 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001217 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1218 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001219 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001220 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001221 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001222 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001223 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001224 Diag(FuncDecl->getLocStart(),
1225 diag::note_callee_decl) << FuncDecl;
1226 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001227 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001228 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1229 }
1230 }
1231
Ted Kremenekac597f32010-03-12 00:46:40 +00001232 if (IC) {
1233 if (Synthesize)
1234 if (ObjCPropertyImplDecl *PPIDecl =
1235 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1236 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1237 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1238 << PropertyIvar;
1239 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1240 }
1241
1242 if (ObjCPropertyImplDecl *PPIDecl
1243 = IC->FindPropertyImplDecl(PropertyId)) {
1244 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1245 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001246 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001247 }
1248 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001249 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001250 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001251 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001252 // Diagnose if an ivar was lazily synthesdized due to a previous
1253 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001254 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001255 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001256 ObjCIvarDecl *Ivar = 0;
1257 if (!Synthesize)
1258 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1259 else {
1260 if (PropertyIvar && PropertyIvar != PropertyId)
1261 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1262 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001263 // Issue diagnostics only if Ivar belongs to current class.
1264 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001265 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001266 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1267 << PropertyId;
1268 Ivar->setInvalidDecl();
1269 }
1270 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001271 } else {
1272 if (Synthesize)
1273 if (ObjCPropertyImplDecl *PPIDecl =
1274 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001275 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001276 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1277 << PropertyIvar;
1278 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1279 }
1280
1281 if (ObjCPropertyImplDecl *PPIDecl =
1282 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001283 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001284 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001285 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001286 }
1287 CatImplClass->addPropertyImplementation(PIDecl);
1288 }
1289
John McCall48871652010-08-21 09:40:31 +00001290 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001291}
1292
1293//===----------------------------------------------------------------------===//
1294// Helper methods.
1295//===----------------------------------------------------------------------===//
1296
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001297/// DiagnosePropertyMismatch - Compares two properties for their
1298/// attributes and types and warns on a variety of inconsistencies.
1299///
1300void
1301Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1302 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001303 const IdentifierInfo *inheritedName,
1304 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001305 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001306 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001307 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001308 SuperProperty->getPropertyAttributes();
1309
1310 // We allow readonly properties without an explicit ownership
1311 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1312 // to be overridden by a property with any explicit ownership in the subclass.
1313 if (!OverridingProtocolProperty &&
1314 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1315 ;
1316 else {
1317 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1318 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1319 Diag(Property->getLocation(), diag::warn_readonly_property)
1320 << Property->getDeclName() << inheritedName;
1321 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1322 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001323 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001324 << Property->getDeclName() << "copy" << inheritedName;
1325 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1326 unsigned CAttrRetain =
1327 (CAttr &
1328 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1329 unsigned SAttrRetain =
1330 (SAttr &
1331 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1332 bool CStrong = (CAttrRetain != 0);
1333 bool SStrong = (SAttrRetain != 0);
1334 if (CStrong != SStrong)
1335 Diag(Property->getLocation(), diag::warn_property_attribute)
1336 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1337 }
John McCall31168b02011-06-15 23:02:42 +00001338 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001339
1340 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001341 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001342 Diag(Property->getLocation(), diag::warn_property_attribute)
1343 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001344 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1345 }
1346 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001347 Diag(Property->getLocation(), diag::warn_property_attribute)
1348 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001349 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1350 }
1351 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001352 Diag(Property->getLocation(), diag::warn_property_attribute)
1353 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001354 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1355 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001356
1357 QualType LHSType =
1358 Context.getCanonicalType(SuperProperty->getType());
1359 QualType RHSType =
1360 Context.getCanonicalType(Property->getType());
1361
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001362 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001363 // Do cases not handled in above.
1364 // FIXME. For future support of covariant property types, revisit this.
1365 bool IncompatibleObjC = false;
1366 QualType ConvertedType;
1367 if (!isObjCPointerConversion(RHSType, LHSType,
1368 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001369 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001370 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1371 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001372 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1373 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001374 }
1375}
1376
1377bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1378 ObjCMethodDecl *GetterMethod,
1379 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001380 if (!GetterMethod)
1381 return false;
1382 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1383 QualType PropertyIvarType = property->getType().getNonReferenceType();
1384 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1385 if (!compat) {
1386 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1387 isa<ObjCObjectPointerType>(GetterType))
1388 compat =
1389 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001390 GetterType->getAs<ObjCObjectPointerType>(),
1391 PropertyIvarType->getAs<ObjCObjectPointerType>());
1392 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001393 != Compatible) {
1394 Diag(Loc, diag::error_property_accessor_type)
1395 << property->getDeclName() << PropertyIvarType
1396 << GetterMethod->getSelector() << GetterType;
1397 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1398 return true;
1399 } else {
1400 compat = true;
1401 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1402 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1403 if (lhsType != rhsType && lhsType->isArithmeticType())
1404 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001405 }
1406 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001407
1408 if (!compat) {
1409 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1410 << property->getDeclName()
1411 << GetterMethod->getSelector();
1412 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1413 return true;
1414 }
1415
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001416 return false;
1417}
1418
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001419/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001420/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001421void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001422 ObjCContainerDecl::PropertyMap &PropMap,
1423 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001424 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1425 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1426 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001427 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001428 PropMap[Prop->getIdentifier()] = Prop;
1429 }
1430 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001431 for (ObjCInterfaceDecl::all_protocol_iterator
1432 PI = IDecl->all_referenced_protocol_begin(),
1433 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001434 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001435 }
1436 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1437 if (!CATDecl->IsClassExtension())
1438 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1439 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001440 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001441 PropMap[Prop->getIdentifier()] = Prop;
1442 }
1443 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001444 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001445 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001446 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001447 }
1448 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1449 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1450 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001451 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001452 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1453 // Exclude property for protocols which conform to class's super-class,
1454 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001455 if (!PropertyFromSuper ||
1456 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001457 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1458 if (!PropEntry)
1459 PropEntry = Prop;
1460 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001461 }
1462 // scan through protocol's protocols.
1463 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1464 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001465 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001466 }
1467}
1468
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001469/// CollectSuperClassPropertyImplementations - This routine collects list of
1470/// properties to be implemented in super class(s) and also coming from their
1471/// conforming protocols.
1472static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001473 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001474 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001475 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001476 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001477 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001478 SDecl = SDecl->getSuperClass();
1479 }
1480 }
1481}
1482
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001483/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1484/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1485/// declared in class 'IFace'.
1486bool
1487Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1488 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1489 if (!IV->getSynthesize())
1490 return false;
1491 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1492 Method->isInstanceMethod());
1493 if (!IMD || !IMD->isPropertyAccessor())
1494 return false;
1495
1496 // look up a property declaration whose one of its accessors is implemented
1497 // by this method.
1498 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1499 E = IFace->prop_end(); P != E; ++P) {
1500 ObjCPropertyDecl *property = *P;
1501 if ((property->getGetterName() == IMD->getSelector() ||
1502 property->getSetterName() == IMD->getSelector()) &&
1503 (property->getPropertyIvarDecl() == IV))
1504 return true;
1505 }
1506 return false;
1507}
1508
1509
James Dennett2a4d13c2012-06-15 07:13:21 +00001510/// \brief Default synthesizes all properties which must be synthesized
1511/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001512void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1513 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001514
Anna Zaks673d76b2012-10-18 19:17:53 +00001515 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001516 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1517 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001518 if (PropMap.empty())
1519 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001520 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001521 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1522
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001523 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1524 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001525 // Is there a matching property synthesize/dynamic?
1526 if (Prop->isInvalidDecl() ||
1527 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1528 continue;
1529 // Property may have been synthesized by user.
1530 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1531 continue;
1532 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1533 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1534 continue;
1535 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1536 continue;
1537 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001538 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001539 if (SuperPropMap[Prop->getIdentifier()]) {
1540 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1541 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1542 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001543 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001544 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1545 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001546 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001547 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001548 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1549 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001550 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001551 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001552 if (ObjCPropertyImplDecl *PID =
1553 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1554 if (PID->getPropertyDecl() != Prop) {
1555 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001556 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001557 if (!PID->getLocation().isInvalid())
1558 Diag(PID->getLocation(), diag::note_property_synthesize);
1559 }
1560 continue;
1561 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001562 if (ObjCProtocolDecl *Proto =
1563 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001564 // We won't auto-synthesize properties declared in protocols.
1565 Diag(IMPDecl->getLocation(),
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001566 diag::warn_auto_synthesizing_protocol_property)
1567 << Prop << Proto;
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001568 Diag(Prop->getLocation(), diag::note_property_declare);
1569 continue;
1570 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001571
1572 // We use invalid SourceLocations for the synthesized ivars since they
1573 // aren't really synthesized at a particular location; they just exist.
1574 // Saying that they are located at the @implementation isn't really going
1575 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001576 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1577 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1578 true,
1579 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001580 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001581 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001582 if (PIDecl) {
1583 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001584 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001585 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001586 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001587}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001588
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001589void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001590 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001591 return;
1592 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1593 if (!IC)
1594 return;
1595 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001596 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001597 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001598}
1599
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001600void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001601 ObjCContainerDecl *CDecl) {
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001602 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1603 ObjCInterfaceDecl *IDecl;
1604 // Gather properties which need not be implemented in this class
1605 // or category.
1606 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1607 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1608 // For categories, no need to implement properties declared in
1609 // its primary class (and its super classes) if property is
1610 // declared in one of those containers.
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001611 if ((IDecl = C->getClassInterface())) {
1612 ObjCInterfaceDecl::PropertyDeclOrder PO;
1613 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1614 }
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001615 }
1616 if (IDecl)
1617 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001618
Anna Zaks673d76b2012-10-18 19:17:53 +00001619 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001620 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001621 if (PropMap.empty())
1622 return;
1623
1624 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1625 for (ObjCImplDecl::propimpl_iterator
1626 I = IMPDecl->propimpl_begin(),
1627 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001628 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001629
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001630 SelectorSet InsMap;
1631 // Collect property accessors implemented in current implementation.
1632 for (ObjCImplementationDecl::instmeth_iterator
1633 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1634 InsMap.insert((*I)->getSelector());
1635
1636 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1637 ObjCInterfaceDecl *PrimaryClass = 0;
1638 if (C && !C->IsClassExtension())
1639 if ((PrimaryClass = C->getClassInterface()))
1640 // Report unimplemented properties in the category as well.
1641 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1642 // When reporting on missing setter/getters, do not report when
1643 // setter/getter is implemented in category's primary class
1644 // implementation.
1645 for (ObjCImplementationDecl::instmeth_iterator
1646 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1647 InsMap.insert((*I)->getSelector());
1648 }
1649
Anna Zaks673d76b2012-10-18 19:17:53 +00001650 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001651 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1652 ObjCPropertyDecl *Prop = P->second;
1653 // Is there a matching propery synthesize/dynamic?
1654 if (Prop->isInvalidDecl() ||
1655 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001656 PropImplMap.count(Prop) ||
1657 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001658 continue;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001659 // When reporting on missing property getter implementation in
1660 // categories, do not report when they are declared in primary class,
1661 // class's protocol, or one of it super classes. This is because,
1662 // the class is going to implement them.
1663 if (!InsMap.count(Prop->getGetterName()) &&
1664 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001665 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001666 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001667 isa<ObjCCategoryDecl>(CDecl) ?
1668 diag::warn_setter_getter_impl_required_in_category :
1669 diag::warn_setter_getter_impl_required)
1670 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001671 Diag(Prop->getLocation(),
1672 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001673 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001674 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001675 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001676 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1677
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001678 }
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001679 // When reporting on missing property setter implementation in
1680 // categories, do not report when they are declared in primary class,
1681 // class's protocol, or one of it super classes. This is because,
1682 // the class is going to implement them.
1683 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1684 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001685 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001686 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001687 isa<ObjCCategoryDecl>(CDecl) ?
1688 diag::warn_setter_getter_impl_required_in_category :
1689 diag::warn_setter_getter_impl_required)
1690 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001691 Diag(Prop->getLocation(),
1692 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001693 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001694 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001695 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001696 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001697 }
1698 }
1699}
1700
1701void
1702Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1703 ObjCContainerDecl* IDecl) {
1704 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001705 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001706 return;
1707 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1708 E = IDecl->prop_end();
1709 I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00001710 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001711 ObjCMethodDecl *GetterMethod = 0;
1712 ObjCMethodDecl *SetterMethod = 0;
1713 bool LookedUpGetterSetter = false;
1714
Bill Wendling44426052012-12-20 19:22:21 +00001715 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001716 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001717
John McCall43192862011-09-13 18:31:23 +00001718 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1719 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001720 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1721 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1722 LookedUpGetterSetter = true;
1723 if (GetterMethod) {
1724 Diag(GetterMethod->getLocation(),
1725 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001726 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001727 Diag(Property->getLocation(), diag::note_property_declare);
1728 }
1729 if (SetterMethod) {
1730 Diag(SetterMethod->getLocation(),
1731 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001732 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001733 Diag(Property->getLocation(), diag::note_property_declare);
1734 }
1735 }
1736
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001737 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001738 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1739 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001740 continue;
1741 if (const ObjCPropertyImplDecl *PIDecl
1742 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1743 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1744 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001745 if (!LookedUpGetterSetter) {
1746 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1747 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1748 LookedUpGetterSetter = true;
1749 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001750 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1751 SourceLocation MethodLoc =
1752 (GetterMethod ? GetterMethod->getLocation()
1753 : SetterMethod->getLocation());
1754 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001755 << Property->getIdentifier() << (GetterMethod != 0)
1756 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001757 // fixit stuff.
1758 if (!AttributesAsWritten) {
1759 if (Property->getLParenLoc().isValid()) {
1760 // @property () ... case.
1761 SourceRange PropSourceRange(Property->getAtLoc(),
1762 Property->getLParenLoc());
1763 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1764 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1765 }
1766 else {
1767 //@property id etc.
1768 SourceLocation endLoc =
1769 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1770 endLoc = endLoc.getLocWithOffset(-1);
1771 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1772 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1773 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1774 }
1775 }
1776 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1777 // @property () ... case.
1778 SourceLocation endLoc = Property->getLParenLoc();
1779 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1780 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1781 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1782 }
1783 else
1784 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001785 Diag(Property->getLocation(), diag::note_property_declare);
1786 }
1787 }
1788 }
1789}
1790
John McCall31168b02011-06-15 23:02:42 +00001791void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001792 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001793 return;
1794
1795 for (ObjCImplementationDecl::propimpl_iterator
1796 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001797 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001798 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001799 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1800 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001801 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1802 if (!method)
1803 continue;
1804 ObjCMethodFamily family = method->getMethodFamily();
1805 if (family == OMF_alloc || family == OMF_copy ||
1806 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001807 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001808 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001809 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001810 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001811 }
1812 }
1813 }
1814}
1815
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001816void Sema::DiagnoseMissingDesignatedInitOverrides(
1817 const ObjCImplementationDecl *ImplD,
1818 const ObjCInterfaceDecl *IFD) {
1819 assert(IFD->hasDesignatedInitializers());
1820 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1821 if (!SuperD)
1822 return;
1823
1824 SelectorSet InitSelSet;
1825 for (ObjCImplementationDecl::instmeth_iterator
1826 I = ImplD->instmeth_begin(), E = ImplD->instmeth_end(); I!=E; ++I)
1827 if ((*I)->getMethodFamily() == OMF_init)
1828 InitSelSet.insert((*I)->getSelector());
1829
1830 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1831 SuperD->getDesignatedInitializers(DesignatedInits);
1832 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1833 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1834 const ObjCMethodDecl *MD = *I;
1835 if (!InitSelSet.count(MD->getSelector())) {
1836 Diag(ImplD->getLocation(),
1837 diag::warn_objc_implementation_missing_designated_init_override)
1838 << MD->getSelector();
1839 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1840 }
1841 }
1842}
1843
John McCallad31b5f2010-11-10 07:01:40 +00001844/// AddPropertyAttrs - Propagates attributes from a property to the
1845/// implicitly-declared getter or setter for that property.
1846static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1847 ObjCPropertyDecl *Property) {
1848 // Should we just clone all attributes over?
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001849 for (Decl::attr_iterator A = Property->attr_begin(),
1850 AEnd = Property->attr_end();
1851 A != AEnd; ++A) {
1852 if (isa<DeprecatedAttr>(*A) ||
1853 isa<UnavailableAttr>(*A) ||
1854 isa<AvailabilityAttr>(*A))
1855 PropertyMethod->addAttr((*A)->clone(S.Context));
1856 }
John McCallad31b5f2010-11-10 07:01:40 +00001857}
1858
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001859/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1860/// have the property type and issue diagnostics if they don't.
1861/// Also synthesize a getter/setter method if none exist (and update the
1862/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1863/// methods is the "right" thing to do.
1864void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001865 ObjCContainerDecl *CD,
1866 ObjCPropertyDecl *redeclaredProperty,
1867 ObjCContainerDecl *lexicalDC) {
1868
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001869 ObjCMethodDecl *GetterMethod, *SetterMethod;
1870
1871 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1872 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1873 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1874 property->getLocation());
1875
1876 if (SetterMethod) {
1877 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1878 property->getPropertyAttributes();
1879 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1880 Context.getCanonicalType(SetterMethod->getResultType()) !=
1881 Context.VoidTy)
1882 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1883 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001884 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001885 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1886 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001887 Diag(property->getLocation(),
1888 diag::warn_accessor_property_type_mismatch)
1889 << property->getDeclName()
1890 << SetterMethod->getSelector();
1891 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1892 }
1893 }
1894
1895 // Synthesize getter/setter methods if none exist.
1896 // Find the default getter and if one not found, add one.
1897 // FIXME: The synthesized property we set here is misleading. We almost always
1898 // synthesize these methods unless the user explicitly provided prototypes
1899 // (which is odd, but allowed). Sema should be typechecking that the
1900 // declarations jive in that situation (which it is not currently).
1901 if (!GetterMethod) {
1902 // No instance method of same name as property getter name was found.
1903 // Declare a getter method and add it to the list of methods
1904 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001905 SourceLocation Loc = redeclaredProperty ?
1906 redeclaredProperty->getLocation() :
1907 property->getLocation();
1908
1909 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1910 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001911 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001912 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001913 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001914 (property->getPropertyImplementation() ==
1915 ObjCPropertyDecl::Optional) ?
1916 ObjCMethodDecl::Optional :
1917 ObjCMethodDecl::Required);
1918 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001919
1920 AddPropertyAttrs(*this, GetterMethod, property);
1921
Ted Kremenek49be9e02010-05-18 21:09:07 +00001922 // FIXME: Eventually this shouldn't be needed, as the lexical context
1923 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001924 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001925 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001926 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1927 GetterMethod->addAttr(
1928 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001929
1930 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1931 GetterMethod->addAttr(
1932 ::new (Context) ObjCReturnsInnerPointerAttr(Loc, Context));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001933
1934 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
1935 GetterMethod->addAttr(::new (Context) SectionAttr(Loc,
1936 Context, SA->getName()));
John McCalle48f3892013-04-04 01:38:37 +00001937
1938 if (getLangOpts().ObjCAutoRefCount)
1939 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001940 } else
1941 // A user declared getter will be synthesize when @synthesize of
1942 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001943 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001944 property->setGetterMethodDecl(GetterMethod);
1945
1946 // Skip setter if property is read-only.
1947 if (!property->isReadOnly()) {
1948 // Find the default setter and if one not found, add one.
1949 if (!SetterMethod) {
1950 // No instance method of same name as property setter name was found.
1951 // Declare a setter method and add it to the list of methods
1952 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001953 SourceLocation Loc = redeclaredProperty ?
1954 redeclaredProperty->getLocation() :
1955 property->getLocation();
1956
1957 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001958 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001959 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001960 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001961 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001962 /*isImplicitlyDeclared=*/true,
1963 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001964 (property->getPropertyImplementation() ==
1965 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001966 ObjCMethodDecl::Optional :
1967 ObjCMethodDecl::Required);
1968
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001969 // Invent the arguments for the setter. We don't bother making a
1970 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00001971 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1972 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001973 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00001974 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001975 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00001976 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001977 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001978 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00001979
1980 AddPropertyAttrs(*this, SetterMethod, property);
1981
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001982 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00001983 // FIXME: Eventually this shouldn't be needed, as the lexical context
1984 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001985 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001986 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001987 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
1988 SetterMethod->addAttr(::new (Context) SectionAttr(Loc,
1989 Context, SA->getName()));
John McCalle48f3892013-04-04 01:38:37 +00001990 // It's possible for the user to have set a very odd custom
1991 // setter selector that causes it to have a method family.
1992 if (getLangOpts().ObjCAutoRefCount)
1993 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001994 } else
1995 // A user declared setter will be synthesize when @synthesize of
1996 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001997 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001998 property->setSetterMethodDecl(SetterMethod);
1999 }
2000 // Add any synthesized methods to the global pool. This allows us to
2001 // handle the following, which is supported by GCC (and part of the design).
2002 //
2003 // @interface Foo
2004 // @property double bar;
2005 // @end
2006 //
2007 // void thisIsUnfortunate() {
2008 // id foo;
2009 // double bar = [foo bar];
2010 // }
2011 //
2012 if (GetterMethod)
2013 AddInstanceMethodToGlobalPool(GetterMethod);
2014 if (SetterMethod)
2015 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002016
2017 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2018 if (!CurrentClass) {
2019 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2020 CurrentClass = Cat->getClassInterface();
2021 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2022 CurrentClass = Impl->getClassInterface();
2023 }
2024 if (GetterMethod)
2025 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2026 if (SetterMethod)
2027 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002028}
2029
John McCall48871652010-08-21 09:40:31 +00002030void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002031 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002032 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002033 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002034 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002035 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002036 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002037
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002038 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2039 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2040 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2041 << "readonly" << "readwrite";
2042
2043 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2044 QualType PropertyTy = PropertyDecl->getType();
2045 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002046
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002047 // 'readonly' property with no obvious lifetime.
2048 // its life time will be determined by its backing ivar.
2049 if (getLangOpts().ObjCAutoRefCount &&
2050 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2051 PropertyTy->isObjCRetainableType() &&
2052 !PropertyOwnership)
2053 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002054
2055 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002056 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002057 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2058 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002059 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002060 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002061 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2062 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2063 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002064 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002065 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002066 }
2067
2068 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002069 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2070 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002071 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2072 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002073 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002074 }
Bill Wendling44426052012-12-20 19:22:21 +00002075 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002076 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2077 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002078 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002079 }
Bill Wendling44426052012-12-20 19:22:21 +00002080 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002081 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2082 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002083 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002084 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002085 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002086 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002087 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2088 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002089 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002090 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002091 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002092 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002093 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2094 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002095 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2096 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002097 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002098 }
Bill Wendling44426052012-12-20 19:22:21 +00002099 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002100 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2101 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002102 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002103 }
Bill Wendling44426052012-12-20 19:22:21 +00002104 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002105 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2106 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002107 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002108 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002109 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002110 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002111 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2112 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002113 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002114 }
Bill Wendling44426052012-12-20 19:22:21 +00002115 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2116 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002117 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2118 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002119 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002120 }
Bill Wendling44426052012-12-20 19:22:21 +00002121 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002124 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002125 }
Bill Wendling44426052012-12-20 19:22:21 +00002126 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002127 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2128 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002129 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002130 }
2131 }
Bill Wendling44426052012-12-20 19:22:21 +00002132 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2133 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002134 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2135 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002136 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002137 }
Bill Wendling44426052012-12-20 19:22:21 +00002138 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2139 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002140 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2141 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002142 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002143 }
2144
Bill Wendling44426052012-12-20 19:22:21 +00002145 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2146 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002147 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2148 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002149 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002150 }
2151
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002152 // Warn if user supplied no assignment attribute, property is
2153 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002154 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002155 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2156 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2157 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002158 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002159 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002160 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002161 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002162 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002163 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002164 bool isAnyClassTy =
2165 (PropertyTy->isObjCClassType() ||
2166 PropertyTy->isObjCQualifiedClassType());
2167 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2168 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002169 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002170 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002171 else if (propertyInPrimaryClass) {
2172 // Don't issue warning on property with no life time in class
2173 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002174 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002175 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002176 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002177
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002178 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002179 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002180 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002181 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002182 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002183
2184 // FIXME: Implement warning dependent on NSCopying being
2185 // implemented. See also:
2186 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2187 // (please trim this list while you are at it).
2188 }
2189
Bill Wendling44426052012-12-20 19:22:21 +00002190 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2191 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002192 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002193 && PropertyTy->isBlockPointerType())
2194 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002195 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2196 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2197 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002198 PropertyTy->isBlockPointerType())
2199 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002200
Bill Wendling44426052012-12-20 19:22:21 +00002201 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2202 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002203 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2204
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002205}