blob: 5323ad47ca97e70fed69a445a7bfee447029d739 [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
Douglas Gregoraabd0942013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregord3297242013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000346 for (ObjCInterfaceDecl::known_extensions_iterator
347 Ext = CCPrimary->known_extensions_begin(),
348 ExtEnd = CCPrimary->known_extensions_end();
349 Ext != ExtEnd; ++Ext) {
350 if (ObjCPropertyDecl *prevDecl
351 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000352 Diag(AtLoc, diag::err_duplicate_property);
353 Diag(prevDecl->getLocation(), diag::note_property_declare);
354 return 0;
355 }
356 }
Douglas Gregord3297242013-01-16 23:00:23 +0000357 }
358
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 // Create a new ObjCPropertyDecl with the DeclContext being
360 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000361 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000362 ObjCPropertyDecl *PDecl =
363 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000364 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000365 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000366 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000367 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanianb7b25652013-02-10 00:16:04 +0000371 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
373 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000375 // Set setter/getter selector name. Needed later.
376 PDecl->setGetterName(GetterSel);
377 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000378 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000379 DC->addDecl(PDecl);
380
381 // We need to look in the @interface to see if the @property was
382 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000383 if (!CCPrimary) {
384 Diag(CDecl->getLocation(), diag::err_continuation_class);
385 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000386 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000387 }
388
389 // Find the property in continuation class's primary class only.
390 ObjCPropertyDecl *PIDecl =
391 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
392
393 if (!PIDecl) {
394 // No matching property found in the primary class. Just fall thru
395 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000396 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000397 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000398 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000399 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000400
401 // A case of continuation class adding a new property in the class. This
402 // is not what it was meant for. However, gcc supports it and so should we.
403 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000404 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000405 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000406 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
407 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000408 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000409 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
410 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000411 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000412 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
413 bool IncompatibleObjC = false;
414 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000415 // Relax the strict type matching for property type in continuation class.
416 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000417 // as it narrows the object type in its primary class property. Note that
418 // this conversion is safe only because the wider type is for a 'readonly'
419 // property in primary class and 'narrowed' type for a 'readwrite' property
420 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000421 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
422 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
423 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
424 ConvertedType, IncompatibleObjC))
425 || IncompatibleObjC) {
426 Diag(AtLoc,
427 diag::err_type_mismatch_continuation_class) << PDecl->getType();
428 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000429 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000430 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000431 }
432
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000433 // The property 'PIDecl's readonly attribute will be over-ridden
434 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000435 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000436 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000437 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000438 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000439 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000440 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
441 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000442 Diag(AtLoc, diag::warn_property_attr_mismatch);
443 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000444 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000445 DeclContext *DC = cast<DeclContext>(CCPrimary);
446 if (!ObjCPropertyDecl::findPropertyDecl(DC,
447 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000448 // Protocol is not in the primary class. Must build one for it.
449 ObjCDeclSpec ProtocolPropertyODS;
450 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
451 // and ObjCPropertyDecl::PropertyAttributeKind have identical
452 // values. Should consolidate both into one enum type.
453 ProtocolPropertyODS.
454 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
455 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000456 // Must re-establish the context from class extension to primary
457 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000458 ContextRAII SavedContext(*this, CCPrimary);
459
John McCalld226f652010-08-21 09:40:31 +0000460 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000461 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000462 PIDecl->getGetterName(),
463 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000464 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000465 MethodImplKind,
466 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000467 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000468 }
469 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000470 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000471 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000472 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000473 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000474 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000475 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
476 PIDecl->setSetterName(SetterSel);
477 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000478 // Tailor the diagnostics for the common case where a readwrite
479 // property is declared both in the @interface and the continuation.
480 // This is a common error where the user often intended the original
481 // declaration to be readonly.
482 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000483 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000484 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
485 ? diag::err_use_continuation_class_redeclaration_readwrite
486 : diag::err_use_continuation_class;
487 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000488 << CCPrimary->getDeclName();
489 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000490 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000491 }
492 *isOverridingProperty = true;
493 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000494 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000495 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
496 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000497 if (ASTMutationListener *L = Context.getASTMutationListener())
498 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000499 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000500}
501
502ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
503 ObjCContainerDecl *CDecl,
504 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000505 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000506 FieldDeclarator &FD,
507 Selector GetterSel,
508 Selector SetterSel,
509 const bool isAssign,
510 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000511 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000512 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000513 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000514 tok::ObjCKeywordKind MethodImplKind,
515 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000516 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000517 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518
519 // Issue a warning if property is 'assign' as default and its object, which is
520 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000521 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000522 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000523 if (const ObjCObjectPointerType *ObjPtrTy =
524 T->getAs<ObjCObjectPointerType>()) {
525 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
526 if (IDecl)
527 if (ObjCProtocolDecl* PNSCopying =
528 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
529 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
530 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000531 }
Eli Friedman27d46442013-07-09 01:38:07 +0000532
533 if (T->isObjCObjectType()) {
534 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
535 StarLoc = PP.getLocForEndOfToken(StarLoc);
536 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
537 << FixItHint::CreateInsertion(StarLoc, "*");
538 T = Context.getObjCObjectPointerType(T);
539 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
540 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
541 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000542
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000543 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000544 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
545 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000546 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000547
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000548 if (ObjCPropertyDecl *prevDecl =
549 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000550 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000551 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000552 PDecl->setInvalidDecl();
553 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000554 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000555 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000556 if (lexicalDC)
557 PDecl->setLexicalDeclContext(lexicalDC);
558 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000559
560 if (T->isArrayType() || T->isFunctionType()) {
561 Diag(AtLoc, diag::err_property_type) << T;
562 PDecl->setInvalidDecl();
563 }
564
565 ProcessDeclAttributes(S, PDecl, FD.D);
566
567 // Regardless of setter/getter attribute, we save the default getter/setter
568 // selector names in anticipation of declaration of setter/getter methods.
569 PDecl->setGetterName(GetterSel);
570 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000571 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000572 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000573
Bill Wendlingad017fa2012-12-20 19:22:21 +0000574 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
576
Bill Wendlingad017fa2012-12-20 19:22:21 +0000577 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
579
Bill Wendlingad017fa2012-12-20 19:22:21 +0000580 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
582
583 if (isReadWrite)
584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
585
Bill Wendlingad017fa2012-12-20 19:22:21 +0000586 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
588
Bill Wendlingad017fa2012-12-20 19:22:21 +0000589 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
591
Bill Wendlingad017fa2012-12-20 19:22:21 +0000592 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000593 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
594
Bill Wendlingad017fa2012-12-20 19:22:21 +0000595 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
597
Bill Wendlingad017fa2012-12-20 19:22:21 +0000598 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000599 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
600
Ted Kremenek28685ab2010-03-12 00:46:40 +0000601 if (isAssign)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
603
John McCall265941b2011-09-13 18:31:23 +0000604 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000605 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000607 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000609
John McCallf85e1932011-06-15 23:02:42 +0000610 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000611 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000612 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
613 if (isAssign)
614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
615
Ted Kremenek28685ab2010-03-12 00:46:40 +0000616 if (MethodImplKind == tok::objc_required)
617 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
618 else if (MethodImplKind == tok::objc_optional)
619 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000620
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000621 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000622}
623
John McCallf85e1932011-06-15 23:02:42 +0000624static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
625 ObjCPropertyDecl *property,
626 ObjCIvarDecl *ivar) {
627 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
628
John McCallf85e1932011-06-15 23:02:42 +0000629 QualType ivarType = ivar->getType();
630 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000631
John McCall265941b2011-09-13 18:31:23 +0000632 // The lifetime implied by the property's attributes.
633 Qualifiers::ObjCLifetime propertyLifetime =
634 getImpliedARCOwnership(property->getPropertyAttributes(),
635 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000636
John McCall265941b2011-09-13 18:31:23 +0000637 // We're fine if they match.
638 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000639
John McCall265941b2011-09-13 18:31:23 +0000640 // These aren't valid lifetimes for object ivars; don't diagnose twice.
641 if (ivarLifetime == Qualifiers::OCL_None ||
642 ivarLifetime == Qualifiers::OCL_Autoreleasing)
643 return;
John McCallf85e1932011-06-15 23:02:42 +0000644
John McCalld64c2eb2012-08-20 23:36:59 +0000645 // If the ivar is private, and it's implicitly __unsafe_unretained
646 // becaues of its type, then pretend it was actually implicitly
647 // __strong. This is only sound because we're processing the
648 // property implementation before parsing any method bodies.
649 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
650 propertyLifetime == Qualifiers::OCL_Strong &&
651 ivar->getAccessControl() == ObjCIvarDecl::Private) {
652 SplitQualType split = ivarType.split();
653 if (split.Quals.hasObjCLifetime()) {
654 assert(ivarType->isObjCARCImplicitlyUnretainedType());
655 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
656 ivarType = S.Context.getQualifiedType(split);
657 ivar->setType(ivarType);
658 return;
659 }
660 }
661
John McCall265941b2011-09-13 18:31:23 +0000662 switch (propertyLifetime) {
663 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000664 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000665 << property->getDeclName()
666 << ivar->getDeclName()
667 << ivarLifetime;
668 break;
John McCallf85e1932011-06-15 23:02:42 +0000669
John McCall265941b2011-09-13 18:31:23 +0000670 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000671 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000672 << property->getDeclName()
673 << ivar->getDeclName();
674 break;
John McCallf85e1932011-06-15 23:02:42 +0000675
John McCall265941b2011-09-13 18:31:23 +0000676 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000677 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000678 << property->getDeclName()
679 << ivar->getDeclName()
680 << ((property->getPropertyAttributesAsWritten()
681 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
682 break;
John McCallf85e1932011-06-15 23:02:42 +0000683
John McCall265941b2011-09-13 18:31:23 +0000684 case Qualifiers::OCL_Autoreleasing:
685 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000686
John McCall265941b2011-09-13 18:31:23 +0000687 case Qualifiers::OCL_None:
688 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000689 return;
690 }
691
692 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000693 if (propertyImplLoc.isValid())
694 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000695}
696
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000697/// setImpliedPropertyAttributeForReadOnlyProperty -
698/// This routine evaludates life-time attributes for a 'readonly'
699/// property with no known lifetime of its own, using backing
700/// 'ivar's attribute, if any. If no backing 'ivar', property's
701/// life-time is assumed 'strong'.
702static void setImpliedPropertyAttributeForReadOnlyProperty(
703 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
704 Qualifiers::ObjCLifetime propertyLifetime =
705 getImpliedARCOwnership(property->getPropertyAttributes(),
706 property->getType());
707 if (propertyLifetime != Qualifiers::OCL_None)
708 return;
709
710 if (!ivar) {
711 // if no backing ivar, make property 'strong'.
712 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
713 return;
714 }
715 // property assumes owenership of backing ivar.
716 QualType ivarType = ivar->getType();
717 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
718 if (ivarLifetime == Qualifiers::OCL_Strong)
719 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
720 else if (ivarLifetime == Qualifiers::OCL_Weak)
721 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
722 return;
723}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000724
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000725/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
726/// in inherited protocols with mismatched types. Since any of them can
727/// be candidate for synthesis.
Benjamin Kramerb1a4d372013-05-23 15:53:44 +0000728static void
729DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
730 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000731 ObjCPropertyDecl *Property) {
732 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
733 for (ObjCInterfaceDecl::all_protocol_iterator
734 PI = ClassDecl->all_referenced_protocol_begin(),
735 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
736 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
737 PDecl->collectInheritedProtocolProperties(Property, PropMap);
738 }
739 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
740 while (SDecl) {
741 for (ObjCInterfaceDecl::all_protocol_iterator
742 PI = SDecl->all_referenced_protocol_begin(),
743 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
744 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
745 PDecl->collectInheritedProtocolProperties(Property, PropMap);
746 }
747 SDecl = SDecl->getSuperClass();
748 }
749
750 if (PropMap.empty())
751 return;
752
753 QualType RHSType = S.Context.getCanonicalType(Property->getType());
754 bool FirsTime = true;
755 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
756 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
757 ObjCPropertyDecl *Prop = I->second;
758 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
759 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
760 bool IncompatibleObjC = false;
761 QualType ConvertedType;
762 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
763 || IncompatibleObjC) {
764 if (FirsTime) {
765 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
766 << Property->getType();
767 FirsTime = false;
768 }
769 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
770 << Prop->getType();
771 }
772 }
773 }
774 if (!FirsTime && AtLoc.isValid())
775 S.Diag(AtLoc, diag::note_property_synthesize);
776}
777
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000778/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
779/// attribute declared in primary class and attributes overridden in any of its
780/// class extensions.
781static void
782DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
783 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000784 unsigned Attributes = property->getPropertyAttributesAsWritten();
785 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000786 for (ObjCInterfaceDecl::known_extensions_iterator
787 Ext = ClassDecl->known_extensions_begin(),
788 ExtEnd = ClassDecl->known_extensions_end();
789 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000790 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000791 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
792 for (unsigned I = 0, N = R.size(); I != N; ++I) {
793 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
794 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000795 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000796 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000797
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000798 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000799 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000800 unsigned classExtPropertyAttr =
801 ClassExtProperty->getPropertyAttributesAsWritten();
802 // We are issuing the warning that we postponed because class extensions
803 // can override readonly->readwrite and 'setter' attributes originally
804 // placed on class's property declaration now make sense in the overridden
805 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000806 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000807 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000808 (classExtPropertyAttr &
809 (ObjCDeclSpec::DQ_PR_readwrite|
810 ObjCDeclSpec::DQ_PR_assign |
811 ObjCDeclSpec::DQ_PR_unsafe_unretained |
812 ObjCDeclSpec::DQ_PR_copy |
813 ObjCDeclSpec::DQ_PR_retain |
814 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000815 continue;
816 warn = true;
817 break;
818 }
819 }
820 }
821 if (warn) {
822 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
823 ObjCDeclSpec::DQ_PR_unsafe_unretained |
824 ObjCDeclSpec::DQ_PR_copy |
825 ObjCDeclSpec::DQ_PR_retain |
826 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000827 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000828 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000829 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000830 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000831 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000832 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000833 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000834 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000835 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000836 "retain" : "strong";
837
838 S.Diag(property->getLocation(),
839 diag::warn_objc_property_attr_mutually_exclusive)
840 << "readonly" << which;
841 }
842 }
843
844
845}
846
Ted Kremenek28685ab2010-03-12 00:46:40 +0000847/// ActOnPropertyImplDecl - This routine performs semantic checks and
848/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000849/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000850///
John McCalld226f652010-08-21 09:40:31 +0000851Decl *Sema::ActOnPropertyImplDecl(Scope *S,
852 SourceLocation AtLoc,
853 SourceLocation PropertyLoc,
854 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000855 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000856 IdentifierInfo *PropertyIvar,
857 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000858 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000859 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000860 // Make sure we have a context for the property implementation declaration.
861 if (!ClassImpDecl) {
862 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000863 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000864 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000865 if (PropertyIvarLoc.isInvalid())
866 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000867 SourceLocation PropertyDiagLoc = PropertyLoc;
868 if (PropertyDiagLoc.isInvalid())
869 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000870 ObjCPropertyDecl *property = 0;
871 ObjCInterfaceDecl* IDecl = 0;
872 // Find the class or category class where this property must have
873 // a declaration.
874 ObjCImplementationDecl *IC = 0;
875 ObjCCategoryImplDecl* CatImplClass = 0;
876 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
877 IDecl = IC->getClassInterface();
878 // We always synthesize an interface for an implementation
879 // without an interface decl. So, IDecl is always non-zero.
880 assert(IDecl &&
881 "ActOnPropertyImplDecl - @implementation without @interface");
882
883 // Look for this property declaration in the @implementation's @interface
884 property = IDecl->FindPropertyDeclaration(PropertyId);
885 if (!property) {
886 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000887 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000888 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000889 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000890 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
891 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000892 if (AtLoc.isValid())
893 Diag(AtLoc, diag::warn_implicit_atomic_property);
894 else
895 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
896 Diag(property->getLocation(), diag::note_property_declare);
897 }
898
Ted Kremenek28685ab2010-03-12 00:46:40 +0000899 if (const ObjCCategoryDecl *CD =
900 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
901 if (!CD->IsClassExtension()) {
902 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
903 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000904 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000905 }
906 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000907 if (Synthesize&&
908 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
909 property->hasAttr<IBOutletAttr>() &&
910 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000911 bool ReadWriteProperty = false;
912 // Search into the class extensions and see if 'readonly property is
913 // redeclared 'readwrite', then no warning is to be issued.
914 for (ObjCInterfaceDecl::known_extensions_iterator
915 Ext = IDecl->known_extensions_begin(),
916 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
917 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
918 if (!R.empty())
919 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
920 PIkind = ExtProp->getPropertyAttributesAsWritten();
921 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
922 ReadWriteProperty = true;
923 break;
924 }
925 }
926 }
927
928 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000929 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
930 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000931 SourceLocation readonlyLoc;
932 if (LocPropertyAttribute(Context, "readonly",
933 property->getLParenLoc(), readonlyLoc)) {
934 SourceLocation endLoc =
935 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
936 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
937 Diag(property->getLocation(),
938 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
939 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
940 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000941 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000942 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000943 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
944 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000945
946 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000947
Ted Kremenek28685ab2010-03-12 00:46:40 +0000948 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
949 if (Synthesize) {
950 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000951 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000952 }
953 IDecl = CatImplClass->getClassInterface();
954 if (!IDecl) {
955 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000956 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000957 }
958 ObjCCategoryDecl *Category =
959 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
960
961 // If category for this implementation not found, it is an error which
962 // has already been reported eralier.
963 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000964 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000965 // Look for this property declaration in @implementation's category
966 property = Category->FindPropertyDeclaration(PropertyId);
967 if (!property) {
968 Diag(PropertyLoc, diag::error_bad_category_property_decl)
969 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000970 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000971 }
972 } else {
973 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000974 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000975 }
976 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000977 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000978 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000979 // Check that we have a valid, previously declared ivar for @synthesize
980 if (Synthesize) {
981 // @synthesize
982 if (!PropertyIvar)
983 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000984 // Check that this is a previously declared 'ivar' in 'IDecl' interface
985 ObjCInterfaceDecl *ClassDeclared;
986 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
987 QualType PropType = property->getType();
988 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000989
990 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000991 diag::err_incomplete_synthesized_property,
992 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000993 Diag(property->getLocation(), diag::note_property_declare);
994 CompleteTypeErr = true;
995 }
996
David Blaikie4e4d0842012-03-11 07:00:24 +0000997 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000998 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000999 ObjCPropertyDecl::OBJC_PR_readonly) &&
1000 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001001 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
1002 }
1003
John McCallf85e1932011-06-15 23:02:42 +00001004 ObjCPropertyDecl::PropertyAttributeKind kind
1005 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001006
1007 // Add GC __weak to the ivar type if the property is weak.
1008 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001009 getLangOpts().getGC() != LangOptions::NonGC) {
1010 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +00001011 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001012 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +00001013 Diag(property->getLocation(), diag::note_property_declare);
1014 } else {
1015 PropertyIvarType =
1016 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001017 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001018 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001019 if (AtLoc.isInvalid()) {
1020 // Check when default synthesizing a property that there is
1021 // an ivar matching property name and issue warning; since this
1022 // is the most common case of not using an ivar used for backing
1023 // property in non-default synthesis case.
1024 ObjCInterfaceDecl *ClassDeclared=0;
1025 ObjCIvarDecl *originalIvar =
1026 IDecl->lookupInstanceVariable(property->getIdentifier(),
1027 ClassDeclared);
1028 if (originalIvar) {
1029 Diag(PropertyDiagLoc,
1030 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +00001031 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +00001032 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001033 Diag(property->getLocation(), diag::note_property_declare);
1034 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +00001035 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001036 }
1037
1038 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +00001039 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +00001040 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +00001041 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +00001042 !PropertyIvarType.getObjCLifetime() &&
1043 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +00001044
John McCall265941b2011-09-13 18:31:23 +00001045 // It's an error if we have to do this and the user didn't
1046 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001047 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +00001048 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001049 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001050 diag::err_arc_objc_property_default_assign_on_object);
1051 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +00001052 } else {
1053 Qualifiers::ObjCLifetime lifetime =
1054 getImpliedARCOwnership(kind, PropertyIvarType);
1055 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001056 if (lifetime == Qualifiers::OCL_Weak) {
1057 bool err = false;
1058 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001059 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1060 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1061 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001062 Diag(property->getLocation(),
1063 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1064 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1065 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001066 err = true;
1067 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001068 }
John McCall0a7dd782012-08-21 02:47:43 +00001069 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001070 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001071 Diag(property->getLocation(), diag::note_property_declare);
1072 }
John McCallf85e1932011-06-15 23:02:42 +00001073 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001074
John McCallf85e1932011-06-15 23:02:42 +00001075 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001076 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001077 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1078 }
John McCallf85e1932011-06-15 23:02:42 +00001079 }
1080
1081 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001082 !getLangOpts().ObjCAutoRefCount &&
1083 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001084 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001085 Diag(property->getLocation(), diag::note_property_declare);
1086 }
1087
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001088 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001089 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001090 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001091 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001092 (Expr *)0, true);
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001093 if (RequireNonAbstractType(PropertyIvarLoc,
1094 PropertyIvarType,
1095 diag::err_abstract_type_in_decl,
1096 AbstractSynthesizedIvarType)) {
1097 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001098 Ivar->setInvalidDecl();
Fariborz Jahanian8540b6e2013-07-05 17:18:11 +00001099 } else if (CompleteTypeErr)
1100 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001101 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001102 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001103
John McCall260611a2012-06-20 06:18:46 +00001104 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001105 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1106 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001107 // Note! I deliberately want it to fall thru so, we have a
1108 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001109 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001110 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001111 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001112 << property->getDeclName() << Ivar->getDeclName()
1113 << ClassDeclared->getDeclName();
1114 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001115 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001116 // Note! I deliberately want it to fall thru so more errors are caught.
1117 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001118 property->setPropertyIvarDecl(Ivar);
1119
Ted Kremenek28685ab2010-03-12 00:46:40 +00001120 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1121
1122 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001123 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001124 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001125 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001126 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001127 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001128 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001129 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001130 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001131 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1132 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001133 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001134 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001135 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001136 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001137 << property->getDeclName() << PropType
1138 << Ivar->getDeclName() << IvarType;
1139 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001140 // Note! I deliberately want it to fall thru so, we have a
1141 // a property implementation and to avoid future warnings.
1142 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001143 else {
1144 // FIXME! Rules for properties are somewhat different that those
1145 // for assignments. Use a new routine to consolidate all cases;
1146 // specifically for property redeclarations as well as for ivars.
1147 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1148 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1149 if (lhsType != rhsType &&
1150 lhsType->isArithmeticType()) {
1151 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1152 << property->getDeclName() << PropType
1153 << Ivar->getDeclName() << IvarType;
1154 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1155 // Fall thru - see previous comment
1156 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001157 }
1158 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001159 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001160 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001161 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001162 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001163 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001164 // Fall thru - see previous comment
1165 }
John McCallf85e1932011-06-15 23:02:42 +00001166 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001167 if ((property->getType()->isObjCObjectPointerType() ||
1168 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001169 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001170 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001171 << property->getDeclName() << Ivar->getDeclName();
1172 // Fall thru - see previous comment
1173 }
1174 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001175 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001176 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001177 } else if (PropertyIvar)
1178 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001179 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001180
Ted Kremenek28685ab2010-03-12 00:46:40 +00001181 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1182 ObjCPropertyImplDecl *PIDecl =
1183 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1184 property,
1185 (Synthesize ?
1186 ObjCPropertyImplDecl::Synthesize
1187 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001188 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001189
Fariborz Jahanian74414712012-05-15 18:12:51 +00001190 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001191 PIDecl->setInvalidDecl();
1192
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001193 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1194 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001195 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001196 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001197 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1198 // returned by the getter as it must conform to C++'s copy-return rules.
1199 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001200 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001201 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1202 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001203 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001204 VK_RValue, PropertyDiagLoc);
1205 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001206 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001207 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001208 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001209 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001210 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001211 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001212 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001213 getterMethod->getResultType(),
1214 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001215 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001216 Owned(IvarRefExpr));
1217 if (!Res.isInvalid()) {
1218 Expr *ResExpr = Res.takeAs<Expr>();
1219 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001220 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001221 PIDecl->setGetterCXXConstructor(ResExpr);
1222 }
1223 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001224 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1225 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1226 Diag(getterMethod->getLocation(),
1227 diag::warn_property_getter_owning_mismatch);
1228 Diag(property->getLocation(), diag::note_property_declare);
1229 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001230 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1231 switch (getterMethod->getMethodFamily()) {
1232 case OMF_retain:
1233 case OMF_retainCount:
1234 case OMF_release:
1235 case OMF_autorelease:
1236 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1237 << 1 << getterMethod->getSelector();
1238 break;
1239 default:
1240 break;
1241 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001242 }
1243 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1244 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001245 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1246 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001247 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001248 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001249 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1250 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001251 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001252 VK_RValue, PropertyDiagLoc);
1253 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001254 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001255 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001256 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001257 SelfExpr, true, true);
1258 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1259 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001260 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001261 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1262 VK_LValue, PropertyDiagLoc);
1263 MarkDeclRefReferenced(rhs);
1264 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001265 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001266 if (property->getPropertyAttributes() &
1267 ObjCPropertyDecl::OBJC_PR_atomic) {
1268 Expr *callExpr = Res.takeAs<Expr>();
1269 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001270 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1271 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001272 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001273 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001274 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001275 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001276 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001277 Diag(FuncDecl->getLocStart(),
1278 diag::note_callee_decl) << FuncDecl;
1279 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001280 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001281 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1282 }
1283 }
1284
Ted Kremenek28685ab2010-03-12 00:46:40 +00001285 if (IC) {
1286 if (Synthesize)
1287 if (ObjCPropertyImplDecl *PPIDecl =
1288 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1289 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1290 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1291 << PropertyIvar;
1292 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1293 }
1294
1295 if (ObjCPropertyImplDecl *PPIDecl
1296 = IC->FindPropertyImplDecl(PropertyId)) {
1297 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1298 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001299 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001300 }
1301 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001302 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001303 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001304 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001305 // Diagnose if an ivar was lazily synthesdized due to a previous
1306 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001307 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001308 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001309 ObjCIvarDecl *Ivar = 0;
1310 if (!Synthesize)
1311 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1312 else {
1313 if (PropertyIvar && PropertyIvar != PropertyId)
1314 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1315 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001316 // Issue diagnostics only if Ivar belongs to current class.
1317 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001318 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001319 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1320 << PropertyId;
1321 Ivar->setInvalidDecl();
1322 }
1323 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001324 } else {
1325 if (Synthesize)
1326 if (ObjCPropertyImplDecl *PPIDecl =
1327 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001328 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001329 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1330 << PropertyIvar;
1331 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1332 }
1333
1334 if (ObjCPropertyImplDecl *PPIDecl =
1335 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001336 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001337 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001338 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001339 }
1340 CatImplClass->addPropertyImplementation(PIDecl);
1341 }
1342
John McCalld226f652010-08-21 09:40:31 +00001343 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001344}
1345
1346//===----------------------------------------------------------------------===//
1347// Helper methods.
1348//===----------------------------------------------------------------------===//
1349
Ted Kremenek9d64c152010-03-12 00:38:38 +00001350/// DiagnosePropertyMismatch - Compares two properties for their
1351/// attributes and types and warns on a variety of inconsistencies.
1352///
1353void
1354Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1355 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001356 const IdentifierInfo *inheritedName,
1357 bool OverridingProtocolProperty) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001358 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001359 Property->getPropertyAttributes();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001360 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001361 SuperProperty->getPropertyAttributes();
1362
1363 // We allow readonly properties without an explicit ownership
1364 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1365 // to be overridden by a property with any explicit ownership in the subclass.
1366 if (!OverridingProtocolProperty &&
1367 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1368 ;
1369 else {
1370 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1371 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1372 Diag(Property->getLocation(), diag::warn_readonly_property)
1373 << Property->getDeclName() << inheritedName;
1374 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1375 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCallf85e1932011-06-15 23:02:42 +00001376 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanian1cd6fab2013-10-04 18:06:08 +00001377 << Property->getDeclName() << "copy" << inheritedName;
1378 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1379 unsigned CAttrRetain =
1380 (CAttr &
1381 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1382 unsigned SAttrRetain =
1383 (SAttr &
1384 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1385 bool CStrong = (CAttrRetain != 0);
1386 bool SStrong = (SAttrRetain != 0);
1387 if (CStrong != SStrong)
1388 Diag(Property->getLocation(), diag::warn_property_attribute)
1389 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1390 }
John McCallf85e1932011-06-15 23:02:42 +00001391 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001392
1393 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001394 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001395 Diag(Property->getLocation(), diag::warn_property_attribute)
1396 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001397 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1398 }
1399 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001400 Diag(Property->getLocation(), diag::warn_property_attribute)
1401 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001402 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1403 }
1404 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001405 Diag(Property->getLocation(), diag::warn_property_attribute)
1406 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001407 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1408 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001409
1410 QualType LHSType =
1411 Context.getCanonicalType(SuperProperty->getType());
1412 QualType RHSType =
1413 Context.getCanonicalType(Property->getType());
1414
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001415 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001416 // Do cases not handled in above.
1417 // FIXME. For future support of covariant property types, revisit this.
1418 bool IncompatibleObjC = false;
1419 QualType ConvertedType;
1420 if (!isObjCPointerConversion(RHSType, LHSType,
1421 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001422 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001423 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1424 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001425 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1426 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001427 }
1428}
1429
1430bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1431 ObjCMethodDecl *GetterMethod,
1432 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001433 if (!GetterMethod)
1434 return false;
1435 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1436 QualType PropertyIvarType = property->getType().getNonReferenceType();
1437 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1438 if (!compat) {
1439 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1440 isa<ObjCObjectPointerType>(GetterType))
1441 compat =
1442 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001443 GetterType->getAs<ObjCObjectPointerType>(),
1444 PropertyIvarType->getAs<ObjCObjectPointerType>());
1445 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001446 != Compatible) {
1447 Diag(Loc, diag::error_property_accessor_type)
1448 << property->getDeclName() << PropertyIvarType
1449 << GetterMethod->getSelector() << GetterType;
1450 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1451 return true;
1452 } else {
1453 compat = true;
1454 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1455 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1456 if (lhsType != rhsType && lhsType->isArithmeticType())
1457 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 }
1459 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001460
1461 if (!compat) {
1462 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1463 << property->getDeclName()
1464 << GetterMethod->getSelector();
1465 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1466 return true;
1467 }
1468
Ted Kremenek9d64c152010-03-12 00:38:38 +00001469 return false;
1470}
1471
Ted Kremenek9d64c152010-03-12 00:38:38 +00001472/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001473/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001474void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001475 ObjCContainerDecl::PropertyMap &PropMap,
1476 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001477 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1478 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1479 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001480 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001481 PropMap[Prop->getIdentifier()] = Prop;
1482 }
1483 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001484 for (ObjCInterfaceDecl::all_protocol_iterator
1485 PI = IDecl->all_referenced_protocol_begin(),
1486 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001487 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001488 }
1489 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1490 if (!CATDecl->IsClassExtension())
1491 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1492 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001493 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001494 PropMap[Prop->getIdentifier()] = Prop;
1495 }
1496 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001497 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001498 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001499 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001500 }
1501 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1502 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1503 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001504 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001505 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1506 // Exclude property for protocols which conform to class's super-class,
1507 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001508 if (!PropertyFromSuper ||
1509 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001510 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1511 if (!PropEntry)
1512 PropEntry = Prop;
1513 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001514 }
1515 // scan through protocol's protocols.
1516 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1517 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001518 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001519 }
1520}
1521
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001522/// CollectSuperClassPropertyImplementations - This routine collects list of
1523/// properties to be implemented in super class(s) and also coming from their
1524/// conforming protocols.
1525static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001526 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001527 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001528 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001529 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001530 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001531 SDecl = SDecl->getSuperClass();
1532 }
1533 }
1534}
1535
Fariborz Jahanian26202292013-02-14 19:07:19 +00001536/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1537/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1538/// declared in class 'IFace'.
1539bool
1540Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1541 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1542 if (!IV->getSynthesize())
1543 return false;
1544 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1545 Method->isInstanceMethod());
1546 if (!IMD || !IMD->isPropertyAccessor())
1547 return false;
1548
1549 // look up a property declaration whose one of its accessors is implemented
1550 // by this method.
1551 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1552 E = IFace->prop_end(); P != E; ++P) {
1553 ObjCPropertyDecl *property = *P;
1554 if ((property->getGetterName() == IMD->getSelector() ||
1555 property->getSetterName() == IMD->getSelector()) &&
1556 (property->getPropertyIvarDecl() == IV))
1557 return true;
1558 }
1559 return false;
1560}
1561
1562
James Dennett699c9042012-06-15 07:13:21 +00001563/// \brief Default synthesizes all properties which must be synthesized
1564/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001565void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1566 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001567
Anna Zaksb36ea372012-10-18 19:17:53 +00001568 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001569 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1570 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001571 if (PropMap.empty())
1572 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001573 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001574 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1575
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001576 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1577 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian6071af92013-06-07 20:26:51 +00001578 // Is there a matching property synthesize/dynamic?
1579 if (Prop->isInvalidDecl() ||
1580 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1581 continue;
1582 // Property may have been synthesized by user.
1583 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1584 continue;
1585 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1586 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1587 continue;
1588 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1589 continue;
1590 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001591 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001592 if (SuperPropMap[Prop->getIdentifier()]) {
1593 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1594 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1595 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001596 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001597 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1598 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001599 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1600 << Prop->getIdentifier()->getName();
1601 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1602 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001603 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001604 }
Fariborz Jahaniana6ba40c2013-06-07 18:32:55 +00001605 if (ObjCPropertyImplDecl *PID =
1606 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1607 if (PID->getPropertyDecl() != Prop) {
1608 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1609 << Prop->getIdentifier()->getName();
1610 if (!PID->getLocation().isInvalid())
1611 Diag(PID->getLocation(), diag::note_property_synthesize);
1612 }
1613 continue;
1614 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001615 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1616 // We won't auto-synthesize properties declared in protocols.
1617 Diag(IMPDecl->getLocation(),
1618 diag::warn_auto_synthesizing_protocol_property);
1619 Diag(Prop->getLocation(), diag::note_property_declare);
1620 continue;
1621 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001622
1623 // We use invalid SourceLocations for the synthesized ivars since they
1624 // aren't really synthesized at a particular location; they just exist.
1625 // Saying that they are located at the @implementation isn't really going
1626 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001627 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1628 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1629 true,
1630 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001631 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001632 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001633 if (PIDecl) {
1634 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001635 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001636 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001637 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001638}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001639
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001640void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001641 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001642 return;
1643 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1644 if (!IC)
1645 return;
1646 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001647 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001648 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001649}
1650
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001651void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001652 ObjCContainerDecl *CDecl) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001653 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1654 ObjCInterfaceDecl *IDecl;
1655 // Gather properties which need not be implemented in this class
1656 // or category.
1657 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1658 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1659 // For categories, no need to implement properties declared in
1660 // its primary class (and its super classes) if property is
1661 // declared in one of those containers.
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001662 if ((IDecl = C->getClassInterface())) {
1663 ObjCInterfaceDecl::PropertyDeclOrder PO;
1664 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1665 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001666 }
1667 if (IDecl)
1668 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001669
Anna Zaksb36ea372012-10-18 19:17:53 +00001670 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001671 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001672 if (PropMap.empty())
1673 return;
1674
1675 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1676 for (ObjCImplDecl::propimpl_iterator
1677 I = IMPDecl->propimpl_begin(),
1678 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001679 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001680
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001681 SelectorSet InsMap;
1682 // Collect property accessors implemented in current implementation.
1683 for (ObjCImplementationDecl::instmeth_iterator
1684 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1685 InsMap.insert((*I)->getSelector());
1686
1687 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1688 ObjCInterfaceDecl *PrimaryClass = 0;
1689 if (C && !C->IsClassExtension())
1690 if ((PrimaryClass = C->getClassInterface()))
1691 // Report unimplemented properties in the category as well.
1692 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1693 // When reporting on missing setter/getters, do not report when
1694 // setter/getter is implemented in category's primary class
1695 // implementation.
1696 for (ObjCImplementationDecl::instmeth_iterator
1697 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1698 InsMap.insert((*I)->getSelector());
1699 }
1700
Anna Zaksb36ea372012-10-18 19:17:53 +00001701 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001702 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1703 ObjCPropertyDecl *Prop = P->second;
1704 // Is there a matching propery synthesize/dynamic?
1705 if (Prop->isInvalidDecl() ||
1706 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001707 PropImplMap.count(Prop) ||
1708 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001709 continue;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001710 // When reporting on missing property getter implementation in
1711 // categories, do not report when they are declared in primary class,
1712 // class's protocol, or one of it super classes. This is because,
1713 // the class is going to implement them.
1714 if (!InsMap.count(Prop->getGetterName()) &&
1715 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001716 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001717 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001718 isa<ObjCCategoryDecl>(CDecl) ?
1719 diag::warn_setter_getter_impl_required_in_category :
1720 diag::warn_setter_getter_impl_required)
1721 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001722 Diag(Prop->getLocation(),
1723 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001724 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001725 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001726 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001727 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1728
Ted Kremenek9d64c152010-03-12 00:38:38 +00001729 }
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001730 // When reporting on missing property setter implementation in
1731 // categories, do not report when they are declared in primary class,
1732 // class's protocol, or one of it super classes. This is because,
1733 // the class is going to implement them.
1734 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1735 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001736 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001737 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001738 isa<ObjCCategoryDecl>(CDecl) ?
1739 diag::warn_setter_getter_impl_required_in_category :
1740 diag::warn_setter_getter_impl_required)
1741 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001742 Diag(Prop->getLocation(),
1743 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001744 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001745 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001746 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001747 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001748 }
1749 }
1750}
1751
1752void
1753Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1754 ObjCContainerDecl* IDecl) {
1755 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001756 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001757 return;
1758 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1759 E = IDecl->prop_end();
1760 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001761 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001762 ObjCMethodDecl *GetterMethod = 0;
1763 ObjCMethodDecl *SetterMethod = 0;
1764 bool LookedUpGetterSetter = false;
1765
Bill Wendlingad017fa2012-12-20 19:22:21 +00001766 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001767 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001768
John McCall265941b2011-09-13 18:31:23 +00001769 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1770 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001771 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1772 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1773 LookedUpGetterSetter = true;
1774 if (GetterMethod) {
1775 Diag(GetterMethod->getLocation(),
1776 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001777 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001778 Diag(Property->getLocation(), diag::note_property_declare);
1779 }
1780 if (SetterMethod) {
1781 Diag(SetterMethod->getLocation(),
1782 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001783 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001784 Diag(Property->getLocation(), diag::note_property_declare);
1785 }
1786 }
1787
Ted Kremenek9d64c152010-03-12 00:38:38 +00001788 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001789 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1790 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001791 continue;
1792 if (const ObjCPropertyImplDecl *PIDecl
1793 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1794 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1795 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001796 if (!LookedUpGetterSetter) {
1797 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1798 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1799 LookedUpGetterSetter = true;
1800 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001801 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1802 SourceLocation MethodLoc =
1803 (GetterMethod ? GetterMethod->getLocation()
1804 : SetterMethod->getLocation());
1805 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001806 << Property->getIdentifier() << (GetterMethod != 0)
1807 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001808 // fixit stuff.
1809 if (!AttributesAsWritten) {
1810 if (Property->getLParenLoc().isValid()) {
1811 // @property () ... case.
1812 SourceRange PropSourceRange(Property->getAtLoc(),
1813 Property->getLParenLoc());
1814 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1815 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1816 }
1817 else {
1818 //@property id etc.
1819 SourceLocation endLoc =
1820 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1821 endLoc = endLoc.getLocWithOffset(-1);
1822 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1823 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1824 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1825 }
1826 }
1827 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1828 // @property () ... case.
1829 SourceLocation endLoc = Property->getLParenLoc();
1830 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1831 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1832 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1833 }
1834 else
1835 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 Diag(Property->getLocation(), diag::note_property_declare);
1837 }
1838 }
1839 }
1840}
1841
John McCallf85e1932011-06-15 23:02:42 +00001842void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001843 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001844 return;
1845
1846 for (ObjCImplementationDecl::propimpl_iterator
1847 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001848 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001849 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1850 continue;
1851
1852 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001853 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1854 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001855 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1856 if (!method)
1857 continue;
1858 ObjCMethodFamily family = method->getMethodFamily();
1859 if (family == OMF_alloc || family == OMF_copy ||
1860 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001861 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001862 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1863 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001864 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001865 Diag(PD->getLocation(), diag::note_property_declare);
1866 }
1867 }
1868 }
1869}
1870
John McCall5de74d12010-11-10 07:01:40 +00001871/// AddPropertyAttrs - Propagates attributes from a property to the
1872/// implicitly-declared getter or setter for that property.
1873static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1874 ObjCPropertyDecl *Property) {
1875 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001876 for (Decl::attr_iterator A = Property->attr_begin(),
1877 AEnd = Property->attr_end();
1878 A != AEnd; ++A) {
1879 if (isa<DeprecatedAttr>(*A) ||
1880 isa<UnavailableAttr>(*A) ||
1881 isa<AvailabilityAttr>(*A))
1882 PropertyMethod->addAttr((*A)->clone(S.Context));
1883 }
John McCall5de74d12010-11-10 07:01:40 +00001884}
1885
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1887/// have the property type and issue diagnostics if they don't.
1888/// Also synthesize a getter/setter method if none exist (and update the
1889/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1890/// methods is the "right" thing to do.
1891void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001892 ObjCContainerDecl *CD,
1893 ObjCPropertyDecl *redeclaredProperty,
1894 ObjCContainerDecl *lexicalDC) {
1895
Ted Kremenek9d64c152010-03-12 00:38:38 +00001896 ObjCMethodDecl *GetterMethod, *SetterMethod;
1897
1898 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1899 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1900 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1901 property->getLocation());
1902
1903 if (SetterMethod) {
1904 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1905 property->getPropertyAttributes();
1906 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1907 Context.getCanonicalType(SetterMethod->getResultType()) !=
1908 Context.VoidTy)
1909 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1910 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001911 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001912 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1913 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001914 Diag(property->getLocation(),
1915 diag::warn_accessor_property_type_mismatch)
1916 << property->getDeclName()
1917 << SetterMethod->getSelector();
1918 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1919 }
1920 }
1921
1922 // Synthesize getter/setter methods if none exist.
1923 // Find the default getter and if one not found, add one.
1924 // FIXME: The synthesized property we set here is misleading. We almost always
1925 // synthesize these methods unless the user explicitly provided prototypes
1926 // (which is odd, but allowed). Sema should be typechecking that the
1927 // declarations jive in that situation (which it is not currently).
1928 if (!GetterMethod) {
1929 // No instance method of same name as property getter name was found.
1930 // Declare a getter method and add it to the list of methods
1931 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001932 SourceLocation Loc = redeclaredProperty ?
1933 redeclaredProperty->getLocation() :
1934 property->getLocation();
1935
1936 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1937 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001938 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001939 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001940 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001941 (property->getPropertyImplementation() ==
1942 ObjCPropertyDecl::Optional) ?
1943 ObjCMethodDecl::Optional :
1944 ObjCMethodDecl::Required);
1945 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001946
1947 AddPropertyAttrs(*this, GetterMethod, property);
1948
Ted Kremenek23173d72010-05-18 21:09:07 +00001949 // FIXME: Eventually this shouldn't be needed, as the lexical context
1950 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001951 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001952 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001953 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1954 GetterMethod->addAttr(
1955 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Fariborz Jahanian937ec1d2013-09-19 16:37:20 +00001956
1957 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1958 GetterMethod->addAttr(
1959 ::new (Context) ObjCReturnsInnerPointerAttr(Loc, Context));
John McCallb8463812013-04-04 01:38:37 +00001960
1961 if (getLangOpts().ObjCAutoRefCount)
1962 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001963 } else
1964 // A user declared getter will be synthesize when @synthesize of
1965 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001966 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001967 property->setGetterMethodDecl(GetterMethod);
1968
1969 // Skip setter if property is read-only.
1970 if (!property->isReadOnly()) {
1971 // Find the default setter and if one not found, add one.
1972 if (!SetterMethod) {
1973 // No instance method of same name as property setter name was found.
1974 // Declare a setter method and add it to the list of methods
1975 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001976 SourceLocation Loc = redeclaredProperty ?
1977 redeclaredProperty->getLocation() :
1978 property->getLocation();
1979
1980 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001981 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001982 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001983 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001984 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001985 /*isImplicitlyDeclared=*/true,
1986 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001987 (property->getPropertyImplementation() ==
1988 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001989 ObjCMethodDecl::Optional :
1990 ObjCMethodDecl::Required);
1991
Ted Kremenek9d64c152010-03-12 00:38:38 +00001992 // Invent the arguments for the setter. We don't bother making a
1993 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001994 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1995 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001996 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001997 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001998 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001999 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002000 0);
Dmitri Gribenko55431692013-05-05 00:41:58 +00002001 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00002002
2003 AddPropertyAttrs(*this, SetterMethod, property);
2004
Ted Kremenek9d64c152010-03-12 00:38:38 +00002005 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002006 // FIXME: Eventually this shouldn't be needed, as the lexical context
2007 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002008 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002009 SetterMethod->setLexicalDeclContext(lexicalDC);
John McCallb8463812013-04-04 01:38:37 +00002010
2011 // It's possible for the user to have set a very odd custom
2012 // setter selector that causes it to have a method family.
2013 if (getLangOpts().ObjCAutoRefCount)
2014 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002015 } else
2016 // A user declared setter will be synthesize when @synthesize of
2017 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002018 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002019 property->setSetterMethodDecl(SetterMethod);
2020 }
2021 // Add any synthesized methods to the global pool. This allows us to
2022 // handle the following, which is supported by GCC (and part of the design).
2023 //
2024 // @interface Foo
2025 // @property double bar;
2026 // @end
2027 //
2028 // void thisIsUnfortunate() {
2029 // id foo;
2030 // double bar = [foo bar];
2031 // }
2032 //
2033 if (GetterMethod)
2034 AddInstanceMethodToGlobalPool(GetterMethod);
2035 if (SetterMethod)
2036 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002037
2038 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2039 if (!CurrentClass) {
2040 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2041 CurrentClass = Cat->getClassInterface();
2042 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2043 CurrentClass = Impl->getClassInterface();
2044 }
2045 if (GetterMethod)
2046 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2047 if (SetterMethod)
2048 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002049}
2050
John McCalld226f652010-08-21 09:40:31 +00002051void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002052 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002053 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002054 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002055 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002056 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002057 return;
2058
2059 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002060 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002061
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002063 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002064 PropertyTy->isObjCRetainableType()) {
2065 // 'readonly' property with no obvious lifetime.
2066 // its life time will be determined by its backing ivar.
2067 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2068 ObjCDeclSpec::DQ_PR_copy |
2069 ObjCDeclSpec::DQ_PR_retain |
2070 ObjCDeclSpec::DQ_PR_strong |
2071 ObjCDeclSpec::DQ_PR_weak |
2072 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002073 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002074 return;
2075 }
2076
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002077 if (propertyInPrimaryClass) {
2078 // we postpone most property diagnosis until class's implementation
2079 // because, its readonly attribute may be overridden in its class
2080 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002081 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2082 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002083 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2084 << "readonly" << "readwrite";
2085 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002086 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002087 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2088 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002089 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002090 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002091 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002092 ObjCDeclSpec::DQ_PR_retain |
2093 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00002094 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002095 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002096 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002097 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002098 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00002099 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002100 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002101 "copy" : "retain";
2102
Bill Wendlingad017fa2012-12-20 19:22:21 +00002103 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002104 diag::err_objc_property_attr_mutually_exclusive :
2105 diag::warn_objc_property_attr_mutually_exclusive)
2106 << "readonly" << which;
2107 }
2108
2109 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002110 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002111 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2112 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002113 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002114 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002115 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2116 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2117 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002118 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002119 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002120 }
2121
2122 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002123 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2124 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002125 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2126 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002127 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002128 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002129 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002130 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2131 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002132 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002133 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002134 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002135 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2136 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002137 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002138 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002139 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002140 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002141 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2142 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002143 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002144 }
Fariborz Jahanian548fba92013-06-25 17:34:50 +00002145 if (PropertyDecl->getAttr<IBOutletCollectionAttr>())
2146 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002147 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2148 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002149 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2150 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002151 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002152 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002153 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002154 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2155 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002157 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002158 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002159 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2160 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002161 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002162 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002163 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002164 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002165 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2166 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002167 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002168 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002169 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2170 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002171 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2172 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002173 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002174 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002175 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002176 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2177 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002178 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002179 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002180 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002181 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2182 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002183 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002184 }
2185 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002186 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2187 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002188 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2189 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002190 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002191 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002192 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2193 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002194 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2195 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002196 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002197 }
2198
Bill Wendlingad017fa2012-12-20 19:22:21 +00002199 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2200 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002201 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2202 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002203 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002204 }
2205
Ted Kremenek9d64c152010-03-12 00:38:38 +00002206 // Warn if user supplied no assignment attribute, property is
2207 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002208 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002209 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2210 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2211 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002212 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002213 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002214 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002215 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002216 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002217 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002218 bool isAnyClassTy =
2219 (PropertyTy->isObjCClassType() ||
2220 PropertyTy->isObjCQualifiedClassType());
2221 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2222 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002223 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002224 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002225 else if (propertyInPrimaryClass) {
2226 // Don't issue warning on property with no life time in class
2227 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002228 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002229 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002230 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002231
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002232 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002233 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002234 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002235 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002236 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002237
2238 // FIXME: Implement warning dependent on NSCopying being
2239 // implemented. See also:
2240 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2241 // (please trim this list while you are at it).
2242 }
2243
Bill Wendlingad017fa2012-12-20 19:22:21 +00002244 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2245 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002246 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002247 && PropertyTy->isBlockPointerType())
2248 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002249 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2250 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2251 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002252 PropertyTy->isBlockPointerType())
2253 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002254
Bill Wendlingad017fa2012-12-20 19:22:21 +00002255 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2256 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002257 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2258
Ted Kremenek9d64c152010-03-12 00:38:38 +00002259}