blob: 82a97f9ee07ebc21341d7ebdee3e9aa2d475fac7 [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 Gregor0dfe23c2013-01-21 18:35:55 +0000656 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
657 for (unsigned I = 0, N = R.size(); I != N; ++I) {
658 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
659 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000660 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000661 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000662
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000663 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000664 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000665 unsigned classExtPropertyAttr =
666 ClassExtProperty->getPropertyAttributesAsWritten();
667 // We are issuing the warning that we postponed because class extensions
668 // can override readonly->readwrite and 'setter' attributes originally
669 // placed on class's property declaration now make sense in the overridden
670 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000671 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000672 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000673 (classExtPropertyAttr &
674 (ObjCDeclSpec::DQ_PR_readwrite|
675 ObjCDeclSpec::DQ_PR_assign |
676 ObjCDeclSpec::DQ_PR_unsafe_unretained |
677 ObjCDeclSpec::DQ_PR_copy |
678 ObjCDeclSpec::DQ_PR_retain |
679 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000680 continue;
681 warn = true;
682 break;
683 }
684 }
685 }
686 if (warn) {
687 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
688 ObjCDeclSpec::DQ_PR_unsafe_unretained |
689 ObjCDeclSpec::DQ_PR_copy |
690 ObjCDeclSpec::DQ_PR_retain |
691 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000692 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000693 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000694 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000695 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000696 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000697 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000698 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000699 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000700 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000701 "retain" : "strong";
702
703 S.Diag(property->getLocation(),
704 diag::warn_objc_property_attr_mutually_exclusive)
705 << "readonly" << which;
706 }
707 }
708
709
710}
711
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712/// ActOnPropertyImplDecl - This routine performs semantic checks and
713/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000714/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000715///
John McCalld226f652010-08-21 09:40:31 +0000716Decl *Sema::ActOnPropertyImplDecl(Scope *S,
717 SourceLocation AtLoc,
718 SourceLocation PropertyLoc,
719 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000720 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000721 IdentifierInfo *PropertyIvar,
722 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000723 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000724 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000725 // Make sure we have a context for the property implementation declaration.
726 if (!ClassImpDecl) {
727 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000728 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000729 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000730 if (PropertyIvarLoc.isInvalid())
731 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000732 SourceLocation PropertyDiagLoc = PropertyLoc;
733 if (PropertyDiagLoc.isInvalid())
734 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000735 ObjCPropertyDecl *property = 0;
736 ObjCInterfaceDecl* IDecl = 0;
737 // Find the class or category class where this property must have
738 // a declaration.
739 ObjCImplementationDecl *IC = 0;
740 ObjCCategoryImplDecl* CatImplClass = 0;
741 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
742 IDecl = IC->getClassInterface();
743 // We always synthesize an interface for an implementation
744 // without an interface decl. So, IDecl is always non-zero.
745 assert(IDecl &&
746 "ActOnPropertyImplDecl - @implementation without @interface");
747
748 // Look for this property declaration in the @implementation's @interface
749 property = IDecl->FindPropertyDeclaration(PropertyId);
750 if (!property) {
751 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000752 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000753 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000754 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000755 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
756 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000757 if (AtLoc.isValid())
758 Diag(AtLoc, diag::warn_implicit_atomic_property);
759 else
760 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
761 Diag(property->getLocation(), diag::note_property_declare);
762 }
763
Ted Kremenek28685ab2010-03-12 00:46:40 +0000764 if (const ObjCCategoryDecl *CD =
765 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
766 if (!CD->IsClassExtension()) {
767 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
768 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000769 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000770 }
771 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000772
773 if (Synthesize&&
774 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
775 property->hasAttr<IBOutletAttr>() &&
776 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000777 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
778 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000779 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000780 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000781 property->getLParenLoc(), readonlyLoc)) {
782 SourceLocation endLoc =
783 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
784 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
785 Diag(property->getLocation(),
786 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
787 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
788 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000789 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000790
791 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000792
Ted Kremenek28685ab2010-03-12 00:46:40 +0000793 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
794 if (Synthesize) {
795 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000796 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000797 }
798 IDecl = CatImplClass->getClassInterface();
799 if (!IDecl) {
800 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000801 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000802 }
803 ObjCCategoryDecl *Category =
804 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
805
806 // If category for this implementation not found, it is an error which
807 // has already been reported eralier.
808 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000809 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000810 // Look for this property declaration in @implementation's category
811 property = Category->FindPropertyDeclaration(PropertyId);
812 if (!property) {
813 Diag(PropertyLoc, diag::error_bad_category_property_decl)
814 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000815 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000816 }
817 } else {
818 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000819 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000820 }
821 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000822 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000823 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000824 // Check that we have a valid, previously declared ivar for @synthesize
825 if (Synthesize) {
826 // @synthesize
827 if (!PropertyIvar)
828 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000829 // Check that this is a previously declared 'ivar' in 'IDecl' interface
830 ObjCInterfaceDecl *ClassDeclared;
831 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
832 QualType PropType = property->getType();
833 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000834
835 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000836 diag::err_incomplete_synthesized_property,
837 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000838 Diag(property->getLocation(), diag::note_property_declare);
839 CompleteTypeErr = true;
840 }
841
David Blaikie4e4d0842012-03-11 07:00:24 +0000842 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000843 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000844 ObjCPropertyDecl::OBJC_PR_readonly) &&
845 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000846 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
847 }
848
John McCallf85e1932011-06-15 23:02:42 +0000849 ObjCPropertyDecl::PropertyAttributeKind kind
850 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000851
852 // Add GC __weak to the ivar type if the property is weak.
853 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000854 getLangOpts().getGC() != LangOptions::NonGC) {
855 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000856 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000857 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000858 Diag(property->getLocation(), diag::note_property_declare);
859 } else {
860 PropertyIvarType =
861 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000862 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000863 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000864 if (AtLoc.isInvalid()) {
865 // Check when default synthesizing a property that there is
866 // an ivar matching property name and issue warning; since this
867 // is the most common case of not using an ivar used for backing
868 // property in non-default synthesis case.
869 ObjCInterfaceDecl *ClassDeclared=0;
870 ObjCIvarDecl *originalIvar =
871 IDecl->lookupInstanceVariable(property->getIdentifier(),
872 ClassDeclared);
873 if (originalIvar) {
874 Diag(PropertyDiagLoc,
875 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000876 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000877 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000878 Diag(property->getLocation(), diag::note_property_declare);
879 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000880 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000881 }
882
883 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000884 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000885 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000886 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000887 !PropertyIvarType.getObjCLifetime() &&
888 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000889
John McCall265941b2011-09-13 18:31:23 +0000890 // It's an error if we have to do this and the user didn't
891 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000892 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000893 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000894 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000895 diag::err_arc_objc_property_default_assign_on_object);
896 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000897 } else {
898 Qualifiers::ObjCLifetime lifetime =
899 getImpliedARCOwnership(kind, PropertyIvarType);
900 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000901 if (lifetime == Qualifiers::OCL_Weak) {
902 bool err = false;
903 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000904 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
905 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
906 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000907 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000908 Diag(property->getLocation(), diag::note_property_declare);
909 err = true;
910 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000911 }
John McCall0a7dd782012-08-21 02:47:43 +0000912 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000913 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000914 Diag(property->getLocation(), diag::note_property_declare);
915 }
John McCallf85e1932011-06-15 23:02:42 +0000916 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000917
John McCallf85e1932011-06-15 23:02:42 +0000918 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000919 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000920 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
921 }
John McCallf85e1932011-06-15 23:02:42 +0000922 }
923
924 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000925 !getLangOpts().ObjCAutoRefCount &&
926 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000927 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000928 Diag(property->getLocation(), diag::note_property_declare);
929 }
930
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000931 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000932 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000933 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000934 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000935 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000936 if (CompleteTypeErr)
937 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000938 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000939 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000940
John McCall260611a2012-06-20 06:18:46 +0000941 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000942 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
943 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000944 // Note! I deliberately want it to fall thru so, we have a
945 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000946 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000947 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000948 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000949 << property->getDeclName() << Ivar->getDeclName()
950 << ClassDeclared->getDeclName();
951 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000952 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000953 // Note! I deliberately want it to fall thru so more errors are caught.
954 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000955 property->setPropertyIvarDecl(Ivar);
956
Ted Kremenek28685ab2010-03-12 00:46:40 +0000957 QualType IvarType = Context.getCanonicalType(Ivar->getType());
958
959 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000960 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000961 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000962 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000963 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000964 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000965 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000966 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000967 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000968 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
969 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000970 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000971 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000972 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000973 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000974 << property->getDeclName() << PropType
975 << Ivar->getDeclName() << IvarType;
976 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000977 // Note! I deliberately want it to fall thru so, we have a
978 // a property implementation and to avoid future warnings.
979 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000980 else {
981 // FIXME! Rules for properties are somewhat different that those
982 // for assignments. Use a new routine to consolidate all cases;
983 // specifically for property redeclarations as well as for ivars.
984 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
985 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
986 if (lhsType != rhsType &&
987 lhsType->isArithmeticType()) {
988 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
989 << property->getDeclName() << PropType
990 << Ivar->getDeclName() << IvarType;
991 Diag(Ivar->getLocation(), diag::note_ivar_decl);
992 // Fall thru - see previous comment
993 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000994 }
995 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000996 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000997 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000998 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000999 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001000 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001001 // Fall thru - see previous comment
1002 }
John McCallf85e1932011-06-15 23:02:42 +00001003 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001004 if ((property->getType()->isObjCObjectPointerType() ||
1005 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001006 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001007 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001008 << property->getDeclName() << Ivar->getDeclName();
1009 // Fall thru - see previous comment
1010 }
1011 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001012 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001013 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001014 } else if (PropertyIvar)
1015 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001016 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001017
Ted Kremenek28685ab2010-03-12 00:46:40 +00001018 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1019 ObjCPropertyImplDecl *PIDecl =
1020 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1021 property,
1022 (Synthesize ?
1023 ObjCPropertyImplDecl::Synthesize
1024 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001025 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001026
Fariborz Jahanian74414712012-05-15 18:12:51 +00001027 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001028 PIDecl->setInvalidDecl();
1029
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001030 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1031 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001032 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001033 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001034 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1035 // returned by the getter as it must conform to C++'s copy-return rules.
1036 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001037 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001038 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1039 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001040 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001041 VK_RValue, PropertyDiagLoc);
1042 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001043 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001044 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001045 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001046 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001047 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001048 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001049 getterMethod->getResultType(),
1050 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001051 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001052 Owned(IvarRefExpr));
1053 if (!Res.isInvalid()) {
1054 Expr *ResExpr = Res.takeAs<Expr>();
1055 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001056 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001057 PIDecl->setGetterCXXConstructor(ResExpr);
1058 }
1059 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001060 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1061 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1062 Diag(getterMethod->getLocation(),
1063 diag::warn_property_getter_owning_mismatch);
1064 Diag(property->getLocation(), diag::note_property_declare);
1065 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001066 }
1067 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1068 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001069 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1070 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001071 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001072 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001073 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1074 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001075 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001076 VK_RValue, PropertyDiagLoc);
1077 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001078 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001079 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001080 SelfExpr, true, true);
1081 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1082 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001083 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001084 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1085 VK_LValue, PropertyDiagLoc);
1086 MarkDeclRefReferenced(rhs);
1087 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001088 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001089 if (property->getPropertyAttributes() &
1090 ObjCPropertyDecl::OBJC_PR_atomic) {
1091 Expr *callExpr = Res.takeAs<Expr>();
1092 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001093 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1094 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001095 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001096 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001097 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001098 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001099 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001100 Diag(FuncDecl->getLocStart(),
1101 diag::note_callee_decl) << FuncDecl;
1102 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001103 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001104 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1105 }
1106 }
1107
Ted Kremenek28685ab2010-03-12 00:46:40 +00001108 if (IC) {
1109 if (Synthesize)
1110 if (ObjCPropertyImplDecl *PPIDecl =
1111 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1112 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1113 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1114 << PropertyIvar;
1115 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1116 }
1117
1118 if (ObjCPropertyImplDecl *PPIDecl
1119 = IC->FindPropertyImplDecl(PropertyId)) {
1120 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1121 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001122 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001123 }
1124 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001125 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001126 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001127 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001128 // Diagnose if an ivar was lazily synthesdized due to a previous
1129 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001130 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001131 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001132 ObjCIvarDecl *Ivar = 0;
1133 if (!Synthesize)
1134 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1135 else {
1136 if (PropertyIvar && PropertyIvar != PropertyId)
1137 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1138 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001139 // Issue diagnostics only if Ivar belongs to current class.
1140 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001141 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001142 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1143 << PropertyId;
1144 Ivar->setInvalidDecl();
1145 }
1146 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001147 } else {
1148 if (Synthesize)
1149 if (ObjCPropertyImplDecl *PPIDecl =
1150 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001151 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001152 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1153 << PropertyIvar;
1154 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1155 }
1156
1157 if (ObjCPropertyImplDecl *PPIDecl =
1158 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001159 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001160 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001161 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001162 }
1163 CatImplClass->addPropertyImplementation(PIDecl);
1164 }
1165
John McCalld226f652010-08-21 09:40:31 +00001166 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001167}
1168
1169//===----------------------------------------------------------------------===//
1170// Helper methods.
1171//===----------------------------------------------------------------------===//
1172
Ted Kremenek9d64c152010-03-12 00:38:38 +00001173/// DiagnosePropertyMismatch - Compares two properties for their
1174/// attributes and types and warns on a variety of inconsistencies.
1175///
1176void
1177Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1178 ObjCPropertyDecl *SuperProperty,
1179 const IdentifierInfo *inheritedName) {
1180 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1181 Property->getPropertyAttributes();
1182 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1183 SuperProperty->getPropertyAttributes();
1184 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1185 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1186 Diag(Property->getLocation(), diag::warn_readonly_property)
1187 << Property->getDeclName() << inheritedName;
1188 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1189 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1190 Diag(Property->getLocation(), diag::warn_property_attribute)
1191 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001192 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001193 unsigned CAttrRetain =
1194 (CAttr &
1195 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1196 unsigned SAttrRetain =
1197 (SAttr &
1198 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1199 bool CStrong = (CAttrRetain != 0);
1200 bool SStrong = (SAttrRetain != 0);
1201 if (CStrong != SStrong)
1202 Diag(Property->getLocation(), diag::warn_property_attribute)
1203 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1204 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001205
1206 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1207 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1208 Diag(Property->getLocation(), diag::warn_property_attribute)
1209 << Property->getDeclName() << "atomic" << inheritedName;
1210 if (Property->getSetterName() != SuperProperty->getSetterName())
1211 Diag(Property->getLocation(), diag::warn_property_attribute)
1212 << Property->getDeclName() << "setter" << inheritedName;
1213 if (Property->getGetterName() != SuperProperty->getGetterName())
1214 Diag(Property->getLocation(), diag::warn_property_attribute)
1215 << Property->getDeclName() << "getter" << inheritedName;
1216
1217 QualType LHSType =
1218 Context.getCanonicalType(SuperProperty->getType());
1219 QualType RHSType =
1220 Context.getCanonicalType(Property->getType());
1221
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001222 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001223 // Do cases not handled in above.
1224 // FIXME. For future support of covariant property types, revisit this.
1225 bool IncompatibleObjC = false;
1226 QualType ConvertedType;
1227 if (!isObjCPointerConversion(RHSType, LHSType,
1228 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001229 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001230 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1231 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001232 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1233 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001234 }
1235}
1236
1237bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1238 ObjCMethodDecl *GetterMethod,
1239 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001240 if (!GetterMethod)
1241 return false;
1242 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1243 QualType PropertyIvarType = property->getType().getNonReferenceType();
1244 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1245 if (!compat) {
1246 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1247 isa<ObjCObjectPointerType>(GetterType))
1248 compat =
1249 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001250 GetterType->getAs<ObjCObjectPointerType>(),
1251 PropertyIvarType->getAs<ObjCObjectPointerType>());
1252 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001253 != Compatible) {
1254 Diag(Loc, diag::error_property_accessor_type)
1255 << property->getDeclName() << PropertyIvarType
1256 << GetterMethod->getSelector() << GetterType;
1257 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1258 return true;
1259 } else {
1260 compat = true;
1261 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1262 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1263 if (lhsType != rhsType && lhsType->isArithmeticType())
1264 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001265 }
1266 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001267
1268 if (!compat) {
1269 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1270 << property->getDeclName()
1271 << GetterMethod->getSelector();
1272 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1273 return true;
1274 }
1275
Ted Kremenek9d64c152010-03-12 00:38:38 +00001276 return false;
1277}
1278
1279/// ComparePropertiesInBaseAndSuper - This routine compares property
1280/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001281/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001282///
1283void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
1284 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1285 if (!SDecl)
1286 return;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001287 // FIXME: We should perform this check when the property in the subclass
1288 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001289 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;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001292 DeclContext::lookup_result Results
1293 = IDecl->lookup(SuperPDecl->getDeclName());
1294 for (unsigned I = 0, N = Results.size(); I != N; ++I) {
1295 if (ObjCPropertyDecl *PDecl = dyn_cast<ObjCPropertyDecl>(Results[I])) {
1296 DiagnosePropertyMismatch(PDecl, SuperPDecl,
1297 SDecl->getIdentifier());
1298 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001299 }
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
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001307Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1308 if (!CDecl)
1309 return;
1310
1311 // Category case.
1312 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1313 // FIXME: We should perform this check when the property in the category
1314 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001315 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1316 if (!CatDecl->IsClassExtension())
1317 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1318 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001319 ObjCPropertyDecl *ProtoProp = *P;
1320 DeclContext::lookup_result R
1321 = CatDecl->lookup(ProtoProp->getDeclName());
1322 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1323 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1324 if (CatProp != ProtoProp) {
1325 // Property protocol already exist in class. Diagnose any mismatch.
1326 DiagnosePropertyMismatch(CatProp, ProtoProp,
1327 PDecl->getIdentifier());
1328 }
1329 }
1330 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001331 }
1332 return;
1333 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001334
1335 // Class
1336 // FIXME: We should perform this check when the property in the class
1337 // is declared.
1338 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001339 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001340 E = PDecl->prop_end(); P != E; ++P) {
1341 ObjCPropertyDecl *ProtoProp = *P;
1342 DeclContext::lookup_result R
1343 = IDecl->lookup(ProtoProp->getDeclName());
1344 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1345 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1346 if (ClassProp != ProtoProp) {
1347 // Property protocol already exist in class. Diagnose any mismatch.
1348 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1349 PDecl->getIdentifier());
1350 }
1351 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001353 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001354}
1355
1356/// CompareProperties - This routine compares properties
1357/// declared in 'ClassOrProtocol' objects (which can be a class or an
1358/// inherited protocol with the list of properties for class/category 'CDecl'
1359///
John McCalld226f652010-08-21 09:40:31 +00001360void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1361 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1363
1364 if (!IDecl) {
1365 // Category
1366 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1367 assert (CatDecl && "CompareProperties");
1368 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1369 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1370 E = MDecl->protocol_end(); P != E; ++P)
1371 // Match properties of category with those of protocol (*P)
1372 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1373
1374 // Go thru the list of protocols for this category and recursively match
1375 // their properties with those in the category.
1376 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1377 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001378 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001379 } else {
1380 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1381 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1382 E = MD->protocol_end(); P != E; ++P)
1383 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1384 }
1385 return;
1386 }
1387
1388 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001389 for (ObjCInterfaceDecl::all_protocol_iterator
1390 P = MDecl->all_referenced_protocol_begin(),
1391 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001392 // Match properties of class IDecl with those of protocol (*P).
1393 MatchOneProtocolPropertiesInClass(IDecl, *P);
1394
1395 // Go thru the list of protocols for this class and recursively match
1396 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001397 for (ObjCInterfaceDecl::all_protocol_iterator
1398 P = IDecl->all_referenced_protocol_begin(),
1399 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001400 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001401 } else {
1402 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1403 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1404 E = MD->protocol_end(); P != E; ++P)
1405 MatchOneProtocolPropertiesInClass(IDecl, *P);
1406 }
1407}
1408
1409/// isPropertyReadonly - Return true if property is readonly, by searching
1410/// for the property in the class and in its categories and implementations
1411///
1412bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1413 ObjCInterfaceDecl *IDecl) {
1414 // by far the most common case.
1415 if (!PDecl->isReadOnly())
1416 return false;
1417 // Even if property is ready only, if interface has a user defined setter,
1418 // it is not considered read only.
1419 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1420 return false;
1421
1422 // Main class has the property as 'readonly'. Must search
1423 // through the category list to see if the property's
1424 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001425 for (ObjCInterfaceDecl::visible_categories_iterator
1426 Cat = IDecl->visible_categories_begin(),
1427 CatEnd = IDecl->visible_categories_end();
1428 Cat != CatEnd; ++Cat) {
1429 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001430 return false;
1431 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001432 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 if (P && !P->isReadOnly())
1434 return false;
1435 }
1436
1437 // Also, check for definition of a setter method in the implementation if
1438 // all else failed.
1439 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1440 if (ObjCImplementationDecl *IMD =
1441 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1442 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1443 return false;
1444 } else if (ObjCCategoryImplDecl *CIMD =
1445 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1446 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1447 return false;
1448 }
1449 }
1450 // Lastly, look through the implementation (if one is in scope).
1451 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1452 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1453 return false;
1454 // If all fails, look at the super class.
1455 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1456 return isPropertyReadonly(PDecl, SIDecl);
1457 return true;
1458}
1459
1460/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001461/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001462void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001463 ObjCContainerDecl::PropertyMap &PropMap,
1464 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001465 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1466 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1467 E = IDecl->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 (ObjCInterfaceDecl::all_protocol_iterator
1473 PI = IDecl->all_referenced_protocol_begin(),
1474 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001475 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001476 }
1477 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1478 if (!CATDecl->IsClassExtension())
1479 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1480 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001481 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001482 PropMap[Prop->getIdentifier()] = Prop;
1483 }
1484 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001485 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001486 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001487 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001488 }
1489 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1490 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1491 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001492 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001493 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1494 // Exclude property for protocols which conform to class's super-class,
1495 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001496 if (!PropertyFromSuper ||
1497 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001498 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1499 if (!PropEntry)
1500 PropEntry = Prop;
1501 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001502 }
1503 // scan through protocol's protocols.
1504 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1505 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001506 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001507 }
1508}
1509
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001510/// CollectSuperClassPropertyImplementations - This routine collects list of
1511/// properties to be implemented in super class(s) and also coming from their
1512/// conforming protocols.
1513static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001514 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001515 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1516 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001517 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001518 SDecl = SDecl->getSuperClass();
1519 }
1520 }
1521}
1522
James Dennett699c9042012-06-15 07:13:21 +00001523/// \brief Default synthesizes all properties which must be synthesized
1524/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001525void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1526 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001527
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 ObjCInterfaceDecl::PropertyMap PropMap;
1529 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001530 if (PropMap.empty())
1531 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001532 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001533 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1534
Anna Zaksb36ea372012-10-18 19:17:53 +00001535 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001536 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1537 ObjCPropertyDecl *Prop = P->second;
1538 // If property to be implemented in the super class, ignore.
1539 if (SuperPropMap[Prop->getIdentifier()])
1540 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001541 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001542 if (Prop->isInvalidDecl() ||
1543 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1544 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1545 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001546 // Property may have been synthesized by user.
1547 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1548 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001549 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1550 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1551 continue;
1552 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1553 continue;
1554 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001555 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1556 // We won't auto-synthesize properties declared in protocols.
1557 Diag(IMPDecl->getLocation(),
1558 diag::warn_auto_synthesizing_protocol_property);
1559 Diag(Prop->getLocation(), diag::note_property_declare);
1560 continue;
1561 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001562
1563 // We use invalid SourceLocations for the synthesized ivars since they
1564 // aren't really synthesized at a particular location; they just exist.
1565 // Saying that they are located at the @implementation isn't really going
1566 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001567 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1568 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1569 true,
1570 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001571 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001572 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001573 if (PIDecl) {
1574 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001575 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001576 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001577 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001578}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001579
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001580void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001581 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001582 return;
1583 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1584 if (!IC)
1585 return;
1586 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001587 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001588 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001589}
1590
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001591void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001592 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001593 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001594 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1595 ObjCInterfaceDecl *IDecl;
1596 // Gather properties which need not be implemented in this class
1597 // or category.
1598 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1599 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1600 // For categories, no need to implement properties declared in
1601 // its primary class (and its super classes) if property is
1602 // declared in one of those containers.
1603 if ((IDecl = C->getClassInterface()))
1604 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1605 }
1606 if (IDecl)
1607 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001608
Anna Zaksb36ea372012-10-18 19:17:53 +00001609 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001610 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611 if (PropMap.empty())
1612 return;
1613
1614 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1615 for (ObjCImplDecl::propimpl_iterator
1616 I = IMPDecl->propimpl_begin(),
1617 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001618 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001619
Anna Zaksb36ea372012-10-18 19:17:53 +00001620 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001621 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1622 ObjCPropertyDecl *Prop = P->second;
1623 // Is there a matching propery synthesize/dynamic?
1624 if (Prop->isInvalidDecl() ||
1625 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001626 PropImplMap.count(Prop) ||
1627 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001628 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001629 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001630 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001631 isa<ObjCCategoryDecl>(CDecl) ?
1632 diag::warn_setter_getter_impl_required_in_category :
1633 diag::warn_setter_getter_impl_required)
1634 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001635 Diag(Prop->getLocation(),
1636 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001637 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001638 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001639 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001640 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1641
Ted Kremenek9d64c152010-03-12 00:38:38 +00001642 }
1643
1644 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001645 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001646 isa<ObjCCategoryDecl>(CDecl) ?
1647 diag::warn_setter_getter_impl_required_in_category :
1648 diag::warn_setter_getter_impl_required)
1649 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001650 Diag(Prop->getLocation(),
1651 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001652 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001653 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001654 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001655 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001656 }
1657 }
1658}
1659
1660void
1661Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1662 ObjCContainerDecl* IDecl) {
1663 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001664 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001665 return;
1666 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1667 E = IDecl->prop_end();
1668 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001669 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001670 ObjCMethodDecl *GetterMethod = 0;
1671 ObjCMethodDecl *SetterMethod = 0;
1672 bool LookedUpGetterSetter = false;
1673
Bill Wendlingad017fa2012-12-20 19:22:21 +00001674 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001675 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001676
John McCall265941b2011-09-13 18:31:23 +00001677 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1678 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001679 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1680 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1681 LookedUpGetterSetter = true;
1682 if (GetterMethod) {
1683 Diag(GetterMethod->getLocation(),
1684 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001685 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001686 Diag(Property->getLocation(), diag::note_property_declare);
1687 }
1688 if (SetterMethod) {
1689 Diag(SetterMethod->getLocation(),
1690 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001691 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001692 Diag(Property->getLocation(), diag::note_property_declare);
1693 }
1694 }
1695
Ted Kremenek9d64c152010-03-12 00:38:38 +00001696 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001697 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1698 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001699 continue;
1700 if (const ObjCPropertyImplDecl *PIDecl
1701 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1702 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1703 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001704 if (!LookedUpGetterSetter) {
1705 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1706 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1707 LookedUpGetterSetter = true;
1708 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001709 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1710 SourceLocation MethodLoc =
1711 (GetterMethod ? GetterMethod->getLocation()
1712 : SetterMethod->getLocation());
1713 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001714 << Property->getIdentifier() << (GetterMethod != 0)
1715 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001716 // fixit stuff.
1717 if (!AttributesAsWritten) {
1718 if (Property->getLParenLoc().isValid()) {
1719 // @property () ... case.
1720 SourceRange PropSourceRange(Property->getAtLoc(),
1721 Property->getLParenLoc());
1722 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1723 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1724 }
1725 else {
1726 //@property id etc.
1727 SourceLocation endLoc =
1728 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1729 endLoc = endLoc.getLocWithOffset(-1);
1730 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1731 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1732 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1733 }
1734 }
1735 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1736 // @property () ... case.
1737 SourceLocation endLoc = Property->getLParenLoc();
1738 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1739 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1740 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1741 }
1742 else
1743 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001744 Diag(Property->getLocation(), diag::note_property_declare);
1745 }
1746 }
1747 }
1748}
1749
John McCallf85e1932011-06-15 23:02:42 +00001750void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001751 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001752 return;
1753
1754 for (ObjCImplementationDecl::propimpl_iterator
1755 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001756 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001757 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1758 continue;
1759
1760 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001761 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1762 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001763 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1764 if (!method)
1765 continue;
1766 ObjCMethodFamily family = method->getMethodFamily();
1767 if (family == OMF_alloc || family == OMF_copy ||
1768 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001769 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001770 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1771 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001772 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001773 Diag(PD->getLocation(), diag::note_property_declare);
1774 }
1775 }
1776 }
1777}
1778
John McCall5de74d12010-11-10 07:01:40 +00001779/// AddPropertyAttrs - Propagates attributes from a property to the
1780/// implicitly-declared getter or setter for that property.
1781static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1782 ObjCPropertyDecl *Property) {
1783 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001784 for (Decl::attr_iterator A = Property->attr_begin(),
1785 AEnd = Property->attr_end();
1786 A != AEnd; ++A) {
1787 if (isa<DeprecatedAttr>(*A) ||
1788 isa<UnavailableAttr>(*A) ||
1789 isa<AvailabilityAttr>(*A))
1790 PropertyMethod->addAttr((*A)->clone(S.Context));
1791 }
John McCall5de74d12010-11-10 07:01:40 +00001792}
1793
Ted Kremenek9d64c152010-03-12 00:38:38 +00001794/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1795/// have the property type and issue diagnostics if they don't.
1796/// Also synthesize a getter/setter method if none exist (and update the
1797/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1798/// methods is the "right" thing to do.
1799void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001800 ObjCContainerDecl *CD,
1801 ObjCPropertyDecl *redeclaredProperty,
1802 ObjCContainerDecl *lexicalDC) {
1803
Ted Kremenek9d64c152010-03-12 00:38:38 +00001804 ObjCMethodDecl *GetterMethod, *SetterMethod;
1805
1806 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1807 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1808 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1809 property->getLocation());
1810
1811 if (SetterMethod) {
1812 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1813 property->getPropertyAttributes();
1814 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1815 Context.getCanonicalType(SetterMethod->getResultType()) !=
1816 Context.VoidTy)
1817 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1818 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001819 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001820 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1821 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001822 Diag(property->getLocation(),
1823 diag::warn_accessor_property_type_mismatch)
1824 << property->getDeclName()
1825 << SetterMethod->getSelector();
1826 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1827 }
1828 }
1829
1830 // Synthesize getter/setter methods if none exist.
1831 // Find the default getter and if one not found, add one.
1832 // FIXME: The synthesized property we set here is misleading. We almost always
1833 // synthesize these methods unless the user explicitly provided prototypes
1834 // (which is odd, but allowed). Sema should be typechecking that the
1835 // declarations jive in that situation (which it is not currently).
1836 if (!GetterMethod) {
1837 // No instance method of same name as property getter name was found.
1838 // Declare a getter method and add it to the list of methods
1839 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001840 SourceLocation Loc = redeclaredProperty ?
1841 redeclaredProperty->getLocation() :
1842 property->getLocation();
1843
1844 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1845 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001846 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001847 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001848 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001849 (property->getPropertyImplementation() ==
1850 ObjCPropertyDecl::Optional) ?
1851 ObjCMethodDecl::Optional :
1852 ObjCMethodDecl::Required);
1853 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001854
1855 AddPropertyAttrs(*this, GetterMethod, property);
1856
Ted Kremenek23173d72010-05-18 21:09:07 +00001857 // FIXME: Eventually this shouldn't be needed, as the lexical context
1858 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001859 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001860 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001861 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1862 GetterMethod->addAttr(
1863 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001864 } else
1865 // A user declared getter will be synthesize when @synthesize of
1866 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001867 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001868 property->setGetterMethodDecl(GetterMethod);
1869
1870 // Skip setter if property is read-only.
1871 if (!property->isReadOnly()) {
1872 // Find the default setter and if one not found, add one.
1873 if (!SetterMethod) {
1874 // No instance method of same name as property setter name was found.
1875 // Declare a setter method and add it to the list of methods
1876 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001877 SourceLocation Loc = redeclaredProperty ?
1878 redeclaredProperty->getLocation() :
1879 property->getLocation();
1880
1881 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001882 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001883 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001884 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001885 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001886 /*isImplicitlyDeclared=*/true,
1887 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001888 (property->getPropertyImplementation() ==
1889 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001890 ObjCMethodDecl::Optional :
1891 ObjCMethodDecl::Required);
1892
Ted Kremenek9d64c152010-03-12 00:38:38 +00001893 // Invent the arguments for the setter. We don't bother making a
1894 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001895 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1896 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001897 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001898 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001899 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001900 SC_None,
1901 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001902 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001903 SetterMethod->setMethodParams(Context, Argument,
1904 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001905
1906 AddPropertyAttrs(*this, SetterMethod, property);
1907
Ted Kremenek9d64c152010-03-12 00:38:38 +00001908 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001909 // FIXME: Eventually this shouldn't be needed, as the lexical context
1910 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001911 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001912 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 } else
1914 // A user declared setter will be synthesize when @synthesize of
1915 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001916 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001917 property->setSetterMethodDecl(SetterMethod);
1918 }
1919 // Add any synthesized methods to the global pool. This allows us to
1920 // handle the following, which is supported by GCC (and part of the design).
1921 //
1922 // @interface Foo
1923 // @property double bar;
1924 // @end
1925 //
1926 // void thisIsUnfortunate() {
1927 // id foo;
1928 // double bar = [foo bar];
1929 // }
1930 //
1931 if (GetterMethod)
1932 AddInstanceMethodToGlobalPool(GetterMethod);
1933 if (SetterMethod)
1934 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001935
1936 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1937 if (!CurrentClass) {
1938 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1939 CurrentClass = Cat->getClassInterface();
1940 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1941 CurrentClass = Impl->getClassInterface();
1942 }
1943 if (GetterMethod)
1944 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1945 if (SetterMethod)
1946 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001947}
1948
John McCalld226f652010-08-21 09:40:31 +00001949void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001950 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001951 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001952 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001953 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001954 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001955 return;
1956
1957 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001958 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001959
David Blaikie4e4d0842012-03-11 07:00:24 +00001960 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001961 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001962 PropertyTy->isObjCRetainableType()) {
1963 // 'readonly' property with no obvious lifetime.
1964 // its life time will be determined by its backing ivar.
1965 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1966 ObjCDeclSpec::DQ_PR_copy |
1967 ObjCDeclSpec::DQ_PR_retain |
1968 ObjCDeclSpec::DQ_PR_strong |
1969 ObjCDeclSpec::DQ_PR_weak |
1970 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001971 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001972 return;
1973 }
1974
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001975 if (propertyInPrimaryClass) {
1976 // we postpone most property diagnosis until class's implementation
1977 // because, its readonly attribute may be overridden in its class
1978 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001979 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1980 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001981 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1982 << "readonly" << "readwrite";
1983 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001984 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001985 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1986 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001988 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001989 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001990 ObjCDeclSpec::DQ_PR_retain |
1991 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00001992 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001994 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001996 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00001997 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001998 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001999 "copy" : "retain";
2000
Bill Wendlingad017fa2012-12-20 19:22:21 +00002001 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002002 diag::err_objc_property_attr_mutually_exclusive :
2003 diag::warn_objc_property_attr_mutually_exclusive)
2004 << "readonly" << which;
2005 }
2006
2007 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002008 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002009 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2010 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002011 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002012 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002013 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2014 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2015 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002016 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002017 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002018 }
2019
2020 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002021 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2022 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002023 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2024 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002025 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002026 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002027 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002028 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2029 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002030 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002031 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002032 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002033 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2034 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002035 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002036 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002037 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002038 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002039 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2040 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002041 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002042 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002043 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2044 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002045 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2046 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002047 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002048 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002049 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002050 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2051 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002052 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002053 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002054 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002055 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2056 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002057 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002058 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002059 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002060 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002061 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2062 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002063 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002064 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002065 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2066 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002067 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2068 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002070 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002071 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002072 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2073 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002074 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002075 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002076 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002079 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002080 }
2081 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2083 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002084 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2085 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002086 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002087 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002088 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2089 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002090 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2091 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002092 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002093 }
2094
Bill Wendlingad017fa2012-12-20 19:22:21 +00002095 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2096 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002097 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2098 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002099 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002100 }
2101
Ted Kremenek9d64c152010-03-12 00:38:38 +00002102 // Warn if user supplied no assignment attribute, property is
2103 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002104 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002105 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2106 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2107 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002108 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002109 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002110 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002111 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002112 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002113 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002114 bool isAnyClassTy =
2115 (PropertyTy->isObjCClassType() ||
2116 PropertyTy->isObjCQualifiedClassType());
2117 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2118 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002119 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002120 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002121 else if (propertyInPrimaryClass) {
2122 // Don't issue warning on property with no life time in class
2123 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002124 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002125 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002126 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002127
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002128 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002129 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002130 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002131 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002132 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002133
2134 // FIXME: Implement warning dependent on NSCopying being
2135 // implemented. See also:
2136 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2137 // (please trim this list while you are at it).
2138 }
2139
Bill Wendlingad017fa2012-12-20 19:22:21 +00002140 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2141 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002142 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002143 && PropertyTy->isBlockPointerType())
2144 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002145 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2146 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2147 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002148 PropertyTy->isBlockPointerType())
2149 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002150
Bill Wendlingad017fa2012-12-20 19:22:21 +00002151 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2152 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002153 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2154
Ted Kremenek9d64c152010-03-12 00:38:38 +00002155}