blob: 8658b687c1cc29ad504d55488b05ded8b6fa19fa [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 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000462 DeclContext *DC = cast<DeclContext>(CCPrimary);
463 if (!ObjCPropertyDecl::findPropertyDecl(DC,
464 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000465 // Protocol is not in the primary class. Must build one for it.
466 ObjCDeclSpec ProtocolPropertyODS;
467 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
468 // and ObjCPropertyDecl::PropertyAttributeKind have identical
469 // values. Should consolidate both into one enum type.
470 ProtocolPropertyODS.
471 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
472 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000473 // Must re-establish the context from class extension to primary
474 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000475 ContextRAII SavedContext(*this, CCPrimary);
476
John McCalld226f652010-08-21 09:40:31 +0000477 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000478 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000479 PIDecl->getGetterName(),
480 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000481 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000482 MethodImplKind,
483 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000484 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000485 }
486 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000487 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000488 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000489 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000490 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000491 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
493 PIDecl->setSetterName(SetterSel);
494 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000495 // Tailor the diagnostics for the common case where a readwrite
496 // property is declared both in the @interface and the continuation.
497 // This is a common error where the user often intended the original
498 // declaration to be readonly.
499 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000500 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000501 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
502 ? diag::err_use_continuation_class_redeclaration_readwrite
503 : diag::err_use_continuation_class;
504 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000505 << CCPrimary->getDeclName();
506 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000507 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000508 }
509 *isOverridingProperty = true;
510 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000511 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000512 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
513 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000514 if (ASTMutationListener *L = Context.getASTMutationListener())
515 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000516 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000517}
518
519ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
520 ObjCContainerDecl *CDecl,
521 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000522 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000523 FieldDeclarator &FD,
524 Selector GetterSel,
525 Selector SetterSel,
526 const bool isAssign,
527 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000528 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000529 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000530 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000531 tok::ObjCKeywordKind MethodImplKind,
532 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000533 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000534 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000535
536 // Issue a warning if property is 'assign' as default and its object, which is
537 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000538 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000539 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000540 if (const ObjCObjectPointerType *ObjPtrTy =
541 T->getAs<ObjCObjectPointerType>()) {
542 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
543 if (IDecl)
544 if (ObjCProtocolDecl* PNSCopying =
545 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
546 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
547 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000548 }
Eli Friedman27d46442013-07-09 01:38:07 +0000549
550 if (T->isObjCObjectType()) {
551 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
552 StarLoc = PP.getLocForEndOfToken(StarLoc);
553 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
554 << FixItHint::CreateInsertion(StarLoc, "*");
555 T = Context.getObjCObjectPointerType(T);
556 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
557 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
558 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000559
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000560 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000561 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
562 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000563 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000564
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000565 if (ObjCPropertyDecl *prevDecl =
566 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000567 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000568 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 PDecl->setInvalidDecl();
570 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000571 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000572 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000573 if (lexicalDC)
574 PDecl->setLexicalDeclContext(lexicalDC);
575 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000576
577 if (T->isArrayType() || T->isFunctionType()) {
578 Diag(AtLoc, diag::err_property_type) << T;
579 PDecl->setInvalidDecl();
580 }
581
582 ProcessDeclAttributes(S, PDecl, FD.D);
583
584 // Regardless of setter/getter attribute, we save the default getter/setter
585 // selector names in anticipation of declaration of setter/getter methods.
586 PDecl->setGetterName(GetterSel);
587 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000588 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000589 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000590
Bill Wendlingad017fa2012-12-20 19:22:21 +0000591 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000592 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
593
Bill Wendlingad017fa2012-12-20 19:22:21 +0000594 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
596
Bill Wendlingad017fa2012-12-20 19:22:21 +0000597 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
599
600 if (isReadWrite)
601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
602
Bill Wendlingad017fa2012-12-20 19:22:21 +0000603 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
605
Bill Wendlingad017fa2012-12-20 19:22:21 +0000606 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
608
Bill Wendlingad017fa2012-12-20 19:22:21 +0000609 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
611
Bill Wendlingad017fa2012-12-20 19:22:21 +0000612 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
614
Bill Wendlingad017fa2012-12-20 19:22:21 +0000615 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
617
Ted Kremenek28685ab2010-03-12 00:46:40 +0000618 if (isAssign)
619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
620
John McCall265941b2011-09-13 18:31:23 +0000621 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000622 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000624 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000625 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000626
John McCallf85e1932011-06-15 23:02:42 +0000627 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000628 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
630 if (isAssign)
631 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
632
Ted Kremenek28685ab2010-03-12 00:46:40 +0000633 if (MethodImplKind == tok::objc_required)
634 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
635 else if (MethodImplKind == tok::objc_optional)
636 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000637
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000638 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000639}
640
John McCallf85e1932011-06-15 23:02:42 +0000641static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
642 ObjCPropertyDecl *property,
643 ObjCIvarDecl *ivar) {
644 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
645
John McCallf85e1932011-06-15 23:02:42 +0000646 QualType ivarType = ivar->getType();
647 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000648
John McCall265941b2011-09-13 18:31:23 +0000649 // The lifetime implied by the property's attributes.
650 Qualifiers::ObjCLifetime propertyLifetime =
651 getImpliedARCOwnership(property->getPropertyAttributes(),
652 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000653
John McCall265941b2011-09-13 18:31:23 +0000654 // We're fine if they match.
655 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000656
John McCall265941b2011-09-13 18:31:23 +0000657 // These aren't valid lifetimes for object ivars; don't diagnose twice.
658 if (ivarLifetime == Qualifiers::OCL_None ||
659 ivarLifetime == Qualifiers::OCL_Autoreleasing)
660 return;
John McCallf85e1932011-06-15 23:02:42 +0000661
John McCalld64c2eb2012-08-20 23:36:59 +0000662 // If the ivar is private, and it's implicitly __unsafe_unretained
663 // becaues of its type, then pretend it was actually implicitly
664 // __strong. This is only sound because we're processing the
665 // property implementation before parsing any method bodies.
666 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
667 propertyLifetime == Qualifiers::OCL_Strong &&
668 ivar->getAccessControl() == ObjCIvarDecl::Private) {
669 SplitQualType split = ivarType.split();
670 if (split.Quals.hasObjCLifetime()) {
671 assert(ivarType->isObjCARCImplicitlyUnretainedType());
672 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
673 ivarType = S.Context.getQualifiedType(split);
674 ivar->setType(ivarType);
675 return;
676 }
677 }
678
John McCall265941b2011-09-13 18:31:23 +0000679 switch (propertyLifetime) {
680 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000681 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000682 << property->getDeclName()
683 << ivar->getDeclName()
684 << ivarLifetime;
685 break;
John McCallf85e1932011-06-15 23:02:42 +0000686
John McCall265941b2011-09-13 18:31:23 +0000687 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000688 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000689 << property->getDeclName()
690 << ivar->getDeclName();
691 break;
John McCallf85e1932011-06-15 23:02:42 +0000692
John McCall265941b2011-09-13 18:31:23 +0000693 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000694 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000695 << property->getDeclName()
696 << ivar->getDeclName()
697 << ((property->getPropertyAttributesAsWritten()
698 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
699 break;
John McCallf85e1932011-06-15 23:02:42 +0000700
John McCall265941b2011-09-13 18:31:23 +0000701 case Qualifiers::OCL_Autoreleasing:
702 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000703
John McCall265941b2011-09-13 18:31:23 +0000704 case Qualifiers::OCL_None:
705 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000706 return;
707 }
708
709 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000710 if (propertyImplLoc.isValid())
711 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000712}
713
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000714/// setImpliedPropertyAttributeForReadOnlyProperty -
715/// This routine evaludates life-time attributes for a 'readonly'
716/// property with no known lifetime of its own, using backing
717/// 'ivar's attribute, if any. If no backing 'ivar', property's
718/// life-time is assumed 'strong'.
719static void setImpliedPropertyAttributeForReadOnlyProperty(
720 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
721 Qualifiers::ObjCLifetime propertyLifetime =
722 getImpliedARCOwnership(property->getPropertyAttributes(),
723 property->getType());
724 if (propertyLifetime != Qualifiers::OCL_None)
725 return;
726
727 if (!ivar) {
728 // if no backing ivar, make property 'strong'.
729 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
730 return;
731 }
732 // property assumes owenership of backing ivar.
733 QualType ivarType = ivar->getType();
734 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
735 if (ivarLifetime == Qualifiers::OCL_Strong)
736 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
737 else if (ivarLifetime == Qualifiers::OCL_Weak)
738 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
739 return;
740}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000741
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000742/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
743/// in inherited protocols with mismatched types. Since any of them can
744/// be candidate for synthesis.
Benjamin Kramerb1a4d372013-05-23 15:53:44 +0000745static void
746DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
747 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000748 ObjCPropertyDecl *Property) {
749 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
750 for (ObjCInterfaceDecl::all_protocol_iterator
751 PI = ClassDecl->all_referenced_protocol_begin(),
752 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
753 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
754 PDecl->collectInheritedProtocolProperties(Property, PropMap);
755 }
756 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
757 while (SDecl) {
758 for (ObjCInterfaceDecl::all_protocol_iterator
759 PI = SDecl->all_referenced_protocol_begin(),
760 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
761 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
762 PDecl->collectInheritedProtocolProperties(Property, PropMap);
763 }
764 SDecl = SDecl->getSuperClass();
765 }
766
767 if (PropMap.empty())
768 return;
769
770 QualType RHSType = S.Context.getCanonicalType(Property->getType());
771 bool FirsTime = true;
772 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
773 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
774 ObjCPropertyDecl *Prop = I->second;
775 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
776 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
777 bool IncompatibleObjC = false;
778 QualType ConvertedType;
779 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
780 || IncompatibleObjC) {
781 if (FirsTime) {
782 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
783 << Property->getType();
784 FirsTime = false;
785 }
786 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
787 << Prop->getType();
788 }
789 }
790 }
791 if (!FirsTime && AtLoc.isValid())
792 S.Diag(AtLoc, diag::note_property_synthesize);
793}
794
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795/// ActOnPropertyImplDecl - This routine performs semantic checks and
796/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000797/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000798///
John McCalld226f652010-08-21 09:40:31 +0000799Decl *Sema::ActOnPropertyImplDecl(Scope *S,
800 SourceLocation AtLoc,
801 SourceLocation PropertyLoc,
802 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000803 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000804 IdentifierInfo *PropertyIvar,
805 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000806 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000807 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000808 // Make sure we have a context for the property implementation declaration.
809 if (!ClassImpDecl) {
810 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000811 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000812 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000813 if (PropertyIvarLoc.isInvalid())
814 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000815 SourceLocation PropertyDiagLoc = PropertyLoc;
816 if (PropertyDiagLoc.isInvalid())
817 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000818 ObjCPropertyDecl *property = 0;
819 ObjCInterfaceDecl* IDecl = 0;
820 // Find the class or category class where this property must have
821 // a declaration.
822 ObjCImplementationDecl *IC = 0;
823 ObjCCategoryImplDecl* CatImplClass = 0;
824 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
825 IDecl = IC->getClassInterface();
826 // We always synthesize an interface for an implementation
827 // without an interface decl. So, IDecl is always non-zero.
828 assert(IDecl &&
829 "ActOnPropertyImplDecl - @implementation without @interface");
830
831 // Look for this property declaration in the @implementation's @interface
832 property = IDecl->FindPropertyDeclaration(PropertyId);
833 if (!property) {
834 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000835 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000836 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000837 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000838 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
839 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000840 if (AtLoc.isValid())
841 Diag(AtLoc, diag::warn_implicit_atomic_property);
842 else
843 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
844 Diag(property->getLocation(), diag::note_property_declare);
845 }
846
Ted Kremenek28685ab2010-03-12 00:46:40 +0000847 if (const ObjCCategoryDecl *CD =
848 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
849 if (!CD->IsClassExtension()) {
850 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
851 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000852 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000853 }
854 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000855 if (Synthesize&&
856 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
857 property->hasAttr<IBOutletAttr>() &&
858 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000859 bool ReadWriteProperty = false;
860 // Search into the class extensions and see if 'readonly property is
861 // redeclared 'readwrite', then no warning is to be issued.
862 for (ObjCInterfaceDecl::known_extensions_iterator
863 Ext = IDecl->known_extensions_begin(),
864 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
865 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
866 if (!R.empty())
867 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
868 PIkind = ExtProp->getPropertyAttributesAsWritten();
869 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
870 ReadWriteProperty = true;
871 break;
872 }
873 }
874 }
875
876 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000877 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
878 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000879 SourceLocation readonlyLoc;
880 if (LocPropertyAttribute(Context, "readonly",
881 property->getLParenLoc(), readonlyLoc)) {
882 SourceLocation endLoc =
883 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
884 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
885 Diag(property->getLocation(),
886 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
887 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
888 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000889 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000890 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000891 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
892 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000893
Ted Kremenek28685ab2010-03-12 00:46:40 +0000894 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
895 if (Synthesize) {
896 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000897 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000898 }
899 IDecl = CatImplClass->getClassInterface();
900 if (!IDecl) {
901 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000902 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000903 }
904 ObjCCategoryDecl *Category =
905 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
906
907 // If category for this implementation not found, it is an error which
908 // has already been reported eralier.
909 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000910 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000911 // Look for this property declaration in @implementation's category
912 property = Category->FindPropertyDeclaration(PropertyId);
913 if (!property) {
914 Diag(PropertyLoc, diag::error_bad_category_property_decl)
915 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000916 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000917 }
918 } else {
919 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000920 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000921 }
922 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000923 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000924 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000925 // Check that we have a valid, previously declared ivar for @synthesize
926 if (Synthesize) {
927 // @synthesize
928 if (!PropertyIvar)
929 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000930 // Check that this is a previously declared 'ivar' in 'IDecl' interface
931 ObjCInterfaceDecl *ClassDeclared;
932 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
933 QualType PropType = property->getType();
934 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000935
936 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000937 diag::err_incomplete_synthesized_property,
938 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000939 Diag(property->getLocation(), diag::note_property_declare);
940 CompleteTypeErr = true;
941 }
942
David Blaikie4e4d0842012-03-11 07:00:24 +0000943 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000944 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000945 ObjCPropertyDecl::OBJC_PR_readonly) &&
946 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000947 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
948 }
949
John McCallf85e1932011-06-15 23:02:42 +0000950 ObjCPropertyDecl::PropertyAttributeKind kind
951 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000952
953 // Add GC __weak to the ivar type if the property is weak.
954 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000955 getLangOpts().getGC() != LangOptions::NonGC) {
956 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000957 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000958 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000959 Diag(property->getLocation(), diag::note_property_declare);
960 } else {
961 PropertyIvarType =
962 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000963 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000964 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000965 if (AtLoc.isInvalid()) {
966 // Check when default synthesizing a property that there is
967 // an ivar matching property name and issue warning; since this
968 // is the most common case of not using an ivar used for backing
969 // property in non-default synthesis case.
970 ObjCInterfaceDecl *ClassDeclared=0;
971 ObjCIvarDecl *originalIvar =
972 IDecl->lookupInstanceVariable(property->getIdentifier(),
973 ClassDeclared);
974 if (originalIvar) {
975 Diag(PropertyDiagLoc,
976 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000977 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000978 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000979 Diag(property->getLocation(), diag::note_property_declare);
980 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000981 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000982 }
983
984 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000985 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000986 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000987 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000988 !PropertyIvarType.getObjCLifetime() &&
989 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000990
John McCall265941b2011-09-13 18:31:23 +0000991 // It's an error if we have to do this and the user didn't
992 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000993 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000994 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000995 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000996 diag::err_arc_objc_property_default_assign_on_object);
997 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000998 } else {
999 Qualifiers::ObjCLifetime lifetime =
1000 getImpliedARCOwnership(kind, PropertyIvarType);
1001 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001002 if (lifetime == Qualifiers::OCL_Weak) {
1003 bool err = false;
1004 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001005 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1006 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1007 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001008 Diag(property->getLocation(),
1009 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1010 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1011 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001012 err = true;
1013 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001014 }
John McCall0a7dd782012-08-21 02:47:43 +00001015 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001016 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001017 Diag(property->getLocation(), diag::note_property_declare);
1018 }
John McCallf85e1932011-06-15 23:02:42 +00001019 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001020
John McCallf85e1932011-06-15 23:02:42 +00001021 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001022 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001023 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1024 }
John McCallf85e1932011-06-15 23:02:42 +00001025 }
1026
1027 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001028 !getLangOpts().ObjCAutoRefCount &&
1029 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001030 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001031 Diag(property->getLocation(), diag::note_property_declare);
1032 }
1033
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001034 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001035 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001036 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001037 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001038 (Expr *)0, true);
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001039 if (RequireNonAbstractType(PropertyIvarLoc,
1040 PropertyIvarType,
1041 diag::err_abstract_type_in_decl,
1042 AbstractSynthesizedIvarType)) {
1043 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001044 Ivar->setInvalidDecl();
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001045 } else if (CompleteTypeErr)
1046 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001047 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001048 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001049
John McCall260611a2012-06-20 06:18:46 +00001050 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001051 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1052 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001053 // Note! I deliberately want it to fall thru so, we have a
1054 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001055 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001056 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001057 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001058 << property->getDeclName() << Ivar->getDeclName()
1059 << ClassDeclared->getDeclName();
1060 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001061 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001062 // Note! I deliberately want it to fall thru so more errors are caught.
1063 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001064 property->setPropertyIvarDecl(Ivar);
1065
Ted Kremenek28685ab2010-03-12 00:46:40 +00001066 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1067
1068 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001069 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001070 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001071 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001072 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001073 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001074 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001075 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001076 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001077 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1078 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001079 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001080 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001081 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001082 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001083 << property->getDeclName() << PropType
1084 << Ivar->getDeclName() << IvarType;
1085 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001086 // Note! I deliberately want it to fall thru so, we have a
1087 // a property implementation and to avoid future warnings.
1088 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001089 else {
1090 // FIXME! Rules for properties are somewhat different that those
1091 // for assignments. Use a new routine to consolidate all cases;
1092 // specifically for property redeclarations as well as for ivars.
1093 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1094 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1095 if (lhsType != rhsType &&
1096 lhsType->isArithmeticType()) {
1097 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1098 << property->getDeclName() << PropType
1099 << Ivar->getDeclName() << IvarType;
1100 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1101 // Fall thru - see previous comment
1102 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001103 }
1104 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001105 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001106 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001107 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001108 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001109 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001110 // Fall thru - see previous comment
1111 }
John McCallf85e1932011-06-15 23:02:42 +00001112 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001113 if ((property->getType()->isObjCObjectPointerType() ||
1114 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001115 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001116 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001117 << property->getDeclName() << Ivar->getDeclName();
1118 // Fall thru - see previous comment
1119 }
1120 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001121 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001122 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001123 } else if (PropertyIvar)
1124 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001125 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001126
Ted Kremenek28685ab2010-03-12 00:46:40 +00001127 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1128 ObjCPropertyImplDecl *PIDecl =
1129 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1130 property,
1131 (Synthesize ?
1132 ObjCPropertyImplDecl::Synthesize
1133 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001134 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001135
Fariborz Jahanian74414712012-05-15 18:12:51 +00001136 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001137 PIDecl->setInvalidDecl();
1138
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001139 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1140 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001141 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001142 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001143 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1144 // returned by the getter as it must conform to C++'s copy-return rules.
1145 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001146 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001147 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1148 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001149 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001150 VK_RValue, PropertyDiagLoc);
1151 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001152 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001153 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001154 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001155 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001156 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001157 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001158 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001159 getterMethod->getResultType(),
1160 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001161 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001162 Owned(IvarRefExpr));
1163 if (!Res.isInvalid()) {
1164 Expr *ResExpr = Res.takeAs<Expr>();
1165 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001166 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001167 PIDecl->setGetterCXXConstructor(ResExpr);
1168 }
1169 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001170 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1171 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1172 Diag(getterMethod->getLocation(),
1173 diag::warn_property_getter_owning_mismatch);
1174 Diag(property->getLocation(), diag::note_property_declare);
1175 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001176 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1177 switch (getterMethod->getMethodFamily()) {
1178 case OMF_retain:
1179 case OMF_retainCount:
1180 case OMF_release:
1181 case OMF_autorelease:
1182 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1183 << 1 << getterMethod->getSelector();
1184 break;
1185 default:
1186 break;
1187 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001188 }
1189 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1190 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001191 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1192 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001193 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001194 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001195 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1196 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001197 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001198 VK_RValue, PropertyDiagLoc);
1199 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001200 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001201 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001202 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001203 SelfExpr, true, true);
1204 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1205 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001206 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001207 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1208 VK_LValue, PropertyDiagLoc);
1209 MarkDeclRefReferenced(rhs);
1210 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001211 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001212 if (property->getPropertyAttributes() &
1213 ObjCPropertyDecl::OBJC_PR_atomic) {
1214 Expr *callExpr = Res.takeAs<Expr>();
1215 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001216 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1217 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001218 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001219 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001220 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001221 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001222 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001223 Diag(FuncDecl->getLocStart(),
1224 diag::note_callee_decl) << FuncDecl;
1225 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001226 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001227 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1228 }
1229 }
1230
Ted Kremenek28685ab2010-03-12 00:46:40 +00001231 if (IC) {
1232 if (Synthesize)
1233 if (ObjCPropertyImplDecl *PPIDecl =
1234 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1235 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1236 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1237 << PropertyIvar;
1238 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1239 }
1240
1241 if (ObjCPropertyImplDecl *PPIDecl
1242 = IC->FindPropertyImplDecl(PropertyId)) {
1243 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1244 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001245 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001246 }
1247 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001248 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001249 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001250 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001251 // Diagnose if an ivar was lazily synthesdized due to a previous
1252 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001253 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001254 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001255 ObjCIvarDecl *Ivar = 0;
1256 if (!Synthesize)
1257 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1258 else {
1259 if (PropertyIvar && PropertyIvar != PropertyId)
1260 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1261 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001262 // Issue diagnostics only if Ivar belongs to current class.
1263 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001264 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001265 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1266 << PropertyId;
1267 Ivar->setInvalidDecl();
1268 }
1269 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001270 } else {
1271 if (Synthesize)
1272 if (ObjCPropertyImplDecl *PPIDecl =
1273 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001274 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001275 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1276 << PropertyIvar;
1277 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1278 }
1279
1280 if (ObjCPropertyImplDecl *PPIDecl =
1281 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001282 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001283 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001284 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001285 }
1286 CatImplClass->addPropertyImplementation(PIDecl);
1287 }
1288
John McCalld226f652010-08-21 09:40:31 +00001289 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001290}
1291
1292//===----------------------------------------------------------------------===//
1293// Helper methods.
1294//===----------------------------------------------------------------------===//
1295
Ted Kremenek9d64c152010-03-12 00:38:38 +00001296/// DiagnosePropertyMismatch - Compares two properties for their
1297/// attributes and types and warns on a variety of inconsistencies.
1298///
1299void
1300Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1301 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001302 const IdentifierInfo *inheritedName,
1303 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001304 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001305 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001306 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001307 SuperProperty->getPropertyAttributes();
1308
1309 // We allow readonly properties without an explicit ownership
1310 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1311 // to be overridden by a property with any explicit ownership in the subclass.
1312 if (!OverridingProtocolProperty &&
1313 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1314 ;
1315 else {
1316 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1317 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1318 Diag(Property->getLocation(), diag::warn_readonly_property)
1319 << Property->getDeclName() << inheritedName;
1320 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1321 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001322 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001323 << Property->getDeclName() << "copy" << inheritedName;
1324 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1325 unsigned CAttrRetain =
1326 (CAttr &
1327 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1328 unsigned SAttrRetain =
1329 (SAttr &
1330 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1331 bool CStrong = (CAttrRetain != 0);
1332 bool SStrong = (SAttrRetain != 0);
1333 if (CStrong != SStrong)
1334 Diag(Property->getLocation(), diag::warn_property_attribute)
1335 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1336 }
John McCallf85e1932011-06-15 23:02:42 +00001337 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001338
1339 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001340 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001341 Diag(Property->getLocation(), diag::warn_property_attribute)
1342 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001343 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1344 }
1345 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001346 Diag(Property->getLocation(), diag::warn_property_attribute)
1347 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001348 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1349 }
1350 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001351 Diag(Property->getLocation(), diag::warn_property_attribute)
1352 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001353 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1354 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001355
1356 QualType LHSType =
1357 Context.getCanonicalType(SuperProperty->getType());
1358 QualType RHSType =
1359 Context.getCanonicalType(Property->getType());
1360
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001361 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001362 // Do cases not handled in above.
1363 // FIXME. For future support of covariant property types, revisit this.
1364 bool IncompatibleObjC = false;
1365 QualType ConvertedType;
1366 if (!isObjCPointerConversion(RHSType, LHSType,
1367 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001368 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001369 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1370 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001371 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1372 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 }
1374}
1375
1376bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1377 ObjCMethodDecl *GetterMethod,
1378 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001379 if (!GetterMethod)
1380 return false;
1381 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1382 QualType PropertyIvarType = property->getType().getNonReferenceType();
1383 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1384 if (!compat) {
1385 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1386 isa<ObjCObjectPointerType>(GetterType))
1387 compat =
1388 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001389 GetterType->getAs<ObjCObjectPointerType>(),
1390 PropertyIvarType->getAs<ObjCObjectPointerType>());
1391 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001392 != Compatible) {
1393 Diag(Loc, diag::error_property_accessor_type)
1394 << property->getDeclName() << PropertyIvarType
1395 << GetterMethod->getSelector() << GetterType;
1396 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1397 return true;
1398 } else {
1399 compat = true;
1400 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1401 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1402 if (lhsType != rhsType && lhsType->isArithmeticType())
1403 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001404 }
1405 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001406
1407 if (!compat) {
1408 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1409 << property->getDeclName()
1410 << GetterMethod->getSelector();
1411 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1412 return true;
1413 }
1414
Ted Kremenek9d64c152010-03-12 00:38:38 +00001415 return false;
1416}
1417
Ted Kremenek9d64c152010-03-12 00:38:38 +00001418/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001419/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001420void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001421 ObjCContainerDecl::PropertyMap &PropMap,
1422 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001423 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1424 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1425 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001426 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001427 PropMap[Prop->getIdentifier()] = Prop;
1428 }
1429 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001430 for (ObjCInterfaceDecl::all_protocol_iterator
1431 PI = IDecl->all_referenced_protocol_begin(),
1432 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001433 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 }
1435 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1436 if (!CATDecl->IsClassExtension())
1437 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1438 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001439 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001440 PropMap[Prop->getIdentifier()] = Prop;
1441 }
1442 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001443 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001444 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001445 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001446 }
1447 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1448 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1449 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001450 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001451 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1452 // Exclude property for protocols which conform to class's super-class,
1453 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001454 if (!PropertyFromSuper ||
1455 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001456 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1457 if (!PropEntry)
1458 PropEntry = Prop;
1459 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001460 }
1461 // scan through protocol's protocols.
1462 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1463 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001464 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001465 }
1466}
1467
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001468/// CollectSuperClassPropertyImplementations - This routine collects list of
1469/// properties to be implemented in super class(s) and also coming from their
1470/// conforming protocols.
1471static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001472 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001473 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001474 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001475 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001476 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001477 SDecl = SDecl->getSuperClass();
1478 }
1479 }
1480}
1481
Fariborz Jahanian26202292013-02-14 19:07:19 +00001482/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1483/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1484/// declared in class 'IFace'.
1485bool
1486Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1487 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1488 if (!IV->getSynthesize())
1489 return false;
1490 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1491 Method->isInstanceMethod());
1492 if (!IMD || !IMD->isPropertyAccessor())
1493 return false;
1494
1495 // look up a property declaration whose one of its accessors is implemented
1496 // by this method.
1497 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1498 E = IFace->prop_end(); P != E; ++P) {
1499 ObjCPropertyDecl *property = *P;
1500 if ((property->getGetterName() == IMD->getSelector() ||
1501 property->getSetterName() == IMD->getSelector()) &&
1502 (property->getPropertyIvarDecl() == IV))
1503 return true;
1504 }
1505 return false;
1506}
1507
1508
James Dennett699c9042012-06-15 07:13:21 +00001509/// \brief Default synthesizes all properties which must be synthesized
1510/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001511void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1512 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001513
Anna Zaksb36ea372012-10-18 19:17:53 +00001514 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001515 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1516 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001517 if (PropMap.empty())
1518 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001519 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001520 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1521
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001522 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1523 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001524 // Is there a matching property synthesize/dynamic?
1525 if (Prop->isInvalidDecl() ||
1526 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1527 continue;
1528 // Property may have been synthesized by user.
1529 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1530 continue;
1531 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1532 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1533 continue;
1534 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1535 continue;
1536 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001537 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001538 if (SuperPropMap[Prop->getIdentifier()]) {
1539 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1540 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1541 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001542 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001543 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1544 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001545 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1546 << Prop->getIdentifier()->getName();
1547 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1548 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001549 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001550 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001551 if (ObjCPropertyImplDecl *PID =
1552 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1553 if (PID->getPropertyDecl() != Prop) {
1554 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1555 << Prop->getIdentifier()->getName();
1556 if (!PID->getLocation().isInvalid())
1557 Diag(PID->getLocation(), diag::note_property_synthesize);
1558 }
1559 continue;
1560 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001561 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1562 // We won't auto-synthesize properties declared in protocols.
1563 Diag(IMPDecl->getLocation(),
1564 diag::warn_auto_synthesizing_protocol_property);
1565 Diag(Prop->getLocation(), diag::note_property_declare);
1566 continue;
1567 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001568
1569 // We use invalid SourceLocations for the synthesized ivars since they
1570 // aren't really synthesized at a particular location; they just exist.
1571 // Saying that they are located at the @implementation isn't really going
1572 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001573 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1574 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1575 true,
1576 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001577 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001578 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001579 if (PIDecl) {
1580 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001581 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001582 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001583 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001584}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001585
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001586void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001587 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001588 return;
1589 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1590 if (!IC)
1591 return;
1592 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001593 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001594 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001595}
1596
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001597void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001598 ObjCContainerDecl *CDecl) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001599 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1600 ObjCInterfaceDecl *IDecl;
1601 // Gather properties which need not be implemented in this class
1602 // or category.
1603 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1604 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1605 // For categories, no need to implement properties declared in
1606 // its primary class (and its super classes) if property is
1607 // declared in one of those containers.
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001608 if ((IDecl = C->getClassInterface())) {
1609 ObjCInterfaceDecl::PropertyDeclOrder PO;
1610 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1611 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001612 }
1613 if (IDecl)
1614 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001615
Anna Zaksb36ea372012-10-18 19:17:53 +00001616 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001617 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001618 if (PropMap.empty())
1619 return;
1620
1621 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1622 for (ObjCImplDecl::propimpl_iterator
1623 I = IMPDecl->propimpl_begin(),
1624 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001625 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001626
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001627 SelectorSet InsMap;
1628 // Collect property accessors implemented in current implementation.
1629 for (ObjCImplementationDecl::instmeth_iterator
1630 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1631 InsMap.insert((*I)->getSelector());
1632
1633 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1634 ObjCInterfaceDecl *PrimaryClass = 0;
1635 if (C && !C->IsClassExtension())
1636 if ((PrimaryClass = C->getClassInterface()))
1637 // Report unimplemented properties in the category as well.
1638 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1639 // When reporting on missing setter/getters, do not report when
1640 // setter/getter is implemented in category's primary class
1641 // implementation.
1642 for (ObjCImplementationDecl::instmeth_iterator
1643 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1644 InsMap.insert((*I)->getSelector());
1645 }
1646
Anna Zaksb36ea372012-10-18 19:17:53 +00001647 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001648 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1649 ObjCPropertyDecl *Prop = P->second;
1650 // Is there a matching propery synthesize/dynamic?
1651 if (Prop->isInvalidDecl() ||
1652 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001653 PropImplMap.count(Prop) ||
1654 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001655 continue;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001656 // When reporting on missing property getter implementation in
1657 // categories, do not report when they are declared in primary class,
1658 // class's protocol, or one of it super classes. This is because,
1659 // the class is going to implement them.
1660 if (!InsMap.count(Prop->getGetterName()) &&
1661 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001662 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001663 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001664 isa<ObjCCategoryDecl>(CDecl) ?
1665 diag::warn_setter_getter_impl_required_in_category :
1666 diag::warn_setter_getter_impl_required)
1667 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001668 Diag(Prop->getLocation(),
1669 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001670 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001671 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001672 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001673 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1674
Ted Kremenek9d64c152010-03-12 00:38:38 +00001675 }
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001676 // When reporting on missing property setter implementation in
1677 // categories, do not report when they are declared in primary class,
1678 // class's protocol, or one of it super classes. This is because,
1679 // the class is going to implement them.
1680 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1681 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001682 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001683 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001684 isa<ObjCCategoryDecl>(CDecl) ?
1685 diag::warn_setter_getter_impl_required_in_category :
1686 diag::warn_setter_getter_impl_required)
1687 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001688 Diag(Prop->getLocation(),
1689 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001690 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001691 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001692 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001693 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001694 }
1695 }
1696}
1697
1698void
1699Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1700 ObjCContainerDecl* IDecl) {
1701 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001702 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001703 return;
1704 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1705 E = IDecl->prop_end();
1706 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001707 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001708 ObjCMethodDecl *GetterMethod = 0;
1709 ObjCMethodDecl *SetterMethod = 0;
1710 bool LookedUpGetterSetter = false;
1711
Bill Wendlingad017fa2012-12-20 19:22:21 +00001712 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001713 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001714
John McCall265941b2011-09-13 18:31:23 +00001715 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1716 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001717 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1718 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1719 LookedUpGetterSetter = true;
1720 if (GetterMethod) {
1721 Diag(GetterMethod->getLocation(),
1722 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001723 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001724 Diag(Property->getLocation(), diag::note_property_declare);
1725 }
1726 if (SetterMethod) {
1727 Diag(SetterMethod->getLocation(),
1728 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001729 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001730 Diag(Property->getLocation(), diag::note_property_declare);
1731 }
1732 }
1733
Ted Kremenek9d64c152010-03-12 00:38:38 +00001734 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001735 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1736 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001737 continue;
1738 if (const ObjCPropertyImplDecl *PIDecl
1739 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1740 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1741 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001742 if (!LookedUpGetterSetter) {
1743 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1744 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1745 LookedUpGetterSetter = true;
1746 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001747 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1748 SourceLocation MethodLoc =
1749 (GetterMethod ? GetterMethod->getLocation()
1750 : SetterMethod->getLocation());
1751 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001752 << Property->getIdentifier() << (GetterMethod != 0)
1753 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001754 // fixit stuff.
1755 if (!AttributesAsWritten) {
1756 if (Property->getLParenLoc().isValid()) {
1757 // @property () ... case.
1758 SourceRange PropSourceRange(Property->getAtLoc(),
1759 Property->getLParenLoc());
1760 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1761 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1762 }
1763 else {
1764 //@property id etc.
1765 SourceLocation endLoc =
1766 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1767 endLoc = endLoc.getLocWithOffset(-1);
1768 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1769 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1770 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1771 }
1772 }
1773 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1774 // @property () ... case.
1775 SourceLocation endLoc = Property->getLParenLoc();
1776 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1777 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1778 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1779 }
1780 else
1781 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001782 Diag(Property->getLocation(), diag::note_property_declare);
1783 }
1784 }
1785 }
1786}
1787
John McCallf85e1932011-06-15 23:02:42 +00001788void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001789 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001790 return;
1791
1792 for (ObjCImplementationDecl::propimpl_iterator
1793 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001794 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001795 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1796 continue;
1797
1798 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001799 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1800 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001801 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1802 if (!method)
1803 continue;
1804 ObjCMethodFamily family = method->getMethodFamily();
1805 if (family == OMF_alloc || family == OMF_copy ||
1806 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001807 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001808 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1809 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001810 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001811 Diag(PD->getLocation(), diag::note_property_declare);
1812 }
1813 }
1814 }
1815}
1816
John McCall5de74d12010-11-10 07:01:40 +00001817/// AddPropertyAttrs - Propagates attributes from a property to the
1818/// implicitly-declared getter or setter for that property.
1819static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1820 ObjCPropertyDecl *Property) {
1821 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001822 for (Decl::attr_iterator A = Property->attr_begin(),
1823 AEnd = Property->attr_end();
1824 A != AEnd; ++A) {
1825 if (isa<DeprecatedAttr>(*A) ||
1826 isa<UnavailableAttr>(*A) ||
1827 isa<AvailabilityAttr>(*A))
1828 PropertyMethod->addAttr((*A)->clone(S.Context));
1829 }
John McCall5de74d12010-11-10 07:01:40 +00001830}
1831
Ted Kremenek9d64c152010-03-12 00:38:38 +00001832/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1833/// have the property type and issue diagnostics if they don't.
1834/// Also synthesize a getter/setter method if none exist (and update the
1835/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1836/// methods is the "right" thing to do.
1837void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001838 ObjCContainerDecl *CD,
1839 ObjCPropertyDecl *redeclaredProperty,
1840 ObjCContainerDecl *lexicalDC) {
1841
Ted Kremenek9d64c152010-03-12 00:38:38 +00001842 ObjCMethodDecl *GetterMethod, *SetterMethod;
1843
1844 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1845 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1846 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1847 property->getLocation());
1848
1849 if (SetterMethod) {
1850 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1851 property->getPropertyAttributes();
1852 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1853 Context.getCanonicalType(SetterMethod->getResultType()) !=
1854 Context.VoidTy)
1855 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1856 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001857 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001858 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1859 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001860 Diag(property->getLocation(),
1861 diag::warn_accessor_property_type_mismatch)
1862 << property->getDeclName()
1863 << SetterMethod->getSelector();
1864 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1865 }
1866 }
1867
1868 // Synthesize getter/setter methods if none exist.
1869 // Find the default getter and if one not found, add one.
1870 // FIXME: The synthesized property we set here is misleading. We almost always
1871 // synthesize these methods unless the user explicitly provided prototypes
1872 // (which is odd, but allowed). Sema should be typechecking that the
1873 // declarations jive in that situation (which it is not currently).
1874 if (!GetterMethod) {
1875 // No instance method of same name as property getter name was found.
1876 // Declare a getter method and add it to the list of methods
1877 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001878 SourceLocation Loc = redeclaredProperty ?
1879 redeclaredProperty->getLocation() :
1880 property->getLocation();
1881
1882 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1883 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001884 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001885 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001886 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001887 (property->getPropertyImplementation() ==
1888 ObjCPropertyDecl::Optional) ?
1889 ObjCMethodDecl::Optional :
1890 ObjCMethodDecl::Required);
1891 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001892
1893 AddPropertyAttrs(*this, GetterMethod, property);
1894
Ted Kremenek23173d72010-05-18 21:09:07 +00001895 // FIXME: Eventually this shouldn't be needed, as the lexical context
1896 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001897 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001898 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001899 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1900 GetterMethod->addAttr(
1901 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001902
1903 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1904 GetterMethod->addAttr(
1905 ::new (Context) ObjCReturnsInnerPointerAttr(Loc, Context));
John McCallb8463812013-04-04 01:38:37 +00001906
1907 if (getLangOpts().ObjCAutoRefCount)
1908 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001909 } else
1910 // A user declared getter will be synthesize when @synthesize of
1911 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001912 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001913 property->setGetterMethodDecl(GetterMethod);
1914
1915 // Skip setter if property is read-only.
1916 if (!property->isReadOnly()) {
1917 // Find the default setter and if one not found, add one.
1918 if (!SetterMethod) {
1919 // No instance method of same name as property setter name was found.
1920 // Declare a setter method and add it to the list of methods
1921 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001922 SourceLocation Loc = redeclaredProperty ?
1923 redeclaredProperty->getLocation() :
1924 property->getLocation();
1925
1926 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001927 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001928 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001929 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001930 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001931 /*isImplicitlyDeclared=*/true,
1932 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001933 (property->getPropertyImplementation() ==
1934 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001935 ObjCMethodDecl::Optional :
1936 ObjCMethodDecl::Required);
1937
Ted Kremenek9d64c152010-03-12 00:38:38 +00001938 // Invent the arguments for the setter. We don't bother making a
1939 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001940 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1941 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001942 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001943 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001944 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001945 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001946 0);
Dmitri Gribenko55431692013-05-05 00:41:58 +00001947 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00001948
1949 AddPropertyAttrs(*this, SetterMethod, property);
1950
Ted Kremenek9d64c152010-03-12 00:38:38 +00001951 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001952 // FIXME: Eventually this shouldn't be needed, as the lexical context
1953 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001954 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001955 SetterMethod->setLexicalDeclContext(lexicalDC);
John McCallb8463812013-04-04 01:38:37 +00001956
1957 // It's possible for the user to have set a very odd custom
1958 // setter selector that causes it to have a method family.
1959 if (getLangOpts().ObjCAutoRefCount)
1960 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001961 } else
1962 // A user declared setter will be synthesize when @synthesize of
1963 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001964 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965 property->setSetterMethodDecl(SetterMethod);
1966 }
1967 // Add any synthesized methods to the global pool. This allows us to
1968 // handle the following, which is supported by GCC (and part of the design).
1969 //
1970 // @interface Foo
1971 // @property double bar;
1972 // @end
1973 //
1974 // void thisIsUnfortunate() {
1975 // id foo;
1976 // double bar = [foo bar];
1977 // }
1978 //
1979 if (GetterMethod)
1980 AddInstanceMethodToGlobalPool(GetterMethod);
1981 if (SetterMethod)
1982 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001983
1984 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1985 if (!CurrentClass) {
1986 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1987 CurrentClass = Cat->getClassInterface();
1988 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1989 CurrentClass = Impl->getClassInterface();
1990 }
1991 if (GetterMethod)
1992 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1993 if (SetterMethod)
1994 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995}
1996
John McCalld226f652010-08-21 09:40:31 +00001997void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001998 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001999 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002000 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002001 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002002 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002003 return;
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002004
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002005 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2006 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2007 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2008 << "readonly" << "readwrite";
2009
2010 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2011 QualType PropertyTy = PropertyDecl->getType();
2012 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002013
Fariborz Jahaniandc1031b2013-10-07 17:20:02 +00002014 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
2015 if (getLangOpts().ObjCAutoRefCount &&
2016 PropertyTy->isObjCRetainableType() &&
2017 !PropertyOwnership) {
2018 // 'readonly' property with no obvious lifetime.
2019 // its life time will be determined by its backing ivar.
2020 return;
2021 }
2022 else if (PropertyOwnership) {
2023 if (!getSourceManager().isInSystemHeader(Loc))
2024 Diag(Loc, diag::warn_objc_property_attr_mutually_exclusive)
2025 << "readonly" << NameOfOwnershipAttribute(Attributes);
2026 return;
2027 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002028 }
2029
2030 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002031 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002032 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2033 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002034 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002035 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002036 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2037 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2038 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002039 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002040 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002041 }
2042
2043 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002044 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2045 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002046 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2047 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002048 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002049 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002050 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002051 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2052 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002053 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002054 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002055 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002056 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2057 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002058 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002059 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002060 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002061 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002062 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2063 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002064 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002065 }
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002066 if (PropertyDecl->getAttr<IBOutletCollectionAttr>())
2067 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002068 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2069 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002070 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2071 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002072 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002073 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002074 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002075 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2076 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002077 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002078 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002079 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002080 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2081 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002082 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002083 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002084 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002085 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002086 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2087 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002088 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002089 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002090 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2091 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002092 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2093 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002094 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002095 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002096 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002097 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2098 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002099 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002100 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002101 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002102 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2103 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002104 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002105 }
2106 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002107 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2108 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002109 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2110 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002111 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002112 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002113 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2114 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002115 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2116 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002117 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002118 }
2119
Bill Wendlingad017fa2012-12-20 19:22:21 +00002120 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2121 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002122 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2123 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002124 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002125 }
2126
Ted Kremenek9d64c152010-03-12 00:38:38 +00002127 // Warn if user supplied no assignment attribute, property is
2128 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002129 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002130 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2131 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2132 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002133 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002134 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002135 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002136 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002137 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002138 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002139 bool isAnyClassTy =
2140 (PropertyTy->isObjCClassType() ||
2141 PropertyTy->isObjCQualifiedClassType());
2142 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2143 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002144 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002145 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002146 else if (propertyInPrimaryClass) {
2147 // Don't issue warning on property with no life time in class
2148 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002149 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002150 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002151 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002152
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002153 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002155 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002156 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002157 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002158
2159 // FIXME: Implement warning dependent on NSCopying being
2160 // implemented. See also:
2161 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2162 // (please trim this list while you are at it).
2163 }
2164
Bill Wendlingad017fa2012-12-20 19:22:21 +00002165 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2166 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002167 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002168 && PropertyTy->isBlockPointerType())
2169 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002170 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2171 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2172 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002173 PropertyTy->isBlockPointerType())
2174 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002175
Bill Wendlingad017fa2012-12-20 19:22:21 +00002176 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2177 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002178 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2179
Ted Kremenek9d64c152010-03-12 00:38:38 +00002180}