blob: 824a249084c579c118f4d5a02330cabe70bcc7f6 [file] [log] [blame]
Ted Kremenek9d64c152010-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 McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenek28685ab2010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall265941b2011-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 McCalld64c2eb2012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall265941b2011-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 McCallf85e1932011-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 McCall265941b2011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-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 McCallf85e1932011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000102 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahaniandad633b2012-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 Gregorb892d702013-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])) {
129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier());
Fariborz Jahanianb501aeb2013-03-25 23:59:42 +0000130 S.mergeDeclAttributes(Prop, ProtoProp, Sema::AMK_Override);
Douglas Gregorb892d702013-01-21 19:42:21 +0000131 return;
132 }
133 }
134
135 // Check this property against any protocols we inherit.
136 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
137 PEnd = Proto->protocol_end();
138 P != PEnd; ++P) {
139 CheckPropertyAgainstProtocol(S, Prop, *P, Known);
140 }
141}
142
John McCalld226f652010-08-21 09:40:31 +0000143Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000144 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000145 FieldDeclarator &FD,
146 ObjCDeclSpec &ODS,
147 Selector GetterSel,
148 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000149 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000150 tok::ObjCKeywordKind MethodImplKind,
151 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000152 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000153 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
154 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000155 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000156
Bill Wendlingad017fa2012-12-20 19:22:21 +0000157 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000158 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000159 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000160 // property is defaulted to 'assign' if it is readwrite and is
161 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000162 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000163 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000164 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
165 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
166 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
167 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
168 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000169
Douglas Gregoraabd0942013-01-21 19:05:22 +0000170 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000171 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000172 ObjCPropertyDecl *Res = 0;
173 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000174 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000175 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000176 FD, GetterSel, SetterSel,
177 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000178 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000179 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000180 isOverridingProperty, TSI,
181 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000182 if (!Res)
183 return 0;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000184 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000185 }
186
187 if (!Res) {
188 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
189 GetterSel, SetterSel, isAssign, isReadWrite,
190 Attributes, ODS.getPropertyAttributes(),
191 TSI, MethodImplKind);
192 if (lexicalDC)
193 Res->setLexicalDeclContext(lexicalDC);
194 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000195
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000196 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000197 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000198 (isa<ObjCInterfaceDecl>(ClassDecl) ||
199 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000200
David Blaikie4e4d0842012-03-11 07:00:24 +0000201 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000202 checkARCPropertyDecl(*this, Res);
203
Douglas Gregorb892d702013-01-21 19:42:21 +0000204 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000205 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000206 // For a class, compare the property against a property in our superclass.
207 bool FoundInSuper = false;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000208 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
209 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000210 for (unsigned I = 0, N = R.size(); I != N; ++I) {
211 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000212 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier());
Fariborz Jahanianb501aeb2013-03-25 23:59:42 +0000213 mergeDeclAttributes(Res, SuperProp, AMK_Override);
Douglas Gregorb892d702013-01-21 19:42:21 +0000214 FoundInSuper = true;
215 break;
216 }
217 }
218 }
219
220 if (FoundInSuper) {
221 // Also compare the property against a property in our protocols.
222 for (ObjCInterfaceDecl::protocol_iterator P = IFace->protocol_begin(),
223 PEnd = IFace->protocol_end();
224 P != PEnd; ++P) {
225 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
226 }
227 } else {
228 // Slower path: look in all protocols we referenced.
229 for (ObjCInterfaceDecl::all_protocol_iterator
230 P = IFace->all_referenced_protocol_begin(),
231 PEnd = IFace->all_referenced_protocol_end();
232 P != PEnd; ++P) {
233 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
234 }
235 }
236 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
237 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
238 PEnd = Cat->protocol_end();
239 P != PEnd; ++P) {
240 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
241 }
242 } else {
243 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
244 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
245 PEnd = Proto->protocol_end();
246 P != PEnd; ++P) {
247 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000248 }
249 }
250
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000251 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000252 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000253}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000254
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000255static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000256makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000257 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000280 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000281 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
282
283 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
284}
285
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000286static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000287 SourceLocation LParenLoc, SourceLocation &Loc) {
288 if (LParenLoc.isMacroID())
289 return false;
290
291 SourceManager &SM = Context.getSourceManager();
292 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
293 // Try to load the file buffer.
294 bool invalidTemp = false;
295 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
296 if (invalidTemp)
297 return false;
298 const char *tokenBegin = file.data() + locInfo.second;
299
300 // Lex from the start of the given location.
301 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
302 Context.getLangOpts(),
303 file.begin(), tokenBegin, file.end());
304 Token Tok;
305 do {
306 lexer.LexFromRawLexer(Tok);
307 if (Tok.is(tok::raw_identifier) &&
308 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
309 Loc = Tok.getLocation();
310 return true;
311 }
312 } while (Tok.isNot(tok::r_paren));
313 return false;
314
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000315}
316
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000317static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000318 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
319 ObjCPropertyDecl::OBJC_PR_retain |
320 ObjCPropertyDecl::OBJC_PR_copy |
321 ObjCPropertyDecl::OBJC_PR_weak |
322 ObjCPropertyDecl::OBJC_PR_strong |
323 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
324}
325
Douglas Gregoraabd0942013-01-21 19:05:22 +0000326ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000327Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000328 SourceLocation AtLoc,
329 SourceLocation LParenLoc,
330 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000331 Selector GetterSel, Selector SetterSel,
332 const bool isAssign,
333 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000334 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000335 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000337 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000338 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000339 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000340 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000341 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000342 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000343 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
344
Douglas Gregord3297242013-01-16 23:00:23 +0000345 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000346 // Check for duplicate declaration of this property in current and
347 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000348 for (ObjCInterfaceDecl::known_extensions_iterator
349 Ext = CCPrimary->known_extensions_begin(),
350 ExtEnd = CCPrimary->known_extensions_end();
351 Ext != ExtEnd; ++Ext) {
352 if (ObjCPropertyDecl *prevDecl
353 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000354 Diag(AtLoc, diag::err_duplicate_property);
355 Diag(prevDecl->getLocation(), diag::note_property_declare);
356 return 0;
357 }
358 }
Douglas Gregord3297242013-01-16 23:00:23 +0000359 }
360
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000361 // Create a new ObjCPropertyDecl with the DeclContext being
362 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000363 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000364 ObjCPropertyDecl *PDecl =
365 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000366 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000367 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000368 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000371 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000373 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
375 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
376 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000377 // Set setter/getter selector name. Needed later.
378 PDecl->setGetterName(GetterSel);
379 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000380 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000381 DC->addDecl(PDecl);
382
383 // We need to look in the @interface to see if the @property was
384 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000385 if (!CCPrimary) {
386 Diag(CDecl->getLocation(), diag::err_continuation_class);
387 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000388 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000389 }
390
391 // Find the property in continuation class's primary class only.
392 ObjCPropertyDecl *PIDecl =
393 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
394
395 if (!PIDecl) {
396 // No matching property found in the primary class. Just fall thru
397 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000398 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000399 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000400 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000401 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000402
403 // A case of continuation class adding a new property in the class. This
404 // is not what it was meant for. However, gcc supports it and so should we.
405 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000406 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000407 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000408 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
409 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000410 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000411 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
412 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000413 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000414 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
415 bool IncompatibleObjC = false;
416 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000417 // Relax the strict type matching for property type in continuation class.
418 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000419 // as it narrows the object type in its primary class property. Note that
420 // this conversion is safe only because the wider type is for a 'readonly'
421 // property in primary class and 'narrowed' type for a 'readwrite' property
422 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000423 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
424 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
425 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
426 ConvertedType, IncompatibleObjC))
427 || IncompatibleObjC) {
428 Diag(AtLoc,
429 diag::err_type_mismatch_continuation_class) << PDecl->getType();
430 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000431 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000432 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000433 }
434
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000435 // The property 'PIDecl's readonly attribute will be over-ridden
436 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000437 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000438 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000439 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000440 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000441 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000442 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
443 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000444 Diag(AtLoc, diag::warn_property_attr_mismatch);
445 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000446 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000447 DeclContext *DC = cast<DeclContext>(CCPrimary);
448 if (!ObjCPropertyDecl::findPropertyDecl(DC,
449 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000450 // Protocol is not in the primary class. Must build one for it.
451 ObjCDeclSpec ProtocolPropertyODS;
452 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
453 // and ObjCPropertyDecl::PropertyAttributeKind have identical
454 // values. Should consolidate both into one enum type.
455 ProtocolPropertyODS.
456 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
457 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000458 // Must re-establish the context from class extension to primary
459 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000460 ContextRAII SavedContext(*this, CCPrimary);
461
John McCalld226f652010-08-21 09:40:31 +0000462 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000463 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000464 PIDecl->getGetterName(),
465 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000466 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000467 MethodImplKind,
468 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000469 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000470 }
471 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000472 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000473 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000474 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000475 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000476 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000477 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
478 PIDecl->setSetterName(SetterSel);
479 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000480 // Tailor the diagnostics for the common case where a readwrite
481 // property is declared both in the @interface and the continuation.
482 // This is a common error where the user often intended the original
483 // declaration to be readonly.
484 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000485 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000486 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
487 ? diag::err_use_continuation_class_redeclaration_readwrite
488 : diag::err_use_continuation_class;
489 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000490 << CCPrimary->getDeclName();
491 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000492 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000493 }
494 *isOverridingProperty = true;
495 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000496 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000497 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
498 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000499 if (ASTMutationListener *L = Context.getASTMutationListener())
500 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000501 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000502}
503
504ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
505 ObjCContainerDecl *CDecl,
506 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000507 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000508 FieldDeclarator &FD,
509 Selector GetterSel,
510 Selector SetterSel,
511 const bool isAssign,
512 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000513 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000514 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000515 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000516 tok::ObjCKeywordKind MethodImplKind,
517 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000518 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000519 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000520
521 // Issue a warning if property is 'assign' as default and its object, which is
522 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000523 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000524 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000525 if (const ObjCObjectPointerType *ObjPtrTy =
526 T->getAs<ObjCObjectPointerType>()) {
527 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
528 if (IDecl)
529 if (ObjCProtocolDecl* PNSCopying =
530 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
531 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
532 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000533 }
John McCallc12c5bb2010-05-15 11:32:37 +0000534 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
536
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000537 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
539 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000540 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000541
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000542 if (ObjCPropertyDecl *prevDecl =
543 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000544 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000545 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000546 PDecl->setInvalidDecl();
547 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000548 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000549 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000550 if (lexicalDC)
551 PDecl->setLexicalDeclContext(lexicalDC);
552 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000553
554 if (T->isArrayType() || T->isFunctionType()) {
555 Diag(AtLoc, diag::err_property_type) << T;
556 PDecl->setInvalidDecl();
557 }
558
559 ProcessDeclAttributes(S, PDecl, FD.D);
560
561 // Regardless of setter/getter attribute, we save the default getter/setter
562 // selector names in anticipation of declaration of setter/getter methods.
563 PDecl->setGetterName(GetterSel);
564 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000565 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000566 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000567
Bill Wendlingad017fa2012-12-20 19:22:21 +0000568 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
570
Bill Wendlingad017fa2012-12-20 19:22:21 +0000571 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000572 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
573
Bill Wendlingad017fa2012-12-20 19:22:21 +0000574 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
576
577 if (isReadWrite)
578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
579
Bill Wendlingad017fa2012-12-20 19:22:21 +0000580 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
582
Bill Wendlingad017fa2012-12-20 19:22:21 +0000583 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
585
Bill Wendlingad017fa2012-12-20 19:22:21 +0000586 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
588
Bill Wendlingad017fa2012-12-20 19:22:21 +0000589 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
591
Bill Wendlingad017fa2012-12-20 19:22:21 +0000592 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000593 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
594
Ted Kremenek28685ab2010-03-12 00:46:40 +0000595 if (isAssign)
596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
597
John McCall265941b2011-09-13 18:31:23 +0000598 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000601 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000603
John McCallf85e1932011-06-15 23:02:42 +0000604 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
607 if (isAssign)
608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
609
Ted Kremenek28685ab2010-03-12 00:46:40 +0000610 if (MethodImplKind == tok::objc_required)
611 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
612 else if (MethodImplKind == tok::objc_optional)
613 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000614
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000615 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000616}
617
John McCallf85e1932011-06-15 23:02:42 +0000618static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
619 ObjCPropertyDecl *property,
620 ObjCIvarDecl *ivar) {
621 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
622
John McCallf85e1932011-06-15 23:02:42 +0000623 QualType ivarType = ivar->getType();
624 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000625
John McCall265941b2011-09-13 18:31:23 +0000626 // The lifetime implied by the property's attributes.
627 Qualifiers::ObjCLifetime propertyLifetime =
628 getImpliedARCOwnership(property->getPropertyAttributes(),
629 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000630
John McCall265941b2011-09-13 18:31:23 +0000631 // We're fine if they match.
632 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000633
John McCall265941b2011-09-13 18:31:23 +0000634 // These aren't valid lifetimes for object ivars; don't diagnose twice.
635 if (ivarLifetime == Qualifiers::OCL_None ||
636 ivarLifetime == Qualifiers::OCL_Autoreleasing)
637 return;
John McCallf85e1932011-06-15 23:02:42 +0000638
John McCalld64c2eb2012-08-20 23:36:59 +0000639 // If the ivar is private, and it's implicitly __unsafe_unretained
640 // becaues of its type, then pretend it was actually implicitly
641 // __strong. This is only sound because we're processing the
642 // property implementation before parsing any method bodies.
643 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
644 propertyLifetime == Qualifiers::OCL_Strong &&
645 ivar->getAccessControl() == ObjCIvarDecl::Private) {
646 SplitQualType split = ivarType.split();
647 if (split.Quals.hasObjCLifetime()) {
648 assert(ivarType->isObjCARCImplicitlyUnretainedType());
649 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
650 ivarType = S.Context.getQualifiedType(split);
651 ivar->setType(ivarType);
652 return;
653 }
654 }
655
John McCall265941b2011-09-13 18:31:23 +0000656 switch (propertyLifetime) {
657 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000658 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000659 << property->getDeclName()
660 << ivar->getDeclName()
661 << ivarLifetime;
662 break;
John McCallf85e1932011-06-15 23:02:42 +0000663
John McCall265941b2011-09-13 18:31:23 +0000664 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000665 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000666 << property->getDeclName()
667 << ivar->getDeclName();
668 break;
John McCallf85e1932011-06-15 23:02:42 +0000669
John McCall265941b2011-09-13 18:31:23 +0000670 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000671 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000672 << property->getDeclName()
673 << ivar->getDeclName()
674 << ((property->getPropertyAttributesAsWritten()
675 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
676 break;
John McCallf85e1932011-06-15 23:02:42 +0000677
John McCall265941b2011-09-13 18:31:23 +0000678 case Qualifiers::OCL_Autoreleasing:
679 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000680
John McCall265941b2011-09-13 18:31:23 +0000681 case Qualifiers::OCL_None:
682 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000683 return;
684 }
685
686 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000687 if (propertyImplLoc.isValid())
688 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000689}
690
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000691/// setImpliedPropertyAttributeForReadOnlyProperty -
692/// This routine evaludates life-time attributes for a 'readonly'
693/// property with no known lifetime of its own, using backing
694/// 'ivar's attribute, if any. If no backing 'ivar', property's
695/// life-time is assumed 'strong'.
696static void setImpliedPropertyAttributeForReadOnlyProperty(
697 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
698 Qualifiers::ObjCLifetime propertyLifetime =
699 getImpliedARCOwnership(property->getPropertyAttributes(),
700 property->getType());
701 if (propertyLifetime != Qualifiers::OCL_None)
702 return;
703
704 if (!ivar) {
705 // if no backing ivar, make property 'strong'.
706 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
707 return;
708 }
709 // property assumes owenership of backing ivar.
710 QualType ivarType = ivar->getType();
711 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
712 if (ivarLifetime == Qualifiers::OCL_Strong)
713 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
714 else if (ivarLifetime == Qualifiers::OCL_Weak)
715 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
716 return;
717}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000718
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000719/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
720/// attribute declared in primary class and attributes overridden in any of its
721/// class extensions.
722static void
723DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
724 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000725 unsigned Attributes = property->getPropertyAttributesAsWritten();
726 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000727 for (ObjCInterfaceDecl::known_extensions_iterator
728 Ext = ClassDecl->known_extensions_begin(),
729 ExtEnd = ClassDecl->known_extensions_end();
730 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000731 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000732 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
733 for (unsigned I = 0, N = R.size(); I != N; ++I) {
734 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
735 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000736 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000737 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000738
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000739 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000740 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000741 unsigned classExtPropertyAttr =
742 ClassExtProperty->getPropertyAttributesAsWritten();
743 // We are issuing the warning that we postponed because class extensions
744 // can override readonly->readwrite and 'setter' attributes originally
745 // placed on class's property declaration now make sense in the overridden
746 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000747 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000748 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000749 (classExtPropertyAttr &
750 (ObjCDeclSpec::DQ_PR_readwrite|
751 ObjCDeclSpec::DQ_PR_assign |
752 ObjCDeclSpec::DQ_PR_unsafe_unretained |
753 ObjCDeclSpec::DQ_PR_copy |
754 ObjCDeclSpec::DQ_PR_retain |
755 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000756 continue;
757 warn = true;
758 break;
759 }
760 }
761 }
762 if (warn) {
763 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
764 ObjCDeclSpec::DQ_PR_unsafe_unretained |
765 ObjCDeclSpec::DQ_PR_copy |
766 ObjCDeclSpec::DQ_PR_retain |
767 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000768 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000769 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000770 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000771 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000772 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000773 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000774 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000775 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000776 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000777 "retain" : "strong";
778
779 S.Diag(property->getLocation(),
780 diag::warn_objc_property_attr_mutually_exclusive)
781 << "readonly" << which;
782 }
783 }
784
785
786}
787
Ted Kremenek28685ab2010-03-12 00:46:40 +0000788/// ActOnPropertyImplDecl - This routine performs semantic checks and
789/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000790/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000791///
John McCalld226f652010-08-21 09:40:31 +0000792Decl *Sema::ActOnPropertyImplDecl(Scope *S,
793 SourceLocation AtLoc,
794 SourceLocation PropertyLoc,
795 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000796 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000797 IdentifierInfo *PropertyIvar,
798 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000799 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000800 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000801 // Make sure we have a context for the property implementation declaration.
802 if (!ClassImpDecl) {
803 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000804 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000806 if (PropertyIvarLoc.isInvalid())
807 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000808 SourceLocation PropertyDiagLoc = PropertyLoc;
809 if (PropertyDiagLoc.isInvalid())
810 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000811 ObjCPropertyDecl *property = 0;
812 ObjCInterfaceDecl* IDecl = 0;
813 // Find the class or category class where this property must have
814 // a declaration.
815 ObjCImplementationDecl *IC = 0;
816 ObjCCategoryImplDecl* CatImplClass = 0;
817 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
818 IDecl = IC->getClassInterface();
819 // We always synthesize an interface for an implementation
820 // without an interface decl. So, IDecl is always non-zero.
821 assert(IDecl &&
822 "ActOnPropertyImplDecl - @implementation without @interface");
823
824 // Look for this property declaration in the @implementation's @interface
825 property = IDecl->FindPropertyDeclaration(PropertyId);
826 if (!property) {
827 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000828 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000829 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000830 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000831 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
832 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000833 if (AtLoc.isValid())
834 Diag(AtLoc, diag::warn_implicit_atomic_property);
835 else
836 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
837 Diag(property->getLocation(), diag::note_property_declare);
838 }
839
Ted Kremenek28685ab2010-03-12 00:46:40 +0000840 if (const ObjCCategoryDecl *CD =
841 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
842 if (!CD->IsClassExtension()) {
843 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
844 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000845 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000846 }
847 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000848 if (Synthesize&&
849 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
850 property->hasAttr<IBOutletAttr>() &&
851 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000852 bool ReadWriteProperty = false;
853 // Search into the class extensions and see if 'readonly property is
854 // redeclared 'readwrite', then no warning is to be issued.
855 for (ObjCInterfaceDecl::known_extensions_iterator
856 Ext = IDecl->known_extensions_begin(),
857 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
858 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
859 if (!R.empty())
860 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
861 PIkind = ExtProp->getPropertyAttributesAsWritten();
862 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
863 ReadWriteProperty = true;
864 break;
865 }
866 }
867 }
868
869 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000870 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
871 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000872 SourceLocation readonlyLoc;
873 if (LocPropertyAttribute(Context, "readonly",
874 property->getLParenLoc(), readonlyLoc)) {
875 SourceLocation endLoc =
876 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
877 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
878 Diag(property->getLocation(),
879 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
880 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
881 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000882 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000883 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000884
885 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000886
Ted Kremenek28685ab2010-03-12 00:46:40 +0000887 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
888 if (Synthesize) {
889 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000890 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000891 }
892 IDecl = CatImplClass->getClassInterface();
893 if (!IDecl) {
894 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000895 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000896 }
897 ObjCCategoryDecl *Category =
898 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
899
900 // If category for this implementation not found, it is an error which
901 // has already been reported eralier.
902 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000903 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000904 // Look for this property declaration in @implementation's category
905 property = Category->FindPropertyDeclaration(PropertyId);
906 if (!property) {
907 Diag(PropertyLoc, diag::error_bad_category_property_decl)
908 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000909 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000910 }
911 } else {
912 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000913 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000914 }
915 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000916 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000917 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000918 // Check that we have a valid, previously declared ivar for @synthesize
919 if (Synthesize) {
920 // @synthesize
921 if (!PropertyIvar)
922 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000923 // Check that this is a previously declared 'ivar' in 'IDecl' interface
924 ObjCInterfaceDecl *ClassDeclared;
925 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
926 QualType PropType = property->getType();
927 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000928
929 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000930 diag::err_incomplete_synthesized_property,
931 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000932 Diag(property->getLocation(), diag::note_property_declare);
933 CompleteTypeErr = true;
934 }
935
David Blaikie4e4d0842012-03-11 07:00:24 +0000936 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000937 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000938 ObjCPropertyDecl::OBJC_PR_readonly) &&
939 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000940 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
941 }
942
John McCallf85e1932011-06-15 23:02:42 +0000943 ObjCPropertyDecl::PropertyAttributeKind kind
944 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000945
946 // Add GC __weak to the ivar type if the property is weak.
947 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000948 getLangOpts().getGC() != LangOptions::NonGC) {
949 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000950 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000951 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000952 Diag(property->getLocation(), diag::note_property_declare);
953 } else {
954 PropertyIvarType =
955 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000956 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000957 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000958 if (AtLoc.isInvalid()) {
959 // Check when default synthesizing a property that there is
960 // an ivar matching property name and issue warning; since this
961 // is the most common case of not using an ivar used for backing
962 // property in non-default synthesis case.
963 ObjCInterfaceDecl *ClassDeclared=0;
964 ObjCIvarDecl *originalIvar =
965 IDecl->lookupInstanceVariable(property->getIdentifier(),
966 ClassDeclared);
967 if (originalIvar) {
968 Diag(PropertyDiagLoc,
969 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000970 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000971 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000972 Diag(property->getLocation(), diag::note_property_declare);
973 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000974 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000975 }
976
977 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000978 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000979 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000980 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000981 !PropertyIvarType.getObjCLifetime() &&
982 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000983
John McCall265941b2011-09-13 18:31:23 +0000984 // It's an error if we have to do this and the user didn't
985 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000986 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000987 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000988 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000989 diag::err_arc_objc_property_default_assign_on_object);
990 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000991 } else {
992 Qualifiers::ObjCLifetime lifetime =
993 getImpliedARCOwnership(kind, PropertyIvarType);
994 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000995 if (lifetime == Qualifiers::OCL_Weak) {
996 bool err = false;
997 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000998 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
999 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1000 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001001 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001002 Diag(property->getLocation(), diag::note_property_declare);
1003 err = true;
1004 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001005 }
John McCall0a7dd782012-08-21 02:47:43 +00001006 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001007 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001008 Diag(property->getLocation(), diag::note_property_declare);
1009 }
John McCallf85e1932011-06-15 23:02:42 +00001010 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001011
John McCallf85e1932011-06-15 23:02:42 +00001012 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001013 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001014 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1015 }
John McCallf85e1932011-06-15 23:02:42 +00001016 }
1017
1018 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001019 !getLangOpts().ObjCAutoRefCount &&
1020 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001021 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001022 Diag(property->getLocation(), diag::note_property_declare);
1023 }
1024
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001025 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001026 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001027 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001028 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001029 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001030 if (CompleteTypeErr)
1031 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001032 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001033 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001034
John McCall260611a2012-06-20 06:18:46 +00001035 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001036 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1037 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001038 // Note! I deliberately want it to fall thru so, we have a
1039 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001040 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001041 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001042 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001043 << property->getDeclName() << Ivar->getDeclName()
1044 << ClassDeclared->getDeclName();
1045 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001046 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001047 // Note! I deliberately want it to fall thru so more errors are caught.
1048 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001049 property->setPropertyIvarDecl(Ivar);
1050
Ted Kremenek28685ab2010-03-12 00:46:40 +00001051 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1052
1053 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001054 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001055 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001056 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001057 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001058 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001059 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001060 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001061 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001062 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1063 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001064 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001065 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001066 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001067 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001068 << property->getDeclName() << PropType
1069 << Ivar->getDeclName() << IvarType;
1070 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001071 // Note! I deliberately want it to fall thru so, we have a
1072 // a property implementation and to avoid future warnings.
1073 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001074 else {
1075 // FIXME! Rules for properties are somewhat different that those
1076 // for assignments. Use a new routine to consolidate all cases;
1077 // specifically for property redeclarations as well as for ivars.
1078 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1079 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1080 if (lhsType != rhsType &&
1081 lhsType->isArithmeticType()) {
1082 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1083 << property->getDeclName() << PropType
1084 << Ivar->getDeclName() << IvarType;
1085 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1086 // Fall thru - see previous comment
1087 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001088 }
1089 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001090 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001091 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001092 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001093 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001094 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001095 // Fall thru - see previous comment
1096 }
John McCallf85e1932011-06-15 23:02:42 +00001097 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001098 if ((property->getType()->isObjCObjectPointerType() ||
1099 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001100 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001101 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001102 << property->getDeclName() << Ivar->getDeclName();
1103 // Fall thru - see previous comment
1104 }
1105 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001106 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001107 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001108 } else if (PropertyIvar)
1109 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001110 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001111
Ted Kremenek28685ab2010-03-12 00:46:40 +00001112 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1113 ObjCPropertyImplDecl *PIDecl =
1114 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1115 property,
1116 (Synthesize ?
1117 ObjCPropertyImplDecl::Synthesize
1118 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001119 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001120
Fariborz Jahanian74414712012-05-15 18:12:51 +00001121 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001122 PIDecl->setInvalidDecl();
1123
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001124 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1125 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001126 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001127 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001128 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1129 // returned by the getter as it must conform to C++'s copy-return rules.
1130 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001131 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001132 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1133 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001134 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001135 VK_RValue, PropertyDiagLoc);
1136 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001137 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001138 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001139 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001140 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001141 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001142 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001143 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001144 getterMethod->getResultType(),
1145 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001146 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001147 Owned(IvarRefExpr));
1148 if (!Res.isInvalid()) {
1149 Expr *ResExpr = Res.takeAs<Expr>();
1150 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001151 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001152 PIDecl->setGetterCXXConstructor(ResExpr);
1153 }
1154 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001155 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1156 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1157 Diag(getterMethod->getLocation(),
1158 diag::warn_property_getter_owning_mismatch);
1159 Diag(property->getLocation(), diag::note_property_declare);
1160 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001161 }
1162 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1163 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001164 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1165 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001166 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001167 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001168 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1169 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001170 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001171 VK_RValue, PropertyDiagLoc);
1172 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001173 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001174 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001175 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001176 SelfExpr, true, true);
1177 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1178 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001179 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001180 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1181 VK_LValue, PropertyDiagLoc);
1182 MarkDeclRefReferenced(rhs);
1183 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001184 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001185 if (property->getPropertyAttributes() &
1186 ObjCPropertyDecl::OBJC_PR_atomic) {
1187 Expr *callExpr = Res.takeAs<Expr>();
1188 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001189 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1190 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001191 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001192 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001193 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001194 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001195 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001196 Diag(FuncDecl->getLocStart(),
1197 diag::note_callee_decl) << FuncDecl;
1198 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001199 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001200 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1201 }
1202 }
1203
Ted Kremenek28685ab2010-03-12 00:46:40 +00001204 if (IC) {
1205 if (Synthesize)
1206 if (ObjCPropertyImplDecl *PPIDecl =
1207 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1208 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1209 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1210 << PropertyIvar;
1211 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1212 }
1213
1214 if (ObjCPropertyImplDecl *PPIDecl
1215 = IC->FindPropertyImplDecl(PropertyId)) {
1216 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1217 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001218 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001219 }
1220 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001222 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001223 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001224 // Diagnose if an ivar was lazily synthesdized due to a previous
1225 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001226 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001227 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001228 ObjCIvarDecl *Ivar = 0;
1229 if (!Synthesize)
1230 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1231 else {
1232 if (PropertyIvar && PropertyIvar != PropertyId)
1233 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1234 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001235 // Issue diagnostics only if Ivar belongs to current class.
1236 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001237 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001238 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1239 << PropertyId;
1240 Ivar->setInvalidDecl();
1241 }
1242 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001243 } else {
1244 if (Synthesize)
1245 if (ObjCPropertyImplDecl *PPIDecl =
1246 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001247 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001248 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1249 << PropertyIvar;
1250 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1251 }
1252
1253 if (ObjCPropertyImplDecl *PPIDecl =
1254 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001255 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001256 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001257 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001258 }
1259 CatImplClass->addPropertyImplementation(PIDecl);
1260 }
1261
John McCalld226f652010-08-21 09:40:31 +00001262 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001263}
1264
1265//===----------------------------------------------------------------------===//
1266// Helper methods.
1267//===----------------------------------------------------------------------===//
1268
Ted Kremenek9d64c152010-03-12 00:38:38 +00001269/// DiagnosePropertyMismatch - Compares two properties for their
1270/// attributes and types and warns on a variety of inconsistencies.
1271///
1272void
1273Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1274 ObjCPropertyDecl *SuperProperty,
1275 const IdentifierInfo *inheritedName) {
1276 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1277 Property->getPropertyAttributes();
1278 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1279 SuperProperty->getPropertyAttributes();
1280 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1281 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1282 Diag(Property->getLocation(), diag::warn_readonly_property)
1283 << Property->getDeclName() << inheritedName;
1284 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1285 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1286 Diag(Property->getLocation(), diag::warn_property_attribute)
1287 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001288 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001289 unsigned CAttrRetain =
1290 (CAttr &
1291 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1292 unsigned SAttrRetain =
1293 (SAttr &
1294 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1295 bool CStrong = (CAttrRetain != 0);
1296 bool SStrong = (SAttrRetain != 0);
1297 if (CStrong != SStrong)
1298 Diag(Property->getLocation(), diag::warn_property_attribute)
1299 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1300 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001301
1302 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001303 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001304 Diag(Property->getLocation(), diag::warn_property_attribute)
1305 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001306 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1307 }
1308 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001309 Diag(Property->getLocation(), diag::warn_property_attribute)
1310 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001311 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1312 }
1313 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001314 Diag(Property->getLocation(), diag::warn_property_attribute)
1315 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001316 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1317 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001318
1319 QualType LHSType =
1320 Context.getCanonicalType(SuperProperty->getType());
1321 QualType RHSType =
1322 Context.getCanonicalType(Property->getType());
1323
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001324 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001325 // Do cases not handled in above.
1326 // FIXME. For future support of covariant property types, revisit this.
1327 bool IncompatibleObjC = false;
1328 QualType ConvertedType;
1329 if (!isObjCPointerConversion(RHSType, LHSType,
1330 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001331 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001332 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1333 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001334 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1335 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001336 }
1337}
1338
1339bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1340 ObjCMethodDecl *GetterMethod,
1341 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001342 if (!GetterMethod)
1343 return false;
1344 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1345 QualType PropertyIvarType = property->getType().getNonReferenceType();
1346 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1347 if (!compat) {
1348 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1349 isa<ObjCObjectPointerType>(GetterType))
1350 compat =
1351 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001352 GetterType->getAs<ObjCObjectPointerType>(),
1353 PropertyIvarType->getAs<ObjCObjectPointerType>());
1354 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001355 != Compatible) {
1356 Diag(Loc, diag::error_property_accessor_type)
1357 << property->getDeclName() << PropertyIvarType
1358 << GetterMethod->getSelector() << GetterType;
1359 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1360 return true;
1361 } else {
1362 compat = true;
1363 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1364 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1365 if (lhsType != rhsType && lhsType->isArithmeticType())
1366 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001367 }
1368 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001369
1370 if (!compat) {
1371 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1372 << property->getDeclName()
1373 << GetterMethod->getSelector();
1374 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1375 return true;
1376 }
1377
Ted Kremenek9d64c152010-03-12 00:38:38 +00001378 return false;
1379}
1380
Ted Kremenek9d64c152010-03-12 00:38:38 +00001381/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1382/// of properties declared in a protocol and compares their attribute against
1383/// the same property declared in the class or category.
1384void
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001385Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1386 if (!CDecl)
1387 return;
1388
1389 // Category case.
1390 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1391 // FIXME: We should perform this check when the property in the category
1392 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001393 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1394 if (!CatDecl->IsClassExtension())
1395 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1396 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001397 ObjCPropertyDecl *ProtoProp = *P;
1398 DeclContext::lookup_result R
1399 = CatDecl->lookup(ProtoProp->getDeclName());
1400 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1401 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1402 if (CatProp != ProtoProp) {
1403 // Property protocol already exist in class. Diagnose any mismatch.
1404 DiagnosePropertyMismatch(CatProp, ProtoProp,
1405 PDecl->getIdentifier());
1406 }
1407 }
1408 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001409 }
1410 return;
1411 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001412
1413 // Class
1414 // FIXME: We should perform this check when the property in the class
1415 // is declared.
1416 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001418 E = PDecl->prop_end(); P != E; ++P) {
1419 ObjCPropertyDecl *ProtoProp = *P;
1420 DeclContext::lookup_result R
Douglas Gregoraabd0942013-01-21 19:05:22 +00001421 = IDecl->lookup(ProtoProp->getDeclName());
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001422 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1423 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1424 if (ClassProp != ProtoProp) {
1425 // Property protocol already exist in class. Diagnose any mismatch.
1426 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1427 PDecl->getIdentifier());
1428 }
1429 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001430 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001431 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001432}
1433
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434/// isPropertyReadonly - Return true if property is readonly, by searching
1435/// for the property in the class and in its categories and implementations
1436///
1437bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1438 ObjCInterfaceDecl *IDecl) {
1439 // by far the most common case.
1440 if (!PDecl->isReadOnly())
1441 return false;
1442 // Even if property is ready only, if interface has a user defined setter,
1443 // it is not considered read only.
1444 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1445 return false;
1446
1447 // Main class has the property as 'readonly'. Must search
1448 // through the category list to see if the property's
1449 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001450 for (ObjCInterfaceDecl::visible_categories_iterator
1451 Cat = IDecl->visible_categories_begin(),
1452 CatEnd = IDecl->visible_categories_end();
1453 Cat != CatEnd; ++Cat) {
1454 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001455 return false;
1456 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001457 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 if (P && !P->isReadOnly())
1459 return false;
1460 }
1461
1462 // Also, check for definition of a setter method in the implementation if
1463 // all else failed.
1464 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1465 if (ObjCImplementationDecl *IMD =
1466 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1467 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1468 return false;
1469 } else if (ObjCCategoryImplDecl *CIMD =
1470 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1471 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1472 return false;
1473 }
1474 }
1475 // Lastly, look through the implementation (if one is in scope).
1476 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1477 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1478 return false;
1479 // If all fails, look at the super class.
1480 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1481 return isPropertyReadonly(PDecl, SIDecl);
1482 return true;
1483}
1484
1485/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001486/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001487void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001488 ObjCContainerDecl::PropertyMap &PropMap,
1489 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001490 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1491 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1492 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001493 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001494 PropMap[Prop->getIdentifier()] = Prop;
1495 }
1496 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001497 for (ObjCInterfaceDecl::all_protocol_iterator
1498 PI = IDecl->all_referenced_protocol_begin(),
1499 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001500 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001501 }
1502 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1503 if (!CATDecl->IsClassExtension())
1504 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1505 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001506 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001507 PropMap[Prop->getIdentifier()] = Prop;
1508 }
1509 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001510 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001511 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001512 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 }
1514 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1515 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1516 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001517 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001518 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1519 // Exclude property for protocols which conform to class's super-class,
1520 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001521 if (!PropertyFromSuper ||
1522 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001523 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1524 if (!PropEntry)
1525 PropEntry = Prop;
1526 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001527 }
1528 // scan through protocol's protocols.
1529 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1530 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001531 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001532 }
1533}
1534
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001535/// CollectSuperClassPropertyImplementations - This routine collects list of
1536/// properties to be implemented in super class(s) and also coming from their
1537/// conforming protocols.
1538static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001539 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001540 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001541 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001542 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001543 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001544 SDecl = SDecl->getSuperClass();
1545 }
1546 }
1547}
1548
Fariborz Jahanian26202292013-02-14 19:07:19 +00001549/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1550/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1551/// declared in class 'IFace'.
1552bool
1553Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1554 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1555 if (!IV->getSynthesize())
1556 return false;
1557 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1558 Method->isInstanceMethod());
1559 if (!IMD || !IMD->isPropertyAccessor())
1560 return false;
1561
1562 // look up a property declaration whose one of its accessors is implemented
1563 // by this method.
1564 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1565 E = IFace->prop_end(); P != E; ++P) {
1566 ObjCPropertyDecl *property = *P;
1567 if ((property->getGetterName() == IMD->getSelector() ||
1568 property->getSetterName() == IMD->getSelector()) &&
1569 (property->getPropertyIvarDecl() == IV))
1570 return true;
1571 }
1572 return false;
1573}
1574
1575
James Dennett699c9042012-06-15 07:13:21 +00001576/// \brief Default synthesizes all properties which must be synthesized
1577/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001578void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1579 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001580
Anna Zaksb36ea372012-10-18 19:17:53 +00001581 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001582 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1583 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001584 if (PropMap.empty())
1585 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001586 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001587 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1588
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001589 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1590 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001591 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001592 if (SuperPropMap[Prop->getIdentifier()]) {
1593 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1594 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1595 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001596 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001597 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1598 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001599 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1600 << Prop->getIdentifier()->getName();
1601 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1602 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001603 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001604 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001605 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001606 if (Prop->isInvalidDecl() ||
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001607 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001608 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001609 if (ObjCPropertyImplDecl *PID =
1610 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1611 if (PID->getPropertyDecl() != Prop) {
1612 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1613 << Prop->getIdentifier()->getName();
1614 if (!PID->getLocation().isInvalid())
1615 Diag(PID->getLocation(), diag::note_property_synthesize);
1616 }
1617 continue;
1618 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001619 // Property may have been synthesized by user.
1620 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1621 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001622 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1623 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1624 continue;
1625 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1626 continue;
1627 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001628 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1629 // We won't auto-synthesize properties declared in protocols.
1630 Diag(IMPDecl->getLocation(),
1631 diag::warn_auto_synthesizing_protocol_property);
1632 Diag(Prop->getLocation(), diag::note_property_declare);
1633 continue;
1634 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001635
1636 // We use invalid SourceLocations for the synthesized ivars since they
1637 // aren't really synthesized at a particular location; they just exist.
1638 // Saying that they are located at the @implementation isn't really going
1639 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001640 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1641 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1642 true,
1643 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001644 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001645 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001646 if (PIDecl) {
1647 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001648 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001649 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001650 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001651}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001652
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001653void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001654 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001655 return;
1656 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1657 if (!IC)
1658 return;
1659 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001660 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001661 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001662}
1663
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001664void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001665 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001666 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001667 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1668 ObjCInterfaceDecl *IDecl;
1669 // Gather properties which need not be implemented in this class
1670 // or category.
1671 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1672 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1673 // For categories, no need to implement properties declared in
1674 // its primary class (and its super classes) if property is
1675 // declared in one of those containers.
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001676 if ((IDecl = C->getClassInterface())) {
1677 ObjCInterfaceDecl::PropertyDeclOrder PO;
1678 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1679 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001680 }
1681 if (IDecl)
1682 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001683
Anna Zaksb36ea372012-10-18 19:17:53 +00001684 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001685 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686 if (PropMap.empty())
1687 return;
1688
1689 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1690 for (ObjCImplDecl::propimpl_iterator
1691 I = IMPDecl->propimpl_begin(),
1692 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001693 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001694
Anna Zaksb36ea372012-10-18 19:17:53 +00001695 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001696 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1697 ObjCPropertyDecl *Prop = P->second;
1698 // Is there a matching propery synthesize/dynamic?
1699 if (Prop->isInvalidDecl() ||
1700 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001701 PropImplMap.count(Prop) ||
1702 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001703 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001704 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001705 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001706 isa<ObjCCategoryDecl>(CDecl) ?
1707 diag::warn_setter_getter_impl_required_in_category :
1708 diag::warn_setter_getter_impl_required)
1709 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001710 Diag(Prop->getLocation(),
1711 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001712 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001713 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001714 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001715 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1716
Ted Kremenek9d64c152010-03-12 00:38:38 +00001717 }
1718
1719 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001720 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001721 isa<ObjCCategoryDecl>(CDecl) ?
1722 diag::warn_setter_getter_impl_required_in_category :
1723 diag::warn_setter_getter_impl_required)
1724 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001725 Diag(Prop->getLocation(),
1726 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001727 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001728 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001729 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001730 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001731 }
1732 }
1733}
1734
1735void
1736Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1737 ObjCContainerDecl* IDecl) {
1738 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001739 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001740 return;
1741 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1742 E = IDecl->prop_end();
1743 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001744 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001745 ObjCMethodDecl *GetterMethod = 0;
1746 ObjCMethodDecl *SetterMethod = 0;
1747 bool LookedUpGetterSetter = false;
1748
Bill Wendlingad017fa2012-12-20 19:22:21 +00001749 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001750 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001751
John McCall265941b2011-09-13 18:31:23 +00001752 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1753 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001754 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1755 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1756 LookedUpGetterSetter = true;
1757 if (GetterMethod) {
1758 Diag(GetterMethod->getLocation(),
1759 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001760 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001761 Diag(Property->getLocation(), diag::note_property_declare);
1762 }
1763 if (SetterMethod) {
1764 Diag(SetterMethod->getLocation(),
1765 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001766 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001767 Diag(Property->getLocation(), diag::note_property_declare);
1768 }
1769 }
1770
Ted Kremenek9d64c152010-03-12 00:38:38 +00001771 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001772 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1773 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001774 continue;
1775 if (const ObjCPropertyImplDecl *PIDecl
1776 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1777 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1778 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001779 if (!LookedUpGetterSetter) {
1780 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1781 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1782 LookedUpGetterSetter = true;
1783 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001784 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1785 SourceLocation MethodLoc =
1786 (GetterMethod ? GetterMethod->getLocation()
1787 : SetterMethod->getLocation());
1788 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001789 << Property->getIdentifier() << (GetterMethod != 0)
1790 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001791 // fixit stuff.
1792 if (!AttributesAsWritten) {
1793 if (Property->getLParenLoc().isValid()) {
1794 // @property () ... case.
1795 SourceRange PropSourceRange(Property->getAtLoc(),
1796 Property->getLParenLoc());
1797 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1798 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1799 }
1800 else {
1801 //@property id etc.
1802 SourceLocation endLoc =
1803 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1804 endLoc = endLoc.getLocWithOffset(-1);
1805 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1806 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1807 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1808 }
1809 }
1810 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1811 // @property () ... case.
1812 SourceLocation endLoc = Property->getLParenLoc();
1813 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1814 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1815 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1816 }
1817 else
1818 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001819 Diag(Property->getLocation(), diag::note_property_declare);
1820 }
1821 }
1822 }
1823}
1824
John McCallf85e1932011-06-15 23:02:42 +00001825void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001826 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001827 return;
1828
1829 for (ObjCImplementationDecl::propimpl_iterator
1830 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001831 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001832 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1833 continue;
1834
1835 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001836 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1837 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001838 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1839 if (!method)
1840 continue;
1841 ObjCMethodFamily family = method->getMethodFamily();
1842 if (family == OMF_alloc || family == OMF_copy ||
1843 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001844 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001845 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1846 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001847 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001848 Diag(PD->getLocation(), diag::note_property_declare);
1849 }
1850 }
1851 }
1852}
1853
John McCall5de74d12010-11-10 07:01:40 +00001854/// AddPropertyAttrs - Propagates attributes from a property to the
1855/// implicitly-declared getter or setter for that property.
1856static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1857 ObjCPropertyDecl *Property) {
1858 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001859 for (Decl::attr_iterator A = Property->attr_begin(),
1860 AEnd = Property->attr_end();
1861 A != AEnd; ++A) {
1862 if (isa<DeprecatedAttr>(*A) ||
1863 isa<UnavailableAttr>(*A) ||
1864 isa<AvailabilityAttr>(*A))
1865 PropertyMethod->addAttr((*A)->clone(S.Context));
1866 }
John McCall5de74d12010-11-10 07:01:40 +00001867}
1868
Ted Kremenek9d64c152010-03-12 00:38:38 +00001869/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1870/// have the property type and issue diagnostics if they don't.
1871/// Also synthesize a getter/setter method if none exist (and update the
1872/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1873/// methods is the "right" thing to do.
1874void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001875 ObjCContainerDecl *CD,
1876 ObjCPropertyDecl *redeclaredProperty,
1877 ObjCContainerDecl *lexicalDC) {
1878
Ted Kremenek9d64c152010-03-12 00:38:38 +00001879 ObjCMethodDecl *GetterMethod, *SetterMethod;
1880
1881 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1882 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1883 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1884 property->getLocation());
1885
1886 if (SetterMethod) {
1887 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1888 property->getPropertyAttributes();
1889 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1890 Context.getCanonicalType(SetterMethod->getResultType()) !=
1891 Context.VoidTy)
1892 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1893 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001894 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001895 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1896 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001897 Diag(property->getLocation(),
1898 diag::warn_accessor_property_type_mismatch)
1899 << property->getDeclName()
1900 << SetterMethod->getSelector();
1901 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1902 }
1903 }
1904
1905 // Synthesize getter/setter methods if none exist.
1906 // Find the default getter and if one not found, add one.
1907 // FIXME: The synthesized property we set here is misleading. We almost always
1908 // synthesize these methods unless the user explicitly provided prototypes
1909 // (which is odd, but allowed). Sema should be typechecking that the
1910 // declarations jive in that situation (which it is not currently).
1911 if (!GetterMethod) {
1912 // No instance method of same name as property getter name was found.
1913 // Declare a getter method and add it to the list of methods
1914 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001915 SourceLocation Loc = redeclaredProperty ?
1916 redeclaredProperty->getLocation() :
1917 property->getLocation();
1918
1919 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1920 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001921 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001922 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001923 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001924 (property->getPropertyImplementation() ==
1925 ObjCPropertyDecl::Optional) ?
1926 ObjCMethodDecl::Optional :
1927 ObjCMethodDecl::Required);
1928 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001929
1930 AddPropertyAttrs(*this, GetterMethod, property);
1931
Ted Kremenek23173d72010-05-18 21:09:07 +00001932 // FIXME: Eventually this shouldn't be needed, as the lexical context
1933 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001934 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001935 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001936 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1937 GetterMethod->addAttr(
1938 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001939 } else
1940 // A user declared getter will be synthesize when @synthesize of
1941 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001942 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001943 property->setGetterMethodDecl(GetterMethod);
1944
1945 // Skip setter if property is read-only.
1946 if (!property->isReadOnly()) {
1947 // Find the default setter and if one not found, add one.
1948 if (!SetterMethod) {
1949 // No instance method of same name as property setter name was found.
1950 // Declare a setter method and add it to the list of methods
1951 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001952 SourceLocation Loc = redeclaredProperty ?
1953 redeclaredProperty->getLocation() :
1954 property->getLocation();
1955
1956 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001957 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001958 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001959 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001960 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001961 /*isImplicitlyDeclared=*/true,
1962 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001963 (property->getPropertyImplementation() ==
1964 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001965 ObjCMethodDecl::Optional :
1966 ObjCMethodDecl::Required);
1967
Ted Kremenek9d64c152010-03-12 00:38:38 +00001968 // Invent the arguments for the setter. We don't bother making a
1969 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001970 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1971 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001972 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001973 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001974 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001975 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001976 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001977 SetterMethod->setMethodParams(Context, Argument,
1978 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001979
1980 AddPropertyAttrs(*this, SetterMethod, property);
1981
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-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 Kremenek8254aa62010-09-21 18:28:43 +00001985 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001986 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987 } else
1988 // A user declared setter will be synthesize when @synthesize of
1989 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001990 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001991 property->setSetterMethodDecl(SetterMethod);
1992 }
1993 // Add any synthesized methods to the global pool. This allows us to
1994 // handle the following, which is supported by GCC (and part of the design).
1995 //
1996 // @interface Foo
1997 // @property double bar;
1998 // @end
1999 //
2000 // void thisIsUnfortunate() {
2001 // id foo;
2002 // double bar = [foo bar];
2003 // }
2004 //
2005 if (GetterMethod)
2006 AddInstanceMethodToGlobalPool(GetterMethod);
2007 if (SetterMethod)
2008 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002009
2010 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2011 if (!CurrentClass) {
2012 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2013 CurrentClass = Cat->getClassInterface();
2014 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2015 CurrentClass = Impl->getClassInterface();
2016 }
2017 if (GetterMethod)
2018 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2019 if (SetterMethod)
2020 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002021}
2022
John McCalld226f652010-08-21 09:40:31 +00002023void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002024 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002025 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002026 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002027 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002028 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002029 return;
2030
2031 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002032 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002033
David Blaikie4e4d0842012-03-11 07:00:24 +00002034 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002035 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002036 PropertyTy->isObjCRetainableType()) {
2037 // 'readonly' property with no obvious lifetime.
2038 // its life time will be determined by its backing ivar.
2039 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2040 ObjCDeclSpec::DQ_PR_copy |
2041 ObjCDeclSpec::DQ_PR_retain |
2042 ObjCDeclSpec::DQ_PR_strong |
2043 ObjCDeclSpec::DQ_PR_weak |
2044 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002045 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002046 return;
2047 }
2048
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002049 if (propertyInPrimaryClass) {
2050 // we postpone most property diagnosis until class's implementation
2051 // because, its readonly attribute may be overridden in its class
2052 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002053 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2054 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002055 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2056 << "readonly" << "readwrite";
2057 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002058 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002059 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2060 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002061 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002062 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002063 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002064 ObjCDeclSpec::DQ_PR_retain |
2065 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00002066 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002067 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002068 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002069 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002070 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00002071 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002072 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002073 "copy" : "retain";
2074
Bill Wendlingad017fa2012-12-20 19:22:21 +00002075 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002076 diag::err_objc_property_attr_mutually_exclusive :
2077 diag::warn_objc_property_attr_mutually_exclusive)
2078 << "readonly" << which;
2079 }
2080
2081 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002083 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2084 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002085 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002086 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002087 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2088 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2089 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002090 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002091 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002092 }
2093
2094 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002095 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2096 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002097 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2098 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002099 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002100 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002101 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002102 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2103 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002104 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002105 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002106 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002107 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2108 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002109 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002110 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002111 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002112 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002113 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2114 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002116 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002117 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2118 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002119 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2120 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002121 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002122 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002123 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002124 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2125 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002126 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002127 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002128 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002129 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2130 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002131 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002132 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002133 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002134 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002135 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2136 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002137 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002138 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002139 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2140 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002141 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2142 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002143 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002144 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002145 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002146 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2147 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002148 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002149 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002150 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002151 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2152 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002153 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002154 }
2155 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2157 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002158 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2159 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002160 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002161 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002162 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2163 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002164 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2165 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002166 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002167 }
2168
Bill Wendlingad017fa2012-12-20 19:22:21 +00002169 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2170 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002173 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002174 }
2175
Ted Kremenek9d64c152010-03-12 00:38:38 +00002176 // Warn if user supplied no assignment attribute, property is
2177 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002178 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002179 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2180 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2181 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002182 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002183 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002184 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002185 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002186 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002187 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002188 bool isAnyClassTy =
2189 (PropertyTy->isObjCClassType() ||
2190 PropertyTy->isObjCQualifiedClassType());
2191 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2192 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002193 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002194 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002195 else if (propertyInPrimaryClass) {
2196 // Don't issue warning on property with no life time in class
2197 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002198 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002199 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002200 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002201
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002202 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002203 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002204 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002205 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002206 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002207
2208 // FIXME: Implement warning dependent on NSCopying being
2209 // implemented. See also:
2210 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2211 // (please trim this list while you are at it).
2212 }
2213
Bill Wendlingad017fa2012-12-20 19:22:21 +00002214 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2215 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002216 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002217 && PropertyTy->isBlockPointerType())
2218 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002219 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2220 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2221 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002222 PropertyTy->isBlockPointerType())
2223 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002224
Bill Wendlingad017fa2012-12-20 19:22:21 +00002225 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2226 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002227 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2228
Ted Kremenek9d64c152010-03-12 00:38:38 +00002229}