blob: 2bdb54b1a527235e3420e50de3a8ff5ecb618da4 [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
John McCalld226f652010-08-21 09:40:31 +0000115Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000116 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000117 FieldDeclarator &FD,
118 ObjCDeclSpec &ODS,
119 Selector GetterSel,
120 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000121 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000122 tok::ObjCKeywordKind MethodImplKind,
123 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000124 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000125 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
126 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000127 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000128
Bill Wendlingad017fa2012-12-20 19:22:21 +0000129 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000130 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000131 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000132 // property is defaulted to 'assign' if it is readwrite and is
133 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000134 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000135 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000136 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
137 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
138 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
139 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
140 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000141
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000142 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000143 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000144 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000145 if (CDecl->IsClassExtension()) {
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000146 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000147 FD, GetterSel, SetterSel,
148 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000150 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000151 isOverridingProperty, TSI,
152 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000153 if (Res) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000154 CheckObjCPropertyAttributes(Res, AtLoc, Attributes, false);
David Blaikie4e4d0842012-03-11 07:00:24 +0000155 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000156 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
157 }
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000158 ActOnDocumentableDecl(Res);
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000159 return Res;
160 }
161
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000162 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
John McCallf85e1932011-06-15 23:02:42 +0000163 GetterSel, SetterSel,
164 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000165 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000166 ODS.getPropertyAttributes(),
167 TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000168 if (lexicalDC)
169 Res->setLexicalDeclContext(lexicalDC);
170
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000171 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000172 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000173 (isa<ObjCInterfaceDecl>(ClassDecl) ||
174 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000175
David Blaikie4e4d0842012-03-11 07:00:24 +0000176 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000177 checkARCPropertyDecl(*this, Res);
178
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000179 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000180 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000181}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000182
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000183static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000184makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000185 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000186 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000187 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000188 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000189 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000190 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000191 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000192 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000193 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000194 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000195 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000196 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000197 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000198 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000199 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000200 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000201 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000202 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000203 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000204 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000205 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000206 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000207 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000208 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000209 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
210
211 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
212}
213
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000214static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000215 SourceLocation LParenLoc, SourceLocation &Loc) {
216 if (LParenLoc.isMacroID())
217 return false;
218
219 SourceManager &SM = Context.getSourceManager();
220 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
221 // Try to load the file buffer.
222 bool invalidTemp = false;
223 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
224 if (invalidTemp)
225 return false;
226 const char *tokenBegin = file.data() + locInfo.second;
227
228 // Lex from the start of the given location.
229 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
230 Context.getLangOpts(),
231 file.begin(), tokenBegin, file.end());
232 Token Tok;
233 do {
234 lexer.LexFromRawLexer(Tok);
235 if (Tok.is(tok::raw_identifier) &&
236 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
237 Loc = Tok.getLocation();
238 return true;
239 }
240 } while (Tok.isNot(tok::r_paren));
241 return false;
242
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000243}
244
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000245static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000246 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
247 ObjCPropertyDecl::OBJC_PR_retain |
248 ObjCPropertyDecl::OBJC_PR_copy |
249 ObjCPropertyDecl::OBJC_PR_weak |
250 ObjCPropertyDecl::OBJC_PR_strong |
251 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
252}
253
John McCalld226f652010-08-21 09:40:31 +0000254Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000255Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000256 SourceLocation AtLoc,
257 SourceLocation LParenLoc,
258 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000259 Selector GetterSel, Selector SetterSel,
260 const bool isAssign,
261 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000263 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000264 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000265 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000266 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000267 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000268 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000269 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000270 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000271 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
272
Douglas Gregord3297242013-01-16 23:00:23 +0000273 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000274 // Check for duplicate declaration of this property in current and
275 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000276 for (ObjCInterfaceDecl::known_extensions_iterator
277 Ext = CCPrimary->known_extensions_begin(),
278 ExtEnd = CCPrimary->known_extensions_end();
279 Ext != ExtEnd; ++Ext) {
280 if (ObjCPropertyDecl *prevDecl
281 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000282 Diag(AtLoc, diag::err_duplicate_property);
283 Diag(prevDecl->getLocation(), diag::note_property_declare);
284 return 0;
285 }
286 }
Douglas Gregord3297242013-01-16 23:00:23 +0000287 }
288
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000289 // Create a new ObjCPropertyDecl with the DeclContext being
290 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000291 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000292 ObjCPropertyDecl *PDecl =
293 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000294 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000295 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000296 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000297 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000298 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000299 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000300 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000301 // Set setter/getter selector name. Needed later.
302 PDecl->setGetterName(GetterSel);
303 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000304 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000305 DC->addDecl(PDecl);
306
307 // We need to look in the @interface to see if the @property was
308 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000309 if (!CCPrimary) {
310 Diag(CDecl->getLocation(), diag::err_continuation_class);
311 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000312 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000313 }
314
315 // Find the property in continuation class's primary class only.
316 ObjCPropertyDecl *PIDecl =
317 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
318
319 if (!PIDecl) {
320 // No matching property found in the primary class. Just fall thru
321 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000322 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000323 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000324 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000325 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000326
327 // A case of continuation class adding a new property in the class. This
328 // is not what it was meant for. However, gcc supports it and so should we.
329 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000330 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000331 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000332 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
333 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000334 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000335 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
336 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000337 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000338 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
339 bool IncompatibleObjC = false;
340 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000341 // Relax the strict type matching for property type in continuation class.
342 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000343 // as it narrows the object type in its primary class property. Note that
344 // this conversion is safe only because the wider type is for a 'readonly'
345 // property in primary class and 'narrowed' type for a 'readwrite' property
346 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000347 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
348 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
349 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
350 ConvertedType, IncompatibleObjC))
351 || IncompatibleObjC) {
352 Diag(AtLoc,
353 diag::err_type_mismatch_continuation_class) << PDecl->getType();
354 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000355 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000356 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000357 }
358
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 // The property 'PIDecl's readonly attribute will be over-ridden
360 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000361 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000362 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000363 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000364 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000365 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000366 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
367 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000368 Diag(AtLoc, diag::warn_property_attr_mismatch);
369 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000370 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000371 DeclContext *DC = cast<DeclContext>(CCPrimary);
372 if (!ObjCPropertyDecl::findPropertyDecl(DC,
373 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000374 // Protocol is not in the primary class. Must build one for it.
375 ObjCDeclSpec ProtocolPropertyODS;
376 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
377 // and ObjCPropertyDecl::PropertyAttributeKind have identical
378 // values. Should consolidate both into one enum type.
379 ProtocolPropertyODS.
380 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
381 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000382 // Must re-establish the context from class extension to primary
383 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000384 ContextRAII SavedContext(*this, CCPrimary);
385
John McCalld226f652010-08-21 09:40:31 +0000386 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000387 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000388 PIDecl->getGetterName(),
389 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000390 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000391 MethodImplKind,
392 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000393 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000394 }
395 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000396 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000397 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000398 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000399 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000400 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000401 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
402 PIDecl->setSetterName(SetterSel);
403 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000404 // Tailor the diagnostics for the common case where a readwrite
405 // property is declared both in the @interface and the continuation.
406 // This is a common error where the user often intended the original
407 // declaration to be readonly.
408 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000409 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000410 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
411 ? diag::err_use_continuation_class_redeclaration_readwrite
412 : diag::err_use_continuation_class;
413 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000414 << CCPrimary->getDeclName();
415 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000416 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000417 }
418 *isOverridingProperty = true;
419 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000420 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000421 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
422 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000423 if (ASTMutationListener *L = Context.getASTMutationListener())
424 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000425 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000426}
427
428ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
429 ObjCContainerDecl *CDecl,
430 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000431 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000432 FieldDeclarator &FD,
433 Selector GetterSel,
434 Selector SetterSel,
435 const bool isAssign,
436 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000437 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000438 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000439 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000440 tok::ObjCKeywordKind MethodImplKind,
441 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000442 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000443 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000444
445 // Issue a warning if property is 'assign' as default and its object, which is
446 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000447 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000448 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000449 if (const ObjCObjectPointerType *ObjPtrTy =
450 T->getAs<ObjCObjectPointerType>()) {
451 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
452 if (IDecl)
453 if (ObjCProtocolDecl* PNSCopying =
454 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
455 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
456 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000457 }
John McCallc12c5bb2010-05-15 11:32:37 +0000458 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000459 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
460
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000461 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000462 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
463 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000464 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000465
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000466 if (ObjCPropertyDecl *prevDecl =
467 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000468 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000469 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000470 PDecl->setInvalidDecl();
471 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000472 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000473 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000474 if (lexicalDC)
475 PDecl->setLexicalDeclContext(lexicalDC);
476 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000477
478 if (T->isArrayType() || T->isFunctionType()) {
479 Diag(AtLoc, diag::err_property_type) << T;
480 PDecl->setInvalidDecl();
481 }
482
483 ProcessDeclAttributes(S, PDecl, FD.D);
484
485 // Regardless of setter/getter attribute, we save the default getter/setter
486 // selector names in anticipation of declaration of setter/getter methods.
487 PDecl->setGetterName(GetterSel);
488 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000489 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000490 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000491
Bill Wendlingad017fa2012-12-20 19:22:21 +0000492 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000493 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
494
Bill Wendlingad017fa2012-12-20 19:22:21 +0000495 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000496 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
497
Bill Wendlingad017fa2012-12-20 19:22:21 +0000498 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000499 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
500
501 if (isReadWrite)
502 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
503
Bill Wendlingad017fa2012-12-20 19:22:21 +0000504 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000505 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
506
Bill Wendlingad017fa2012-12-20 19:22:21 +0000507 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000508 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
509
Bill Wendlingad017fa2012-12-20 19:22:21 +0000510 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000511 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
512
Bill Wendlingad017fa2012-12-20 19:22:21 +0000513 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000514 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
515
Bill Wendlingad017fa2012-12-20 19:22:21 +0000516 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000517 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
518
Ted Kremenek28685ab2010-03-12 00:46:40 +0000519 if (isAssign)
520 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
521
John McCall265941b2011-09-13 18:31:23 +0000522 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000523 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000524 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000525 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000526 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000527
John McCallf85e1932011-06-15 23:02:42 +0000528 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000529 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000530 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
531 if (isAssign)
532 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
533
Ted Kremenek28685ab2010-03-12 00:46:40 +0000534 if (MethodImplKind == tok::objc_required)
535 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
536 else if (MethodImplKind == tok::objc_optional)
537 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000539 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000540}
541
John McCallf85e1932011-06-15 23:02:42 +0000542static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
543 ObjCPropertyDecl *property,
544 ObjCIvarDecl *ivar) {
545 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
546
John McCallf85e1932011-06-15 23:02:42 +0000547 QualType ivarType = ivar->getType();
548 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000549
John McCall265941b2011-09-13 18:31:23 +0000550 // The lifetime implied by the property's attributes.
551 Qualifiers::ObjCLifetime propertyLifetime =
552 getImpliedARCOwnership(property->getPropertyAttributes(),
553 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000554
John McCall265941b2011-09-13 18:31:23 +0000555 // We're fine if they match.
556 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000557
John McCall265941b2011-09-13 18:31:23 +0000558 // These aren't valid lifetimes for object ivars; don't diagnose twice.
559 if (ivarLifetime == Qualifiers::OCL_None ||
560 ivarLifetime == Qualifiers::OCL_Autoreleasing)
561 return;
John McCallf85e1932011-06-15 23:02:42 +0000562
John McCalld64c2eb2012-08-20 23:36:59 +0000563 // If the ivar is private, and it's implicitly __unsafe_unretained
564 // becaues of its type, then pretend it was actually implicitly
565 // __strong. This is only sound because we're processing the
566 // property implementation before parsing any method bodies.
567 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
568 propertyLifetime == Qualifiers::OCL_Strong &&
569 ivar->getAccessControl() == ObjCIvarDecl::Private) {
570 SplitQualType split = ivarType.split();
571 if (split.Quals.hasObjCLifetime()) {
572 assert(ivarType->isObjCARCImplicitlyUnretainedType());
573 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
574 ivarType = S.Context.getQualifiedType(split);
575 ivar->setType(ivarType);
576 return;
577 }
578 }
579
John McCall265941b2011-09-13 18:31:23 +0000580 switch (propertyLifetime) {
581 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000582 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000583 << property->getDeclName()
584 << ivar->getDeclName()
585 << ivarLifetime;
586 break;
John McCallf85e1932011-06-15 23:02:42 +0000587
John McCall265941b2011-09-13 18:31:23 +0000588 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000589 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000590 << property->getDeclName()
591 << ivar->getDeclName();
592 break;
John McCallf85e1932011-06-15 23:02:42 +0000593
John McCall265941b2011-09-13 18:31:23 +0000594 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000595 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000596 << property->getDeclName()
597 << ivar->getDeclName()
598 << ((property->getPropertyAttributesAsWritten()
599 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
600 break;
John McCallf85e1932011-06-15 23:02:42 +0000601
John McCall265941b2011-09-13 18:31:23 +0000602 case Qualifiers::OCL_Autoreleasing:
603 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000604
John McCall265941b2011-09-13 18:31:23 +0000605 case Qualifiers::OCL_None:
606 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000607 return;
608 }
609
610 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000611 if (propertyImplLoc.isValid())
612 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000613}
614
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000615/// setImpliedPropertyAttributeForReadOnlyProperty -
616/// This routine evaludates life-time attributes for a 'readonly'
617/// property with no known lifetime of its own, using backing
618/// 'ivar's attribute, if any. If no backing 'ivar', property's
619/// life-time is assumed 'strong'.
620static void setImpliedPropertyAttributeForReadOnlyProperty(
621 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
622 Qualifiers::ObjCLifetime propertyLifetime =
623 getImpliedARCOwnership(property->getPropertyAttributes(),
624 property->getType());
625 if (propertyLifetime != Qualifiers::OCL_None)
626 return;
627
628 if (!ivar) {
629 // if no backing ivar, make property 'strong'.
630 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
631 return;
632 }
633 // property assumes owenership of backing ivar.
634 QualType ivarType = ivar->getType();
635 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
636 if (ivarLifetime == Qualifiers::OCL_Strong)
637 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
638 else if (ivarLifetime == Qualifiers::OCL_Weak)
639 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
640 return;
641}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000642
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000643/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
644/// attribute declared in primary class and attributes overridden in any of its
645/// class extensions.
646static void
647DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
648 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000649 unsigned Attributes = property->getPropertyAttributesAsWritten();
650 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000651 for (ObjCInterfaceDecl::known_extensions_iterator
652 Ext = ClassDecl->known_extensions_begin(),
653 ExtEnd = ClassDecl->known_extensions_end();
654 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000655 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregord3297242013-01-16 23:00:23 +0000656 for (ObjCContainerDecl::prop_iterator P = Ext->prop_begin(),
657 E = Ext->prop_end();
658 P != E; ++P) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000659 if ((*P)->getIdentifier() == property->getIdentifier()) {
660 ClassExtProperty = *P;
661 break;
662 }
663 }
664 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000665 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000666 unsigned classExtPropertyAttr =
667 ClassExtProperty->getPropertyAttributesAsWritten();
668 // We are issuing the warning that we postponed because class extensions
669 // can override readonly->readwrite and 'setter' attributes originally
670 // placed on class's property declaration now make sense in the overridden
671 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000672 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000673 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000674 (classExtPropertyAttr &
675 (ObjCDeclSpec::DQ_PR_readwrite|
676 ObjCDeclSpec::DQ_PR_assign |
677 ObjCDeclSpec::DQ_PR_unsafe_unretained |
678 ObjCDeclSpec::DQ_PR_copy |
679 ObjCDeclSpec::DQ_PR_retain |
680 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000681 continue;
682 warn = true;
683 break;
684 }
685 }
686 }
687 if (warn) {
688 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
689 ObjCDeclSpec::DQ_PR_unsafe_unretained |
690 ObjCDeclSpec::DQ_PR_copy |
691 ObjCDeclSpec::DQ_PR_retain |
692 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000693 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000694 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000695 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000696 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000697 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000698 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000699 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000700 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000701 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000702 "retain" : "strong";
703
704 S.Diag(property->getLocation(),
705 diag::warn_objc_property_attr_mutually_exclusive)
706 << "readonly" << which;
707 }
708 }
709
710
711}
712
Ted Kremenek28685ab2010-03-12 00:46:40 +0000713/// ActOnPropertyImplDecl - This routine performs semantic checks and
714/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000715/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716///
John McCalld226f652010-08-21 09:40:31 +0000717Decl *Sema::ActOnPropertyImplDecl(Scope *S,
718 SourceLocation AtLoc,
719 SourceLocation PropertyLoc,
720 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000721 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000722 IdentifierInfo *PropertyIvar,
723 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000724 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000725 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000726 // Make sure we have a context for the property implementation declaration.
727 if (!ClassImpDecl) {
728 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000729 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000730 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000731 if (PropertyIvarLoc.isInvalid())
732 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000733 SourceLocation PropertyDiagLoc = PropertyLoc;
734 if (PropertyDiagLoc.isInvalid())
735 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000736 ObjCPropertyDecl *property = 0;
737 ObjCInterfaceDecl* IDecl = 0;
738 // Find the class or category class where this property must have
739 // a declaration.
740 ObjCImplementationDecl *IC = 0;
741 ObjCCategoryImplDecl* CatImplClass = 0;
742 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
743 IDecl = IC->getClassInterface();
744 // We always synthesize an interface for an implementation
745 // without an interface decl. So, IDecl is always non-zero.
746 assert(IDecl &&
747 "ActOnPropertyImplDecl - @implementation without @interface");
748
749 // Look for this property declaration in the @implementation's @interface
750 property = IDecl->FindPropertyDeclaration(PropertyId);
751 if (!property) {
752 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000753 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000754 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000755 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000756 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
757 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000758 if (AtLoc.isValid())
759 Diag(AtLoc, diag::warn_implicit_atomic_property);
760 else
761 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
762 Diag(property->getLocation(), diag::note_property_declare);
763 }
764
Ted Kremenek28685ab2010-03-12 00:46:40 +0000765 if (const ObjCCategoryDecl *CD =
766 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
767 if (!CD->IsClassExtension()) {
768 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
769 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000770 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000771 }
772 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000773
774 if (Synthesize&&
775 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
776 property->hasAttr<IBOutletAttr>() &&
777 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000778 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
779 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000780 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000781 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000782 property->getLParenLoc(), readonlyLoc)) {
783 SourceLocation endLoc =
784 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
785 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
786 Diag(property->getLocation(),
787 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
788 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
789 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000790 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000791
792 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000793
Ted Kremenek28685ab2010-03-12 00:46:40 +0000794 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
795 if (Synthesize) {
796 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000797 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000798 }
799 IDecl = CatImplClass->getClassInterface();
800 if (!IDecl) {
801 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000802 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000803 }
804 ObjCCategoryDecl *Category =
805 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
806
807 // If category for this implementation not found, it is an error which
808 // has already been reported eralier.
809 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000810 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000811 // Look for this property declaration in @implementation's category
812 property = Category->FindPropertyDeclaration(PropertyId);
813 if (!property) {
814 Diag(PropertyLoc, diag::error_bad_category_property_decl)
815 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000816 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 }
818 } else {
819 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000820 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000821 }
822 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000823 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000824 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000825 // Check that we have a valid, previously declared ivar for @synthesize
826 if (Synthesize) {
827 // @synthesize
828 if (!PropertyIvar)
829 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000830 // Check that this is a previously declared 'ivar' in 'IDecl' interface
831 ObjCInterfaceDecl *ClassDeclared;
832 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
833 QualType PropType = property->getType();
834 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000835
836 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000837 diag::err_incomplete_synthesized_property,
838 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000839 Diag(property->getLocation(), diag::note_property_declare);
840 CompleteTypeErr = true;
841 }
842
David Blaikie4e4d0842012-03-11 07:00:24 +0000843 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000844 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000845 ObjCPropertyDecl::OBJC_PR_readonly) &&
846 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000847 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
848 }
849
John McCallf85e1932011-06-15 23:02:42 +0000850 ObjCPropertyDecl::PropertyAttributeKind kind
851 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000852
853 // Add GC __weak to the ivar type if the property is weak.
854 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000855 getLangOpts().getGC() != LangOptions::NonGC) {
856 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000857 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000858 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000859 Diag(property->getLocation(), diag::note_property_declare);
860 } else {
861 PropertyIvarType =
862 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000863 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000864 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000865 if (AtLoc.isInvalid()) {
866 // Check when default synthesizing a property that there is
867 // an ivar matching property name and issue warning; since this
868 // is the most common case of not using an ivar used for backing
869 // property in non-default synthesis case.
870 ObjCInterfaceDecl *ClassDeclared=0;
871 ObjCIvarDecl *originalIvar =
872 IDecl->lookupInstanceVariable(property->getIdentifier(),
873 ClassDeclared);
874 if (originalIvar) {
875 Diag(PropertyDiagLoc,
876 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000877 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000878 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000879 Diag(property->getLocation(), diag::note_property_declare);
880 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000881 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000882 }
883
884 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000885 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000886 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000887 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000888 !PropertyIvarType.getObjCLifetime() &&
889 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000890
John McCall265941b2011-09-13 18:31:23 +0000891 // It's an error if we have to do this and the user didn't
892 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000893 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000894 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000895 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000896 diag::err_arc_objc_property_default_assign_on_object);
897 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000898 } else {
899 Qualifiers::ObjCLifetime lifetime =
900 getImpliedARCOwnership(kind, PropertyIvarType);
901 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000902 if (lifetime == Qualifiers::OCL_Weak) {
903 bool err = false;
904 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000905 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
906 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
907 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000908 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000909 Diag(property->getLocation(), diag::note_property_declare);
910 err = true;
911 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000912 }
John McCall0a7dd782012-08-21 02:47:43 +0000913 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000914 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000915 Diag(property->getLocation(), diag::note_property_declare);
916 }
John McCallf85e1932011-06-15 23:02:42 +0000917 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000918
John McCallf85e1932011-06-15 23:02:42 +0000919 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000920 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000921 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
922 }
John McCallf85e1932011-06-15 23:02:42 +0000923 }
924
925 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000926 !getLangOpts().ObjCAutoRefCount &&
927 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000928 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000929 Diag(property->getLocation(), diag::note_property_declare);
930 }
931
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000932 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000933 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000934 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000935 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000936 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000937 if (CompleteTypeErr)
938 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000939 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000940 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000941
John McCall260611a2012-06-20 06:18:46 +0000942 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000943 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
944 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000945 // Note! I deliberately want it to fall thru so, we have a
946 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000947 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000948 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000949 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000950 << property->getDeclName() << Ivar->getDeclName()
951 << ClassDeclared->getDeclName();
952 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000953 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000954 // Note! I deliberately want it to fall thru so more errors are caught.
955 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000956 property->setPropertyIvarDecl(Ivar);
957
Ted Kremenek28685ab2010-03-12 00:46:40 +0000958 QualType IvarType = Context.getCanonicalType(Ivar->getType());
959
960 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000961 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000962 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000963 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000964 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000965 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000966 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000967 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000968 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000969 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
970 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000971 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000972 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000973 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000974 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000975 << property->getDeclName() << PropType
976 << Ivar->getDeclName() << IvarType;
977 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000978 // Note! I deliberately want it to fall thru so, we have a
979 // a property implementation and to avoid future warnings.
980 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000981 else {
982 // FIXME! Rules for properties are somewhat different that those
983 // for assignments. Use a new routine to consolidate all cases;
984 // specifically for property redeclarations as well as for ivars.
985 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
986 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
987 if (lhsType != rhsType &&
988 lhsType->isArithmeticType()) {
989 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
990 << property->getDeclName() << PropType
991 << Ivar->getDeclName() << IvarType;
992 Diag(Ivar->getLocation(), diag::note_ivar_decl);
993 // Fall thru - see previous comment
994 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000995 }
996 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000997 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000998 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000999 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001000 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001001 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001002 // Fall thru - see previous comment
1003 }
John McCallf85e1932011-06-15 23:02:42 +00001004 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001005 if ((property->getType()->isObjCObjectPointerType() ||
1006 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001007 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001008 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001009 << property->getDeclName() << Ivar->getDeclName();
1010 // Fall thru - see previous comment
1011 }
1012 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001013 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001014 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001015 } else if (PropertyIvar)
1016 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001017 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001018
Ted Kremenek28685ab2010-03-12 00:46:40 +00001019 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1020 ObjCPropertyImplDecl *PIDecl =
1021 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1022 property,
1023 (Synthesize ?
1024 ObjCPropertyImplDecl::Synthesize
1025 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001026 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001027
Fariborz Jahanian74414712012-05-15 18:12:51 +00001028 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001029 PIDecl->setInvalidDecl();
1030
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001031 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1032 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001033 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001034 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001035 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1036 // returned by the getter as it must conform to C++'s copy-return rules.
1037 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001038 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001039 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1040 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001041 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001042 VK_RValue, PropertyDiagLoc);
1043 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001044 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001045 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001046 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001047 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001048 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001049 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001050 getterMethod->getResultType(),
1051 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001052 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001053 Owned(IvarRefExpr));
1054 if (!Res.isInvalid()) {
1055 Expr *ResExpr = Res.takeAs<Expr>();
1056 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001057 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001058 PIDecl->setGetterCXXConstructor(ResExpr);
1059 }
1060 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001061 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1062 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1063 Diag(getterMethod->getLocation(),
1064 diag::warn_property_getter_owning_mismatch);
1065 Diag(property->getLocation(), diag::note_property_declare);
1066 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001067 }
1068 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1069 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001070 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1071 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001072 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001073 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001074 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1075 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001076 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001077 VK_RValue, PropertyDiagLoc);
1078 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001079 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001080 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001081 SelfExpr, true, true);
1082 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1083 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001084 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001085 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1086 VK_LValue, PropertyDiagLoc);
1087 MarkDeclRefReferenced(rhs);
1088 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001089 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001090 if (property->getPropertyAttributes() &
1091 ObjCPropertyDecl::OBJC_PR_atomic) {
1092 Expr *callExpr = Res.takeAs<Expr>();
1093 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001094 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1095 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001096 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001097 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001098 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001099 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001100 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001101 Diag(FuncDecl->getLocStart(),
1102 diag::note_callee_decl) << FuncDecl;
1103 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001104 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001105 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1106 }
1107 }
1108
Ted Kremenek28685ab2010-03-12 00:46:40 +00001109 if (IC) {
1110 if (Synthesize)
1111 if (ObjCPropertyImplDecl *PPIDecl =
1112 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1113 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1114 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1115 << PropertyIvar;
1116 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1117 }
1118
1119 if (ObjCPropertyImplDecl *PPIDecl
1120 = IC->FindPropertyImplDecl(PropertyId)) {
1121 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1122 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001123 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001124 }
1125 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001126 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001127 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001128 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001129 // Diagnose if an ivar was lazily synthesdized due to a previous
1130 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001131 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001132 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001133 ObjCIvarDecl *Ivar = 0;
1134 if (!Synthesize)
1135 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1136 else {
1137 if (PropertyIvar && PropertyIvar != PropertyId)
1138 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1139 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001140 // Issue diagnostics only if Ivar belongs to current class.
1141 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001142 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001143 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1144 << PropertyId;
1145 Ivar->setInvalidDecl();
1146 }
1147 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001148 } else {
1149 if (Synthesize)
1150 if (ObjCPropertyImplDecl *PPIDecl =
1151 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001152 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001153 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1154 << PropertyIvar;
1155 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1156 }
1157
1158 if (ObjCPropertyImplDecl *PPIDecl =
1159 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001160 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001161 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001162 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001163 }
1164 CatImplClass->addPropertyImplementation(PIDecl);
1165 }
1166
John McCalld226f652010-08-21 09:40:31 +00001167 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001168}
1169
1170//===----------------------------------------------------------------------===//
1171// Helper methods.
1172//===----------------------------------------------------------------------===//
1173
Ted Kremenek9d64c152010-03-12 00:38:38 +00001174/// DiagnosePropertyMismatch - Compares two properties for their
1175/// attributes and types and warns on a variety of inconsistencies.
1176///
1177void
1178Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1179 ObjCPropertyDecl *SuperProperty,
1180 const IdentifierInfo *inheritedName) {
1181 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1182 Property->getPropertyAttributes();
1183 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1184 SuperProperty->getPropertyAttributes();
1185 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1186 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1187 Diag(Property->getLocation(), diag::warn_readonly_property)
1188 << Property->getDeclName() << inheritedName;
1189 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1190 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1191 Diag(Property->getLocation(), diag::warn_property_attribute)
1192 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001193 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001194 unsigned CAttrRetain =
1195 (CAttr &
1196 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1197 unsigned SAttrRetain =
1198 (SAttr &
1199 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1200 bool CStrong = (CAttrRetain != 0);
1201 bool SStrong = (SAttrRetain != 0);
1202 if (CStrong != SStrong)
1203 Diag(Property->getLocation(), diag::warn_property_attribute)
1204 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1205 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001206
1207 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1208 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1209 Diag(Property->getLocation(), diag::warn_property_attribute)
1210 << Property->getDeclName() << "atomic" << inheritedName;
1211 if (Property->getSetterName() != SuperProperty->getSetterName())
1212 Diag(Property->getLocation(), diag::warn_property_attribute)
1213 << Property->getDeclName() << "setter" << inheritedName;
1214 if (Property->getGetterName() != SuperProperty->getGetterName())
1215 Diag(Property->getLocation(), diag::warn_property_attribute)
1216 << Property->getDeclName() << "getter" << inheritedName;
1217
1218 QualType LHSType =
1219 Context.getCanonicalType(SuperProperty->getType());
1220 QualType RHSType =
1221 Context.getCanonicalType(Property->getType());
1222
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001223 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001224 // Do cases not handled in above.
1225 // FIXME. For future support of covariant property types, revisit this.
1226 bool IncompatibleObjC = false;
1227 QualType ConvertedType;
1228 if (!isObjCPointerConversion(RHSType, LHSType,
1229 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001230 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001231 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1232 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001233 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1234 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001235 }
1236}
1237
1238bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1239 ObjCMethodDecl *GetterMethod,
1240 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001241 if (!GetterMethod)
1242 return false;
1243 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1244 QualType PropertyIvarType = property->getType().getNonReferenceType();
1245 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1246 if (!compat) {
1247 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1248 isa<ObjCObjectPointerType>(GetterType))
1249 compat =
1250 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001251 GetterType->getAs<ObjCObjectPointerType>(),
1252 PropertyIvarType->getAs<ObjCObjectPointerType>());
1253 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001254 != Compatible) {
1255 Diag(Loc, diag::error_property_accessor_type)
1256 << property->getDeclName() << PropertyIvarType
1257 << GetterMethod->getSelector() << GetterType;
1258 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1259 return true;
1260 } else {
1261 compat = true;
1262 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1263 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1264 if (lhsType != rhsType && lhsType->isArithmeticType())
1265 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001266 }
1267 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001268
1269 if (!compat) {
1270 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1271 << property->getDeclName()
1272 << GetterMethod->getSelector();
1273 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1274 return true;
1275 }
1276
Ted Kremenek9d64c152010-03-12 00:38:38 +00001277 return false;
1278}
1279
1280/// ComparePropertiesInBaseAndSuper - This routine compares property
1281/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001282/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283///
1284void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1285 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1286 if (!SDecl)
1287 return;
1288 // FIXME: O(N^2)
1289 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
1290 E = SDecl->prop_end(); S != E; ++S) {
David Blaikie581deb32012-06-06 20:45:41 +00001291 ObjCPropertyDecl *SuperPDecl = *S;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001292 // Does property in super class has declaration in current class?
1293 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
1294 E = IDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001295 ObjCPropertyDecl *PDecl = *I;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001296 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
1297 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1298 SDecl->getIdentifier());
1299 }
1300 }
1301}
1302
1303/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1304/// of properties declared in a protocol and compares their attribute against
1305/// the same property declared in the class or category.
1306void
1307Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1308 ObjCProtocolDecl *PDecl) {
1309 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1310 if (!IDecl) {
1311 // Category
1312 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1313 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1314 if (!CatDecl->IsClassExtension())
1315 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1316 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001317 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001318 ObjCCategoryDecl::prop_iterator CP, CE;
1319 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001320 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001321 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001322 break;
1323 if (CP != CE)
1324 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001325 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001326 }
1327 return;
1328 }
1329 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1330 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001331 ObjCPropertyDecl *Pr = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001332 ObjCInterfaceDecl::prop_iterator CP, CE;
1333 // Is this property already in class's list of properties?
1334 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
David Blaikie262bc182012-04-30 02:36:29 +00001335 if (CP->getIdentifier() == Pr->getIdentifier())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001336 break;
1337 if (CP != CE)
1338 // Property protocol already exist in class. Diagnose any mismatch.
David Blaikie581deb32012-06-06 20:45:41 +00001339 DiagnosePropertyMismatch(*CP, Pr, PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 }
1341}
1342
1343/// CompareProperties - This routine compares properties
1344/// declared in 'ClassOrProtocol' objects (which can be a class or an
1345/// inherited protocol with the list of properties for class/category 'CDecl'
1346///
John McCalld226f652010-08-21 09:40:31 +00001347void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1348 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001349 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1350
1351 if (!IDecl) {
1352 // Category
1353 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1354 assert (CatDecl && "CompareProperties");
1355 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1356 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1357 E = MDecl->protocol_end(); P != E; ++P)
1358 // Match properties of category with those of protocol (*P)
1359 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1360
1361 // Go thru the list of protocols for this category and recursively match
1362 // their properties with those in the category.
1363 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1364 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001365 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001366 } else {
1367 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1368 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1369 E = MD->protocol_end(); P != E; ++P)
1370 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1371 }
1372 return;
1373 }
1374
1375 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001376 for (ObjCInterfaceDecl::all_protocol_iterator
1377 P = MDecl->all_referenced_protocol_begin(),
1378 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001379 // Match properties of class IDecl with those of protocol (*P).
1380 MatchOneProtocolPropertiesInClass(IDecl, *P);
1381
1382 // Go thru the list of protocols for this class and recursively match
1383 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001384 for (ObjCInterfaceDecl::all_protocol_iterator
1385 P = IDecl->all_referenced_protocol_begin(),
1386 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001387 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001388 } else {
1389 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1390 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1391 E = MD->protocol_end(); P != E; ++P)
1392 MatchOneProtocolPropertiesInClass(IDecl, *P);
1393 }
1394}
1395
1396/// isPropertyReadonly - Return true if property is readonly, by searching
1397/// for the property in the class and in its categories and implementations
1398///
1399bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1400 ObjCInterfaceDecl *IDecl) {
1401 // by far the most common case.
1402 if (!PDecl->isReadOnly())
1403 return false;
1404 // Even if property is ready only, if interface has a user defined setter,
1405 // it is not considered read only.
1406 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1407 return false;
1408
1409 // Main class has the property as 'readonly'. Must search
1410 // through the category list to see if the property's
1411 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001412 for (ObjCInterfaceDecl::visible_categories_iterator
1413 Cat = IDecl->visible_categories_begin(),
1414 CatEnd = IDecl->visible_categories_end();
1415 Cat != CatEnd; ++Cat) {
1416 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001417 return false;
1418 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001419 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001420 if (P && !P->isReadOnly())
1421 return false;
1422 }
1423
1424 // Also, check for definition of a setter method in the implementation if
1425 // all else failed.
1426 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1427 if (ObjCImplementationDecl *IMD =
1428 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1429 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1430 return false;
1431 } else if (ObjCCategoryImplDecl *CIMD =
1432 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1433 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1434 return false;
1435 }
1436 }
1437 // Lastly, look through the implementation (if one is in scope).
1438 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1439 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1440 return false;
1441 // If all fails, look at the super class.
1442 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1443 return isPropertyReadonly(PDecl, SIDecl);
1444 return true;
1445}
1446
1447/// CollectImmediateProperties - This routine collects all properties in
1448/// the class and its conforming protocols; but not those it its super class.
1449void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001450 ObjCContainerDecl::PropertyMap &PropMap,
1451 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1453 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1454 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001455 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001456 PropMap[Prop->getIdentifier()] = Prop;
1457 }
1458 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001459 for (ObjCInterfaceDecl::all_protocol_iterator
1460 PI = IDecl->all_referenced_protocol_begin(),
1461 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001462 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001463 }
1464 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1465 if (!CATDecl->IsClassExtension())
1466 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1467 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001468 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001469 PropMap[Prop->getIdentifier()] = Prop;
1470 }
1471 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001472 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001474 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 }
1476 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1477 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1478 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001479 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001480 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1481 // Exclude property for protocols which conform to class's super-class,
1482 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001483 if (!PropertyFromSuper ||
1484 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001485 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1486 if (!PropEntry)
1487 PropEntry = Prop;
1488 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001489 }
1490 // scan through protocol's protocols.
1491 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1492 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001493 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001494 }
1495}
1496
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001497/// CollectSuperClassPropertyImplementations - This routine collects list of
1498/// properties to be implemented in super class(s) and also coming from their
1499/// conforming protocols.
1500static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001501 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001502 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1503 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001504 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001505 SDecl = SDecl->getSuperClass();
1506 }
1507 }
1508}
1509
James Dennett699c9042012-06-15 07:13:21 +00001510/// \brief Default synthesizes all properties which must be synthesized
1511/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001512void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1513 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001514
Anna Zaksb36ea372012-10-18 19:17:53 +00001515 ObjCInterfaceDecl::PropertyMap PropMap;
1516 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001517 if (PropMap.empty())
1518 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001519 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001520 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1521
Anna Zaksb36ea372012-10-18 19:17:53 +00001522 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001523 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1524 ObjCPropertyDecl *Prop = P->second;
1525 // If property to be implemented in the super class, ignore.
1526 if (SuperPropMap[Prop->getIdentifier()])
1527 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001529 if (Prop->isInvalidDecl() ||
1530 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1531 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1532 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001533 // Property may have been synthesized by user.
1534 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1535 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001536 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1537 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1538 continue;
1539 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1540 continue;
1541 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001542 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1543 // We won't auto-synthesize properties declared in protocols.
1544 Diag(IMPDecl->getLocation(),
1545 diag::warn_auto_synthesizing_protocol_property);
1546 Diag(Prop->getLocation(), diag::note_property_declare);
1547 continue;
1548 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001549
1550 // We use invalid SourceLocations for the synthesized ivars since they
1551 // aren't really synthesized at a particular location; they just exist.
1552 // Saying that they are located at the @implementation isn't really going
1553 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001554 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1555 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1556 true,
1557 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001558 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001559 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001560 if (PIDecl) {
1561 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001562 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001563 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001564 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001565}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001566
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001567void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001568 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001569 return;
1570 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1571 if (!IC)
1572 return;
1573 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001574 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001575 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001576}
1577
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001578void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001579 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001580 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001581 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1582 ObjCInterfaceDecl *IDecl;
1583 // Gather properties which need not be implemented in this class
1584 // or category.
1585 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1586 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1587 // For categories, no need to implement properties declared in
1588 // its primary class (and its super classes) if property is
1589 // declared in one of those containers.
1590 if ((IDecl = C->getClassInterface()))
1591 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1592 }
1593 if (IDecl)
1594 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001595
Anna Zaksb36ea372012-10-18 19:17:53 +00001596 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001597 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001598 if (PropMap.empty())
1599 return;
1600
1601 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1602 for (ObjCImplDecl::propimpl_iterator
1603 I = IMPDecl->propimpl_begin(),
1604 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001605 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001606
Anna Zaksb36ea372012-10-18 19:17:53 +00001607 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001608 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1609 ObjCPropertyDecl *Prop = P->second;
1610 // Is there a matching propery synthesize/dynamic?
1611 if (Prop->isInvalidDecl() ||
1612 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001613 PropImplMap.count(Prop) ||
1614 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001615 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001616 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001617 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001618 isa<ObjCCategoryDecl>(CDecl) ?
1619 diag::warn_setter_getter_impl_required_in_category :
1620 diag::warn_setter_getter_impl_required)
1621 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001622 Diag(Prop->getLocation(),
1623 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001624 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001625 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001626 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001627 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1628
Ted Kremenek9d64c152010-03-12 00:38:38 +00001629 }
1630
1631 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001632 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001633 isa<ObjCCategoryDecl>(CDecl) ?
1634 diag::warn_setter_getter_impl_required_in_category :
1635 diag::warn_setter_getter_impl_required)
1636 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001637 Diag(Prop->getLocation(),
1638 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001639 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001640 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001641 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001642 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001643 }
1644 }
1645}
1646
1647void
1648Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1649 ObjCContainerDecl* IDecl) {
1650 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001651 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001652 return;
1653 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1654 E = IDecl->prop_end();
1655 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001656 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001657 ObjCMethodDecl *GetterMethod = 0;
1658 ObjCMethodDecl *SetterMethod = 0;
1659 bool LookedUpGetterSetter = false;
1660
Bill Wendlingad017fa2012-12-20 19:22:21 +00001661 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001662 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001663
John McCall265941b2011-09-13 18:31:23 +00001664 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1665 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001666 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1667 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1668 LookedUpGetterSetter = true;
1669 if (GetterMethod) {
1670 Diag(GetterMethod->getLocation(),
1671 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001672 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001673 Diag(Property->getLocation(), diag::note_property_declare);
1674 }
1675 if (SetterMethod) {
1676 Diag(SetterMethod->getLocation(),
1677 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001678 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001679 Diag(Property->getLocation(), diag::note_property_declare);
1680 }
1681 }
1682
Ted Kremenek9d64c152010-03-12 00:38:38 +00001683 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001684 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1685 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001686 continue;
1687 if (const ObjCPropertyImplDecl *PIDecl
1688 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1689 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1690 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001691 if (!LookedUpGetterSetter) {
1692 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1693 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1694 LookedUpGetterSetter = true;
1695 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001696 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1697 SourceLocation MethodLoc =
1698 (GetterMethod ? GetterMethod->getLocation()
1699 : SetterMethod->getLocation());
1700 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001701 << Property->getIdentifier() << (GetterMethod != 0)
1702 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001703 // fixit stuff.
1704 if (!AttributesAsWritten) {
1705 if (Property->getLParenLoc().isValid()) {
1706 // @property () ... case.
1707 SourceRange PropSourceRange(Property->getAtLoc(),
1708 Property->getLParenLoc());
1709 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1710 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1711 }
1712 else {
1713 //@property id etc.
1714 SourceLocation endLoc =
1715 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1716 endLoc = endLoc.getLocWithOffset(-1);
1717 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1718 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1719 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1720 }
1721 }
1722 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1723 // @property () ... case.
1724 SourceLocation endLoc = Property->getLParenLoc();
1725 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1726 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1727 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1728 }
1729 else
1730 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001731 Diag(Property->getLocation(), diag::note_property_declare);
1732 }
1733 }
1734 }
1735}
1736
John McCallf85e1932011-06-15 23:02:42 +00001737void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001738 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001739 return;
1740
1741 for (ObjCImplementationDecl::propimpl_iterator
1742 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001743 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001744 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1745 continue;
1746
1747 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001748 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1749 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001750 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1751 if (!method)
1752 continue;
1753 ObjCMethodFamily family = method->getMethodFamily();
1754 if (family == OMF_alloc || family == OMF_copy ||
1755 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001756 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001757 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1758 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001759 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001760 Diag(PD->getLocation(), diag::note_property_declare);
1761 }
1762 }
1763 }
1764}
1765
John McCall5de74d12010-11-10 07:01:40 +00001766/// AddPropertyAttrs - Propagates attributes from a property to the
1767/// implicitly-declared getter or setter for that property.
1768static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1769 ObjCPropertyDecl *Property) {
1770 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001771 for (Decl::attr_iterator A = Property->attr_begin(),
1772 AEnd = Property->attr_end();
1773 A != AEnd; ++A) {
1774 if (isa<DeprecatedAttr>(*A) ||
1775 isa<UnavailableAttr>(*A) ||
1776 isa<AvailabilityAttr>(*A))
1777 PropertyMethod->addAttr((*A)->clone(S.Context));
1778 }
John McCall5de74d12010-11-10 07:01:40 +00001779}
1780
Ted Kremenek9d64c152010-03-12 00:38:38 +00001781/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1782/// have the property type and issue diagnostics if they don't.
1783/// Also synthesize a getter/setter method if none exist (and update the
1784/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1785/// methods is the "right" thing to do.
1786void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001787 ObjCContainerDecl *CD,
1788 ObjCPropertyDecl *redeclaredProperty,
1789 ObjCContainerDecl *lexicalDC) {
1790
Ted Kremenek9d64c152010-03-12 00:38:38 +00001791 ObjCMethodDecl *GetterMethod, *SetterMethod;
1792
1793 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1794 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1795 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1796 property->getLocation());
1797
1798 if (SetterMethod) {
1799 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1800 property->getPropertyAttributes();
1801 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1802 Context.getCanonicalType(SetterMethod->getResultType()) !=
1803 Context.VoidTy)
1804 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1805 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001806 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001807 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1808 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001809 Diag(property->getLocation(),
1810 diag::warn_accessor_property_type_mismatch)
1811 << property->getDeclName()
1812 << SetterMethod->getSelector();
1813 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1814 }
1815 }
1816
1817 // Synthesize getter/setter methods if none exist.
1818 // Find the default getter and if one not found, add one.
1819 // FIXME: The synthesized property we set here is misleading. We almost always
1820 // synthesize these methods unless the user explicitly provided prototypes
1821 // (which is odd, but allowed). Sema should be typechecking that the
1822 // declarations jive in that situation (which it is not currently).
1823 if (!GetterMethod) {
1824 // No instance method of same name as property getter name was found.
1825 // Declare a getter method and add it to the list of methods
1826 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001827 SourceLocation Loc = redeclaredProperty ?
1828 redeclaredProperty->getLocation() :
1829 property->getLocation();
1830
1831 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1832 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001833 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001834 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001835 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 (property->getPropertyImplementation() ==
1837 ObjCPropertyDecl::Optional) ?
1838 ObjCMethodDecl::Optional :
1839 ObjCMethodDecl::Required);
1840 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001841
1842 AddPropertyAttrs(*this, GetterMethod, property);
1843
Ted Kremenek23173d72010-05-18 21:09:07 +00001844 // FIXME: Eventually this shouldn't be needed, as the lexical context
1845 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001846 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001847 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001848 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1849 GetterMethod->addAttr(
1850 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001851 } else
1852 // A user declared getter will be synthesize when @synthesize of
1853 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001854 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001855 property->setGetterMethodDecl(GetterMethod);
1856
1857 // Skip setter if property is read-only.
1858 if (!property->isReadOnly()) {
1859 // Find the default setter and if one not found, add one.
1860 if (!SetterMethod) {
1861 // No instance method of same name as property setter name was found.
1862 // Declare a setter method and add it to the list of methods
1863 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001864 SourceLocation Loc = redeclaredProperty ?
1865 redeclaredProperty->getLocation() :
1866 property->getLocation();
1867
1868 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001869 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001870 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001871 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001872 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001873 /*isImplicitlyDeclared=*/true,
1874 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001875 (property->getPropertyImplementation() ==
1876 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001877 ObjCMethodDecl::Optional :
1878 ObjCMethodDecl::Required);
1879
Ted Kremenek9d64c152010-03-12 00:38:38 +00001880 // Invent the arguments for the setter. We don't bother making a
1881 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001882 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1883 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001884 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001885 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001887 SC_None,
1888 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001889 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001890 SetterMethod->setMethodParams(Context, Argument,
1891 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001892
1893 AddPropertyAttrs(*this, SetterMethod, property);
1894
Ted Kremenek9d64c152010-03-12 00:38:38 +00001895 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001896 // FIXME: Eventually this shouldn't be needed, as the lexical context
1897 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001898 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001899 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001900 } else
1901 // A user declared setter will be synthesize when @synthesize of
1902 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001903 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001904 property->setSetterMethodDecl(SetterMethod);
1905 }
1906 // Add any synthesized methods to the global pool. This allows us to
1907 // handle the following, which is supported by GCC (and part of the design).
1908 //
1909 // @interface Foo
1910 // @property double bar;
1911 // @end
1912 //
1913 // void thisIsUnfortunate() {
1914 // id foo;
1915 // double bar = [foo bar];
1916 // }
1917 //
1918 if (GetterMethod)
1919 AddInstanceMethodToGlobalPool(GetterMethod);
1920 if (SetterMethod)
1921 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001922
1923 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1924 if (!CurrentClass) {
1925 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1926 CurrentClass = Cat->getClassInterface();
1927 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1928 CurrentClass = Impl->getClassInterface();
1929 }
1930 if (GetterMethod)
1931 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1932 if (SetterMethod)
1933 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001934}
1935
John McCalld226f652010-08-21 09:40:31 +00001936void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001937 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001938 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001939 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001940 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001941 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001942 return;
1943
1944 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001945 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001946
David Blaikie4e4d0842012-03-11 07:00:24 +00001947 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001948 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001949 PropertyTy->isObjCRetainableType()) {
1950 // 'readonly' property with no obvious lifetime.
1951 // its life time will be determined by its backing ivar.
1952 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1953 ObjCDeclSpec::DQ_PR_copy |
1954 ObjCDeclSpec::DQ_PR_retain |
1955 ObjCDeclSpec::DQ_PR_strong |
1956 ObjCDeclSpec::DQ_PR_weak |
1957 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001958 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001959 return;
1960 }
1961
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001962 if (propertyInPrimaryClass) {
1963 // we postpone most property diagnosis until class's implementation
1964 // because, its readonly attribute may be overridden in its class
1965 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001966 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1967 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001968 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1969 << "readonly" << "readwrite";
1970 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001971 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001972 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1973 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001974 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001975 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001976 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001977 ObjCDeclSpec::DQ_PR_retain |
1978 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00001979 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001980 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001981 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001983 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00001984 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001985 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001986 "copy" : "retain";
1987
Bill Wendlingad017fa2012-12-20 19:22:21 +00001988 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001989 diag::err_objc_property_attr_mutually_exclusive :
1990 diag::warn_objc_property_attr_mutually_exclusive)
1991 << "readonly" << which;
1992 }
1993
1994 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001995 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001996 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1997 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001998 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001999 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002000 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2001 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2002 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002003 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002004 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002005 }
2006
2007 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002008 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2009 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002010 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2011 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002012 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002013 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002014 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002015 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2016 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002017 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002018 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002019 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002020 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2021 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002022 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002023 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002024 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002025 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002026 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2027 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002028 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002029 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002030 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2031 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002032 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2033 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002034 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002035 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002036 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002037 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2038 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002039 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002040 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002041 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002042 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2043 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002044 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002045 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002046 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002047 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002048 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2049 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002050 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002051 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002052 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2053 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002054 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2055 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002056 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002057 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002058 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002059 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2060 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002061 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002062 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002063 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002064 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2065 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002066 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002067 }
2068 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2070 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002071 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2072 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002073 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002074 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002075 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2076 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002079 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002080 }
2081
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2083 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002084 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2085 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002086 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002087 }
2088
Ted Kremenek9d64c152010-03-12 00:38:38 +00002089 // Warn if user supplied no assignment attribute, property is
2090 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002091 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002092 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2093 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2094 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002095 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002096 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002097 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002098 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002099 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002100 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002101 bool isAnyClassTy =
2102 (PropertyTy->isObjCClassType() ||
2103 PropertyTy->isObjCQualifiedClassType());
2104 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2105 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002106 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002107 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002108 else if (propertyInPrimaryClass) {
2109 // Don't issue warning on property with no life time in class
2110 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002111 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002112 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002113 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002114
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002115 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002116 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002117 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002118 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002119 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002120
2121 // FIXME: Implement warning dependent on NSCopying being
2122 // implemented. See also:
2123 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2124 // (please trim this list while you are at it).
2125 }
2126
Bill Wendlingad017fa2012-12-20 19:22:21 +00002127 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2128 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002130 && PropertyTy->isBlockPointerType())
2131 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002132 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2133 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2134 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002135 PropertyTy->isBlockPointerType())
2136 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002137
Bill Wendlingad017fa2012-12-20 19:22:21 +00002138 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2139 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002140 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2141
Ted Kremenek9d64c152010-03-12 00:38:38 +00002142}