blob: 98d70a58abf6b7743da443cc811b91d2464a56c1 [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
Douglas Gregoraabd0942013-01-21 19:05:22 +0000142 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000143 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000144 ObjCPropertyDecl *Res = 0;
145 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000146 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000147 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000148 FD, GetterSel, SetterSel,
149 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000150 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000151 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000152 isOverridingProperty, TSI,
153 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000154 if (!Res)
155 return 0;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000156 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000157 }
158
159 if (!Res) {
160 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
161 GetterSel, SetterSel, isAssign, isReadWrite,
162 Attributes, ODS.getPropertyAttributes(),
163 TSI, MethodImplKind);
164 if (lexicalDC)
165 Res->setLexicalDeclContext(lexicalDC);
166 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000167
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000168 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000169 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000170 (isa<ObjCInterfaceDecl>(ClassDecl) ||
171 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000172
David Blaikie4e4d0842012-03-11 07:00:24 +0000173 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000174 checkARCPropertyDecl(*this, Res);
175
Douglas Gregoraabd0942013-01-21 19:05:22 +0000176 // Compare this property against the property in our superclass.
177 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
178 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
179 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
180 for (unsigned I = 0, N = R.size(); I != N; ++I)
181 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I]))
182 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier());
183 }
184 }
185
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000186 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000187 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000188}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000189
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000190static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000191makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000192 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000193 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000195 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000197 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000198 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000199 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000200 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000201 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000202 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000203 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000204 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000205 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000206 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000207 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000208 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000209 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000210 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000211 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000212 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000213 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000214 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000215 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000216 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
217
218 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
219}
220
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000221static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000222 SourceLocation LParenLoc, SourceLocation &Loc) {
223 if (LParenLoc.isMacroID())
224 return false;
225
226 SourceManager &SM = Context.getSourceManager();
227 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
228 // Try to load the file buffer.
229 bool invalidTemp = false;
230 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
231 if (invalidTemp)
232 return false;
233 const char *tokenBegin = file.data() + locInfo.second;
234
235 // Lex from the start of the given location.
236 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
237 Context.getLangOpts(),
238 file.begin(), tokenBegin, file.end());
239 Token Tok;
240 do {
241 lexer.LexFromRawLexer(Tok);
242 if (Tok.is(tok::raw_identifier) &&
243 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
244 Loc = Tok.getLocation();
245 return true;
246 }
247 } while (Tok.isNot(tok::r_paren));
248 return false;
249
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000250}
251
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000252static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000253 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
254 ObjCPropertyDecl::OBJC_PR_retain |
255 ObjCPropertyDecl::OBJC_PR_copy |
256 ObjCPropertyDecl::OBJC_PR_weak |
257 ObjCPropertyDecl::OBJC_PR_strong |
258 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
259}
260
Douglas Gregoraabd0942013-01-21 19:05:22 +0000261ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000262Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000263 SourceLocation AtLoc,
264 SourceLocation LParenLoc,
265 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000266 Selector GetterSel, Selector SetterSel,
267 const bool isAssign,
268 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000269 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000270 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000271 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000272 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000273 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000274 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000275 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000276 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000277 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000278 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
279
Douglas Gregord3297242013-01-16 23:00:23 +0000280 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000281 // Check for duplicate declaration of this property in current and
282 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000283 for (ObjCInterfaceDecl::known_extensions_iterator
284 Ext = CCPrimary->known_extensions_begin(),
285 ExtEnd = CCPrimary->known_extensions_end();
286 Ext != ExtEnd; ++Ext) {
287 if (ObjCPropertyDecl *prevDecl
288 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000289 Diag(AtLoc, diag::err_duplicate_property);
290 Diag(prevDecl->getLocation(), diag::note_property_declare);
291 return 0;
292 }
293 }
Douglas Gregord3297242013-01-16 23:00:23 +0000294 }
295
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000296 // Create a new ObjCPropertyDecl with the DeclContext being
297 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000298 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000299 ObjCPropertyDecl *PDecl =
300 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000301 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000302 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000303 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000304 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000305 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000306 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000307 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000308 // Set setter/getter selector name. Needed later.
309 PDecl->setGetterName(GetterSel);
310 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000311 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000312 DC->addDecl(PDecl);
313
314 // We need to look in the @interface to see if the @property was
315 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000316 if (!CCPrimary) {
317 Diag(CDecl->getLocation(), diag::err_continuation_class);
318 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000319 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000320 }
321
322 // Find the property in continuation class's primary class only.
323 ObjCPropertyDecl *PIDecl =
324 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
325
326 if (!PIDecl) {
327 // No matching property found in the primary class. Just fall thru
328 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000329 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000330 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000331 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000332 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000333
334 // A case of continuation class adding a new property in the class. This
335 // is not what it was meant for. However, gcc supports it and so should we.
336 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000337 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000338 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000339 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
340 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000341 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000342 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
343 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000344 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000345 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
346 bool IncompatibleObjC = false;
347 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000348 // Relax the strict type matching for property type in continuation class.
349 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000350 // as it narrows the object type in its primary class property. Note that
351 // this conversion is safe only because the wider type is for a 'readonly'
352 // property in primary class and 'narrowed' type for a 'readwrite' property
353 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000354 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
355 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
356 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
357 ConvertedType, IncompatibleObjC))
358 || IncompatibleObjC) {
359 Diag(AtLoc,
360 diag::err_type_mismatch_continuation_class) << PDecl->getType();
361 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000362 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000363 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000364 }
365
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000366 // The property 'PIDecl's readonly attribute will be over-ridden
367 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000368 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000369 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000370 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000371 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000372 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000373 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
374 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000375 Diag(AtLoc, diag::warn_property_attr_mismatch);
376 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000377 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000378 DeclContext *DC = cast<DeclContext>(CCPrimary);
379 if (!ObjCPropertyDecl::findPropertyDecl(DC,
380 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000381 // Protocol is not in the primary class. Must build one for it.
382 ObjCDeclSpec ProtocolPropertyODS;
383 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
384 // and ObjCPropertyDecl::PropertyAttributeKind have identical
385 // values. Should consolidate both into one enum type.
386 ProtocolPropertyODS.
387 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
388 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000389 // Must re-establish the context from class extension to primary
390 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000391 ContextRAII SavedContext(*this, CCPrimary);
392
John McCalld226f652010-08-21 09:40:31 +0000393 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000394 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000395 PIDecl->getGetterName(),
396 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000397 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000398 MethodImplKind,
399 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000400 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000401 }
402 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000403 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000404 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000405 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000406 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000407 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000408 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
409 PIDecl->setSetterName(SetterSel);
410 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000411 // Tailor the diagnostics for the common case where a readwrite
412 // property is declared both in the @interface and the continuation.
413 // This is a common error where the user often intended the original
414 // declaration to be readonly.
415 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000416 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000417 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
418 ? diag::err_use_continuation_class_redeclaration_readwrite
419 : diag::err_use_continuation_class;
420 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000421 << CCPrimary->getDeclName();
422 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000423 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000424 }
425 *isOverridingProperty = true;
426 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000427 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000428 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
429 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000430 if (ASTMutationListener *L = Context.getASTMutationListener())
431 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000432 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000433}
434
435ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
436 ObjCContainerDecl *CDecl,
437 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000438 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000439 FieldDeclarator &FD,
440 Selector GetterSel,
441 Selector SetterSel,
442 const bool isAssign,
443 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000444 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000445 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000446 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000447 tok::ObjCKeywordKind MethodImplKind,
448 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000449 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000450 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000451
452 // Issue a warning if property is 'assign' as default and its object, which is
453 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000454 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000455 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000456 if (const ObjCObjectPointerType *ObjPtrTy =
457 T->getAs<ObjCObjectPointerType>()) {
458 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
459 if (IDecl)
460 if (ObjCProtocolDecl* PNSCopying =
461 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
462 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
463 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000464 }
John McCallc12c5bb2010-05-15 11:32:37 +0000465 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000466 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
467
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000468 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000469 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
470 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000471 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000472
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000473 if (ObjCPropertyDecl *prevDecl =
474 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000475 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000476 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000477 PDecl->setInvalidDecl();
478 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000479 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000480 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000481 if (lexicalDC)
482 PDecl->setLexicalDeclContext(lexicalDC);
483 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000484
485 if (T->isArrayType() || T->isFunctionType()) {
486 Diag(AtLoc, diag::err_property_type) << T;
487 PDecl->setInvalidDecl();
488 }
489
490 ProcessDeclAttributes(S, PDecl, FD.D);
491
492 // Regardless of setter/getter attribute, we save the default getter/setter
493 // selector names in anticipation of declaration of setter/getter methods.
494 PDecl->setGetterName(GetterSel);
495 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000496 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000497 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000498
Bill Wendlingad017fa2012-12-20 19:22:21 +0000499 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000500 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
501
Bill Wendlingad017fa2012-12-20 19:22:21 +0000502 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000503 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
504
Bill Wendlingad017fa2012-12-20 19:22:21 +0000505 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000506 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
507
508 if (isReadWrite)
509 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
510
Bill Wendlingad017fa2012-12-20 19:22:21 +0000511 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000512 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
513
Bill Wendlingad017fa2012-12-20 19:22:21 +0000514 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000515 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
516
Bill Wendlingad017fa2012-12-20 19:22:21 +0000517 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000518 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
519
Bill Wendlingad017fa2012-12-20 19:22:21 +0000520 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000521 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
522
Bill Wendlingad017fa2012-12-20 19:22:21 +0000523 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000524 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
525
Ted Kremenek28685ab2010-03-12 00:46:40 +0000526 if (isAssign)
527 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
528
John McCall265941b2011-09-13 18:31:23 +0000529 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000530 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000531 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000532 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000533 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000534
John McCallf85e1932011-06-15 23:02:42 +0000535 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000536 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000537 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
538 if (isAssign)
539 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
540
Ted Kremenek28685ab2010-03-12 00:46:40 +0000541 if (MethodImplKind == tok::objc_required)
542 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
543 else if (MethodImplKind == tok::objc_optional)
544 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000545
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000546 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000547}
548
John McCallf85e1932011-06-15 23:02:42 +0000549static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
550 ObjCPropertyDecl *property,
551 ObjCIvarDecl *ivar) {
552 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
553
John McCallf85e1932011-06-15 23:02:42 +0000554 QualType ivarType = ivar->getType();
555 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000556
John McCall265941b2011-09-13 18:31:23 +0000557 // The lifetime implied by the property's attributes.
558 Qualifiers::ObjCLifetime propertyLifetime =
559 getImpliedARCOwnership(property->getPropertyAttributes(),
560 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000561
John McCall265941b2011-09-13 18:31:23 +0000562 // We're fine if they match.
563 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000564
John McCall265941b2011-09-13 18:31:23 +0000565 // These aren't valid lifetimes for object ivars; don't diagnose twice.
566 if (ivarLifetime == Qualifiers::OCL_None ||
567 ivarLifetime == Qualifiers::OCL_Autoreleasing)
568 return;
John McCallf85e1932011-06-15 23:02:42 +0000569
John McCalld64c2eb2012-08-20 23:36:59 +0000570 // If the ivar is private, and it's implicitly __unsafe_unretained
571 // becaues of its type, then pretend it was actually implicitly
572 // __strong. This is only sound because we're processing the
573 // property implementation before parsing any method bodies.
574 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
575 propertyLifetime == Qualifiers::OCL_Strong &&
576 ivar->getAccessControl() == ObjCIvarDecl::Private) {
577 SplitQualType split = ivarType.split();
578 if (split.Quals.hasObjCLifetime()) {
579 assert(ivarType->isObjCARCImplicitlyUnretainedType());
580 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
581 ivarType = S.Context.getQualifiedType(split);
582 ivar->setType(ivarType);
583 return;
584 }
585 }
586
John McCall265941b2011-09-13 18:31:23 +0000587 switch (propertyLifetime) {
588 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000589 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000590 << property->getDeclName()
591 << ivar->getDeclName()
592 << ivarLifetime;
593 break;
John McCallf85e1932011-06-15 23:02:42 +0000594
John McCall265941b2011-09-13 18:31:23 +0000595 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000596 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000597 << property->getDeclName()
598 << ivar->getDeclName();
599 break;
John McCallf85e1932011-06-15 23:02:42 +0000600
John McCall265941b2011-09-13 18:31:23 +0000601 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000602 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000603 << property->getDeclName()
604 << ivar->getDeclName()
605 << ((property->getPropertyAttributesAsWritten()
606 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
607 break;
John McCallf85e1932011-06-15 23:02:42 +0000608
John McCall265941b2011-09-13 18:31:23 +0000609 case Qualifiers::OCL_Autoreleasing:
610 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000611
John McCall265941b2011-09-13 18:31:23 +0000612 case Qualifiers::OCL_None:
613 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000614 return;
615 }
616
617 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000618 if (propertyImplLoc.isValid())
619 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000620}
621
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000622/// setImpliedPropertyAttributeForReadOnlyProperty -
623/// This routine evaludates life-time attributes for a 'readonly'
624/// property with no known lifetime of its own, using backing
625/// 'ivar's attribute, if any. If no backing 'ivar', property's
626/// life-time is assumed 'strong'.
627static void setImpliedPropertyAttributeForReadOnlyProperty(
628 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
629 Qualifiers::ObjCLifetime propertyLifetime =
630 getImpliedARCOwnership(property->getPropertyAttributes(),
631 property->getType());
632 if (propertyLifetime != Qualifiers::OCL_None)
633 return;
634
635 if (!ivar) {
636 // if no backing ivar, make property 'strong'.
637 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
638 return;
639 }
640 // property assumes owenership of backing ivar.
641 QualType ivarType = ivar->getType();
642 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
643 if (ivarLifetime == Qualifiers::OCL_Strong)
644 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
645 else if (ivarLifetime == Qualifiers::OCL_Weak)
646 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
647 return;
648}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000649
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000650/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
651/// attribute declared in primary class and attributes overridden in any of its
652/// class extensions.
653static void
654DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
655 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000656 unsigned Attributes = property->getPropertyAttributesAsWritten();
657 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000658 for (ObjCInterfaceDecl::known_extensions_iterator
659 Ext = ClassDecl->known_extensions_begin(),
660 ExtEnd = ClassDecl->known_extensions_end();
661 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000662 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000663 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
664 for (unsigned I = 0, N = R.size(); I != N; ++I) {
665 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
666 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000667 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000668 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000669
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000670 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000671 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000672 unsigned classExtPropertyAttr =
673 ClassExtProperty->getPropertyAttributesAsWritten();
674 // We are issuing the warning that we postponed because class extensions
675 // can override readonly->readwrite and 'setter' attributes originally
676 // placed on class's property declaration now make sense in the overridden
677 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000678 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000679 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000680 (classExtPropertyAttr &
681 (ObjCDeclSpec::DQ_PR_readwrite|
682 ObjCDeclSpec::DQ_PR_assign |
683 ObjCDeclSpec::DQ_PR_unsafe_unretained |
684 ObjCDeclSpec::DQ_PR_copy |
685 ObjCDeclSpec::DQ_PR_retain |
686 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000687 continue;
688 warn = true;
689 break;
690 }
691 }
692 }
693 if (warn) {
694 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
695 ObjCDeclSpec::DQ_PR_unsafe_unretained |
696 ObjCDeclSpec::DQ_PR_copy |
697 ObjCDeclSpec::DQ_PR_retain |
698 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000699 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000700 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000701 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000702 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000703 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000704 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000705 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000706 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000707 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000708 "retain" : "strong";
709
710 S.Diag(property->getLocation(),
711 diag::warn_objc_property_attr_mutually_exclusive)
712 << "readonly" << which;
713 }
714 }
715
716
717}
718
Ted Kremenek28685ab2010-03-12 00:46:40 +0000719/// ActOnPropertyImplDecl - This routine performs semantic checks and
720/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000721/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000722///
John McCalld226f652010-08-21 09:40:31 +0000723Decl *Sema::ActOnPropertyImplDecl(Scope *S,
724 SourceLocation AtLoc,
725 SourceLocation PropertyLoc,
726 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000727 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000728 IdentifierInfo *PropertyIvar,
729 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000730 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000731 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 // Make sure we have a context for the property implementation declaration.
733 if (!ClassImpDecl) {
734 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000735 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000736 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000737 if (PropertyIvarLoc.isInvalid())
738 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000739 SourceLocation PropertyDiagLoc = PropertyLoc;
740 if (PropertyDiagLoc.isInvalid())
741 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000742 ObjCPropertyDecl *property = 0;
743 ObjCInterfaceDecl* IDecl = 0;
744 // Find the class or category class where this property must have
745 // a declaration.
746 ObjCImplementationDecl *IC = 0;
747 ObjCCategoryImplDecl* CatImplClass = 0;
748 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
749 IDecl = IC->getClassInterface();
750 // We always synthesize an interface for an implementation
751 // without an interface decl. So, IDecl is always non-zero.
752 assert(IDecl &&
753 "ActOnPropertyImplDecl - @implementation without @interface");
754
755 // Look for this property declaration in the @implementation's @interface
756 property = IDecl->FindPropertyDeclaration(PropertyId);
757 if (!property) {
758 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000759 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000760 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000761 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000762 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
763 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000764 if (AtLoc.isValid())
765 Diag(AtLoc, diag::warn_implicit_atomic_property);
766 else
767 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
768 Diag(property->getLocation(), diag::note_property_declare);
769 }
770
Ted Kremenek28685ab2010-03-12 00:46:40 +0000771 if (const ObjCCategoryDecl *CD =
772 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
773 if (!CD->IsClassExtension()) {
774 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
775 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000776 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000777 }
778 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000779
780 if (Synthesize&&
781 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
782 property->hasAttr<IBOutletAttr>() &&
783 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000784 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
785 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000786 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000787 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000788 property->getLParenLoc(), readonlyLoc)) {
789 SourceLocation endLoc =
790 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
791 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
792 Diag(property->getLocation(),
793 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
794 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
795 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000796 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000797
798 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000799
Ted Kremenek28685ab2010-03-12 00:46:40 +0000800 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
801 if (Synthesize) {
802 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000803 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000804 }
805 IDecl = CatImplClass->getClassInterface();
806 if (!IDecl) {
807 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000808 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809 }
810 ObjCCategoryDecl *Category =
811 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
812
813 // If category for this implementation not found, it is an error which
814 // has already been reported eralier.
815 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000816 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000817 // Look for this property declaration in @implementation's category
818 property = Category->FindPropertyDeclaration(PropertyId);
819 if (!property) {
820 Diag(PropertyLoc, diag::error_bad_category_property_decl)
821 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000822 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000823 }
824 } else {
825 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000826 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000827 }
828 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000829 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000830 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000831 // Check that we have a valid, previously declared ivar for @synthesize
832 if (Synthesize) {
833 // @synthesize
834 if (!PropertyIvar)
835 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000836 // Check that this is a previously declared 'ivar' in 'IDecl' interface
837 ObjCInterfaceDecl *ClassDeclared;
838 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
839 QualType PropType = property->getType();
840 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000841
842 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000843 diag::err_incomplete_synthesized_property,
844 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000845 Diag(property->getLocation(), diag::note_property_declare);
846 CompleteTypeErr = true;
847 }
848
David Blaikie4e4d0842012-03-11 07:00:24 +0000849 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000850 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000851 ObjCPropertyDecl::OBJC_PR_readonly) &&
852 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000853 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
854 }
855
John McCallf85e1932011-06-15 23:02:42 +0000856 ObjCPropertyDecl::PropertyAttributeKind kind
857 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000858
859 // Add GC __weak to the ivar type if the property is weak.
860 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000861 getLangOpts().getGC() != LangOptions::NonGC) {
862 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000863 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000864 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000865 Diag(property->getLocation(), diag::note_property_declare);
866 } else {
867 PropertyIvarType =
868 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000869 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000870 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000871 if (AtLoc.isInvalid()) {
872 // Check when default synthesizing a property that there is
873 // an ivar matching property name and issue warning; since this
874 // is the most common case of not using an ivar used for backing
875 // property in non-default synthesis case.
876 ObjCInterfaceDecl *ClassDeclared=0;
877 ObjCIvarDecl *originalIvar =
878 IDecl->lookupInstanceVariable(property->getIdentifier(),
879 ClassDeclared);
880 if (originalIvar) {
881 Diag(PropertyDiagLoc,
882 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000883 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000884 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000885 Diag(property->getLocation(), diag::note_property_declare);
886 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000887 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000888 }
889
890 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000891 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000892 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000893 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000894 !PropertyIvarType.getObjCLifetime() &&
895 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000896
John McCall265941b2011-09-13 18:31:23 +0000897 // It's an error if we have to do this and the user didn't
898 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000899 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000900 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000901 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000902 diag::err_arc_objc_property_default_assign_on_object);
903 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000904 } else {
905 Qualifiers::ObjCLifetime lifetime =
906 getImpliedARCOwnership(kind, PropertyIvarType);
907 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000908 if (lifetime == Qualifiers::OCL_Weak) {
909 bool err = false;
910 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000911 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
912 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
913 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000914 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000915 Diag(property->getLocation(), diag::note_property_declare);
916 err = true;
917 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000918 }
John McCall0a7dd782012-08-21 02:47:43 +0000919 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000920 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000921 Diag(property->getLocation(), diag::note_property_declare);
922 }
John McCallf85e1932011-06-15 23:02:42 +0000923 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000924
John McCallf85e1932011-06-15 23:02:42 +0000925 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000926 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000927 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
928 }
John McCallf85e1932011-06-15 23:02:42 +0000929 }
930
931 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000932 !getLangOpts().ObjCAutoRefCount &&
933 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000934 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000935 Diag(property->getLocation(), diag::note_property_declare);
936 }
937
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000938 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000939 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000940 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000941 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000942 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +0000943 if (CompleteTypeErr)
944 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000945 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +0000946 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000947
John McCall260611a2012-06-20 06:18:46 +0000948 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +0000949 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
950 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000951 // Note! I deliberately want it to fall thru so, we have a
952 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +0000953 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +0000954 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000955 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000956 << property->getDeclName() << Ivar->getDeclName()
957 << ClassDeclared->getDeclName();
958 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000959 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000960 // Note! I deliberately want it to fall thru so more errors are caught.
961 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +0000962 property->setPropertyIvarDecl(Ivar);
963
Ted Kremenek28685ab2010-03-12 00:46:40 +0000964 QualType IvarType = Context.getCanonicalType(Ivar->getType());
965
966 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +0000967 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +0000968 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000969 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +0000970 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000971 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000972 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000973 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000974 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000975 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
976 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000977 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000978 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000979 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000980 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000981 << property->getDeclName() << PropType
982 << Ivar->getDeclName() << IvarType;
983 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000984 // Note! I deliberately want it to fall thru so, we have a
985 // a property implementation and to avoid future warnings.
986 }
Fariborz Jahanian74414712012-05-15 18:12:51 +0000987 else {
988 // FIXME! Rules for properties are somewhat different that those
989 // for assignments. Use a new routine to consolidate all cases;
990 // specifically for property redeclarations as well as for ivars.
991 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
992 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
993 if (lhsType != rhsType &&
994 lhsType->isArithmeticType()) {
995 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
996 << property->getDeclName() << PropType
997 << Ivar->getDeclName() << IvarType;
998 Diag(Ivar->getLocation(), diag::note_ivar_decl);
999 // Fall thru - see previous comment
1000 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001001 }
1002 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001003 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001004 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001005 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001006 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001007 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001008 // Fall thru - see previous comment
1009 }
John McCallf85e1932011-06-15 23:02:42 +00001010 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001011 if ((property->getType()->isObjCObjectPointerType() ||
1012 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001013 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001014 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001015 << property->getDeclName() << Ivar->getDeclName();
1016 // Fall thru - see previous comment
1017 }
1018 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001019 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001020 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001021 } else if (PropertyIvar)
1022 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001023 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001024
Ted Kremenek28685ab2010-03-12 00:46:40 +00001025 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1026 ObjCPropertyImplDecl *PIDecl =
1027 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1028 property,
1029 (Synthesize ?
1030 ObjCPropertyImplDecl::Synthesize
1031 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001032 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001033
Fariborz Jahanian74414712012-05-15 18:12:51 +00001034 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001035 PIDecl->setInvalidDecl();
1036
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001037 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1038 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001039 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001040 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001041 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1042 // returned by the getter as it must conform to C++'s copy-return rules.
1043 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001044 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001045 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1046 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001047 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001048 VK_RValue, PropertyDiagLoc);
1049 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001050 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001051 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001052 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001053 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001054 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001055 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001056 getterMethod->getResultType(),
1057 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001058 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001059 Owned(IvarRefExpr));
1060 if (!Res.isInvalid()) {
1061 Expr *ResExpr = Res.takeAs<Expr>();
1062 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001063 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001064 PIDecl->setGetterCXXConstructor(ResExpr);
1065 }
1066 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001067 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1068 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1069 Diag(getterMethod->getLocation(),
1070 diag::warn_property_getter_owning_mismatch);
1071 Diag(property->getLocation(), diag::note_property_declare);
1072 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001073 }
1074 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1075 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001076 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1077 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001078 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001079 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001080 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1081 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001082 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001083 VK_RValue, PropertyDiagLoc);
1084 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001085 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001086 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001087 SelfExpr, true, true);
1088 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1089 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001090 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001091 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1092 VK_LValue, PropertyDiagLoc);
1093 MarkDeclRefReferenced(rhs);
1094 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001095 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001096 if (property->getPropertyAttributes() &
1097 ObjCPropertyDecl::OBJC_PR_atomic) {
1098 Expr *callExpr = Res.takeAs<Expr>();
1099 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001100 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1101 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001102 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001103 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001104 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001105 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001106 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001107 Diag(FuncDecl->getLocStart(),
1108 diag::note_callee_decl) << FuncDecl;
1109 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001110 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001111 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1112 }
1113 }
1114
Ted Kremenek28685ab2010-03-12 00:46:40 +00001115 if (IC) {
1116 if (Synthesize)
1117 if (ObjCPropertyImplDecl *PPIDecl =
1118 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1119 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1120 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1121 << PropertyIvar;
1122 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1123 }
1124
1125 if (ObjCPropertyImplDecl *PPIDecl
1126 = IC->FindPropertyImplDecl(PropertyId)) {
1127 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1128 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001129 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001130 }
1131 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001132 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001133 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001134 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001135 // Diagnose if an ivar was lazily synthesdized due to a previous
1136 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001137 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001138 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001139 ObjCIvarDecl *Ivar = 0;
1140 if (!Synthesize)
1141 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1142 else {
1143 if (PropertyIvar && PropertyIvar != PropertyId)
1144 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1145 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001146 // Issue diagnostics only if Ivar belongs to current class.
1147 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001148 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001149 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1150 << PropertyId;
1151 Ivar->setInvalidDecl();
1152 }
1153 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001154 } else {
1155 if (Synthesize)
1156 if (ObjCPropertyImplDecl *PPIDecl =
1157 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001158 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001159 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1160 << PropertyIvar;
1161 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1162 }
1163
1164 if (ObjCPropertyImplDecl *PPIDecl =
1165 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001166 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001167 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001168 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001169 }
1170 CatImplClass->addPropertyImplementation(PIDecl);
1171 }
1172
John McCalld226f652010-08-21 09:40:31 +00001173 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001174}
1175
1176//===----------------------------------------------------------------------===//
1177// Helper methods.
1178//===----------------------------------------------------------------------===//
1179
Ted Kremenek9d64c152010-03-12 00:38:38 +00001180/// DiagnosePropertyMismatch - Compares two properties for their
1181/// attributes and types and warns on a variety of inconsistencies.
1182///
1183void
1184Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1185 ObjCPropertyDecl *SuperProperty,
1186 const IdentifierInfo *inheritedName) {
1187 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1188 Property->getPropertyAttributes();
1189 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1190 SuperProperty->getPropertyAttributes();
1191 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1192 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1193 Diag(Property->getLocation(), diag::warn_readonly_property)
1194 << Property->getDeclName() << inheritedName;
1195 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1196 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1197 Diag(Property->getLocation(), diag::warn_property_attribute)
1198 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001199 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001200 unsigned CAttrRetain =
1201 (CAttr &
1202 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1203 unsigned SAttrRetain =
1204 (SAttr &
1205 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1206 bool CStrong = (CAttrRetain != 0);
1207 bool SStrong = (SAttrRetain != 0);
1208 if (CStrong != SStrong)
1209 Diag(Property->getLocation(), diag::warn_property_attribute)
1210 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1211 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001212
1213 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1214 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1215 Diag(Property->getLocation(), diag::warn_property_attribute)
1216 << Property->getDeclName() << "atomic" << inheritedName;
1217 if (Property->getSetterName() != SuperProperty->getSetterName())
1218 Diag(Property->getLocation(), diag::warn_property_attribute)
1219 << Property->getDeclName() << "setter" << inheritedName;
1220 if (Property->getGetterName() != SuperProperty->getGetterName())
1221 Diag(Property->getLocation(), diag::warn_property_attribute)
1222 << Property->getDeclName() << "getter" << inheritedName;
1223
1224 QualType LHSType =
1225 Context.getCanonicalType(SuperProperty->getType());
1226 QualType RHSType =
1227 Context.getCanonicalType(Property->getType());
1228
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001229 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001230 // Do cases not handled in above.
1231 // FIXME. For future support of covariant property types, revisit this.
1232 bool IncompatibleObjC = false;
1233 QualType ConvertedType;
1234 if (!isObjCPointerConversion(RHSType, LHSType,
1235 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001236 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001237 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1238 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001239 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1240 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001241 }
1242}
1243
1244bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1245 ObjCMethodDecl *GetterMethod,
1246 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001247 if (!GetterMethod)
1248 return false;
1249 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1250 QualType PropertyIvarType = property->getType().getNonReferenceType();
1251 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1252 if (!compat) {
1253 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1254 isa<ObjCObjectPointerType>(GetterType))
1255 compat =
1256 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001257 GetterType->getAs<ObjCObjectPointerType>(),
1258 PropertyIvarType->getAs<ObjCObjectPointerType>());
1259 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001260 != Compatible) {
1261 Diag(Loc, diag::error_property_accessor_type)
1262 << property->getDeclName() << PropertyIvarType
1263 << GetterMethod->getSelector() << GetterType;
1264 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1265 return true;
1266 } else {
1267 compat = true;
1268 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1269 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1270 if (lhsType != rhsType && lhsType->isArithmeticType())
1271 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001272 }
1273 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001274
1275 if (!compat) {
1276 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1277 << property->getDeclName()
1278 << GetterMethod->getSelector();
1279 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1280 return true;
1281 }
1282
Ted Kremenek9d64c152010-03-12 00:38:38 +00001283 return false;
1284}
1285
Ted Kremenek9d64c152010-03-12 00:38:38 +00001286/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1287/// of properties declared in a protocol and compares their attribute against
1288/// the same property declared in the class or category.
1289void
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001290Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1291 if (!CDecl)
1292 return;
1293
1294 // Category case.
1295 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1296 // FIXME: We should perform this check when the property in the category
1297 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001298 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1299 if (!CatDecl->IsClassExtension())
1300 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1301 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001302 ObjCPropertyDecl *ProtoProp = *P;
1303 DeclContext::lookup_result R
1304 = CatDecl->lookup(ProtoProp->getDeclName());
1305 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1306 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1307 if (CatProp != ProtoProp) {
1308 // Property protocol already exist in class. Diagnose any mismatch.
1309 DiagnosePropertyMismatch(CatProp, ProtoProp,
1310 PDecl->getIdentifier());
1311 }
1312 }
1313 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001314 }
1315 return;
1316 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001317
1318 // Class
1319 // FIXME: We should perform this check when the property in the class
1320 // is declared.
1321 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001322 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001323 E = PDecl->prop_end(); P != E; ++P) {
1324 ObjCPropertyDecl *ProtoProp = *P;
1325 DeclContext::lookup_result R
Douglas Gregoraabd0942013-01-21 19:05:22 +00001326 = IDecl->lookup(ProtoProp->getDeclName());
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001327 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1328 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1329 if (ClassProp != ProtoProp) {
1330 // Property protocol already exist in class. Diagnose any mismatch.
1331 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1332 PDecl->getIdentifier());
1333 }
1334 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001336 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001337}
1338
1339/// CompareProperties - This routine compares properties
1340/// declared in 'ClassOrProtocol' objects (which can be a class or an
1341/// inherited protocol with the list of properties for class/category 'CDecl'
1342///
John McCalld226f652010-08-21 09:40:31 +00001343void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1344 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001345 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1346
1347 if (!IDecl) {
1348 // Category
1349 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1350 assert (CatDecl && "CompareProperties");
1351 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1352 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1353 E = MDecl->protocol_end(); P != E; ++P)
1354 // Match properties of category with those of protocol (*P)
1355 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1356
1357 // Go thru the list of protocols for this category and recursively match
1358 // their properties with those in the category.
1359 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1360 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001361 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 } else {
1363 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1364 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1365 E = MD->protocol_end(); P != E; ++P)
1366 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1367 }
1368 return;
1369 }
1370
1371 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001372 for (ObjCInterfaceDecl::all_protocol_iterator
1373 P = MDecl->all_referenced_protocol_begin(),
1374 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001375 // Match properties of class IDecl with those of protocol (*P).
1376 MatchOneProtocolPropertiesInClass(IDecl, *P);
1377
1378 // Go thru the list of protocols for this class and recursively match
1379 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001380 for (ObjCInterfaceDecl::all_protocol_iterator
1381 P = IDecl->all_referenced_protocol_begin(),
1382 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001383 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001384 } else {
1385 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1386 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1387 E = MD->protocol_end(); P != E; ++P)
1388 MatchOneProtocolPropertiesInClass(IDecl, *P);
1389 }
1390}
1391
1392/// isPropertyReadonly - Return true if property is readonly, by searching
1393/// for the property in the class and in its categories and implementations
1394///
1395bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1396 ObjCInterfaceDecl *IDecl) {
1397 // by far the most common case.
1398 if (!PDecl->isReadOnly())
1399 return false;
1400 // Even if property is ready only, if interface has a user defined setter,
1401 // it is not considered read only.
1402 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1403 return false;
1404
1405 // Main class has the property as 'readonly'. Must search
1406 // through the category list to see if the property's
1407 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001408 for (ObjCInterfaceDecl::visible_categories_iterator
1409 Cat = IDecl->visible_categories_begin(),
1410 CatEnd = IDecl->visible_categories_end();
1411 Cat != CatEnd; ++Cat) {
1412 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001413 return false;
1414 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001415 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001416 if (P && !P->isReadOnly())
1417 return false;
1418 }
1419
1420 // Also, check for definition of a setter method in the implementation if
1421 // all else failed.
1422 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1423 if (ObjCImplementationDecl *IMD =
1424 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1425 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1426 return false;
1427 } else if (ObjCCategoryImplDecl *CIMD =
1428 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1429 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1430 return false;
1431 }
1432 }
1433 // Lastly, look through the implementation (if one is in scope).
1434 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1435 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1436 return false;
1437 // If all fails, look at the super class.
1438 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1439 return isPropertyReadonly(PDecl, SIDecl);
1440 return true;
1441}
1442
1443/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001444/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001445void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001446 ObjCContainerDecl::PropertyMap &PropMap,
1447 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1449 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1450 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001451 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001452 PropMap[Prop->getIdentifier()] = Prop;
1453 }
1454 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001455 for (ObjCInterfaceDecl::all_protocol_iterator
1456 PI = IDecl->all_referenced_protocol_begin(),
1457 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001458 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 }
1460 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1461 if (!CATDecl->IsClassExtension())
1462 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1463 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001464 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001465 PropMap[Prop->getIdentifier()] = Prop;
1466 }
1467 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001468 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001469 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001470 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001471 }
1472 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1473 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1474 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001475 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001476 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1477 // Exclude property for protocols which conform to class's super-class,
1478 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001479 if (!PropertyFromSuper ||
1480 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001481 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1482 if (!PropEntry)
1483 PropEntry = Prop;
1484 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001485 }
1486 // scan through protocol's protocols.
1487 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1488 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001489 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001490 }
1491}
1492
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001493/// CollectSuperClassPropertyImplementations - This routine collects list of
1494/// properties to be implemented in super class(s) and also coming from their
1495/// conforming protocols.
1496static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001497 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001498 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1499 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001500 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001501 SDecl = SDecl->getSuperClass();
1502 }
1503 }
1504}
1505
James Dennett699c9042012-06-15 07:13:21 +00001506/// \brief Default synthesizes all properties which must be synthesized
1507/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001508void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1509 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001510
Anna Zaksb36ea372012-10-18 19:17:53 +00001511 ObjCInterfaceDecl::PropertyMap PropMap;
1512 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001513 if (PropMap.empty())
1514 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001515 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001516 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1517
Anna Zaksb36ea372012-10-18 19:17:53 +00001518 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001519 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1520 ObjCPropertyDecl *Prop = P->second;
1521 // If property to be implemented in the super class, ignore.
1522 if (SuperPropMap[Prop->getIdentifier()])
1523 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001524 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001525 if (Prop->isInvalidDecl() ||
1526 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1527 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1528 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001529 // Property may have been synthesized by user.
1530 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1531 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001532 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1533 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1534 continue;
1535 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1536 continue;
1537 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001538 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1539 // We won't auto-synthesize properties declared in protocols.
1540 Diag(IMPDecl->getLocation(),
1541 diag::warn_auto_synthesizing_protocol_property);
1542 Diag(Prop->getLocation(), diag::note_property_declare);
1543 continue;
1544 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001545
1546 // We use invalid SourceLocations for the synthesized ivars since they
1547 // aren't really synthesized at a particular location; they just exist.
1548 // Saying that they are located at the @implementation isn't really going
1549 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001550 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1551 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1552 true,
1553 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001554 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001555 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001556 if (PIDecl) {
1557 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001558 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001559 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001560 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001561}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001562
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001563void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001564 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001565 return;
1566 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1567 if (!IC)
1568 return;
1569 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001570 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001571 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001572}
1573
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001574void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001575 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001576 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001577 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1578 ObjCInterfaceDecl *IDecl;
1579 // Gather properties which need not be implemented in this class
1580 // or category.
1581 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1582 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1583 // For categories, no need to implement properties declared in
1584 // its primary class (and its super classes) if property is
1585 // declared in one of those containers.
1586 if ((IDecl = C->getClassInterface()))
1587 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1588 }
1589 if (IDecl)
1590 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001591
Anna Zaksb36ea372012-10-18 19:17:53 +00001592 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001593 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001594 if (PropMap.empty())
1595 return;
1596
1597 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1598 for (ObjCImplDecl::propimpl_iterator
1599 I = IMPDecl->propimpl_begin(),
1600 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001601 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001602
Anna Zaksb36ea372012-10-18 19:17:53 +00001603 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1605 ObjCPropertyDecl *Prop = P->second;
1606 // Is there a matching propery synthesize/dynamic?
1607 if (Prop->isInvalidDecl() ||
1608 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001609 PropImplMap.count(Prop) ||
1610 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001612 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001613 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001614 isa<ObjCCategoryDecl>(CDecl) ?
1615 diag::warn_setter_getter_impl_required_in_category :
1616 diag::warn_setter_getter_impl_required)
1617 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001618 Diag(Prop->getLocation(),
1619 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001620 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001621 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001622 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001623 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1624
Ted Kremenek9d64c152010-03-12 00:38:38 +00001625 }
1626
1627 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001628 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001629 isa<ObjCCategoryDecl>(CDecl) ?
1630 diag::warn_setter_getter_impl_required_in_category :
1631 diag::warn_setter_getter_impl_required)
1632 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001633 Diag(Prop->getLocation(),
1634 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001635 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001636 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001637 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001638 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001639 }
1640 }
1641}
1642
1643void
1644Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1645 ObjCContainerDecl* IDecl) {
1646 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001647 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 return;
1649 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1650 E = IDecl->prop_end();
1651 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001652 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001653 ObjCMethodDecl *GetterMethod = 0;
1654 ObjCMethodDecl *SetterMethod = 0;
1655 bool LookedUpGetterSetter = false;
1656
Bill Wendlingad017fa2012-12-20 19:22:21 +00001657 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001658 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001659
John McCall265941b2011-09-13 18:31:23 +00001660 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1661 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001662 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1663 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1664 LookedUpGetterSetter = true;
1665 if (GetterMethod) {
1666 Diag(GetterMethod->getLocation(),
1667 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001668 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001669 Diag(Property->getLocation(), diag::note_property_declare);
1670 }
1671 if (SetterMethod) {
1672 Diag(SetterMethod->getLocation(),
1673 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001674 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001675 Diag(Property->getLocation(), diag::note_property_declare);
1676 }
1677 }
1678
Ted Kremenek9d64c152010-03-12 00:38:38 +00001679 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001680 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1681 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001682 continue;
1683 if (const ObjCPropertyImplDecl *PIDecl
1684 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1685 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1686 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001687 if (!LookedUpGetterSetter) {
1688 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1689 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1690 LookedUpGetterSetter = true;
1691 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001692 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1693 SourceLocation MethodLoc =
1694 (GetterMethod ? GetterMethod->getLocation()
1695 : SetterMethod->getLocation());
1696 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001697 << Property->getIdentifier() << (GetterMethod != 0)
1698 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001699 // fixit stuff.
1700 if (!AttributesAsWritten) {
1701 if (Property->getLParenLoc().isValid()) {
1702 // @property () ... case.
1703 SourceRange PropSourceRange(Property->getAtLoc(),
1704 Property->getLParenLoc());
1705 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1706 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1707 }
1708 else {
1709 //@property id etc.
1710 SourceLocation endLoc =
1711 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1712 endLoc = endLoc.getLocWithOffset(-1);
1713 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1714 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1715 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1716 }
1717 }
1718 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1719 // @property () ... case.
1720 SourceLocation endLoc = Property->getLParenLoc();
1721 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1722 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1723 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1724 }
1725 else
1726 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001727 Diag(Property->getLocation(), diag::note_property_declare);
1728 }
1729 }
1730 }
1731}
1732
John McCallf85e1932011-06-15 23:02:42 +00001733void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001734 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001735 return;
1736
1737 for (ObjCImplementationDecl::propimpl_iterator
1738 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001739 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001740 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1741 continue;
1742
1743 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001744 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1745 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001746 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1747 if (!method)
1748 continue;
1749 ObjCMethodFamily family = method->getMethodFamily();
1750 if (family == OMF_alloc || family == OMF_copy ||
1751 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001752 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001753 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1754 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001755 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001756 Diag(PD->getLocation(), diag::note_property_declare);
1757 }
1758 }
1759 }
1760}
1761
John McCall5de74d12010-11-10 07:01:40 +00001762/// AddPropertyAttrs - Propagates attributes from a property to the
1763/// implicitly-declared getter or setter for that property.
1764static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1765 ObjCPropertyDecl *Property) {
1766 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001767 for (Decl::attr_iterator A = Property->attr_begin(),
1768 AEnd = Property->attr_end();
1769 A != AEnd; ++A) {
1770 if (isa<DeprecatedAttr>(*A) ||
1771 isa<UnavailableAttr>(*A) ||
1772 isa<AvailabilityAttr>(*A))
1773 PropertyMethod->addAttr((*A)->clone(S.Context));
1774 }
John McCall5de74d12010-11-10 07:01:40 +00001775}
1776
Ted Kremenek9d64c152010-03-12 00:38:38 +00001777/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1778/// have the property type and issue diagnostics if they don't.
1779/// Also synthesize a getter/setter method if none exist (and update the
1780/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1781/// methods is the "right" thing to do.
1782void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001783 ObjCContainerDecl *CD,
1784 ObjCPropertyDecl *redeclaredProperty,
1785 ObjCContainerDecl *lexicalDC) {
1786
Ted Kremenek9d64c152010-03-12 00:38:38 +00001787 ObjCMethodDecl *GetterMethod, *SetterMethod;
1788
1789 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1790 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1791 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1792 property->getLocation());
1793
1794 if (SetterMethod) {
1795 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1796 property->getPropertyAttributes();
1797 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1798 Context.getCanonicalType(SetterMethod->getResultType()) !=
1799 Context.VoidTy)
1800 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1801 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001802 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001803 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1804 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001805 Diag(property->getLocation(),
1806 diag::warn_accessor_property_type_mismatch)
1807 << property->getDeclName()
1808 << SetterMethod->getSelector();
1809 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1810 }
1811 }
1812
1813 // Synthesize getter/setter methods if none exist.
1814 // Find the default getter and if one not found, add one.
1815 // FIXME: The synthesized property we set here is misleading. We almost always
1816 // synthesize these methods unless the user explicitly provided prototypes
1817 // (which is odd, but allowed). Sema should be typechecking that the
1818 // declarations jive in that situation (which it is not currently).
1819 if (!GetterMethod) {
1820 // No instance method of same name as property getter name was found.
1821 // Declare a getter method and add it to the list of methods
1822 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001823 SourceLocation Loc = redeclaredProperty ?
1824 redeclaredProperty->getLocation() :
1825 property->getLocation();
1826
1827 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1828 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001829 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001830 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001831 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001832 (property->getPropertyImplementation() ==
1833 ObjCPropertyDecl::Optional) ?
1834 ObjCMethodDecl::Optional :
1835 ObjCMethodDecl::Required);
1836 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001837
1838 AddPropertyAttrs(*this, GetterMethod, property);
1839
Ted Kremenek23173d72010-05-18 21:09:07 +00001840 // FIXME: Eventually this shouldn't be needed, as the lexical context
1841 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001842 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001843 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001844 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1845 GetterMethod->addAttr(
1846 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001847 } else
1848 // A user declared getter will be synthesize when @synthesize of
1849 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001850 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001851 property->setGetterMethodDecl(GetterMethod);
1852
1853 // Skip setter if property is read-only.
1854 if (!property->isReadOnly()) {
1855 // Find the default setter and if one not found, add one.
1856 if (!SetterMethod) {
1857 // No instance method of same name as property setter name was found.
1858 // Declare a setter method and add it to the list of methods
1859 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001860 SourceLocation Loc = redeclaredProperty ?
1861 redeclaredProperty->getLocation() :
1862 property->getLocation();
1863
1864 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001865 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001866 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001867 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001868 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001869 /*isImplicitlyDeclared=*/true,
1870 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001871 (property->getPropertyImplementation() ==
1872 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001873 ObjCMethodDecl::Optional :
1874 ObjCMethodDecl::Required);
1875
Ted Kremenek9d64c152010-03-12 00:38:38 +00001876 // Invent the arguments for the setter. We don't bother making a
1877 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001878 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1879 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001880 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001881 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001882 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001883 SC_None,
1884 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001885 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001886 SetterMethod->setMethodParams(Context, Argument,
1887 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001888
1889 AddPropertyAttrs(*this, SetterMethod, property);
1890
Ted Kremenek9d64c152010-03-12 00:38:38 +00001891 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001892 // FIXME: Eventually this shouldn't be needed, as the lexical context
1893 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001894 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001895 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001896 } else
1897 // A user declared setter will be synthesize when @synthesize of
1898 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001899 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001900 property->setSetterMethodDecl(SetterMethod);
1901 }
1902 // Add any synthesized methods to the global pool. This allows us to
1903 // handle the following, which is supported by GCC (and part of the design).
1904 //
1905 // @interface Foo
1906 // @property double bar;
1907 // @end
1908 //
1909 // void thisIsUnfortunate() {
1910 // id foo;
1911 // double bar = [foo bar];
1912 // }
1913 //
1914 if (GetterMethod)
1915 AddInstanceMethodToGlobalPool(GetterMethod);
1916 if (SetterMethod)
1917 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001918
1919 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1920 if (!CurrentClass) {
1921 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1922 CurrentClass = Cat->getClassInterface();
1923 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1924 CurrentClass = Impl->getClassInterface();
1925 }
1926 if (GetterMethod)
1927 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1928 if (SetterMethod)
1929 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001930}
1931
John McCalld226f652010-08-21 09:40:31 +00001932void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001933 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001934 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001935 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001936 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001937 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001938 return;
1939
1940 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001941 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942
David Blaikie4e4d0842012-03-11 07:00:24 +00001943 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001944 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001945 PropertyTy->isObjCRetainableType()) {
1946 // 'readonly' property with no obvious lifetime.
1947 // its life time will be determined by its backing ivar.
1948 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1949 ObjCDeclSpec::DQ_PR_copy |
1950 ObjCDeclSpec::DQ_PR_retain |
1951 ObjCDeclSpec::DQ_PR_strong |
1952 ObjCDeclSpec::DQ_PR_weak |
1953 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001954 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001955 return;
1956 }
1957
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001958 if (propertyInPrimaryClass) {
1959 // we postpone most property diagnosis until class's implementation
1960 // because, its readonly attribute may be overridden in its class
1961 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001962 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1963 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001964 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1965 << "readonly" << "readwrite";
1966 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001967 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001968 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1969 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001970 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001971 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001972 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001973 ObjCDeclSpec::DQ_PR_retain |
1974 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00001975 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001976 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001977 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001978 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001979 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00001980 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001981 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 "copy" : "retain";
1983
Bill Wendlingad017fa2012-12-20 19:22:21 +00001984 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001985 diag::err_objc_property_attr_mutually_exclusive :
1986 diag::warn_objc_property_attr_mutually_exclusive)
1987 << "readonly" << which;
1988 }
1989
1990 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001991 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001992 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1993 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001994 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00001996 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1997 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1998 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001999 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002000 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002001 }
2002
2003 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002004 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2005 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002006 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2007 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002008 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002009 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002010 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002011 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2012 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002013 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002014 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002015 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002016 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2017 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002018 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002019 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002020 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002021 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002022 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2023 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002024 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002025 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002026 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2027 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002028 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2029 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002030 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002031 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002032 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002033 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2034 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002035 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002036 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002037 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002038 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2039 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002040 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002041 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002042 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002043 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002044 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2045 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002046 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002047 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002048 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2049 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002050 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2051 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002052 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +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 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002057 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002058 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002059 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002060 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2061 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002062 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002063 }
2064 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002065 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2066 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002067 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2068 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002070 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002071 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2072 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002073 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2074 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002075 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002076 }
2077
Bill Wendlingad017fa2012-12-20 19:22:21 +00002078 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2079 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002080 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2081 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002083 }
2084
Ted Kremenek9d64c152010-03-12 00:38:38 +00002085 // Warn if user supplied no assignment attribute, property is
2086 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002087 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002088 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2089 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2090 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002091 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002092 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002093 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002094 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002095 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002096 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002097 bool isAnyClassTy =
2098 (PropertyTy->isObjCClassType() ||
2099 PropertyTy->isObjCQualifiedClassType());
2100 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2101 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002102 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002103 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002104 else if (propertyInPrimaryClass) {
2105 // Don't issue warning on property with no life time in class
2106 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002107 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002108 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002109 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002110
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002111 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002112 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002113 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002114 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002115 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002116
2117 // FIXME: Implement warning dependent on NSCopying being
2118 // implemented. See also:
2119 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2120 // (please trim this list while you are at it).
2121 }
2122
Bill Wendlingad017fa2012-12-20 19:22:21 +00002123 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2124 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002125 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002126 && PropertyTy->isBlockPointerType())
2127 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002128 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2129 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2130 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002131 PropertyTy->isBlockPointerType())
2132 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002133
Bill Wendlingad017fa2012-12-20 19:22:21 +00002134 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2135 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002136 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2137
Ted Kremenek9d64c152010-03-12 00:38:38 +00002138}