blob: 9d18c659c27eced3f364c0dd158e24e8bbf48b9f [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])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb892d702013-01-21 19:42:21 +0000130 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])) {
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +0000211 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
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
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +0000324static const char *NameOfOwnershipAttribute(unsigned attr) {
325 if (attr & ObjCPropertyDecl::OBJC_PR_assign)
326 return "assign";
327 if (attr & ObjCPropertyDecl::OBJC_PR_retain )
328 return "retain";
329 if (attr & ObjCPropertyDecl::OBJC_PR_copy)
330 return "copy";
331 if (attr & ObjCPropertyDecl::OBJC_PR_weak)
332 return "weak";
333 if (attr & ObjCPropertyDecl::OBJC_PR_strong)
334 return "strong";
335 assert(attr & ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
336 return "unsafe_unretained";
337}
338
Douglas Gregoraabd0942013-01-21 19:05:22 +0000339ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000340Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000341 SourceLocation AtLoc,
342 SourceLocation LParenLoc,
343 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000344 Selector GetterSel, Selector SetterSel,
345 const bool isAssign,
346 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000347 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000348 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000349 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000350 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000351 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000352 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000353 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000354 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000355 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000356 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
357
Douglas Gregord3297242013-01-16 23:00:23 +0000358 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000359 // Check for duplicate declaration of this property in current and
360 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000361 for (ObjCInterfaceDecl::known_extensions_iterator
362 Ext = CCPrimary->known_extensions_begin(),
363 ExtEnd = CCPrimary->known_extensions_end();
364 Ext != ExtEnd; ++Ext) {
365 if (ObjCPropertyDecl *prevDecl
366 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000367 Diag(AtLoc, diag::err_duplicate_property);
368 Diag(prevDecl->getLocation(), diag::note_property_declare);
369 return 0;
370 }
371 }
Douglas Gregord3297242013-01-16 23:00:23 +0000372 }
373
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000374 // Create a new ObjCPropertyDecl with the DeclContext being
375 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000376 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000377 ObjCPropertyDecl *PDecl =
378 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000379 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000380 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000381 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000382 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000383 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000384 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000385 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000386 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
387 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
388 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
389 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000390 // Set setter/getter selector name. Needed later.
391 PDecl->setGetterName(GetterSel);
392 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000393 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000394 DC->addDecl(PDecl);
395
396 // We need to look in the @interface to see if the @property was
397 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000398 if (!CCPrimary) {
399 Diag(CDecl->getLocation(), diag::err_continuation_class);
400 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000401 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000402 }
403
404 // Find the property in continuation class's primary class only.
405 ObjCPropertyDecl *PIDecl =
406 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
407
408 if (!PIDecl) {
409 // No matching property found in the primary class. Just fall thru
410 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000411 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000412 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000413 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000414 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000415
416 // A case of continuation class adding a new property in the class. This
417 // is not what it was meant for. However, gcc supports it and so should we.
418 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000419 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000420 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000421 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
422 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000423 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000424 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
425 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000426 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000427 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
428 bool IncompatibleObjC = false;
429 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000430 // Relax the strict type matching for property type in continuation class.
431 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000432 // as it narrows the object type in its primary class property. Note that
433 // this conversion is safe only because the wider type is for a 'readonly'
434 // property in primary class and 'narrowed' type for a 'readwrite' property
435 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000436 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
437 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
438 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
439 ConvertedType, IncompatibleObjC))
440 || IncompatibleObjC) {
441 Diag(AtLoc,
442 diag::err_type_mismatch_continuation_class) << PDecl->getType();
443 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000444 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000445 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000446 }
447
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000448 // The property 'PIDecl's readonly attribute will be over-ridden
449 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000450 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000451 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +0000452 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
453 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000454 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000455 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000456 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000457 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
458 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000459 Diag(AtLoc, diag::warn_property_attr_mismatch);
460 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000461 }
Fariborz Jahanian9d9a9432013-10-26 00:35:39 +0000462 else if (getLangOpts().ObjCAutoRefCount) {
463 QualType PrimaryPropertyQT =
464 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
465 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
466 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
467 PrimaryPropertyQT.getObjCLifetime();
468 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
469 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
470 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
471 Diag(PIDecl->getLocation(), diag::note_property_declare);
472 }
473 }
474 }
475
Ted Kremenek9944c762010-03-18 01:22:36 +0000476 DeclContext *DC = cast<DeclContext>(CCPrimary);
477 if (!ObjCPropertyDecl::findPropertyDecl(DC,
478 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000479 // Protocol is not in the primary class. Must build one for it.
480 ObjCDeclSpec ProtocolPropertyODS;
481 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
482 // and ObjCPropertyDecl::PropertyAttributeKind have identical
483 // values. Should consolidate both into one enum type.
484 ProtocolPropertyODS.
485 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
486 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000487 // Must re-establish the context from class extension to primary
488 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000489 ContextRAII SavedContext(*this, CCPrimary);
490
John McCalld226f652010-08-21 09:40:31 +0000491 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000492 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000493 PIDecl->getGetterName(),
494 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000495 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000496 MethodImplKind,
497 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000498 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000499 }
500 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000501 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000502 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000503 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000504 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000505 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000506 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
507 PIDecl->setSetterName(SetterSel);
508 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000509 // Tailor the diagnostics for the common case where a readwrite
510 // property is declared both in the @interface and the continuation.
511 // This is a common error where the user often intended the original
512 // declaration to be readonly.
513 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000514 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000515 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
516 ? diag::err_use_continuation_class_redeclaration_readwrite
517 : diag::err_use_continuation_class;
518 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000519 << CCPrimary->getDeclName();
520 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000521 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000522 }
523 *isOverridingProperty = true;
524 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000525 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000526 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
527 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000528 if (ASTMutationListener *L = Context.getASTMutationListener())
529 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000530 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000531}
532
533ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
534 ObjCContainerDecl *CDecl,
535 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000536 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000537 FieldDeclarator &FD,
538 Selector GetterSel,
539 Selector SetterSel,
540 const bool isAssign,
541 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000542 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000543 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000544 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000545 tok::ObjCKeywordKind MethodImplKind,
546 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000547 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000548 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000549
550 // Issue a warning if property is 'assign' as default and its object, which is
551 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000552 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000553 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000554 if (const ObjCObjectPointerType *ObjPtrTy =
555 T->getAs<ObjCObjectPointerType>()) {
556 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
557 if (IDecl)
558 if (ObjCProtocolDecl* PNSCopying =
559 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
560 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
561 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000562 }
Eli Friedman27d46442013-07-09 01:38:07 +0000563
564 if (T->isObjCObjectType()) {
565 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
566 StarLoc = PP.getLocForEndOfToken(StarLoc);
567 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
568 << FixItHint::CreateInsertion(StarLoc, "*");
569 T = Context.getObjCObjectPointerType(T);
570 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
571 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
572 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000574 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
576 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000577 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000578
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000579 if (ObjCPropertyDecl *prevDecl =
580 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000582 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000583 PDecl->setInvalidDecl();
584 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000585 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000586 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000587 if (lexicalDC)
588 PDecl->setLexicalDeclContext(lexicalDC);
589 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000590
591 if (T->isArrayType() || T->isFunctionType()) {
592 Diag(AtLoc, diag::err_property_type) << T;
593 PDecl->setInvalidDecl();
594 }
595
596 ProcessDeclAttributes(S, PDecl, FD.D);
597
598 // Regardless of setter/getter attribute, we save the default getter/setter
599 // selector names in anticipation of declaration of setter/getter methods.
600 PDecl->setGetterName(GetterSel);
601 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000602 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000603 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000604
Bill Wendlingad017fa2012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
607
Bill Wendlingad017fa2012-12-20 19:22:21 +0000608 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000609 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
610
Bill Wendlingad017fa2012-12-20 19:22:21 +0000611 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
613
614 if (isReadWrite)
615 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
616
Bill Wendlingad017fa2012-12-20 19:22:21 +0000617 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000618 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
619
Bill Wendlingad017fa2012-12-20 19:22:21 +0000620 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000621 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
622
Bill Wendlingad017fa2012-12-20 19:22:21 +0000623 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000624 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
625
Bill Wendlingad017fa2012-12-20 19:22:21 +0000626 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000627 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
628
Bill Wendlingad017fa2012-12-20 19:22:21 +0000629 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000630 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
631
Ted Kremenek28685ab2010-03-12 00:46:40 +0000632 if (isAssign)
633 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
634
John McCall265941b2011-09-13 18:31:23 +0000635 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000636 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000637 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000638 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000639 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000640
John McCallf85e1932011-06-15 23:02:42 +0000641 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000642 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000643 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
644 if (isAssign)
645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
646
Ted Kremenek28685ab2010-03-12 00:46:40 +0000647 if (MethodImplKind == tok::objc_required)
648 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
649 else if (MethodImplKind == tok::objc_optional)
650 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000651
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000652 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000653}
654
John McCallf85e1932011-06-15 23:02:42 +0000655static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
656 ObjCPropertyDecl *property,
657 ObjCIvarDecl *ivar) {
658 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
659
John McCallf85e1932011-06-15 23:02:42 +0000660 QualType ivarType = ivar->getType();
661 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000662
John McCall265941b2011-09-13 18:31:23 +0000663 // The lifetime implied by the property's attributes.
664 Qualifiers::ObjCLifetime propertyLifetime =
665 getImpliedARCOwnership(property->getPropertyAttributes(),
666 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000667
John McCall265941b2011-09-13 18:31:23 +0000668 // We're fine if they match.
669 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000670
John McCall265941b2011-09-13 18:31:23 +0000671 // These aren't valid lifetimes for object ivars; don't diagnose twice.
672 if (ivarLifetime == Qualifiers::OCL_None ||
673 ivarLifetime == Qualifiers::OCL_Autoreleasing)
674 return;
John McCallf85e1932011-06-15 23:02:42 +0000675
John McCalld64c2eb2012-08-20 23:36:59 +0000676 // If the ivar is private, and it's implicitly __unsafe_unretained
677 // becaues of its type, then pretend it was actually implicitly
678 // __strong. This is only sound because we're processing the
679 // property implementation before parsing any method bodies.
680 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
681 propertyLifetime == Qualifiers::OCL_Strong &&
682 ivar->getAccessControl() == ObjCIvarDecl::Private) {
683 SplitQualType split = ivarType.split();
684 if (split.Quals.hasObjCLifetime()) {
685 assert(ivarType->isObjCARCImplicitlyUnretainedType());
686 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
687 ivarType = S.Context.getQualifiedType(split);
688 ivar->setType(ivarType);
689 return;
690 }
691 }
692
John McCall265941b2011-09-13 18:31:23 +0000693 switch (propertyLifetime) {
694 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000695 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000696 << property->getDeclName()
697 << ivar->getDeclName()
698 << ivarLifetime;
699 break;
John McCallf85e1932011-06-15 23:02:42 +0000700
John McCall265941b2011-09-13 18:31:23 +0000701 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000702 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000703 << property->getDeclName()
704 << ivar->getDeclName();
705 break;
John McCallf85e1932011-06-15 23:02:42 +0000706
John McCall265941b2011-09-13 18:31:23 +0000707 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000708 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000709 << property->getDeclName()
710 << ivar->getDeclName()
711 << ((property->getPropertyAttributesAsWritten()
712 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
713 break;
John McCallf85e1932011-06-15 23:02:42 +0000714
John McCall265941b2011-09-13 18:31:23 +0000715 case Qualifiers::OCL_Autoreleasing:
716 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000717
John McCall265941b2011-09-13 18:31:23 +0000718 case Qualifiers::OCL_None:
719 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000720 return;
721 }
722
723 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000724 if (propertyImplLoc.isValid())
725 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000726}
727
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000728/// setImpliedPropertyAttributeForReadOnlyProperty -
729/// This routine evaludates life-time attributes for a 'readonly'
730/// property with no known lifetime of its own, using backing
731/// 'ivar's attribute, if any. If no backing 'ivar', property's
732/// life-time is assumed 'strong'.
733static void setImpliedPropertyAttributeForReadOnlyProperty(
734 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
735 Qualifiers::ObjCLifetime propertyLifetime =
736 getImpliedARCOwnership(property->getPropertyAttributes(),
737 property->getType());
738 if (propertyLifetime != Qualifiers::OCL_None)
739 return;
740
741 if (!ivar) {
742 // if no backing ivar, make property 'strong'.
743 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
744 return;
745 }
746 // property assumes owenership of backing ivar.
747 QualType ivarType = ivar->getType();
748 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
749 if (ivarLifetime == Qualifiers::OCL_Strong)
750 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
751 else if (ivarLifetime == Qualifiers::OCL_Weak)
752 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
753 return;
754}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000755
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000756/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
757/// in inherited protocols with mismatched types. Since any of them can
758/// be candidate for synthesis.
Benjamin Kramerb1a4d372013-05-23 15:53:44 +0000759static void
760DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
761 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000762 ObjCPropertyDecl *Property) {
763 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
764 for (ObjCInterfaceDecl::all_protocol_iterator
765 PI = ClassDecl->all_referenced_protocol_begin(),
766 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
767 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
768 PDecl->collectInheritedProtocolProperties(Property, PropMap);
769 }
770 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
771 while (SDecl) {
772 for (ObjCInterfaceDecl::all_protocol_iterator
773 PI = SDecl->all_referenced_protocol_begin(),
774 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
775 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
776 PDecl->collectInheritedProtocolProperties(Property, PropMap);
777 }
778 SDecl = SDecl->getSuperClass();
779 }
780
781 if (PropMap.empty())
782 return;
783
784 QualType RHSType = S.Context.getCanonicalType(Property->getType());
785 bool FirsTime = true;
786 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
787 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
788 ObjCPropertyDecl *Prop = I->second;
789 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
790 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
791 bool IncompatibleObjC = false;
792 QualType ConvertedType;
793 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
794 || IncompatibleObjC) {
795 if (FirsTime) {
796 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
797 << Property->getType();
798 FirsTime = false;
799 }
800 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
801 << Prop->getType();
802 }
803 }
804 }
805 if (!FirsTime && AtLoc.isValid())
806 S.Diag(AtLoc, diag::note_property_synthesize);
807}
808
Ted Kremenek28685ab2010-03-12 00:46:40 +0000809/// ActOnPropertyImplDecl - This routine performs semantic checks and
810/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000811/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000812///
John McCalld226f652010-08-21 09:40:31 +0000813Decl *Sema::ActOnPropertyImplDecl(Scope *S,
814 SourceLocation AtLoc,
815 SourceLocation PropertyLoc,
816 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000817 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000818 IdentifierInfo *PropertyIvar,
819 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000820 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000821 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000822 // Make sure we have a context for the property implementation declaration.
823 if (!ClassImpDecl) {
824 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000825 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000826 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000827 if (PropertyIvarLoc.isInvalid())
828 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000829 SourceLocation PropertyDiagLoc = PropertyLoc;
830 if (PropertyDiagLoc.isInvalid())
831 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000832 ObjCPropertyDecl *property = 0;
833 ObjCInterfaceDecl* IDecl = 0;
834 // Find the class or category class where this property must have
835 // a declaration.
836 ObjCImplementationDecl *IC = 0;
837 ObjCCategoryImplDecl* CatImplClass = 0;
838 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
839 IDecl = IC->getClassInterface();
840 // We always synthesize an interface for an implementation
841 // without an interface decl. So, IDecl is always non-zero.
842 assert(IDecl &&
843 "ActOnPropertyImplDecl - @implementation without @interface");
844
845 // Look for this property declaration in the @implementation's @interface
846 property = IDecl->FindPropertyDeclaration(PropertyId);
847 if (!property) {
848 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000849 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000850 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000851 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000852 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
853 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000854 if (AtLoc.isValid())
855 Diag(AtLoc, diag::warn_implicit_atomic_property);
856 else
857 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
858 Diag(property->getLocation(), diag::note_property_declare);
859 }
860
Ted Kremenek28685ab2010-03-12 00:46:40 +0000861 if (const ObjCCategoryDecl *CD =
862 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
863 if (!CD->IsClassExtension()) {
864 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
865 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000866 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000867 }
868 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000869 if (Synthesize&&
870 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
871 property->hasAttr<IBOutletAttr>() &&
872 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000873 bool ReadWriteProperty = false;
874 // Search into the class extensions and see if 'readonly property is
875 // redeclared 'readwrite', then no warning is to be issued.
876 for (ObjCInterfaceDecl::known_extensions_iterator
877 Ext = IDecl->known_extensions_begin(),
878 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
879 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
880 if (!R.empty())
881 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
882 PIkind = ExtProp->getPropertyAttributesAsWritten();
883 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
884 ReadWriteProperty = true;
885 break;
886 }
887 }
888 }
889
890 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000891 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
892 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000893 SourceLocation readonlyLoc;
894 if (LocPropertyAttribute(Context, "readonly",
895 property->getLParenLoc(), readonlyLoc)) {
896 SourceLocation endLoc =
897 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
898 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
899 Diag(property->getLocation(),
900 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
901 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
902 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000903 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000904 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000905 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
906 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000907
Ted Kremenek28685ab2010-03-12 00:46:40 +0000908 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
909 if (Synthesize) {
910 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000911 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000912 }
913 IDecl = CatImplClass->getClassInterface();
914 if (!IDecl) {
915 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000916 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000917 }
918 ObjCCategoryDecl *Category =
919 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
920
921 // If category for this implementation not found, it is an error which
922 // has already been reported eralier.
923 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000924 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000925 // Look for this property declaration in @implementation's category
926 property = Category->FindPropertyDeclaration(PropertyId);
927 if (!property) {
928 Diag(PropertyLoc, diag::error_bad_category_property_decl)
929 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000930 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000931 }
932 } else {
933 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000934 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000935 }
936 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000937 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000938 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000939 // Check that we have a valid, previously declared ivar for @synthesize
940 if (Synthesize) {
941 // @synthesize
942 if (!PropertyIvar)
943 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000944 // Check that this is a previously declared 'ivar' in 'IDecl' interface
945 ObjCInterfaceDecl *ClassDeclared;
946 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
947 QualType PropType = property->getType();
948 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000949
950 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000951 diag::err_incomplete_synthesized_property,
952 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000953 Diag(property->getLocation(), diag::note_property_declare);
954 CompleteTypeErr = true;
955 }
956
David Blaikie4e4d0842012-03-11 07:00:24 +0000957 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000958 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000959 ObjCPropertyDecl::OBJC_PR_readonly) &&
960 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000961 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
962 }
963
John McCallf85e1932011-06-15 23:02:42 +0000964 ObjCPropertyDecl::PropertyAttributeKind kind
965 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000966
967 // Add GC __weak to the ivar type if the property is weak.
968 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000969 getLangOpts().getGC() != LangOptions::NonGC) {
970 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000971 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000972 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000973 Diag(property->getLocation(), diag::note_property_declare);
974 } else {
975 PropertyIvarType =
976 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000977 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000978 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000979 if (AtLoc.isInvalid()) {
980 // Check when default synthesizing a property that there is
981 // an ivar matching property name and issue warning; since this
982 // is the most common case of not using an ivar used for backing
983 // property in non-default synthesis case.
984 ObjCInterfaceDecl *ClassDeclared=0;
985 ObjCIvarDecl *originalIvar =
986 IDecl->lookupInstanceVariable(property->getIdentifier(),
987 ClassDeclared);
988 if (originalIvar) {
989 Diag(PropertyDiagLoc,
990 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000991 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000992 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000993 Diag(property->getLocation(), diag::note_property_declare);
994 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000995 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000996 }
997
998 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000999 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +00001000 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +00001001 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +00001002 !PropertyIvarType.getObjCLifetime() &&
1003 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +00001004
John McCall265941b2011-09-13 18:31:23 +00001005 // It's an error if we have to do this and the user didn't
1006 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001007 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +00001008 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001009 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001010 diag::err_arc_objc_property_default_assign_on_object);
1011 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +00001012 } else {
1013 Qualifiers::ObjCLifetime lifetime =
1014 getImpliedARCOwnership(kind, PropertyIvarType);
1015 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001016 if (lifetime == Qualifiers::OCL_Weak) {
1017 bool err = false;
1018 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001019 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1020 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1021 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001022 Diag(property->getLocation(),
1023 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1024 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1025 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001026 err = true;
1027 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001028 }
John McCall0a7dd782012-08-21 02:47:43 +00001029 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001030 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001031 Diag(property->getLocation(), diag::note_property_declare);
1032 }
John McCallf85e1932011-06-15 23:02:42 +00001033 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001034
John McCallf85e1932011-06-15 23:02:42 +00001035 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001036 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001037 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1038 }
John McCallf85e1932011-06-15 23:02:42 +00001039 }
1040
1041 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001042 !getLangOpts().ObjCAutoRefCount &&
1043 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001044 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001045 Diag(property->getLocation(), diag::note_property_declare);
1046 }
1047
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001048 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001049 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001050 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001051 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001052 (Expr *)0, true);
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001053 if (RequireNonAbstractType(PropertyIvarLoc,
1054 PropertyIvarType,
1055 diag::err_abstract_type_in_decl,
1056 AbstractSynthesizedIvarType)) {
1057 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001058 Ivar->setInvalidDecl();
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001059 } else if (CompleteTypeErr)
1060 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001061 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001062 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001063
John McCall260611a2012-06-20 06:18:46 +00001064 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001065 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1066 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001067 // Note! I deliberately want it to fall thru so, we have a
1068 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001069 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001070 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001071 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001072 << property->getDeclName() << Ivar->getDeclName()
1073 << ClassDeclared->getDeclName();
1074 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001075 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001076 // Note! I deliberately want it to fall thru so more errors are caught.
1077 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001078 property->setPropertyIvarDecl(Ivar);
1079
Ted Kremenek28685ab2010-03-12 00:46:40 +00001080 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1081
1082 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001083 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001084 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001085 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001086 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001087 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001088 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001089 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001090 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001091 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1092 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001093 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001094 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001095 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001096 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001097 << property->getDeclName() << PropType
1098 << Ivar->getDeclName() << IvarType;
1099 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001100 // Note! I deliberately want it to fall thru so, we have a
1101 // a property implementation and to avoid future warnings.
1102 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001103 else {
1104 // FIXME! Rules for properties are somewhat different that those
1105 // for assignments. Use a new routine to consolidate all cases;
1106 // specifically for property redeclarations as well as for ivars.
1107 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1108 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1109 if (lhsType != rhsType &&
1110 lhsType->isArithmeticType()) {
1111 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1112 << property->getDeclName() << PropType
1113 << Ivar->getDeclName() << IvarType;
1114 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1115 // Fall thru - see previous comment
1116 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001117 }
1118 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001119 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001120 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001121 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001122 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001123 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001124 // Fall thru - see previous comment
1125 }
John McCallf85e1932011-06-15 23:02:42 +00001126 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001127 if ((property->getType()->isObjCObjectPointerType() ||
1128 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001129 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001130 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001131 << property->getDeclName() << Ivar->getDeclName();
1132 // Fall thru - see previous comment
1133 }
1134 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001135 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001136 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001137 } else if (PropertyIvar)
1138 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001139 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001140
Ted Kremenek28685ab2010-03-12 00:46:40 +00001141 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1142 ObjCPropertyImplDecl *PIDecl =
1143 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1144 property,
1145 (Synthesize ?
1146 ObjCPropertyImplDecl::Synthesize
1147 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001148 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001149
Fariborz Jahanian74414712012-05-15 18:12:51 +00001150 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001151 PIDecl->setInvalidDecl();
1152
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001153 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1154 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001155 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001156 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001157 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1158 // returned by the getter as it must conform to C++'s copy-return rules.
1159 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001160 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001161 ImplicitParamDecl *SelfDecl = getterMethod->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 *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001167 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001168 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001169 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001170 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001171 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001172 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001173 getterMethod->getResultType(),
1174 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001175 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001176 Owned(IvarRefExpr));
1177 if (!Res.isInvalid()) {
1178 Expr *ResExpr = Res.takeAs<Expr>();
1179 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001180 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001181 PIDecl->setGetterCXXConstructor(ResExpr);
1182 }
1183 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001184 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1185 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1186 Diag(getterMethod->getLocation(),
1187 diag::warn_property_getter_owning_mismatch);
1188 Diag(property->getLocation(), diag::note_property_declare);
1189 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001190 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1191 switch (getterMethod->getMethodFamily()) {
1192 case OMF_retain:
1193 case OMF_retainCount:
1194 case OMF_release:
1195 case OMF_autorelease:
1196 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1197 << 1 << getterMethod->getSelector();
1198 break;
1199 default:
1200 break;
1201 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001202 }
1203 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1204 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001205 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1206 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001207 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001208 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001209 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1210 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001211 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001212 VK_RValue, PropertyDiagLoc);
1213 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001214 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001215 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001216 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001217 SelfExpr, true, true);
1218 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1219 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001220 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001221 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1222 VK_LValue, PropertyDiagLoc);
1223 MarkDeclRefReferenced(rhs);
1224 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001225 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001226 if (property->getPropertyAttributes() &
1227 ObjCPropertyDecl::OBJC_PR_atomic) {
1228 Expr *callExpr = Res.takeAs<Expr>();
1229 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001230 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1231 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001232 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001233 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001234 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001235 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001236 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001237 Diag(FuncDecl->getLocStart(),
1238 diag::note_callee_decl) << FuncDecl;
1239 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001240 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001241 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1242 }
1243 }
1244
Ted Kremenek28685ab2010-03-12 00:46:40 +00001245 if (IC) {
1246 if (Synthesize)
1247 if (ObjCPropertyImplDecl *PPIDecl =
1248 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1249 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1250 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1251 << PropertyIvar;
1252 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1253 }
1254
1255 if (ObjCPropertyImplDecl *PPIDecl
1256 = IC->FindPropertyImplDecl(PropertyId)) {
1257 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1258 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001259 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001260 }
1261 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001262 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001263 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001264 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001265 // Diagnose if an ivar was lazily synthesdized due to a previous
1266 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001267 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001268 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001269 ObjCIvarDecl *Ivar = 0;
1270 if (!Synthesize)
1271 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1272 else {
1273 if (PropertyIvar && PropertyIvar != PropertyId)
1274 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1275 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001276 // Issue diagnostics only if Ivar belongs to current class.
1277 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001278 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001279 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1280 << PropertyId;
1281 Ivar->setInvalidDecl();
1282 }
1283 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001284 } else {
1285 if (Synthesize)
1286 if (ObjCPropertyImplDecl *PPIDecl =
1287 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001288 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001289 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1290 << PropertyIvar;
1291 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1292 }
1293
1294 if (ObjCPropertyImplDecl *PPIDecl =
1295 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001296 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001297 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001298 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001299 }
1300 CatImplClass->addPropertyImplementation(PIDecl);
1301 }
1302
John McCalld226f652010-08-21 09:40:31 +00001303 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001304}
1305
1306//===----------------------------------------------------------------------===//
1307// Helper methods.
1308//===----------------------------------------------------------------------===//
1309
Ted Kremenek9d64c152010-03-12 00:38:38 +00001310/// DiagnosePropertyMismatch - Compares two properties for their
1311/// attributes and types and warns on a variety of inconsistencies.
1312///
1313void
1314Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1315 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001316 const IdentifierInfo *inheritedName,
1317 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001318 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001319 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001320 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001321 SuperProperty->getPropertyAttributes();
1322
1323 // We allow readonly properties without an explicit ownership
1324 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1325 // to be overridden by a property with any explicit ownership in the subclass.
1326 if (!OverridingProtocolProperty &&
1327 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1328 ;
1329 else {
1330 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1331 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1332 Diag(Property->getLocation(), diag::warn_readonly_property)
1333 << Property->getDeclName() << inheritedName;
1334 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1335 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001336 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001337 << Property->getDeclName() << "copy" << inheritedName;
1338 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1339 unsigned CAttrRetain =
1340 (CAttr &
1341 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1342 unsigned SAttrRetain =
1343 (SAttr &
1344 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1345 bool CStrong = (CAttrRetain != 0);
1346 bool SStrong = (SAttrRetain != 0);
1347 if (CStrong != SStrong)
1348 Diag(Property->getLocation(), diag::warn_property_attribute)
1349 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1350 }
John McCallf85e1932011-06-15 23:02:42 +00001351 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001352
1353 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001354 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001355 Diag(Property->getLocation(), diag::warn_property_attribute)
1356 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001357 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1358 }
1359 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001360 Diag(Property->getLocation(), diag::warn_property_attribute)
1361 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001362 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1363 }
1364 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001365 Diag(Property->getLocation(), diag::warn_property_attribute)
1366 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001367 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1368 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001369
1370 QualType LHSType =
1371 Context.getCanonicalType(SuperProperty->getType());
1372 QualType RHSType =
1373 Context.getCanonicalType(Property->getType());
1374
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001375 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001376 // Do cases not handled in above.
1377 // FIXME. For future support of covariant property types, revisit this.
1378 bool IncompatibleObjC = false;
1379 QualType ConvertedType;
1380 if (!isObjCPointerConversion(RHSType, LHSType,
1381 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001382 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001383 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1384 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001385 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1386 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001387 }
1388}
1389
1390bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1391 ObjCMethodDecl *GetterMethod,
1392 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001393 if (!GetterMethod)
1394 return false;
1395 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1396 QualType PropertyIvarType = property->getType().getNonReferenceType();
1397 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1398 if (!compat) {
1399 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1400 isa<ObjCObjectPointerType>(GetterType))
1401 compat =
1402 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001403 GetterType->getAs<ObjCObjectPointerType>(),
1404 PropertyIvarType->getAs<ObjCObjectPointerType>());
1405 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001406 != Compatible) {
1407 Diag(Loc, diag::error_property_accessor_type)
1408 << property->getDeclName() << PropertyIvarType
1409 << GetterMethod->getSelector() << GetterType;
1410 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1411 return true;
1412 } else {
1413 compat = true;
1414 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1415 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1416 if (lhsType != rhsType && lhsType->isArithmeticType())
1417 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001418 }
1419 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001420
1421 if (!compat) {
1422 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1423 << property->getDeclName()
1424 << GetterMethod->getSelector();
1425 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1426 return true;
1427 }
1428
Ted Kremenek9d64c152010-03-12 00:38:38 +00001429 return false;
1430}
1431
Ted Kremenek9d64c152010-03-12 00:38:38 +00001432/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001433/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001435 ObjCContainerDecl::PropertyMap &PropMap,
1436 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001437 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1438 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1439 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001440 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001441 PropMap[Prop->getIdentifier()] = Prop;
1442 }
1443 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001444 for (ObjCInterfaceDecl::all_protocol_iterator
1445 PI = IDecl->all_referenced_protocol_begin(),
1446 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001447 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 }
1449 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1450 if (!CATDecl->IsClassExtension())
1451 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1452 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001453 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001454 PropMap[Prop->getIdentifier()] = Prop;
1455 }
1456 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001457 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001459 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 }
1461 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1462 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1463 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001464 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001465 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1466 // Exclude property for protocols which conform to class's super-class,
1467 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001468 if (!PropertyFromSuper ||
1469 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001470 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1471 if (!PropEntry)
1472 PropEntry = Prop;
1473 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001474 }
1475 // scan through protocol's protocols.
1476 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1477 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001478 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001479 }
1480}
1481
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001482/// CollectSuperClassPropertyImplementations - This routine collects list of
1483/// properties to be implemented in super class(s) and also coming from their
1484/// conforming protocols.
1485static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001486 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001487 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001488 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001489 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001490 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001491 SDecl = SDecl->getSuperClass();
1492 }
1493 }
1494}
1495
Fariborz Jahanian26202292013-02-14 19:07:19 +00001496/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1497/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1498/// declared in class 'IFace'.
1499bool
1500Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1501 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1502 if (!IV->getSynthesize())
1503 return false;
1504 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1505 Method->isInstanceMethod());
1506 if (!IMD || !IMD->isPropertyAccessor())
1507 return false;
1508
1509 // look up a property declaration whose one of its accessors is implemented
1510 // by this method.
1511 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1512 E = IFace->prop_end(); P != E; ++P) {
1513 ObjCPropertyDecl *property = *P;
1514 if ((property->getGetterName() == IMD->getSelector() ||
1515 property->getSetterName() == IMD->getSelector()) &&
1516 (property->getPropertyIvarDecl() == IV))
1517 return true;
1518 }
1519 return false;
1520}
1521
1522
James Dennett699c9042012-06-15 07:13:21 +00001523/// \brief Default synthesizes all properties which must be synthesized
1524/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001525void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1526 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001527
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001529 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1530 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001531 if (PropMap.empty())
1532 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001533 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001534 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1535
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001536 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1537 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001538 // Is there a matching property synthesize/dynamic?
1539 if (Prop->isInvalidDecl() ||
1540 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1541 continue;
1542 // Property may have been synthesized by user.
1543 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1544 continue;
1545 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1546 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1547 continue;
1548 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1549 continue;
1550 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001551 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001552 if (SuperPropMap[Prop->getIdentifier()]) {
1553 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1554 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1555 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001556 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001557 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1558 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001559 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1560 << Prop->getIdentifier()->getName();
1561 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1562 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001563 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001564 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001565 if (ObjCPropertyImplDecl *PID =
1566 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1567 if (PID->getPropertyDecl() != Prop) {
1568 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1569 << Prop->getIdentifier()->getName();
1570 if (!PID->getLocation().isInvalid())
1571 Diag(PID->getLocation(), diag::note_property_synthesize);
1572 }
1573 continue;
1574 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001575 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1576 // We won't auto-synthesize properties declared in protocols.
1577 Diag(IMPDecl->getLocation(),
1578 diag::warn_auto_synthesizing_protocol_property);
1579 Diag(Prop->getLocation(), diag::note_property_declare);
1580 continue;
1581 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001582
1583 // We use invalid SourceLocations for the synthesized ivars since they
1584 // aren't really synthesized at a particular location; they just exist.
1585 // Saying that they are located at the @implementation isn't really going
1586 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001587 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1588 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1589 true,
1590 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001591 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001592 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001593 if (PIDecl) {
1594 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001595 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001596 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001597 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001598}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001599
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001600void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001601 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001602 return;
1603 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1604 if (!IC)
1605 return;
1606 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001607 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001608 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001609}
1610
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001611void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001612 ObjCContainerDecl *CDecl) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001613 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1614 ObjCInterfaceDecl *IDecl;
1615 // Gather properties which need not be implemented in this class
1616 // or category.
1617 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1618 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1619 // For categories, no need to implement properties declared in
1620 // its primary class (and its super classes) if property is
1621 // declared in one of those containers.
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001622 if ((IDecl = C->getClassInterface())) {
1623 ObjCInterfaceDecl::PropertyDeclOrder PO;
1624 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1625 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001626 }
1627 if (IDecl)
1628 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001629
Anna Zaksb36ea372012-10-18 19:17:53 +00001630 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001631 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001632 if (PropMap.empty())
1633 return;
1634
1635 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1636 for (ObjCImplDecl::propimpl_iterator
1637 I = IMPDecl->propimpl_begin(),
1638 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001639 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001640
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001641 SelectorSet InsMap;
1642 // Collect property accessors implemented in current implementation.
1643 for (ObjCImplementationDecl::instmeth_iterator
1644 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1645 InsMap.insert((*I)->getSelector());
1646
1647 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1648 ObjCInterfaceDecl *PrimaryClass = 0;
1649 if (C && !C->IsClassExtension())
1650 if ((PrimaryClass = C->getClassInterface()))
1651 // Report unimplemented properties in the category as well.
1652 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1653 // When reporting on missing setter/getters, do not report when
1654 // setter/getter is implemented in category's primary class
1655 // implementation.
1656 for (ObjCImplementationDecl::instmeth_iterator
1657 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1658 InsMap.insert((*I)->getSelector());
1659 }
1660
Anna Zaksb36ea372012-10-18 19:17:53 +00001661 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001662 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1663 ObjCPropertyDecl *Prop = P->second;
1664 // Is there a matching propery synthesize/dynamic?
1665 if (Prop->isInvalidDecl() ||
1666 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001667 PropImplMap.count(Prop) ||
1668 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001669 continue;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001670 // When reporting on missing property getter implementation in
1671 // categories, do not report when they are declared in primary class,
1672 // class's protocol, or one of it super classes. This is because,
1673 // the class is going to implement them.
1674 if (!InsMap.count(Prop->getGetterName()) &&
1675 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001676 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001677 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001678 isa<ObjCCategoryDecl>(CDecl) ?
1679 diag::warn_setter_getter_impl_required_in_category :
1680 diag::warn_setter_getter_impl_required)
1681 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001682 Diag(Prop->getLocation(),
1683 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001684 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001685 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001686 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001687 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1688
Ted Kremenek9d64c152010-03-12 00:38:38 +00001689 }
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001690 // When reporting on missing property setter implementation in
1691 // categories, do not report when they are declared in primary class,
1692 // class's protocol, or one of it super classes. This is because,
1693 // the class is going to implement them.
1694 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1695 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001696 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001697 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001698 isa<ObjCCategoryDecl>(CDecl) ?
1699 diag::warn_setter_getter_impl_required_in_category :
1700 diag::warn_setter_getter_impl_required)
1701 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001702 Diag(Prop->getLocation(),
1703 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001704 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001705 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001706 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001707 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001708 }
1709 }
1710}
1711
1712void
1713Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1714 ObjCContainerDecl* IDecl) {
1715 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001716 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001717 return;
1718 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1719 E = IDecl->prop_end();
1720 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001721 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001722 ObjCMethodDecl *GetterMethod = 0;
1723 ObjCMethodDecl *SetterMethod = 0;
1724 bool LookedUpGetterSetter = false;
1725
Bill Wendlingad017fa2012-12-20 19:22:21 +00001726 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001727 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001728
John McCall265941b2011-09-13 18:31:23 +00001729 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1730 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001731 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1732 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1733 LookedUpGetterSetter = true;
1734 if (GetterMethod) {
1735 Diag(GetterMethod->getLocation(),
1736 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001737 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001738 Diag(Property->getLocation(), diag::note_property_declare);
1739 }
1740 if (SetterMethod) {
1741 Diag(SetterMethod->getLocation(),
1742 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001743 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001744 Diag(Property->getLocation(), diag::note_property_declare);
1745 }
1746 }
1747
Ted Kremenek9d64c152010-03-12 00:38:38 +00001748 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001749 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1750 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001751 continue;
1752 if (const ObjCPropertyImplDecl *PIDecl
1753 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1754 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1755 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001756 if (!LookedUpGetterSetter) {
1757 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1758 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1759 LookedUpGetterSetter = true;
1760 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001761 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1762 SourceLocation MethodLoc =
1763 (GetterMethod ? GetterMethod->getLocation()
1764 : SetterMethod->getLocation());
1765 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001766 << Property->getIdentifier() << (GetterMethod != 0)
1767 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001768 // fixit stuff.
1769 if (!AttributesAsWritten) {
1770 if (Property->getLParenLoc().isValid()) {
1771 // @property () ... case.
1772 SourceRange PropSourceRange(Property->getAtLoc(),
1773 Property->getLParenLoc());
1774 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1775 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1776 }
1777 else {
1778 //@property id etc.
1779 SourceLocation endLoc =
1780 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1781 endLoc = endLoc.getLocWithOffset(-1);
1782 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1783 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1784 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1785 }
1786 }
1787 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1788 // @property () ... case.
1789 SourceLocation endLoc = Property->getLParenLoc();
1790 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1791 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1792 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1793 }
1794 else
1795 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001796 Diag(Property->getLocation(), diag::note_property_declare);
1797 }
1798 }
1799 }
1800}
1801
John McCallf85e1932011-06-15 23:02:42 +00001802void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001803 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001804 return;
1805
1806 for (ObjCImplementationDecl::propimpl_iterator
1807 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001808 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001809 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1810 continue;
1811
1812 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001813 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1814 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001815 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1816 if (!method)
1817 continue;
1818 ObjCMethodFamily family = method->getMethodFamily();
1819 if (family == OMF_alloc || family == OMF_copy ||
1820 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001821 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001822 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1823 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001824 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001825 Diag(PD->getLocation(), diag::note_property_declare);
1826 }
1827 }
1828 }
1829}
1830
John McCall5de74d12010-11-10 07:01:40 +00001831/// AddPropertyAttrs - Propagates attributes from a property to the
1832/// implicitly-declared getter or setter for that property.
1833static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1834 ObjCPropertyDecl *Property) {
1835 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001836 for (Decl::attr_iterator A = Property->attr_begin(),
1837 AEnd = Property->attr_end();
1838 A != AEnd; ++A) {
1839 if (isa<DeprecatedAttr>(*A) ||
1840 isa<UnavailableAttr>(*A) ||
1841 isa<AvailabilityAttr>(*A))
1842 PropertyMethod->addAttr((*A)->clone(S.Context));
1843 }
John McCall5de74d12010-11-10 07:01:40 +00001844}
1845
Ted Kremenek9d64c152010-03-12 00:38:38 +00001846/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1847/// have the property type and issue diagnostics if they don't.
1848/// Also synthesize a getter/setter method if none exist (and update the
1849/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1850/// methods is the "right" thing to do.
1851void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001852 ObjCContainerDecl *CD,
1853 ObjCPropertyDecl *redeclaredProperty,
1854 ObjCContainerDecl *lexicalDC) {
1855
Ted Kremenek9d64c152010-03-12 00:38:38 +00001856 ObjCMethodDecl *GetterMethod, *SetterMethod;
1857
1858 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1859 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1860 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1861 property->getLocation());
1862
1863 if (SetterMethod) {
1864 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1865 property->getPropertyAttributes();
1866 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1867 Context.getCanonicalType(SetterMethod->getResultType()) !=
1868 Context.VoidTy)
1869 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1870 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001871 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001872 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1873 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001874 Diag(property->getLocation(),
1875 diag::warn_accessor_property_type_mismatch)
1876 << property->getDeclName()
1877 << SetterMethod->getSelector();
1878 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1879 }
1880 }
1881
1882 // Synthesize getter/setter methods if none exist.
1883 // Find the default getter and if one not found, add one.
1884 // FIXME: The synthesized property we set here is misleading. We almost always
1885 // synthesize these methods unless the user explicitly provided prototypes
1886 // (which is odd, but allowed). Sema should be typechecking that the
1887 // declarations jive in that situation (which it is not currently).
1888 if (!GetterMethod) {
1889 // No instance method of same name as property getter name was found.
1890 // Declare a getter method and add it to the list of methods
1891 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001892 SourceLocation Loc = redeclaredProperty ?
1893 redeclaredProperty->getLocation() :
1894 property->getLocation();
1895
1896 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1897 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001898 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001899 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001900 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001901 (property->getPropertyImplementation() ==
1902 ObjCPropertyDecl::Optional) ?
1903 ObjCMethodDecl::Optional :
1904 ObjCMethodDecl::Required);
1905 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001906
1907 AddPropertyAttrs(*this, GetterMethod, property);
1908
Ted Kremenek23173d72010-05-18 21:09:07 +00001909 // FIXME: Eventually this shouldn't be needed, as the lexical context
1910 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001911 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001912 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001913 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1914 GetterMethod->addAttr(
1915 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001916
1917 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1918 GetterMethod->addAttr(
1919 ::new (Context) ObjCReturnsInnerPointerAttr(Loc, Context));
John McCallb8463812013-04-04 01:38:37 +00001920
1921 if (getLangOpts().ObjCAutoRefCount)
1922 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001923 } else
1924 // A user declared getter will be synthesize when @synthesize of
1925 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001926 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001927 property->setGetterMethodDecl(GetterMethod);
1928
1929 // Skip setter if property is read-only.
1930 if (!property->isReadOnly()) {
1931 // Find the default setter and if one not found, add one.
1932 if (!SetterMethod) {
1933 // No instance method of same name as property setter name was found.
1934 // Declare a setter method and add it to the list of methods
1935 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001936 SourceLocation Loc = redeclaredProperty ?
1937 redeclaredProperty->getLocation() :
1938 property->getLocation();
1939
1940 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001941 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001942 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001943 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001944 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001945 /*isImplicitlyDeclared=*/true,
1946 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001947 (property->getPropertyImplementation() ==
1948 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001949 ObjCMethodDecl::Optional :
1950 ObjCMethodDecl::Required);
1951
Ted Kremenek9d64c152010-03-12 00:38:38 +00001952 // Invent the arguments for the setter. We don't bother making a
1953 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001954 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1955 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001956 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001957 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001958 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001959 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001960 0);
Dmitri Gribenko55431692013-05-05 00:41:58 +00001961 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00001962
1963 AddPropertyAttrs(*this, SetterMethod, property);
1964
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001966 // FIXME: Eventually this shouldn't be needed, as the lexical context
1967 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001968 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001969 SetterMethod->setLexicalDeclContext(lexicalDC);
John McCallb8463812013-04-04 01:38:37 +00001970
1971 // It's possible for the user to have set a very odd custom
1972 // setter selector that causes it to have a method family.
1973 if (getLangOpts().ObjCAutoRefCount)
1974 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001975 } else
1976 // A user declared setter will be synthesize when @synthesize of
1977 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001978 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001979 property->setSetterMethodDecl(SetterMethod);
1980 }
1981 // Add any synthesized methods to the global pool. This allows us to
1982 // handle the following, which is supported by GCC (and part of the design).
1983 //
1984 // @interface Foo
1985 // @property double bar;
1986 // @end
1987 //
1988 // void thisIsUnfortunate() {
1989 // id foo;
1990 // double bar = [foo bar];
1991 // }
1992 //
1993 if (GetterMethod)
1994 AddInstanceMethodToGlobalPool(GetterMethod);
1995 if (SetterMethod)
1996 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001997
1998 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1999 if (!CurrentClass) {
2000 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2001 CurrentClass = Cat->getClassInterface();
2002 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2003 CurrentClass = Impl->getClassInterface();
2004 }
2005 if (GetterMethod)
2006 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2007 if (SetterMethod)
2008 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002009}
2010
John McCalld226f652010-08-21 09:40:31 +00002011void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002012 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002013 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002014 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002015 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002016 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002017 return;
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002018
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002019 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2020 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2021 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2022 << "readonly" << "readwrite";
2023
2024 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2025 QualType PropertyTy = PropertyDecl->getType();
2026 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002027
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002028 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2029 if (getLangOpts().ObjCAutoRefCount &&
2030 PropertyTy->isObjCRetainableType() &&
2031 !PropertyOwnership) {
2032 // 'readonly' property with no obvious lifetime.
2033 // its life time will be determined by its backing ivar.
2034 return;
2035 }
2036 else if (PropertyOwnership) {
2037 if (!getSourceManager().isInSystemHeader(Loc))
2038 Diag(Loc, diag::warn_objc_property_attr_mutually_exclusive)
2039 << "readonly" << NameOfOwnershipAttribute(Attributes);
2040 return;
2041 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002042 }
2043
2044 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002045 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002046 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2047 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002048 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002049 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002050 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2051 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2052 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002053 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002054 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002055 }
2056
2057 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002058 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2059 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002060 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2061 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002062 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002063 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002064 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002067 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002068 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002070 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2071 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002072 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002073 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002074 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002075 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002076 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2077 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002078 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002079 }
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002080 if (PropertyDecl->getAttr<IBOutletCollectionAttr>())
2081 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2083 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002084 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2085 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002086 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002087 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002088 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002089 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2090 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002091 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002092 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002093 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002094 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2095 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002096 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002097 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002098 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002099 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002100 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2101 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002102 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002103 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002104 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2105 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002106 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2107 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002108 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002109 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002110 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002111 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2112 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002113 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002114 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002116 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2117 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002118 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002119 }
2120 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002121 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2122 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002123 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2124 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002125 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002126 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002127 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2128 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002129 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2130 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002131 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002132 }
2133
Bill Wendlingad017fa2012-12-20 19:22:21 +00002134 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2135 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002136 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2137 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002138 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002139 }
2140
Ted Kremenek9d64c152010-03-12 00:38:38 +00002141 // Warn if user supplied no assignment attribute, property is
2142 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002143 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002144 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2145 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2146 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002147 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002148 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002149 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002150 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002151 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002152 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002153 bool isAnyClassTy =
2154 (PropertyTy->isObjCClassType() ||
2155 PropertyTy->isObjCQualifiedClassType());
2156 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2157 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002158 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002159 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002160 else if (propertyInPrimaryClass) {
2161 // Don't issue warning on property with no life time in class
2162 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002163 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002164 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002165 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002166
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002167 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002168 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002169 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002170 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002171 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002172
2173 // FIXME: Implement warning dependent on NSCopying being
2174 // implemented. See also:
2175 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2176 // (please trim this list while you are at it).
2177 }
2178
Bill Wendlingad017fa2012-12-20 19:22:21 +00002179 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2180 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002181 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002182 && PropertyTy->isBlockPointerType())
2183 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002184 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2185 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2186 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002187 PropertyTy->isBlockPointerType())
2188 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002189
Bill Wendlingad017fa2012-12-20 19:22:21 +00002190 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2191 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002192 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2193
Ted Kremenek9d64c152010-03-12 00:38:38 +00002194}