blob: 298bad8efbb7a8859eb02a34993bc9b32ddb1945 [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenek28685ab2010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall265941b2011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld64c2eb2012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall265941b2011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallf85e1932011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall265941b2011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000102 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb892d702013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
120 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
121 // Have we seen this protocol before?
122 if (!Known.insert(Proto))
123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier());
130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
135 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
136 PEnd = Proto->protocol_end();
137 P != PEnd; ++P) {
138 CheckPropertyAgainstProtocol(S, Prop, *P, Known);
139 }
140}
141
John McCalld226f652010-08-21 09:40:31 +0000142Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000143 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000144 FieldDeclarator &FD,
145 ObjCDeclSpec &ODS,
146 Selector GetterSel,
147 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000148 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000149 tok::ObjCKeywordKind MethodImplKind,
150 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000151 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000152 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
153 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000154 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000155
Bill Wendlingad017fa2012-12-20 19:22:21 +0000156 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000157 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000158 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000159 // property is defaulted to 'assign' if it is readwrite and is
160 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000161 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000162 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000163 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
165 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
166 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
167 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000168
Douglas Gregoraabd0942013-01-21 19:05:22 +0000169 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000170 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000171 ObjCPropertyDecl *Res = 0;
172 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000173 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000174 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000175 FD, GetterSel, SetterSel,
176 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000177 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000178 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000179 isOverridingProperty, TSI,
180 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000181 if (!Res)
182 return 0;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000183 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000184 }
185
186 if (!Res) {
187 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
188 GetterSel, SetterSel, isAssign, isReadWrite,
189 Attributes, ODS.getPropertyAttributes(),
190 TSI, MethodImplKind);
191 if (lexicalDC)
192 Res->setLexicalDeclContext(lexicalDC);
193 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000194
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000195 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000196 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000197 (isa<ObjCInterfaceDecl>(ClassDecl) ||
198 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000199
David Blaikie4e4d0842012-03-11 07:00:24 +0000200 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000201 checkARCPropertyDecl(*this, Res);
202
Douglas Gregorb892d702013-01-21 19:42:21 +0000203 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000204 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000205 // For a class, compare the property against a property in our superclass.
206 bool FoundInSuper = false;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000207 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
208 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000209 for (unsigned I = 0, N = R.size(); I != N; ++I) {
210 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000211 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier());
Douglas Gregorb892d702013-01-21 19:42:21 +0000212 FoundInSuper = true;
213 break;
214 }
215 }
216 }
217
218 if (FoundInSuper) {
219 // Also compare the property against a property in our protocols.
220 for (ObjCInterfaceDecl::protocol_iterator P = IFace->protocol_begin(),
221 PEnd = IFace->protocol_end();
222 P != PEnd; ++P) {
223 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
227 for (ObjCInterfaceDecl::all_protocol_iterator
228 P = IFace->all_referenced_protocol_begin(),
229 PEnd = IFace->all_referenced_protocol_end();
230 P != PEnd; ++P) {
231 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
232 }
233 }
234 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
235 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
236 PEnd = Cat->protocol_end();
237 P != PEnd; ++P) {
238 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
239 }
240 } else {
241 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
242 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
243 PEnd = Proto->protocol_end();
244 P != PEnd; ++P) {
245 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000246 }
247 }
248
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000249 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000250 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000252
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000253static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000254makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000255 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
280
281 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
282}
283
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000284static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000285 SourceLocation LParenLoc, SourceLocation &Loc) {
286 if (LParenLoc.isMacroID())
287 return false;
288
289 SourceManager &SM = Context.getSourceManager();
290 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
291 // Try to load the file buffer.
292 bool invalidTemp = false;
293 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
294 if (invalidTemp)
295 return false;
296 const char *tokenBegin = file.data() + locInfo.second;
297
298 // Lex from the start of the given location.
299 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
300 Context.getLangOpts(),
301 file.begin(), tokenBegin, file.end());
302 Token Tok;
303 do {
304 lexer.LexFromRawLexer(Tok);
305 if (Tok.is(tok::raw_identifier) &&
306 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
307 Loc = Tok.getLocation();
308 return true;
309 }
310 } while (Tok.isNot(tok::r_paren));
311 return false;
312
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000313}
314
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000315static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000316 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
317 ObjCPropertyDecl::OBJC_PR_retain |
318 ObjCPropertyDecl::OBJC_PR_copy |
319 ObjCPropertyDecl::OBJC_PR_weak |
320 ObjCPropertyDecl::OBJC_PR_strong |
321 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
322}
323
Douglas Gregoraabd0942013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregord3297242013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000346 for (ObjCInterfaceDecl::known_extensions_iterator
347 Ext = CCPrimary->known_extensions_begin(),
348 ExtEnd = CCPrimary->known_extensions_end();
349 Ext != ExtEnd; ++Ext) {
350 if (ObjCPropertyDecl *prevDecl
351 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000352 Diag(AtLoc, diag::err_duplicate_property);
353 Diag(prevDecl->getLocation(), diag::note_property_declare);
354 return 0;
355 }
356 }
Douglas Gregord3297242013-01-16 23:00:23 +0000357 }
358
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 // Create a new ObjCPropertyDecl with the DeclContext being
360 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000361 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000362 ObjCPropertyDecl *PDecl =
363 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000364 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000365 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000366 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000367 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000371 // Set setter/getter selector name. Needed later.
372 PDecl->setGetterName(GetterSel);
373 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000374 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000375 DC->addDecl(PDecl);
376
377 // We need to look in the @interface to see if the @property was
378 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000379 if (!CCPrimary) {
380 Diag(CDecl->getLocation(), diag::err_continuation_class);
381 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000382 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000383 }
384
385 // Find the property in continuation class's primary class only.
386 ObjCPropertyDecl *PIDecl =
387 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
388
389 if (!PIDecl) {
390 // No matching property found in the primary class. Just fall thru
391 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000392 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000393 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000394 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000395 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000396
397 // A case of continuation class adding a new property in the class. This
398 // is not what it was meant for. However, gcc supports it and so should we.
399 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000400 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000401 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000402 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
403 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000404 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000405 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
406 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000407 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000408 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
409 bool IncompatibleObjC = false;
410 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000411 // Relax the strict type matching for property type in continuation class.
412 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000413 // as it narrows the object type in its primary class property. Note that
414 // this conversion is safe only because the wider type is for a 'readonly'
415 // property in primary class and 'narrowed' type for a 'readwrite' property
416 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000417 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
418 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
419 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
420 ConvertedType, IncompatibleObjC))
421 || IncompatibleObjC) {
422 Diag(AtLoc,
423 diag::err_type_mismatch_continuation_class) << PDecl->getType();
424 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000425 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000426 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000427 }
428
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000429 // The property 'PIDecl's readonly attribute will be over-ridden
430 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000431 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000432 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000433 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000434 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000435 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000436 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
437 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000438 Diag(AtLoc, diag::warn_property_attr_mismatch);
439 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000440 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000441 DeclContext *DC = cast<DeclContext>(CCPrimary);
442 if (!ObjCPropertyDecl::findPropertyDecl(DC,
443 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000444 // Protocol is not in the primary class. Must build one for it.
445 ObjCDeclSpec ProtocolPropertyODS;
446 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
447 // and ObjCPropertyDecl::PropertyAttributeKind have identical
448 // values. Should consolidate both into one enum type.
449 ProtocolPropertyODS.
450 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
451 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000452 // Must re-establish the context from class extension to primary
453 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000454 ContextRAII SavedContext(*this, CCPrimary);
455
John McCalld226f652010-08-21 09:40:31 +0000456 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000457 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000458 PIDecl->getGetterName(),
459 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000460 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000461 MethodImplKind,
462 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000463 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000464 }
465 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000466 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000467 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000468 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000469 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000470 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000471 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
472 PIDecl->setSetterName(SetterSel);
473 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000474 // Tailor the diagnostics for the common case where a readwrite
475 // property is declared both in the @interface and the continuation.
476 // This is a common error where the user often intended the original
477 // declaration to be readonly.
478 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000479 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000480 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
481 ? diag::err_use_continuation_class_redeclaration_readwrite
482 : diag::err_use_continuation_class;
483 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000484 << CCPrimary->getDeclName();
485 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000486 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000487 }
488 *isOverridingProperty = true;
489 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000490 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000491 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
492 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000493 if (ASTMutationListener *L = Context.getASTMutationListener())
494 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000495 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000496}
497
498ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
499 ObjCContainerDecl *CDecl,
500 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000501 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000502 FieldDeclarator &FD,
503 Selector GetterSel,
504 Selector SetterSel,
505 const bool isAssign,
506 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000507 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000508 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000509 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000510 tok::ObjCKeywordKind MethodImplKind,
511 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000512 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000513 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000514
515 // Issue a warning if property is 'assign' as default and its object, which is
516 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000517 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000518 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000519 if (const ObjCObjectPointerType *ObjPtrTy =
520 T->getAs<ObjCObjectPointerType>()) {
521 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
522 if (IDecl)
523 if (ObjCProtocolDecl* PNSCopying =
524 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
525 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
526 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000527 }
John McCallc12c5bb2010-05-15 11:32:37 +0000528 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000529 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
530
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000531 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000532 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
533 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000534 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000535
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000536 if (ObjCPropertyDecl *prevDecl =
537 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000539 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000540 PDecl->setInvalidDecl();
541 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000542 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000543 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000544 if (lexicalDC)
545 PDecl->setLexicalDeclContext(lexicalDC);
546 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000547
548 if (T->isArrayType() || T->isFunctionType()) {
549 Diag(AtLoc, diag::err_property_type) << T;
550 PDecl->setInvalidDecl();
551 }
552
553 ProcessDeclAttributes(S, PDecl, FD.D);
554
555 // Regardless of setter/getter attribute, we save the default getter/setter
556 // selector names in anticipation of declaration of setter/getter methods.
557 PDecl->setGetterName(GetterSel);
558 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000559 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000560 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000561
Bill Wendlingad017fa2012-12-20 19:22:21 +0000562 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000563 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
564
Bill Wendlingad017fa2012-12-20 19:22:21 +0000565 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000566 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
567
Bill Wendlingad017fa2012-12-20 19:22:21 +0000568 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
570
571 if (isReadWrite)
572 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
573
Bill Wendlingad017fa2012-12-20 19:22:21 +0000574 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
576
Bill Wendlingad017fa2012-12-20 19:22:21 +0000577 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
579
Bill Wendlingad017fa2012-12-20 19:22:21 +0000580 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
582
Bill Wendlingad017fa2012-12-20 19:22:21 +0000583 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
585
Bill Wendlingad017fa2012-12-20 19:22:21 +0000586 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
588
Ted Kremenek28685ab2010-03-12 00:46:40 +0000589 if (isAssign)
590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
591
John McCall265941b2011-09-13 18:31:23 +0000592 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000593 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000595 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000597
John McCallf85e1932011-06-15 23:02:42 +0000598 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
601 if (isAssign)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
603
Ted Kremenek28685ab2010-03-12 00:46:40 +0000604 if (MethodImplKind == tok::objc_required)
605 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
606 else if (MethodImplKind == tok::objc_optional)
607 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000608
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000609 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000610}
611
John McCallf85e1932011-06-15 23:02:42 +0000612static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
613 ObjCPropertyDecl *property,
614 ObjCIvarDecl *ivar) {
615 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
616
John McCallf85e1932011-06-15 23:02:42 +0000617 QualType ivarType = ivar->getType();
618 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000619
John McCall265941b2011-09-13 18:31:23 +0000620 // The lifetime implied by the property's attributes.
621 Qualifiers::ObjCLifetime propertyLifetime =
622 getImpliedARCOwnership(property->getPropertyAttributes(),
623 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000624
John McCall265941b2011-09-13 18:31:23 +0000625 // We're fine if they match.
626 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000627
John McCall265941b2011-09-13 18:31:23 +0000628 // These aren't valid lifetimes for object ivars; don't diagnose twice.
629 if (ivarLifetime == Qualifiers::OCL_None ||
630 ivarLifetime == Qualifiers::OCL_Autoreleasing)
631 return;
John McCallf85e1932011-06-15 23:02:42 +0000632
John McCalld64c2eb2012-08-20 23:36:59 +0000633 // If the ivar is private, and it's implicitly __unsafe_unretained
634 // becaues of its type, then pretend it was actually implicitly
635 // __strong. This is only sound because we're processing the
636 // property implementation before parsing any method bodies.
637 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
638 propertyLifetime == Qualifiers::OCL_Strong &&
639 ivar->getAccessControl() == ObjCIvarDecl::Private) {
640 SplitQualType split = ivarType.split();
641 if (split.Quals.hasObjCLifetime()) {
642 assert(ivarType->isObjCARCImplicitlyUnretainedType());
643 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
644 ivarType = S.Context.getQualifiedType(split);
645 ivar->setType(ivarType);
646 return;
647 }
648 }
649
John McCall265941b2011-09-13 18:31:23 +0000650 switch (propertyLifetime) {
651 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000652 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000653 << property->getDeclName()
654 << ivar->getDeclName()
655 << ivarLifetime;
656 break;
John McCallf85e1932011-06-15 23:02:42 +0000657
John McCall265941b2011-09-13 18:31:23 +0000658 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000659 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000660 << property->getDeclName()
661 << ivar->getDeclName();
662 break;
John McCallf85e1932011-06-15 23:02:42 +0000663
John McCall265941b2011-09-13 18:31:23 +0000664 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000665 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000666 << property->getDeclName()
667 << ivar->getDeclName()
668 << ((property->getPropertyAttributesAsWritten()
669 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
670 break;
John McCallf85e1932011-06-15 23:02:42 +0000671
John McCall265941b2011-09-13 18:31:23 +0000672 case Qualifiers::OCL_Autoreleasing:
673 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000674
John McCall265941b2011-09-13 18:31:23 +0000675 case Qualifiers::OCL_None:
676 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000677 return;
678 }
679
680 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000681 if (propertyImplLoc.isValid())
682 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000683}
684
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000685/// setImpliedPropertyAttributeForReadOnlyProperty -
686/// This routine evaludates life-time attributes for a 'readonly'
687/// property with no known lifetime of its own, using backing
688/// 'ivar's attribute, if any. If no backing 'ivar', property's
689/// life-time is assumed 'strong'.
690static void setImpliedPropertyAttributeForReadOnlyProperty(
691 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
692 Qualifiers::ObjCLifetime propertyLifetime =
693 getImpliedARCOwnership(property->getPropertyAttributes(),
694 property->getType());
695 if (propertyLifetime != Qualifiers::OCL_None)
696 return;
697
698 if (!ivar) {
699 // if no backing ivar, make property 'strong'.
700 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
701 return;
702 }
703 // property assumes owenership of backing ivar.
704 QualType ivarType = ivar->getType();
705 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
706 if (ivarLifetime == Qualifiers::OCL_Strong)
707 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
708 else if (ivarLifetime == Qualifiers::OCL_Weak)
709 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
710 return;
711}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000713/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
714/// attribute declared in primary class and attributes overridden in any of its
715/// class extensions.
716static void
717DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
718 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000719 unsigned Attributes = property->getPropertyAttributesAsWritten();
720 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000721 for (ObjCInterfaceDecl::known_extensions_iterator
722 Ext = ClassDecl->known_extensions_begin(),
723 ExtEnd = ClassDecl->known_extensions_end();
724 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000725 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000726 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
727 for (unsigned I = 0, N = R.size(); I != N; ++I) {
728 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
729 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000730 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000731 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000732
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000733 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000734 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000735 unsigned classExtPropertyAttr =
736 ClassExtProperty->getPropertyAttributesAsWritten();
737 // We are issuing the warning that we postponed because class extensions
738 // can override readonly->readwrite and 'setter' attributes originally
739 // placed on class's property declaration now make sense in the overridden
740 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000741 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000742 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000743 (classExtPropertyAttr &
744 (ObjCDeclSpec::DQ_PR_readwrite|
745 ObjCDeclSpec::DQ_PR_assign |
746 ObjCDeclSpec::DQ_PR_unsafe_unretained |
747 ObjCDeclSpec::DQ_PR_copy |
748 ObjCDeclSpec::DQ_PR_retain |
749 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000750 continue;
751 warn = true;
752 break;
753 }
754 }
755 }
756 if (warn) {
757 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
758 ObjCDeclSpec::DQ_PR_unsafe_unretained |
759 ObjCDeclSpec::DQ_PR_copy |
760 ObjCDeclSpec::DQ_PR_retain |
761 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000762 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000763 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000764 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000765 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000766 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000767 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000768 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000769 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000770 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000771 "retain" : "strong";
772
773 S.Diag(property->getLocation(),
774 diag::warn_objc_property_attr_mutually_exclusive)
775 << "readonly" << which;
776 }
777 }
778
779
780}
781
Ted Kremenek28685ab2010-03-12 00:46:40 +0000782/// ActOnPropertyImplDecl - This routine performs semantic checks and
783/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000784/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000785///
John McCalld226f652010-08-21 09:40:31 +0000786Decl *Sema::ActOnPropertyImplDecl(Scope *S,
787 SourceLocation AtLoc,
788 SourceLocation PropertyLoc,
789 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000790 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000791 IdentifierInfo *PropertyIvar,
792 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000793 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000794 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795 // Make sure we have a context for the property implementation declaration.
796 if (!ClassImpDecl) {
797 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000798 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000799 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000800 if (PropertyIvarLoc.isInvalid())
801 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000802 SourceLocation PropertyDiagLoc = PropertyLoc;
803 if (PropertyDiagLoc.isInvalid())
804 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 ObjCPropertyDecl *property = 0;
806 ObjCInterfaceDecl* IDecl = 0;
807 // Find the class or category class where this property must have
808 // a declaration.
809 ObjCImplementationDecl *IC = 0;
810 ObjCCategoryImplDecl* CatImplClass = 0;
811 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
812 IDecl = IC->getClassInterface();
813 // We always synthesize an interface for an implementation
814 // without an interface decl. So, IDecl is always non-zero.
815 assert(IDecl &&
816 "ActOnPropertyImplDecl - @implementation without @interface");
817
818 // Look for this property declaration in the @implementation's @interface
819 property = IDecl->FindPropertyDeclaration(PropertyId);
820 if (!property) {
821 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000822 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000823 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000824 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000825 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
826 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000827 if (AtLoc.isValid())
828 Diag(AtLoc, diag::warn_implicit_atomic_property);
829 else
830 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
831 Diag(property->getLocation(), diag::note_property_declare);
832 }
833
Ted Kremenek28685ab2010-03-12 00:46:40 +0000834 if (const ObjCCategoryDecl *CD =
835 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
836 if (!CD->IsClassExtension()) {
837 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
838 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000839 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000840 }
841 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000842 if (Synthesize&&
843 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
844 property->hasAttr<IBOutletAttr>() &&
845 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000846 bool ReadWriteProperty = false;
847 // Search into the class extensions and see if 'readonly property is
848 // redeclared 'readwrite', then no warning is to be issued.
849 for (ObjCInterfaceDecl::known_extensions_iterator
850 Ext = IDecl->known_extensions_begin(),
851 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
852 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
853 if (!R.empty())
854 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
855 PIkind = ExtProp->getPropertyAttributesAsWritten();
856 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
857 ReadWriteProperty = true;
858 break;
859 }
860 }
861 }
862
863 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000864 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
865 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000866 SourceLocation readonlyLoc;
867 if (LocPropertyAttribute(Context, "readonly",
868 property->getLParenLoc(), readonlyLoc)) {
869 SourceLocation endLoc =
870 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
871 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
872 Diag(property->getLocation(),
873 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
874 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
875 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000876 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000877 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000878
879 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000880
Ted Kremenek28685ab2010-03-12 00:46:40 +0000881 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
882 if (Synthesize) {
883 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000884 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000885 }
886 IDecl = CatImplClass->getClassInterface();
887 if (!IDecl) {
888 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000889 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000890 }
891 ObjCCategoryDecl *Category =
892 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
893
894 // If category for this implementation not found, it is an error which
895 // has already been reported eralier.
896 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000897 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000898 // Look for this property declaration in @implementation's category
899 property = Category->FindPropertyDeclaration(PropertyId);
900 if (!property) {
901 Diag(PropertyLoc, diag::error_bad_category_property_decl)
902 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000903 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000904 }
905 } else {
906 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000907 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000908 }
909 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000910 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000911 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000912 // Check that we have a valid, previously declared ivar for @synthesize
913 if (Synthesize) {
914 // @synthesize
915 if (!PropertyIvar)
916 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000917 // Check that this is a previously declared 'ivar' in 'IDecl' interface
918 ObjCInterfaceDecl *ClassDeclared;
919 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
920 QualType PropType = property->getType();
921 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000922
923 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000924 diag::err_incomplete_synthesized_property,
925 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000926 Diag(property->getLocation(), diag::note_property_declare);
927 CompleteTypeErr = true;
928 }
929
David Blaikie4e4d0842012-03-11 07:00:24 +0000930 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000931 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000932 ObjCPropertyDecl::OBJC_PR_readonly) &&
933 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000934 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
935 }
936
John McCallf85e1932011-06-15 23:02:42 +0000937 ObjCPropertyDecl::PropertyAttributeKind kind
938 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000939
940 // Add GC __weak to the ivar type if the property is weak.
941 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000942 getLangOpts().getGC() != LangOptions::NonGC) {
943 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000944 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000945 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000946 Diag(property->getLocation(), diag::note_property_declare);
947 } else {
948 PropertyIvarType =
949 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000950 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000951 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000952 if (AtLoc.isInvalid()) {
953 // Check when default synthesizing a property that there is
954 // an ivar matching property name and issue warning; since this
955 // is the most common case of not using an ivar used for backing
956 // property in non-default synthesis case.
957 ObjCInterfaceDecl *ClassDeclared=0;
958 ObjCIvarDecl *originalIvar =
959 IDecl->lookupInstanceVariable(property->getIdentifier(),
960 ClassDeclared);
961 if (originalIvar) {
962 Diag(PropertyDiagLoc,
963 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000964 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000965 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000966 Diag(property->getLocation(), diag::note_property_declare);
967 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000968 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000969 }
970
971 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000972 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000973 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000974 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000975 !PropertyIvarType.getObjCLifetime() &&
976 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000977
John McCall265941b2011-09-13 18:31:23 +0000978 // It's an error if we have to do this and the user didn't
979 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000980 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000981 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000982 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000983 diag::err_arc_objc_property_default_assign_on_object);
984 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000985 } else {
986 Qualifiers::ObjCLifetime lifetime =
987 getImpliedARCOwnership(kind, PropertyIvarType);
988 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000989 if (lifetime == Qualifiers::OCL_Weak) {
990 bool err = false;
991 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000992 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
993 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
994 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000995 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000996 Diag(property->getLocation(), diag::note_property_declare);
997 err = true;
998 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000999 }
John McCall0a7dd782012-08-21 02:47:43 +00001000 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001001 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001002 Diag(property->getLocation(), diag::note_property_declare);
1003 }
John McCallf85e1932011-06-15 23:02:42 +00001004 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001005
John McCallf85e1932011-06-15 23:02:42 +00001006 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001007 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001008 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1009 }
John McCallf85e1932011-06-15 23:02:42 +00001010 }
1011
1012 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001013 !getLangOpts().ObjCAutoRefCount &&
1014 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001015 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001016 Diag(property->getLocation(), diag::note_property_declare);
1017 }
1018
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001019 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001020 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001021 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001022 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001023 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001024 if (CompleteTypeErr)
1025 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001026 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001027 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001028
John McCall260611a2012-06-20 06:18:46 +00001029 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001030 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1031 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001032 // Note! I deliberately want it to fall thru so, we have a
1033 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001034 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001035 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001036 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001037 << property->getDeclName() << Ivar->getDeclName()
1038 << ClassDeclared->getDeclName();
1039 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001040 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001041 // Note! I deliberately want it to fall thru so more errors are caught.
1042 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001043 property->setPropertyIvarDecl(Ivar);
1044
Ted Kremenek28685ab2010-03-12 00:46:40 +00001045 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1046
1047 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001048 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001049 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001050 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001051 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001052 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001053 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001054 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001055 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001056 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1057 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001058 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001059 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001060 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001061 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001062 << property->getDeclName() << PropType
1063 << Ivar->getDeclName() << IvarType;
1064 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001065 // Note! I deliberately want it to fall thru so, we have a
1066 // a property implementation and to avoid future warnings.
1067 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001068 else {
1069 // FIXME! Rules for properties are somewhat different that those
1070 // for assignments. Use a new routine to consolidate all cases;
1071 // specifically for property redeclarations as well as for ivars.
1072 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1073 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1074 if (lhsType != rhsType &&
1075 lhsType->isArithmeticType()) {
1076 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1077 << property->getDeclName() << PropType
1078 << Ivar->getDeclName() << IvarType;
1079 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1080 // Fall thru - see previous comment
1081 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001082 }
1083 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001084 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001085 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001086 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001087 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001088 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001089 // Fall thru - see previous comment
1090 }
John McCallf85e1932011-06-15 23:02:42 +00001091 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001092 if ((property->getType()->isObjCObjectPointerType() ||
1093 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001094 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001095 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001096 << property->getDeclName() << Ivar->getDeclName();
1097 // Fall thru - see previous comment
1098 }
1099 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001100 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001101 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001102 } else if (PropertyIvar)
1103 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001104 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001105
Ted Kremenek28685ab2010-03-12 00:46:40 +00001106 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1107 ObjCPropertyImplDecl *PIDecl =
1108 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1109 property,
1110 (Synthesize ?
1111 ObjCPropertyImplDecl::Synthesize
1112 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001113 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001114
Fariborz Jahanian74414712012-05-15 18:12:51 +00001115 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001116 PIDecl->setInvalidDecl();
1117
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001118 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1119 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001120 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001121 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001122 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1123 // returned by the getter as it must conform to C++'s copy-return rules.
1124 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001125 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001126 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1127 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001128 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001129 VK_RValue, PropertyDiagLoc);
1130 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001131 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001132 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001133 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001134 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001135 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001136 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001137 getterMethod->getResultType(),
1138 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001139 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001140 Owned(IvarRefExpr));
1141 if (!Res.isInvalid()) {
1142 Expr *ResExpr = Res.takeAs<Expr>();
1143 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001144 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001145 PIDecl->setGetterCXXConstructor(ResExpr);
1146 }
1147 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001148 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1149 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1150 Diag(getterMethod->getLocation(),
1151 diag::warn_property_getter_owning_mismatch);
1152 Diag(property->getLocation(), diag::note_property_declare);
1153 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001154 }
1155 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1156 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001157 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1158 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001159 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001160 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001161 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1162 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001163 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001164 VK_RValue, PropertyDiagLoc);
1165 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001166 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001167 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001168 SelfExpr, true, true);
1169 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1170 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001171 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001172 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1173 VK_LValue, PropertyDiagLoc);
1174 MarkDeclRefReferenced(rhs);
1175 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001176 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001177 if (property->getPropertyAttributes() &
1178 ObjCPropertyDecl::OBJC_PR_atomic) {
1179 Expr *callExpr = Res.takeAs<Expr>();
1180 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001181 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1182 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001183 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001184 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001185 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001186 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001187 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001188 Diag(FuncDecl->getLocStart(),
1189 diag::note_callee_decl) << FuncDecl;
1190 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001191 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001192 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1193 }
1194 }
1195
Ted Kremenek28685ab2010-03-12 00:46:40 +00001196 if (IC) {
1197 if (Synthesize)
1198 if (ObjCPropertyImplDecl *PPIDecl =
1199 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1200 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1201 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1202 << PropertyIvar;
1203 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1204 }
1205
1206 if (ObjCPropertyImplDecl *PPIDecl
1207 = IC->FindPropertyImplDecl(PropertyId)) {
1208 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1209 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001210 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001211 }
1212 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001213 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001214 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001215 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001216 // Diagnose if an ivar was lazily synthesdized due to a previous
1217 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001218 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001219 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001220 ObjCIvarDecl *Ivar = 0;
1221 if (!Synthesize)
1222 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1223 else {
1224 if (PropertyIvar && PropertyIvar != PropertyId)
1225 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1226 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001227 // Issue diagnostics only if Ivar belongs to current class.
1228 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001229 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001230 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1231 << PropertyId;
1232 Ivar->setInvalidDecl();
1233 }
1234 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001235 } else {
1236 if (Synthesize)
1237 if (ObjCPropertyImplDecl *PPIDecl =
1238 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001239 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001240 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1241 << PropertyIvar;
1242 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1243 }
1244
1245 if (ObjCPropertyImplDecl *PPIDecl =
1246 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001247 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001248 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001249 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001250 }
1251 CatImplClass->addPropertyImplementation(PIDecl);
1252 }
1253
John McCalld226f652010-08-21 09:40:31 +00001254 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001255}
1256
1257//===----------------------------------------------------------------------===//
1258// Helper methods.
1259//===----------------------------------------------------------------------===//
1260
Ted Kremenek9d64c152010-03-12 00:38:38 +00001261/// DiagnosePropertyMismatch - Compares two properties for their
1262/// attributes and types and warns on a variety of inconsistencies.
1263///
1264void
1265Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1266 ObjCPropertyDecl *SuperProperty,
1267 const IdentifierInfo *inheritedName) {
1268 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1269 Property->getPropertyAttributes();
1270 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1271 SuperProperty->getPropertyAttributes();
1272 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1273 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1274 Diag(Property->getLocation(), diag::warn_readonly_property)
1275 << Property->getDeclName() << inheritedName;
1276 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1277 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1278 Diag(Property->getLocation(), diag::warn_property_attribute)
1279 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001280 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001281 unsigned CAttrRetain =
1282 (CAttr &
1283 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1284 unsigned SAttrRetain =
1285 (SAttr &
1286 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1287 bool CStrong = (CAttrRetain != 0);
1288 bool SStrong = (SAttrRetain != 0);
1289 if (CStrong != SStrong)
1290 Diag(Property->getLocation(), diag::warn_property_attribute)
1291 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1292 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001293
1294 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1295 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1296 Diag(Property->getLocation(), diag::warn_property_attribute)
1297 << Property->getDeclName() << "atomic" << inheritedName;
1298 if (Property->getSetterName() != SuperProperty->getSetterName())
1299 Diag(Property->getLocation(), diag::warn_property_attribute)
1300 << Property->getDeclName() << "setter" << inheritedName;
1301 if (Property->getGetterName() != SuperProperty->getGetterName())
1302 Diag(Property->getLocation(), diag::warn_property_attribute)
1303 << Property->getDeclName() << "getter" << inheritedName;
1304
1305 QualType LHSType =
1306 Context.getCanonicalType(SuperProperty->getType());
1307 QualType RHSType =
1308 Context.getCanonicalType(Property->getType());
1309
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001310 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001311 // Do cases not handled in above.
1312 // FIXME. For future support of covariant property types, revisit this.
1313 bool IncompatibleObjC = false;
1314 QualType ConvertedType;
1315 if (!isObjCPointerConversion(RHSType, LHSType,
1316 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001317 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001318 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1319 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001320 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1321 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001322 }
1323}
1324
1325bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1326 ObjCMethodDecl *GetterMethod,
1327 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001328 if (!GetterMethod)
1329 return false;
1330 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1331 QualType PropertyIvarType = property->getType().getNonReferenceType();
1332 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1333 if (!compat) {
1334 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1335 isa<ObjCObjectPointerType>(GetterType))
1336 compat =
1337 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001338 GetterType->getAs<ObjCObjectPointerType>(),
1339 PropertyIvarType->getAs<ObjCObjectPointerType>());
1340 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001341 != Compatible) {
1342 Diag(Loc, diag::error_property_accessor_type)
1343 << property->getDeclName() << PropertyIvarType
1344 << GetterMethod->getSelector() << GetterType;
1345 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1346 return true;
1347 } else {
1348 compat = true;
1349 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1350 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1351 if (lhsType != rhsType && lhsType->isArithmeticType())
1352 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001353 }
1354 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001355
1356 if (!compat) {
1357 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1358 << property->getDeclName()
1359 << GetterMethod->getSelector();
1360 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1361 return true;
1362 }
1363
Ted Kremenek9d64c152010-03-12 00:38:38 +00001364 return false;
1365}
1366
Ted Kremenek9d64c152010-03-12 00:38:38 +00001367/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1368/// of properties declared in a protocol and compares their attribute against
1369/// the same property declared in the class or category.
1370void
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001371Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1372 if (!CDecl)
1373 return;
1374
1375 // Category case.
1376 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1377 // FIXME: We should perform this check when the property in the category
1378 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001379 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1380 if (!CatDecl->IsClassExtension())
1381 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1382 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001383 ObjCPropertyDecl *ProtoProp = *P;
1384 DeclContext::lookup_result R
1385 = CatDecl->lookup(ProtoProp->getDeclName());
1386 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1387 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1388 if (CatProp != ProtoProp) {
1389 // Property protocol already exist in class. Diagnose any mismatch.
1390 DiagnosePropertyMismatch(CatProp, ProtoProp,
1391 PDecl->getIdentifier());
1392 }
1393 }
1394 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001395 }
1396 return;
1397 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001398
1399 // Class
1400 // FIXME: We should perform this check when the property in the class
1401 // is declared.
1402 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001403 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001404 E = PDecl->prop_end(); P != E; ++P) {
1405 ObjCPropertyDecl *ProtoProp = *P;
1406 DeclContext::lookup_result R
Douglas Gregoraabd0942013-01-21 19:05:22 +00001407 = IDecl->lookup(ProtoProp->getDeclName());
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001408 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1409 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1410 if (ClassProp != ProtoProp) {
1411 // Property protocol already exist in class. Diagnose any mismatch.
1412 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1413 PDecl->getIdentifier());
1414 }
1415 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001416 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001417 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001418}
1419
Ted Kremenek9d64c152010-03-12 00:38:38 +00001420/// isPropertyReadonly - Return true if property is readonly, by searching
1421/// for the property in the class and in its categories and implementations
1422///
1423bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1424 ObjCInterfaceDecl *IDecl) {
1425 // by far the most common case.
1426 if (!PDecl->isReadOnly())
1427 return false;
1428 // Even if property is ready only, if interface has a user defined setter,
1429 // it is not considered read only.
1430 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1431 return false;
1432
1433 // Main class has the property as 'readonly'. Must search
1434 // through the category list to see if the property's
1435 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001436 for (ObjCInterfaceDecl::visible_categories_iterator
1437 Cat = IDecl->visible_categories_begin(),
1438 CatEnd = IDecl->visible_categories_end();
1439 Cat != CatEnd; ++Cat) {
1440 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001441 return false;
1442 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001443 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001444 if (P && !P->isReadOnly())
1445 return false;
1446 }
1447
1448 // Also, check for definition of a setter method in the implementation if
1449 // all else failed.
1450 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1451 if (ObjCImplementationDecl *IMD =
1452 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1453 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1454 return false;
1455 } else if (ObjCCategoryImplDecl *CIMD =
1456 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1457 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1458 return false;
1459 }
1460 }
1461 // Lastly, look through the implementation (if one is in scope).
1462 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1463 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1464 return false;
1465 // If all fails, look at the super class.
1466 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1467 return isPropertyReadonly(PDecl, SIDecl);
1468 return true;
1469}
1470
1471/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001472/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001473void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001474 ObjCContainerDecl::PropertyMap &PropMap,
1475 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001476 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1477 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1478 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001479 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001480 PropMap[Prop->getIdentifier()] = Prop;
1481 }
1482 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001483 for (ObjCInterfaceDecl::all_protocol_iterator
1484 PI = IDecl->all_referenced_protocol_begin(),
1485 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001486 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001487 }
1488 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1489 if (!CATDecl->IsClassExtension())
1490 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1491 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001492 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001493 PropMap[Prop->getIdentifier()] = Prop;
1494 }
1495 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001496 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001497 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001498 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001499 }
1500 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1501 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1502 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001503 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001504 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1505 // Exclude property for protocols which conform to class's super-class,
1506 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001507 if (!PropertyFromSuper ||
1508 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001509 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1510 if (!PropEntry)
1511 PropEntry = Prop;
1512 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001513 }
1514 // scan through protocol's protocols.
1515 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1516 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001517 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001518 }
1519}
1520
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001521/// CollectSuperClassPropertyImplementations - This routine collects list of
1522/// properties to be implemented in super class(s) and also coming from their
1523/// conforming protocols.
1524static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001525 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001526 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1527 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001529 SDecl = SDecl->getSuperClass();
1530 }
1531 }
1532}
1533
James Dennett699c9042012-06-15 07:13:21 +00001534/// \brief Default synthesizes all properties which must be synthesized
1535/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001536void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1537 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001538
Anna Zaksb36ea372012-10-18 19:17:53 +00001539 ObjCInterfaceDecl::PropertyMap PropMap;
1540 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001541 if (PropMap.empty())
1542 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001543 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001544 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1545
Anna Zaksb36ea372012-10-18 19:17:53 +00001546 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001547 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1548 ObjCPropertyDecl *Prop = P->second;
1549 // If property to be implemented in the super class, ignore.
1550 if (SuperPropMap[Prop->getIdentifier()])
1551 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001552 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001553 if (Prop->isInvalidDecl() ||
1554 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1555 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1556 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001557 // Property may have been synthesized by user.
1558 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1559 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001560 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1561 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1562 continue;
1563 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1564 continue;
1565 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001566 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1567 // We won't auto-synthesize properties declared in protocols.
1568 Diag(IMPDecl->getLocation(),
1569 diag::warn_auto_synthesizing_protocol_property);
1570 Diag(Prop->getLocation(), diag::note_property_declare);
1571 continue;
1572 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001573
1574 // We use invalid SourceLocations for the synthesized ivars since they
1575 // aren't really synthesized at a particular location; they just exist.
1576 // Saying that they are located at the @implementation isn't really going
1577 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001578 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1579 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1580 true,
1581 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001582 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001583 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001584 if (PIDecl) {
1585 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001586 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001587 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001588 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001589}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001590
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001591void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001592 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001593 return;
1594 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1595 if (!IC)
1596 return;
1597 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001598 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001599 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001600}
1601
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001602void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001603 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001604 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001605 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1606 ObjCInterfaceDecl *IDecl;
1607 // Gather properties which need not be implemented in this class
1608 // or category.
1609 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1610 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1611 // For categories, no need to implement properties declared in
1612 // its primary class (and its super classes) if property is
1613 // declared in one of those containers.
1614 if ((IDecl = C->getClassInterface()))
1615 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1616 }
1617 if (IDecl)
1618 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001619
Anna Zaksb36ea372012-10-18 19:17:53 +00001620 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001621 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001622 if (PropMap.empty())
1623 return;
1624
1625 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1626 for (ObjCImplDecl::propimpl_iterator
1627 I = IMPDecl->propimpl_begin(),
1628 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001629 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001630
Anna Zaksb36ea372012-10-18 19:17:53 +00001631 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001632 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1633 ObjCPropertyDecl *Prop = P->second;
1634 // Is there a matching propery synthesize/dynamic?
1635 if (Prop->isInvalidDecl() ||
1636 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001637 PropImplMap.count(Prop) ||
1638 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001639 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001640 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001641 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001642 isa<ObjCCategoryDecl>(CDecl) ?
1643 diag::warn_setter_getter_impl_required_in_category :
1644 diag::warn_setter_getter_impl_required)
1645 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001646 Diag(Prop->getLocation(),
1647 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001648 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001649 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001650 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001651 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1652
Ted Kremenek9d64c152010-03-12 00:38:38 +00001653 }
1654
1655 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001656 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001657 isa<ObjCCategoryDecl>(CDecl) ?
1658 diag::warn_setter_getter_impl_required_in_category :
1659 diag::warn_setter_getter_impl_required)
1660 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001661 Diag(Prop->getLocation(),
1662 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001663 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001664 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001665 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001666 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001667 }
1668 }
1669}
1670
1671void
1672Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1673 ObjCContainerDecl* IDecl) {
1674 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001675 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001676 return;
1677 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1678 E = IDecl->prop_end();
1679 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001680 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001681 ObjCMethodDecl *GetterMethod = 0;
1682 ObjCMethodDecl *SetterMethod = 0;
1683 bool LookedUpGetterSetter = false;
1684
Bill Wendlingad017fa2012-12-20 19:22:21 +00001685 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001686 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001687
John McCall265941b2011-09-13 18:31:23 +00001688 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1689 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001690 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1691 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1692 LookedUpGetterSetter = true;
1693 if (GetterMethod) {
1694 Diag(GetterMethod->getLocation(),
1695 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001696 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001697 Diag(Property->getLocation(), diag::note_property_declare);
1698 }
1699 if (SetterMethod) {
1700 Diag(SetterMethod->getLocation(),
1701 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001702 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001703 Diag(Property->getLocation(), diag::note_property_declare);
1704 }
1705 }
1706
Ted Kremenek9d64c152010-03-12 00:38:38 +00001707 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001708 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1709 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001710 continue;
1711 if (const ObjCPropertyImplDecl *PIDecl
1712 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1713 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1714 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001715 if (!LookedUpGetterSetter) {
1716 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1717 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1718 LookedUpGetterSetter = true;
1719 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001720 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1721 SourceLocation MethodLoc =
1722 (GetterMethod ? GetterMethod->getLocation()
1723 : SetterMethod->getLocation());
1724 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001725 << Property->getIdentifier() << (GetterMethod != 0)
1726 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001727 // fixit stuff.
1728 if (!AttributesAsWritten) {
1729 if (Property->getLParenLoc().isValid()) {
1730 // @property () ... case.
1731 SourceRange PropSourceRange(Property->getAtLoc(),
1732 Property->getLParenLoc());
1733 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1734 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1735 }
1736 else {
1737 //@property id etc.
1738 SourceLocation endLoc =
1739 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1740 endLoc = endLoc.getLocWithOffset(-1);
1741 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1742 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1743 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1744 }
1745 }
1746 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1747 // @property () ... case.
1748 SourceLocation endLoc = Property->getLParenLoc();
1749 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1750 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1751 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1752 }
1753 else
1754 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001755 Diag(Property->getLocation(), diag::note_property_declare);
1756 }
1757 }
1758 }
1759}
1760
John McCallf85e1932011-06-15 23:02:42 +00001761void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001762 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001763 return;
1764
1765 for (ObjCImplementationDecl::propimpl_iterator
1766 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001767 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001768 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1769 continue;
1770
1771 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001772 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1773 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001774 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1775 if (!method)
1776 continue;
1777 ObjCMethodFamily family = method->getMethodFamily();
1778 if (family == OMF_alloc || family == OMF_copy ||
1779 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001780 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001781 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1782 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001783 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001784 Diag(PD->getLocation(), diag::note_property_declare);
1785 }
1786 }
1787 }
1788}
1789
John McCall5de74d12010-11-10 07:01:40 +00001790/// AddPropertyAttrs - Propagates attributes from a property to the
1791/// implicitly-declared getter or setter for that property.
1792static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1793 ObjCPropertyDecl *Property) {
1794 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001795 for (Decl::attr_iterator A = Property->attr_begin(),
1796 AEnd = Property->attr_end();
1797 A != AEnd; ++A) {
1798 if (isa<DeprecatedAttr>(*A) ||
1799 isa<UnavailableAttr>(*A) ||
1800 isa<AvailabilityAttr>(*A))
1801 PropertyMethod->addAttr((*A)->clone(S.Context));
1802 }
John McCall5de74d12010-11-10 07:01:40 +00001803}
1804
Ted Kremenek9d64c152010-03-12 00:38:38 +00001805/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1806/// have the property type and issue diagnostics if they don't.
1807/// Also synthesize a getter/setter method if none exist (and update the
1808/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1809/// methods is the "right" thing to do.
1810void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001811 ObjCContainerDecl *CD,
1812 ObjCPropertyDecl *redeclaredProperty,
1813 ObjCContainerDecl *lexicalDC) {
1814
Ted Kremenek9d64c152010-03-12 00:38:38 +00001815 ObjCMethodDecl *GetterMethod, *SetterMethod;
1816
1817 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1818 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1819 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1820 property->getLocation());
1821
1822 if (SetterMethod) {
1823 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1824 property->getPropertyAttributes();
1825 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1826 Context.getCanonicalType(SetterMethod->getResultType()) !=
1827 Context.VoidTy)
1828 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1829 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001830 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001831 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1832 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001833 Diag(property->getLocation(),
1834 diag::warn_accessor_property_type_mismatch)
1835 << property->getDeclName()
1836 << SetterMethod->getSelector();
1837 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1838 }
1839 }
1840
1841 // Synthesize getter/setter methods if none exist.
1842 // Find the default getter and if one not found, add one.
1843 // FIXME: The synthesized property we set here is misleading. We almost always
1844 // synthesize these methods unless the user explicitly provided prototypes
1845 // (which is odd, but allowed). Sema should be typechecking that the
1846 // declarations jive in that situation (which it is not currently).
1847 if (!GetterMethod) {
1848 // No instance method of same name as property getter name was found.
1849 // Declare a getter method and add it to the list of methods
1850 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001851 SourceLocation Loc = redeclaredProperty ?
1852 redeclaredProperty->getLocation() :
1853 property->getLocation();
1854
1855 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1856 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001857 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001858 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001859 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001860 (property->getPropertyImplementation() ==
1861 ObjCPropertyDecl::Optional) ?
1862 ObjCMethodDecl::Optional :
1863 ObjCMethodDecl::Required);
1864 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001865
1866 AddPropertyAttrs(*this, GetterMethod, property);
1867
Ted Kremenek23173d72010-05-18 21:09:07 +00001868 // FIXME: Eventually this shouldn't be needed, as the lexical context
1869 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001870 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001871 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001872 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1873 GetterMethod->addAttr(
1874 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001875 } else
1876 // A user declared getter will be synthesize when @synthesize of
1877 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001878 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001879 property->setGetterMethodDecl(GetterMethod);
1880
1881 // Skip setter if property is read-only.
1882 if (!property->isReadOnly()) {
1883 // Find the default setter and if one not found, add one.
1884 if (!SetterMethod) {
1885 // No instance method of same name as property setter name was found.
1886 // Declare a setter method and add it to the list of methods
1887 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001888 SourceLocation Loc = redeclaredProperty ?
1889 redeclaredProperty->getLocation() :
1890 property->getLocation();
1891
1892 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001893 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001894 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001895 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001896 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001897 /*isImplicitlyDeclared=*/true,
1898 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001899 (property->getPropertyImplementation() ==
1900 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001901 ObjCMethodDecl::Optional :
1902 ObjCMethodDecl::Required);
1903
Ted Kremenek9d64c152010-03-12 00:38:38 +00001904 // Invent the arguments for the setter. We don't bother making a
1905 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001906 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1907 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001908 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001909 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001910 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001911 SC_None,
1912 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001914 SetterMethod->setMethodParams(Context, Argument,
1915 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001916
1917 AddPropertyAttrs(*this, SetterMethod, property);
1918
Ted Kremenek9d64c152010-03-12 00:38:38 +00001919 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001920 // FIXME: Eventually this shouldn't be needed, as the lexical context
1921 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001922 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001923 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001924 } else
1925 // A user declared setter will be synthesize when @synthesize of
1926 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001927 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001928 property->setSetterMethodDecl(SetterMethod);
1929 }
1930 // Add any synthesized methods to the global pool. This allows us to
1931 // handle the following, which is supported by GCC (and part of the design).
1932 //
1933 // @interface Foo
1934 // @property double bar;
1935 // @end
1936 //
1937 // void thisIsUnfortunate() {
1938 // id foo;
1939 // double bar = [foo bar];
1940 // }
1941 //
1942 if (GetterMethod)
1943 AddInstanceMethodToGlobalPool(GetterMethod);
1944 if (SetterMethod)
1945 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001946
1947 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1948 if (!CurrentClass) {
1949 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1950 CurrentClass = Cat->getClassInterface();
1951 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1952 CurrentClass = Impl->getClassInterface();
1953 }
1954 if (GetterMethod)
1955 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1956 if (SetterMethod)
1957 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001958}
1959
John McCalld226f652010-08-21 09:40:31 +00001960void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001961 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001962 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001963 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001964 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001965 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001966 return;
1967
1968 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001969 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001970
David Blaikie4e4d0842012-03-11 07:00:24 +00001971 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001972 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001973 PropertyTy->isObjCRetainableType()) {
1974 // 'readonly' property with no obvious lifetime.
1975 // its life time will be determined by its backing ivar.
1976 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1977 ObjCDeclSpec::DQ_PR_copy |
1978 ObjCDeclSpec::DQ_PR_retain |
1979 ObjCDeclSpec::DQ_PR_strong |
1980 ObjCDeclSpec::DQ_PR_weak |
1981 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001982 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001983 return;
1984 }
1985
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001986 if (propertyInPrimaryClass) {
1987 // we postpone most property diagnosis until class's implementation
1988 // because, its readonly attribute may be overridden in its class
1989 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001990 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1991 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001992 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1993 << "readonly" << "readwrite";
1994 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001996 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1997 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001998 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001999 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002000 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002001 ObjCDeclSpec::DQ_PR_retain |
2002 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00002003 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002004 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002005 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002006 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002007 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00002008 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002009 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002010 "copy" : "retain";
2011
Bill Wendlingad017fa2012-12-20 19:22:21 +00002012 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002013 diag::err_objc_property_attr_mutually_exclusive :
2014 diag::warn_objc_property_attr_mutually_exclusive)
2015 << "readonly" << which;
2016 }
2017
2018 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002019 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002020 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2021 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002022 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002023 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002024 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2025 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2026 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002027 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002028 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002029 }
2030
2031 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002032 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2033 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002034 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2035 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002036 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002037 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002038 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002039 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2040 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002041 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002042 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002043 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002044 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2045 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002046 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002047 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002048 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002049 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002050 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2051 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002052 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002053 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002054 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2055 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002056 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2057 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002058 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002059 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002060 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002061 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2062 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002063 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002064 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002065 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002066 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2067 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002068 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002069 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002070 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002071 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002072 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2073 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002074 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002075 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002076 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2077 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002078 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2079 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002080 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002081 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002083 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2084 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002085 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002086 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002087 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002088 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2089 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002090 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002091 }
2092 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002093 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2094 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002095 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2096 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002097 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002098 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002099 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2100 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002101 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2102 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002103 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002104 }
2105
Bill Wendlingad017fa2012-12-20 19:22:21 +00002106 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2107 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002108 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2109 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002110 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002111 }
2112
Ted Kremenek9d64c152010-03-12 00:38:38 +00002113 // Warn if user supplied no assignment attribute, property is
2114 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002116 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2117 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2118 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002119 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002120 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002121 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002122 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002123 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002124 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002125 bool isAnyClassTy =
2126 (PropertyTy->isObjCClassType() ||
2127 PropertyTy->isObjCQualifiedClassType());
2128 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2129 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002130 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002131 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002132 else if (propertyInPrimaryClass) {
2133 // Don't issue warning on property with no life time in class
2134 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002135 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002136 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002137 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002138
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002139 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002140 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002141 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002142 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002143 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002144
2145 // FIXME: Implement warning dependent on NSCopying being
2146 // implemented. See also:
2147 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2148 // (please trim this list while you are at it).
2149 }
2150
Bill Wendlingad017fa2012-12-20 19:22:21 +00002151 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2152 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002153 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002154 && PropertyTy->isBlockPointerType())
2155 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2157 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2158 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002159 PropertyTy->isBlockPointerType())
2160 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002161
Bill Wendlingad017fa2012-12-20 19:22:21 +00002162 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2163 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002164 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2165
Ted Kremenek9d64c152010-03-12 00:38:38 +00002166}