blob: eea265ade17f84469c316e3b79d91418c3c7d25b [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCall50df6ae2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenek28685ab2010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall265941b2011-09-13 18:31:23 +000033/// getImpliedARCOwnership - Given a set of property attributes and a
34/// type, infer an expected lifetime. The type's ownership qualification
35/// is not considered.
36///
37/// Returns OCL_None if the attributes as stated do not imply an ownership.
38/// Never returns OCL_Autoreleasing.
39static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
41 QualType type) {
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
John McCalld64c2eb2012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall265941b2011-09-13 18:31:23 +000048 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
52 }
53
54 // assign can appear on other types, so we have to check the
55 // property type.
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
59 }
60
61 return Qualifiers::OCL_None;
62}
63
John McCallf85e1932011-06-15 23:02:42 +000064/// Check the internal consistency of a property declaration.
65static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
67
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
72
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
75
John McCall265941b2011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000080 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
82 // qualifier.
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
88 } else {
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
91 }
92 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +0000102 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000106static unsigned deduceWeakPropertyFromType(Sema &S, QualType T) {
107 if ((S.getLangOpts().getGC() != LangOptions::NonGC &&
108 T.isObjCGCWeak()) ||
109 (S.getLangOpts().ObjCAutoRefCount &&
110 T.getObjCLifetime() == Qualifiers::OCL_Weak))
111 return ObjCDeclSpec::DQ_PR_weak;
112 return 0;
113}
114
Douglas Gregorb892d702013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
120 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
121 // Have we seen this protocol before?
122 if (!Known.insert(Proto))
123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier());
130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
135 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
136 PEnd = Proto->protocol_end();
137 P != PEnd; ++P) {
138 CheckPropertyAgainstProtocol(S, Prop, *P, Known);
139 }
140}
141
John McCalld226f652010-08-21 09:40:31 +0000142Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000143 SourceLocation LParenLoc,
John McCalld226f652010-08-21 09:40:31 +0000144 FieldDeclarator &FD,
145 ObjCDeclSpec &ODS,
146 Selector GetterSel,
147 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000148 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000149 tok::ObjCKeywordKind MethodImplKind,
150 DeclContext *lexicalDC) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000151 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000152 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
153 QualType T = TSI->getType();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000154 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000155
Bill Wendlingad017fa2012-12-20 19:22:21 +0000156 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000157 // default is readwrite!
Bill Wendlingad017fa2012-12-20 19:22:21 +0000158 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenek28685ab2010-03-12 00:46:40 +0000159 // property is defaulted to 'assign' if it is readwrite and is
160 // not retain or copy
Bill Wendlingad017fa2012-12-20 19:22:21 +0000161 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenek28685ab2010-03-12 00:46:40 +0000162 (isReadWrite &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000163 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
164 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
165 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
166 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
167 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000168
Douglas Gregoraabd0942013-01-21 19:05:22 +0000169 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000170 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000171 ObjCPropertyDecl *Res = 0;
172 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000173 if (CDecl->IsClassExtension()) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000174 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000175 FD, GetterSel, SetterSel,
176 isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000177 Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000178 ODS.getPropertyAttributes(),
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000179 isOverridingProperty, TSI,
180 MethodImplKind);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000181 if (!Res)
182 return 0;
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000183 }
Douglas Gregoraabd0942013-01-21 19:05:22 +0000184 }
185
186 if (!Res) {
187 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
188 GetterSel, SetterSel, isAssign, isReadWrite,
189 Attributes, ODS.getPropertyAttributes(),
190 TSI, MethodImplKind);
191 if (lexicalDC)
192 Res->setLexicalDeclContext(lexicalDC);
193 }
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000194
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000195 // Validate the attributes on the @property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000196 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000197 (isa<ObjCInterfaceDecl>(ClassDecl) ||
198 isa<ObjCProtocolDecl>(ClassDecl)));
John McCallf85e1932011-06-15 23:02:42 +0000199
David Blaikie4e4d0842012-03-11 07:00:24 +0000200 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +0000201 checkARCPropertyDecl(*this, Res);
202
Douglas Gregorb892d702013-01-21 19:42:21 +0000203 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000204 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb892d702013-01-21 19:42:21 +0000205 // For a class, compare the property against a property in our superclass.
206 bool FoundInSuper = false;
Douglas Gregoraabd0942013-01-21 19:05:22 +0000207 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
208 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb892d702013-01-21 19:42:21 +0000209 for (unsigned I = 0, N = R.size(); I != N; ++I) {
210 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Douglas Gregoraabd0942013-01-21 19:05:22 +0000211 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier());
Douglas Gregorb892d702013-01-21 19:42:21 +0000212 FoundInSuper = true;
213 break;
214 }
215 }
216 }
217
218 if (FoundInSuper) {
219 // Also compare the property against a property in our protocols.
220 for (ObjCInterfaceDecl::protocol_iterator P = IFace->protocol_begin(),
221 PEnd = IFace->protocol_end();
222 P != PEnd; ++P) {
223 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
224 }
225 } else {
226 // Slower path: look in all protocols we referenced.
227 for (ObjCInterfaceDecl::all_protocol_iterator
228 P = IFace->all_referenced_protocol_begin(),
229 PEnd = IFace->all_referenced_protocol_end();
230 P != PEnd; ++P) {
231 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
232 }
233 }
234 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
235 for (ObjCCategoryDecl::protocol_iterator P = Cat->protocol_begin(),
236 PEnd = Cat->protocol_end();
237 P != PEnd; ++P) {
238 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
239 }
240 } else {
241 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
242 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
243 PEnd = Proto->protocol_end();
244 P != PEnd; ++P) {
245 CheckPropertyAgainstProtocol(*this, Res, *P, KnownProtos);
Douglas Gregoraabd0942013-01-21 19:05:22 +0000246 }
247 }
248
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +0000249 ActOnDocumentableDecl(Res);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000250 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000252
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000253static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendlingad017fa2012-12-20 19:22:21 +0000254makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000255 unsigned attributesAsWritten = 0;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendlingad017fa2012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
280
281 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
282}
283
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000284static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000285 SourceLocation LParenLoc, SourceLocation &Loc) {
286 if (LParenLoc.isMacroID())
287 return false;
288
289 SourceManager &SM = Context.getSourceManager();
290 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
291 // Try to load the file buffer.
292 bool invalidTemp = false;
293 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
294 if (invalidTemp)
295 return false;
296 const char *tokenBegin = file.data() + locInfo.second;
297
298 // Lex from the start of the given location.
299 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
300 Context.getLangOpts(),
301 file.begin(), tokenBegin, file.end());
302 Token Tok;
303 do {
304 lexer.LexFromRawLexer(Tok);
305 if (Tok.is(tok::raw_identifier) &&
306 StringRef(Tok.getRawIdentifierData(), Tok.getLength()) == attrName) {
307 Loc = Tok.getLocation();
308 return true;
309 }
310 } while (Tok.isNot(tok::r_paren));
311 return false;
312
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000313}
314
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000315static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000316 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
317 ObjCPropertyDecl::OBJC_PR_retain |
318 ObjCPropertyDecl::OBJC_PR_copy |
319 ObjCPropertyDecl::OBJC_PR_weak |
320 ObjCPropertyDecl::OBJC_PR_strong |
321 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
322}
323
Douglas Gregoraabd0942013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregord3297242013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Douglas Gregord3297242013-01-16 23:00:23 +0000346 for (ObjCInterfaceDecl::known_extensions_iterator
347 Ext = CCPrimary->known_extensions_begin(),
348 ExtEnd = CCPrimary->known_extensions_end();
349 Ext != ExtEnd; ++Ext) {
350 if (ObjCPropertyDecl *prevDecl
351 = ObjCPropertyDecl::findPropertyDecl(*Ext, PropertyId)) {
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000352 Diag(AtLoc, diag::err_duplicate_property);
353 Diag(prevDecl->getLocation(), diag::note_property_declare);
354 return 0;
355 }
356 }
Douglas Gregord3297242013-01-16 23:00:23 +0000357 }
358
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000359 // Create a new ObjCPropertyDecl with the DeclContext being
360 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000361 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000362 ObjCPropertyDecl *PDecl =
363 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000364 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000365 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000366 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendlingad017fa2012-12-20 19:22:21 +0000367 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000371 // Set setter/getter selector name. Needed later.
372 PDecl->setGetterName(GetterSel);
373 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000374 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000375 DC->addDecl(PDecl);
376
377 // We need to look in the @interface to see if the @property was
378 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000379 if (!CCPrimary) {
380 Diag(CDecl->getLocation(), diag::err_continuation_class);
381 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000382 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000383 }
384
385 // Find the property in continuation class's primary class only.
386 ObjCPropertyDecl *PIDecl =
387 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
388
389 if (!PIDecl) {
390 // No matching property found in the primary class. Just fall thru
391 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000392 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000393 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000394 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000395 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000396
397 // A case of continuation class adding a new property in the class. This
398 // is not what it was meant for. However, gcc supports it and so should we.
399 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000400 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000401 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000402 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
403 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000404 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000405 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
406 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000407 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000408 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
409 bool IncompatibleObjC = false;
410 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000411 // Relax the strict type matching for property type in continuation class.
412 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000413 // as it narrows the object type in its primary class property. Note that
414 // this conversion is safe only because the wider type is for a 'readonly'
415 // property in primary class and 'narrowed' type for a 'readwrite' property
416 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000417 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
418 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
419 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
420 ConvertedType, IncompatibleObjC))
421 || IncompatibleObjC) {
422 Diag(AtLoc,
423 diag::err_type_mismatch_continuation_class) << PDecl->getType();
424 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000425 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000426 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000427 }
428
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000429 // The property 'PIDecl's readonly attribute will be over-ridden
430 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000431 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000432 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000433 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000434 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000435 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000436 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
437 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000438 Diag(AtLoc, diag::warn_property_attr_mismatch);
439 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000440 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000441 DeclContext *DC = cast<DeclContext>(CCPrimary);
442 if (!ObjCPropertyDecl::findPropertyDecl(DC,
443 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000444 // Protocol is not in the primary class. Must build one for it.
445 ObjCDeclSpec ProtocolPropertyODS;
446 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
447 // and ObjCPropertyDecl::PropertyAttributeKind have identical
448 // values. Should consolidate both into one enum type.
449 ProtocolPropertyODS.
450 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
451 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000452 // Must re-establish the context from class extension to primary
453 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000454 ContextRAII SavedContext(*this, CCPrimary);
455
John McCalld226f652010-08-21 09:40:31 +0000456 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000457 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000458 PIDecl->getGetterName(),
459 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000460 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000461 MethodImplKind,
462 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000463 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000464 }
465 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000466 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000467 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000468 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000469 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000470 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000471 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
472 PIDecl->setSetterName(SetterSel);
473 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000474 // Tailor the diagnostics for the common case where a readwrite
475 // property is declared both in the @interface and the continuation.
476 // This is a common error where the user often intended the original
477 // declaration to be readonly.
478 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000479 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000480 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
481 ? diag::err_use_continuation_class_redeclaration_readwrite
482 : diag::err_use_continuation_class;
483 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000484 << CCPrimary->getDeclName();
485 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000486 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000487 }
488 *isOverridingProperty = true;
489 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000490 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000491 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
492 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000493 if (ASTMutationListener *L = Context.getASTMutationListener())
494 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000495 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000496}
497
498ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
499 ObjCContainerDecl *CDecl,
500 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000501 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000502 FieldDeclarator &FD,
503 Selector GetterSel,
504 Selector SetterSel,
505 const bool isAssign,
506 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000507 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000508 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000509 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000510 tok::ObjCKeywordKind MethodImplKind,
511 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000512 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000513 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000514
515 // Issue a warning if property is 'assign' as default and its object, which is
516 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000517 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000518 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000519 if (const ObjCObjectPointerType *ObjPtrTy =
520 T->getAs<ObjCObjectPointerType>()) {
521 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
522 if (IDecl)
523 if (ObjCProtocolDecl* PNSCopying =
524 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
525 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
526 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000527 }
John McCallc12c5bb2010-05-15 11:32:37 +0000528 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000529 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
530
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000531 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000532 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
533 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000534 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000535
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000536 if (ObjCPropertyDecl *prevDecl =
537 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000539 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000540 PDecl->setInvalidDecl();
541 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000542 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000543 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000544 if (lexicalDC)
545 PDecl->setLexicalDeclContext(lexicalDC);
546 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000547
548 if (T->isArrayType() || T->isFunctionType()) {
549 Diag(AtLoc, diag::err_property_type) << T;
550 PDecl->setInvalidDecl();
551 }
552
553 ProcessDeclAttributes(S, PDecl, FD.D);
554
555 // Regardless of setter/getter attribute, we save the default getter/setter
556 // selector names in anticipation of declaration of setter/getter methods.
557 PDecl->setGetterName(GetterSel);
558 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000559 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000560 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000561
Bill Wendlingad017fa2012-12-20 19:22:21 +0000562 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000563 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
564
Bill Wendlingad017fa2012-12-20 19:22:21 +0000565 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000566 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
567
Bill Wendlingad017fa2012-12-20 19:22:21 +0000568 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000569 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
570
571 if (isReadWrite)
572 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
573
Bill Wendlingad017fa2012-12-20 19:22:21 +0000574 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000575 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
576
Bill Wendlingad017fa2012-12-20 19:22:21 +0000577 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000578 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
579
Bill Wendlingad017fa2012-12-20 19:22:21 +0000580 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000581 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
582
Bill Wendlingad017fa2012-12-20 19:22:21 +0000583 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
585
Bill Wendlingad017fa2012-12-20 19:22:21 +0000586 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000587 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
588
Ted Kremenek28685ab2010-03-12 00:46:40 +0000589 if (isAssign)
590 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
591
John McCall265941b2011-09-13 18:31:23 +0000592 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000593 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000595 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000596 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000597
John McCallf85e1932011-06-15 23:02:42 +0000598 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000599 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
601 if (isAssign)
602 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
603
Ted Kremenek28685ab2010-03-12 00:46:40 +0000604 if (MethodImplKind == tok::objc_required)
605 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
606 else if (MethodImplKind == tok::objc_optional)
607 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000608
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000609 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000610}
611
John McCallf85e1932011-06-15 23:02:42 +0000612static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
613 ObjCPropertyDecl *property,
614 ObjCIvarDecl *ivar) {
615 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
616
John McCallf85e1932011-06-15 23:02:42 +0000617 QualType ivarType = ivar->getType();
618 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000619
John McCall265941b2011-09-13 18:31:23 +0000620 // The lifetime implied by the property's attributes.
621 Qualifiers::ObjCLifetime propertyLifetime =
622 getImpliedARCOwnership(property->getPropertyAttributes(),
623 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000624
John McCall265941b2011-09-13 18:31:23 +0000625 // We're fine if they match.
626 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000627
John McCall265941b2011-09-13 18:31:23 +0000628 // These aren't valid lifetimes for object ivars; don't diagnose twice.
629 if (ivarLifetime == Qualifiers::OCL_None ||
630 ivarLifetime == Qualifiers::OCL_Autoreleasing)
631 return;
John McCallf85e1932011-06-15 23:02:42 +0000632
John McCalld64c2eb2012-08-20 23:36:59 +0000633 // If the ivar is private, and it's implicitly __unsafe_unretained
634 // becaues of its type, then pretend it was actually implicitly
635 // __strong. This is only sound because we're processing the
636 // property implementation before parsing any method bodies.
637 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
638 propertyLifetime == Qualifiers::OCL_Strong &&
639 ivar->getAccessControl() == ObjCIvarDecl::Private) {
640 SplitQualType split = ivarType.split();
641 if (split.Quals.hasObjCLifetime()) {
642 assert(ivarType->isObjCARCImplicitlyUnretainedType());
643 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
644 ivarType = S.Context.getQualifiedType(split);
645 ivar->setType(ivarType);
646 return;
647 }
648 }
649
John McCall265941b2011-09-13 18:31:23 +0000650 switch (propertyLifetime) {
651 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000652 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000653 << property->getDeclName()
654 << ivar->getDeclName()
655 << ivarLifetime;
656 break;
John McCallf85e1932011-06-15 23:02:42 +0000657
John McCall265941b2011-09-13 18:31:23 +0000658 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000659 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000660 << property->getDeclName()
661 << ivar->getDeclName();
662 break;
John McCallf85e1932011-06-15 23:02:42 +0000663
John McCall265941b2011-09-13 18:31:23 +0000664 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000665 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000666 << property->getDeclName()
667 << ivar->getDeclName()
668 << ((property->getPropertyAttributesAsWritten()
669 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
670 break;
John McCallf85e1932011-06-15 23:02:42 +0000671
John McCall265941b2011-09-13 18:31:23 +0000672 case Qualifiers::OCL_Autoreleasing:
673 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000674
John McCall265941b2011-09-13 18:31:23 +0000675 case Qualifiers::OCL_None:
676 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000677 return;
678 }
679
680 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000681 if (propertyImplLoc.isValid())
682 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000683}
684
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000685/// setImpliedPropertyAttributeForReadOnlyProperty -
686/// This routine evaludates life-time attributes for a 'readonly'
687/// property with no known lifetime of its own, using backing
688/// 'ivar's attribute, if any. If no backing 'ivar', property's
689/// life-time is assumed 'strong'.
690static void setImpliedPropertyAttributeForReadOnlyProperty(
691 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
692 Qualifiers::ObjCLifetime propertyLifetime =
693 getImpliedARCOwnership(property->getPropertyAttributes(),
694 property->getType());
695 if (propertyLifetime != Qualifiers::OCL_None)
696 return;
697
698 if (!ivar) {
699 // if no backing ivar, make property 'strong'.
700 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
701 return;
702 }
703 // property assumes owenership of backing ivar.
704 QualType ivarType = ivar->getType();
705 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
706 if (ivarLifetime == Qualifiers::OCL_Strong)
707 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
708 else if (ivarLifetime == Qualifiers::OCL_Weak)
709 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
710 return;
711}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000713/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
714/// attribute declared in primary class and attributes overridden in any of its
715/// class extensions.
716static void
717DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
718 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000719 unsigned Attributes = property->getPropertyAttributesAsWritten();
720 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000721 for (ObjCInterfaceDecl::known_extensions_iterator
722 Ext = ClassDecl->known_extensions_begin(),
723 ExtEnd = ClassDecl->known_extensions_end();
724 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000725 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000726 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
727 for (unsigned I = 0, N = R.size(); I != N; ++I) {
728 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
729 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000730 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000731 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000732
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000733 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000734 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000735 unsigned classExtPropertyAttr =
736 ClassExtProperty->getPropertyAttributesAsWritten();
737 // We are issuing the warning that we postponed because class extensions
738 // can override readonly->readwrite and 'setter' attributes originally
739 // placed on class's property declaration now make sense in the overridden
740 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000741 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000742 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000743 (classExtPropertyAttr &
744 (ObjCDeclSpec::DQ_PR_readwrite|
745 ObjCDeclSpec::DQ_PR_assign |
746 ObjCDeclSpec::DQ_PR_unsafe_unretained |
747 ObjCDeclSpec::DQ_PR_copy |
748 ObjCDeclSpec::DQ_PR_retain |
749 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000750 continue;
751 warn = true;
752 break;
753 }
754 }
755 }
756 if (warn) {
757 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
758 ObjCDeclSpec::DQ_PR_unsafe_unretained |
759 ObjCDeclSpec::DQ_PR_copy |
760 ObjCDeclSpec::DQ_PR_retain |
761 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000762 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000763 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000764 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000765 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000766 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000767 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000768 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000769 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000770 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000771 "retain" : "strong";
772
773 S.Diag(property->getLocation(),
774 diag::warn_objc_property_attr_mutually_exclusive)
775 << "readonly" << which;
776 }
777 }
778
779
780}
781
Ted Kremenek28685ab2010-03-12 00:46:40 +0000782/// ActOnPropertyImplDecl - This routine performs semantic checks and
783/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000784/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000785///
John McCalld226f652010-08-21 09:40:31 +0000786Decl *Sema::ActOnPropertyImplDecl(Scope *S,
787 SourceLocation AtLoc,
788 SourceLocation PropertyLoc,
789 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000790 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000791 IdentifierInfo *PropertyIvar,
792 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000793 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000794 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000795 // Make sure we have a context for the property implementation declaration.
796 if (!ClassImpDecl) {
797 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000798 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000799 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000800 if (PropertyIvarLoc.isInvalid())
801 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000802 SourceLocation PropertyDiagLoc = PropertyLoc;
803 if (PropertyDiagLoc.isInvalid())
804 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000805 ObjCPropertyDecl *property = 0;
806 ObjCInterfaceDecl* IDecl = 0;
807 // Find the class or category class where this property must have
808 // a declaration.
809 ObjCImplementationDecl *IC = 0;
810 ObjCCategoryImplDecl* CatImplClass = 0;
811 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
812 IDecl = IC->getClassInterface();
813 // We always synthesize an interface for an implementation
814 // without an interface decl. So, IDecl is always non-zero.
815 assert(IDecl &&
816 "ActOnPropertyImplDecl - @implementation without @interface");
817
818 // Look for this property declaration in the @implementation's @interface
819 property = IDecl->FindPropertyDeclaration(PropertyId);
820 if (!property) {
821 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000822 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000823 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000824 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000825 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
826 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000827 if (AtLoc.isValid())
828 Diag(AtLoc, diag::warn_implicit_atomic_property);
829 else
830 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
831 Diag(property->getLocation(), diag::note_property_declare);
832 }
833
Ted Kremenek28685ab2010-03-12 00:46:40 +0000834 if (const ObjCCategoryDecl *CD =
835 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
836 if (!CD->IsClassExtension()) {
837 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
838 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000839 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000840 }
841 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000842
843 if (Synthesize&&
844 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
845 property->hasAttr<IBOutletAttr>() &&
846 !AtLoc.isValid()) {
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000847 Diag(IC->getLocation(), diag::warn_auto_readonly_iboutlet_property);
848 Diag(property->getLocation(), diag::note_property_declare);
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000849 SourceLocation readonlyLoc;
Fariborz Jahanian5bf0e352012-05-21 17:10:28 +0000850 if (LocPropertyAttribute(Context, "readonly",
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000851 property->getLParenLoc(), readonlyLoc)) {
852 SourceLocation endLoc =
853 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
854 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
855 Diag(property->getLocation(),
856 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
857 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
858 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000859 }
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000860
861 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000862
Ted Kremenek28685ab2010-03-12 00:46:40 +0000863 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
864 if (Synthesize) {
865 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000866 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000867 }
868 IDecl = CatImplClass->getClassInterface();
869 if (!IDecl) {
870 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000871 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000872 }
873 ObjCCategoryDecl *Category =
874 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
875
876 // If category for this implementation not found, it is an error which
877 // has already been reported eralier.
878 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000879 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000880 // Look for this property declaration in @implementation's category
881 property = Category->FindPropertyDeclaration(PropertyId);
882 if (!property) {
883 Diag(PropertyLoc, diag::error_bad_category_property_decl)
884 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000885 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000886 }
887 } else {
888 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000889 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000890 }
891 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000892 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000893 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000894 // Check that we have a valid, previously declared ivar for @synthesize
895 if (Synthesize) {
896 // @synthesize
897 if (!PropertyIvar)
898 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000899 // Check that this is a previously declared 'ivar' in 'IDecl' interface
900 ObjCInterfaceDecl *ClassDeclared;
901 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
902 QualType PropType = property->getType();
903 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000904
905 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000906 diag::err_incomplete_synthesized_property,
907 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000908 Diag(property->getLocation(), diag::note_property_declare);
909 CompleteTypeErr = true;
910 }
911
David Blaikie4e4d0842012-03-11 07:00:24 +0000912 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000913 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000914 ObjCPropertyDecl::OBJC_PR_readonly) &&
915 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000916 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
917 }
918
John McCallf85e1932011-06-15 23:02:42 +0000919 ObjCPropertyDecl::PropertyAttributeKind kind
920 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000921
922 // Add GC __weak to the ivar type if the property is weak.
923 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000924 getLangOpts().getGC() != LangOptions::NonGC) {
925 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +0000926 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000927 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +0000928 Diag(property->getLocation(), diag::note_property_declare);
929 } else {
930 PropertyIvarType =
931 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000932 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000933 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000934 if (AtLoc.isInvalid()) {
935 // Check when default synthesizing a property that there is
936 // an ivar matching property name and issue warning; since this
937 // is the most common case of not using an ivar used for backing
938 // property in non-default synthesis case.
939 ObjCInterfaceDecl *ClassDeclared=0;
940 ObjCIvarDecl *originalIvar =
941 IDecl->lookupInstanceVariable(property->getIdentifier(),
942 ClassDeclared);
943 if (originalIvar) {
944 Diag(PropertyDiagLoc,
945 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +0000946 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +0000947 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000948 Diag(property->getLocation(), diag::note_property_declare);
949 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +0000950 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +0000951 }
952
953 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000954 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000955 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +0000956 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000957 !PropertyIvarType.getObjCLifetime() &&
958 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000959
John McCall265941b2011-09-13 18:31:23 +0000960 // It's an error if we have to do this and the user didn't
961 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000962 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000963 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000964 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000965 diag::err_arc_objc_property_default_assign_on_object);
966 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000967 } else {
968 Qualifiers::ObjCLifetime lifetime =
969 getImpliedARCOwnership(kind, PropertyIvarType);
970 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000971 if (lifetime == Qualifiers::OCL_Weak) {
972 bool err = false;
973 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +0000974 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
975 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
976 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000977 Diag(PropertyDiagLoc, diag::err_arc_weak_unavailable_property);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000978 Diag(property->getLocation(), diag::note_property_declare);
979 err = true;
980 }
Richard Smitha8eaf002012-08-23 06:16:52 +0000981 }
John McCall0a7dd782012-08-21 02:47:43 +0000982 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000983 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000984 Diag(property->getLocation(), diag::note_property_declare);
985 }
John McCallf85e1932011-06-15 23:02:42 +0000986 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +0000987
John McCallf85e1932011-06-15 23:02:42 +0000988 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000989 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000990 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
991 }
John McCallf85e1932011-06-15 23:02:42 +0000992 }
993
994 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +0000995 !getLangOpts().ObjCAutoRefCount &&
996 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000997 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +0000998 Diag(property->getLocation(), diag::note_property_declare);
999 }
1000
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001001 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001002 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001003 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001004 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001005 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001006 if (CompleteTypeErr)
1007 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001008 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001009 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001010
John McCall260611a2012-06-20 06:18:46 +00001011 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001012 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1013 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001014 // Note! I deliberately want it to fall thru so, we have a
1015 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001016 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001017 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001018 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001019 << property->getDeclName() << Ivar->getDeclName()
1020 << ClassDeclared->getDeclName();
1021 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001022 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001023 // Note! I deliberately want it to fall thru so more errors are caught.
1024 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001025 property->setPropertyIvarDecl(Ivar);
1026
Ted Kremenek28685ab2010-03-12 00:46:40 +00001027 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1028
1029 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001030 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001031 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001032 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001033 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001034 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001035 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001036 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001037 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001038 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1039 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001040 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001041 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001042 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001043 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001044 << property->getDeclName() << PropType
1045 << Ivar->getDeclName() << IvarType;
1046 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001047 // Note! I deliberately want it to fall thru so, we have a
1048 // a property implementation and to avoid future warnings.
1049 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001050 else {
1051 // FIXME! Rules for properties are somewhat different that those
1052 // for assignments. Use a new routine to consolidate all cases;
1053 // specifically for property redeclarations as well as for ivars.
1054 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1055 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1056 if (lhsType != rhsType &&
1057 lhsType->isArithmeticType()) {
1058 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1059 << property->getDeclName() << PropType
1060 << Ivar->getDeclName() << IvarType;
1061 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1062 // Fall thru - see previous comment
1063 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001064 }
1065 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001066 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001067 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001068 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001069 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001070 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001071 // Fall thru - see previous comment
1072 }
John McCallf85e1932011-06-15 23:02:42 +00001073 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001074 if ((property->getType()->isObjCObjectPointerType() ||
1075 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001076 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001077 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001078 << property->getDeclName() << Ivar->getDeclName();
1079 // Fall thru - see previous comment
1080 }
1081 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001082 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001083 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001084 } else if (PropertyIvar)
1085 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001086 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001087
Ted Kremenek28685ab2010-03-12 00:46:40 +00001088 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1089 ObjCPropertyImplDecl *PIDecl =
1090 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1091 property,
1092 (Synthesize ?
1093 ObjCPropertyImplDecl::Synthesize
1094 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001095 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001096
Fariborz Jahanian74414712012-05-15 18:12:51 +00001097 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001098 PIDecl->setInvalidDecl();
1099
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001100 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1101 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001102 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001103 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001104 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1105 // returned by the getter as it must conform to C++'s copy-return rules.
1106 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001107 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001108 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1109 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001110 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001111 VK_RValue, PropertyDiagLoc);
1112 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001113 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001114 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001115 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001116 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001117 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001118 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001119 getterMethod->getResultType(),
1120 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001121 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001122 Owned(IvarRefExpr));
1123 if (!Res.isInvalid()) {
1124 Expr *ResExpr = Res.takeAs<Expr>();
1125 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001126 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001127 PIDecl->setGetterCXXConstructor(ResExpr);
1128 }
1129 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001130 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1131 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1132 Diag(getterMethod->getLocation(),
1133 diag::warn_property_getter_owning_mismatch);
1134 Diag(property->getLocation(), diag::note_property_declare);
1135 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001136 }
1137 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1138 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001139 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1140 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001141 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001142 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001143 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1144 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001145 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001146 VK_RValue, PropertyDiagLoc);
1147 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001148 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001149 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001150 SelfExpr, true, true);
1151 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1152 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001153 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001154 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1155 VK_LValue, PropertyDiagLoc);
1156 MarkDeclRefReferenced(rhs);
1157 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001158 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001159 if (property->getPropertyAttributes() &
1160 ObjCPropertyDecl::OBJC_PR_atomic) {
1161 Expr *callExpr = Res.takeAs<Expr>();
1162 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001163 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1164 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001165 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001166 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001167 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001168 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001169 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001170 Diag(FuncDecl->getLocStart(),
1171 diag::note_callee_decl) << FuncDecl;
1172 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001173 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001174 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1175 }
1176 }
1177
Ted Kremenek28685ab2010-03-12 00:46:40 +00001178 if (IC) {
1179 if (Synthesize)
1180 if (ObjCPropertyImplDecl *PPIDecl =
1181 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1182 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1183 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1184 << PropertyIvar;
1185 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1186 }
1187
1188 if (ObjCPropertyImplDecl *PPIDecl
1189 = IC->FindPropertyImplDecl(PropertyId)) {
1190 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1191 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001192 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001193 }
1194 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001195 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001196 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001197 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001198 // Diagnose if an ivar was lazily synthesdized due to a previous
1199 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001200 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001201 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001202 ObjCIvarDecl *Ivar = 0;
1203 if (!Synthesize)
1204 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1205 else {
1206 if (PropertyIvar && PropertyIvar != PropertyId)
1207 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1208 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001209 // Issue diagnostics only if Ivar belongs to current class.
1210 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001211 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001212 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1213 << PropertyId;
1214 Ivar->setInvalidDecl();
1215 }
1216 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001217 } else {
1218 if (Synthesize)
1219 if (ObjCPropertyImplDecl *PPIDecl =
1220 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001221 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001222 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1223 << PropertyIvar;
1224 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1225 }
1226
1227 if (ObjCPropertyImplDecl *PPIDecl =
1228 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001229 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001230 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001231 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001232 }
1233 CatImplClass->addPropertyImplementation(PIDecl);
1234 }
1235
John McCalld226f652010-08-21 09:40:31 +00001236 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001237}
1238
1239//===----------------------------------------------------------------------===//
1240// Helper methods.
1241//===----------------------------------------------------------------------===//
1242
Ted Kremenek9d64c152010-03-12 00:38:38 +00001243/// DiagnosePropertyMismatch - Compares two properties for their
1244/// attributes and types and warns on a variety of inconsistencies.
1245///
1246void
1247Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1248 ObjCPropertyDecl *SuperProperty,
1249 const IdentifierInfo *inheritedName) {
1250 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1251 Property->getPropertyAttributes();
1252 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1253 SuperProperty->getPropertyAttributes();
1254 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1255 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1256 Diag(Property->getLocation(), diag::warn_readonly_property)
1257 << Property->getDeclName() << inheritedName;
1258 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1259 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1260 Diag(Property->getLocation(), diag::warn_property_attribute)
1261 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001262 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001263 unsigned CAttrRetain =
1264 (CAttr &
1265 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1266 unsigned SAttrRetain =
1267 (SAttr &
1268 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1269 bool CStrong = (CAttrRetain != 0);
1270 bool SStrong = (SAttrRetain != 0);
1271 if (CStrong != SStrong)
1272 Diag(Property->getLocation(), diag::warn_property_attribute)
1273 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1274 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001275
1276 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1277 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
1278 Diag(Property->getLocation(), diag::warn_property_attribute)
1279 << Property->getDeclName() << "atomic" << inheritedName;
1280 if (Property->getSetterName() != SuperProperty->getSetterName())
1281 Diag(Property->getLocation(), diag::warn_property_attribute)
1282 << Property->getDeclName() << "setter" << inheritedName;
1283 if (Property->getGetterName() != SuperProperty->getGetterName())
1284 Diag(Property->getLocation(), diag::warn_property_attribute)
1285 << Property->getDeclName() << "getter" << inheritedName;
1286
1287 QualType LHSType =
1288 Context.getCanonicalType(SuperProperty->getType());
1289 QualType RHSType =
1290 Context.getCanonicalType(Property->getType());
1291
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001292 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001293 // Do cases not handled in above.
1294 // FIXME. For future support of covariant property types, revisit this.
1295 bool IncompatibleObjC = false;
1296 QualType ConvertedType;
1297 if (!isObjCPointerConversion(RHSType, LHSType,
1298 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001299 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001300 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1301 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001302 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1303 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001304 }
1305}
1306
1307bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1308 ObjCMethodDecl *GetterMethod,
1309 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001310 if (!GetterMethod)
1311 return false;
1312 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1313 QualType PropertyIvarType = property->getType().getNonReferenceType();
1314 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1315 if (!compat) {
1316 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1317 isa<ObjCObjectPointerType>(GetterType))
1318 compat =
1319 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001320 GetterType->getAs<ObjCObjectPointerType>(),
1321 PropertyIvarType->getAs<ObjCObjectPointerType>());
1322 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001323 != Compatible) {
1324 Diag(Loc, diag::error_property_accessor_type)
1325 << property->getDeclName() << PropertyIvarType
1326 << GetterMethod->getSelector() << GetterType;
1327 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1328 return true;
1329 } else {
1330 compat = true;
1331 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1332 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1333 if (lhsType != rhsType && lhsType->isArithmeticType())
1334 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335 }
1336 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001337
1338 if (!compat) {
1339 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1340 << property->getDeclName()
1341 << GetterMethod->getSelector();
1342 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1343 return true;
1344 }
1345
Ted Kremenek9d64c152010-03-12 00:38:38 +00001346 return false;
1347}
1348
Ted Kremenek9d64c152010-03-12 00:38:38 +00001349/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1350/// of properties declared in a protocol and compares their attribute against
1351/// the same property declared in the class or category.
1352void
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001353Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1354 if (!CDecl)
1355 return;
1356
1357 // Category case.
1358 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1359 // FIXME: We should perform this check when the property in the category
1360 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001361 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1362 if (!CatDecl->IsClassExtension())
1363 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1364 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001365 ObjCPropertyDecl *ProtoProp = *P;
1366 DeclContext::lookup_result R
1367 = CatDecl->lookup(ProtoProp->getDeclName());
1368 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1369 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1370 if (CatProp != ProtoProp) {
1371 // Property protocol already exist in class. Diagnose any mismatch.
1372 DiagnosePropertyMismatch(CatProp, ProtoProp,
1373 PDecl->getIdentifier());
1374 }
1375 }
1376 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001377 }
1378 return;
1379 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001380
1381 // Class
1382 // FIXME: We should perform this check when the property in the class
1383 // is declared.
1384 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001385 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001386 E = PDecl->prop_end(); P != E; ++P) {
1387 ObjCPropertyDecl *ProtoProp = *P;
1388 DeclContext::lookup_result R
Douglas Gregoraabd0942013-01-21 19:05:22 +00001389 = IDecl->lookup(ProtoProp->getDeclName());
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001390 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1391 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1392 if (ClassProp != ProtoProp) {
1393 // Property protocol already exist in class. Diagnose any mismatch.
1394 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1395 PDecl->getIdentifier());
1396 }
1397 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001398 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001399 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001400}
1401
Ted Kremenek9d64c152010-03-12 00:38:38 +00001402/// isPropertyReadonly - Return true if property is readonly, by searching
1403/// for the property in the class and in its categories and implementations
1404///
1405bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1406 ObjCInterfaceDecl *IDecl) {
1407 // by far the most common case.
1408 if (!PDecl->isReadOnly())
1409 return false;
1410 // Even if property is ready only, if interface has a user defined setter,
1411 // it is not considered read only.
1412 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1413 return false;
1414
1415 // Main class has the property as 'readonly'. Must search
1416 // through the category list to see if the property's
1417 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001418 for (ObjCInterfaceDecl::visible_categories_iterator
1419 Cat = IDecl->visible_categories_begin(),
1420 CatEnd = IDecl->visible_categories_end();
1421 Cat != CatEnd; ++Cat) {
1422 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001423 return false;
1424 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001425 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001426 if (P && !P->isReadOnly())
1427 return false;
1428 }
1429
1430 // Also, check for definition of a setter method in the implementation if
1431 // all else failed.
1432 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1433 if (ObjCImplementationDecl *IMD =
1434 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1435 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1436 return false;
1437 } else if (ObjCCategoryImplDecl *CIMD =
1438 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1439 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1440 return false;
1441 }
1442 }
1443 // Lastly, look through the implementation (if one is in scope).
1444 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1445 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1446 return false;
1447 // If all fails, look at the super class.
1448 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1449 return isPropertyReadonly(PDecl, SIDecl);
1450 return true;
1451}
1452
1453/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001454/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001455void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001456 ObjCContainerDecl::PropertyMap &PropMap,
1457 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001458 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1459 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1460 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001461 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001462 PropMap[Prop->getIdentifier()] = Prop;
1463 }
1464 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001465 for (ObjCInterfaceDecl::all_protocol_iterator
1466 PI = IDecl->all_referenced_protocol_begin(),
1467 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001468 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001469 }
1470 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1471 if (!CATDecl->IsClassExtension())
1472 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1473 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001474 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 PropMap[Prop->getIdentifier()] = Prop;
1476 }
1477 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001478 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001479 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001480 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001481 }
1482 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1483 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1484 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001485 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001486 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1487 // Exclude property for protocols which conform to class's super-class,
1488 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001489 if (!PropertyFromSuper ||
1490 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001491 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1492 if (!PropEntry)
1493 PropEntry = Prop;
1494 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001495 }
1496 // scan through protocol's protocols.
1497 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1498 E = PDecl->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}
1502
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001503/// CollectSuperClassPropertyImplementations - This routine collects list of
1504/// properties to be implemented in super class(s) and also coming from their
1505/// conforming protocols.
1506static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001507 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001508 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1509 while (SDecl) {
Anna Zaksb36ea372012-10-18 19:17:53 +00001510 SDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001511 SDecl = SDecl->getSuperClass();
1512 }
1513 }
1514}
1515
James Dennett699c9042012-06-15 07:13:21 +00001516/// \brief Default synthesizes all properties which must be synthesized
1517/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001518void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1519 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001520
Anna Zaksb36ea372012-10-18 19:17:53 +00001521 ObjCInterfaceDecl::PropertyMap PropMap;
1522 IDecl->collectPropertiesToImplement(PropMap);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001523 if (PropMap.empty())
1524 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001525 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001526 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1527
Anna Zaksb36ea372012-10-18 19:17:53 +00001528 for (ObjCInterfaceDecl::PropertyMap::iterator
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001529 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1530 ObjCPropertyDecl *Prop = P->second;
1531 // If property to be implemented in the super class, ignore.
1532 if (SuperPropMap[Prop->getIdentifier()])
1533 continue;
Anna Zaksb36ea372012-10-18 19:17:53 +00001534 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001535 if (Prop->isInvalidDecl() ||
1536 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1537 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1538 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001539 // Property may have been synthesized by user.
1540 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1541 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001542 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1543 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1544 continue;
1545 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1546 continue;
1547 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001548 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1549 // We won't auto-synthesize properties declared in protocols.
1550 Diag(IMPDecl->getLocation(),
1551 diag::warn_auto_synthesizing_protocol_property);
1552 Diag(Prop->getLocation(), diag::note_property_declare);
1553 continue;
1554 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001555
1556 // We use invalid SourceLocations for the synthesized ivars since they
1557 // aren't really synthesized at a particular location; they just exist.
1558 // Saying that they are located at the @implementation isn't really going
1559 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001560 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1561 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1562 true,
1563 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001564 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001565 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001566 if (PIDecl) {
1567 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001568 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001569 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001570 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001571}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001572
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001573void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001574 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001575 return;
1576 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1577 if (!IC)
1578 return;
1579 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001580 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001581 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001582}
1583
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001584void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001585 ObjCContainerDecl *CDecl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001586 const SelectorSet &InsMap) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001587 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1588 ObjCInterfaceDecl *IDecl;
1589 // Gather properties which need not be implemented in this class
1590 // or category.
1591 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1592 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1593 // For categories, no need to implement properties declared in
1594 // its primary class (and its super classes) if property is
1595 // declared in one of those containers.
1596 if ((IDecl = C->getClassInterface()))
1597 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap);
1598 }
1599 if (IDecl)
1600 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001601
Anna Zaksb36ea372012-10-18 19:17:53 +00001602 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001603 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001604 if (PropMap.empty())
1605 return;
1606
1607 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1608 for (ObjCImplDecl::propimpl_iterator
1609 I = IMPDecl->propimpl_begin(),
1610 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001611 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001612
Anna Zaksb36ea372012-10-18 19:17:53 +00001613 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001614 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1615 ObjCPropertyDecl *Prop = P->second;
1616 // Is there a matching propery synthesize/dynamic?
1617 if (Prop->isInvalidDecl() ||
1618 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001619 PropImplMap.count(Prop) ||
1620 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001621 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001622 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001623 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001624 isa<ObjCCategoryDecl>(CDecl) ?
1625 diag::warn_setter_getter_impl_required_in_category :
1626 diag::warn_setter_getter_impl_required)
1627 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001628 Diag(Prop->getLocation(),
1629 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001630 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001631 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001632 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001633 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1634
Ted Kremenek9d64c152010-03-12 00:38:38 +00001635 }
1636
1637 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001638 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001639 isa<ObjCCategoryDecl>(CDecl) ?
1640 diag::warn_setter_getter_impl_required_in_category :
1641 diag::warn_setter_getter_impl_required)
1642 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001643 Diag(Prop->getLocation(),
1644 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001645 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001646 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001647 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001648 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001649 }
1650 }
1651}
1652
1653void
1654Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1655 ObjCContainerDecl* IDecl) {
1656 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001657 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001658 return;
1659 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1660 E = IDecl->prop_end();
1661 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001662 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001663 ObjCMethodDecl *GetterMethod = 0;
1664 ObjCMethodDecl *SetterMethod = 0;
1665 bool LookedUpGetterSetter = false;
1666
Bill Wendlingad017fa2012-12-20 19:22:21 +00001667 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001668 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001669
John McCall265941b2011-09-13 18:31:23 +00001670 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1671 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001672 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1673 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1674 LookedUpGetterSetter = true;
1675 if (GetterMethod) {
1676 Diag(GetterMethod->getLocation(),
1677 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001678 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001679 Diag(Property->getLocation(), diag::note_property_declare);
1680 }
1681 if (SetterMethod) {
1682 Diag(SetterMethod->getLocation(),
1683 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001684 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001685 Diag(Property->getLocation(), diag::note_property_declare);
1686 }
1687 }
1688
Ted Kremenek9d64c152010-03-12 00:38:38 +00001689 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001690 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1691 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001692 continue;
1693 if (const ObjCPropertyImplDecl *PIDecl
1694 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1695 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1696 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001697 if (!LookedUpGetterSetter) {
1698 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1699 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1700 LookedUpGetterSetter = true;
1701 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001702 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1703 SourceLocation MethodLoc =
1704 (GetterMethod ? GetterMethod->getLocation()
1705 : SetterMethod->getLocation());
1706 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001707 << Property->getIdentifier() << (GetterMethod != 0)
1708 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001709 // fixit stuff.
1710 if (!AttributesAsWritten) {
1711 if (Property->getLParenLoc().isValid()) {
1712 // @property () ... case.
1713 SourceRange PropSourceRange(Property->getAtLoc(),
1714 Property->getLParenLoc());
1715 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1716 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1717 }
1718 else {
1719 //@property id etc.
1720 SourceLocation endLoc =
1721 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1722 endLoc = endLoc.getLocWithOffset(-1);
1723 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1724 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1725 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1726 }
1727 }
1728 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1729 // @property () ... case.
1730 SourceLocation endLoc = Property->getLParenLoc();
1731 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1732 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1733 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1734 }
1735 else
1736 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001737 Diag(Property->getLocation(), diag::note_property_declare);
1738 }
1739 }
1740 }
1741}
1742
John McCallf85e1932011-06-15 23:02:42 +00001743void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001744 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001745 return;
1746
1747 for (ObjCImplementationDecl::propimpl_iterator
1748 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001749 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001750 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1751 continue;
1752
1753 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001754 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1755 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001756 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1757 if (!method)
1758 continue;
1759 ObjCMethodFamily family = method->getMethodFamily();
1760 if (family == OMF_alloc || family == OMF_copy ||
1761 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001762 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001763 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1764 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001765 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001766 Diag(PD->getLocation(), diag::note_property_declare);
1767 }
1768 }
1769 }
1770}
1771
John McCall5de74d12010-11-10 07:01:40 +00001772/// AddPropertyAttrs - Propagates attributes from a property to the
1773/// implicitly-declared getter or setter for that property.
1774static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1775 ObjCPropertyDecl *Property) {
1776 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001777 for (Decl::attr_iterator A = Property->attr_begin(),
1778 AEnd = Property->attr_end();
1779 A != AEnd; ++A) {
1780 if (isa<DeprecatedAttr>(*A) ||
1781 isa<UnavailableAttr>(*A) ||
1782 isa<AvailabilityAttr>(*A))
1783 PropertyMethod->addAttr((*A)->clone(S.Context));
1784 }
John McCall5de74d12010-11-10 07:01:40 +00001785}
1786
Ted Kremenek9d64c152010-03-12 00:38:38 +00001787/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1788/// have the property type and issue diagnostics if they don't.
1789/// Also synthesize a getter/setter method if none exist (and update the
1790/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1791/// methods is the "right" thing to do.
1792void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001793 ObjCContainerDecl *CD,
1794 ObjCPropertyDecl *redeclaredProperty,
1795 ObjCContainerDecl *lexicalDC) {
1796
Ted Kremenek9d64c152010-03-12 00:38:38 +00001797 ObjCMethodDecl *GetterMethod, *SetterMethod;
1798
1799 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1800 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1801 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1802 property->getLocation());
1803
1804 if (SetterMethod) {
1805 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1806 property->getPropertyAttributes();
1807 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1808 Context.getCanonicalType(SetterMethod->getResultType()) !=
1809 Context.VoidTy)
1810 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1811 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001812 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001813 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1814 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001815 Diag(property->getLocation(),
1816 diag::warn_accessor_property_type_mismatch)
1817 << property->getDeclName()
1818 << SetterMethod->getSelector();
1819 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1820 }
1821 }
1822
1823 // Synthesize getter/setter methods if none exist.
1824 // Find the default getter and if one not found, add one.
1825 // FIXME: The synthesized property we set here is misleading. We almost always
1826 // synthesize these methods unless the user explicitly provided prototypes
1827 // (which is odd, but allowed). Sema should be typechecking that the
1828 // declarations jive in that situation (which it is not currently).
1829 if (!GetterMethod) {
1830 // No instance method of same name as property getter name was found.
1831 // Declare a getter method and add it to the list of methods
1832 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001833 SourceLocation Loc = redeclaredProperty ?
1834 redeclaredProperty->getLocation() :
1835 property->getLocation();
1836
1837 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1838 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001839 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001840 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001841 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001842 (property->getPropertyImplementation() ==
1843 ObjCPropertyDecl::Optional) ?
1844 ObjCMethodDecl::Optional :
1845 ObjCMethodDecl::Required);
1846 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001847
1848 AddPropertyAttrs(*this, GetterMethod, property);
1849
Ted Kremenek23173d72010-05-18 21:09:07 +00001850 // FIXME: Eventually this shouldn't be needed, as the lexical context
1851 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001852 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001853 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001854 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1855 GetterMethod->addAttr(
1856 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001857 } else
1858 // A user declared getter will be synthesize when @synthesize of
1859 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001860 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001861 property->setGetterMethodDecl(GetterMethod);
1862
1863 // Skip setter if property is read-only.
1864 if (!property->isReadOnly()) {
1865 // Find the default setter and if one not found, add one.
1866 if (!SetterMethod) {
1867 // No instance method of same name as property setter name was found.
1868 // Declare a setter method and add it to the list of methods
1869 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001870 SourceLocation Loc = redeclaredProperty ?
1871 redeclaredProperty->getLocation() :
1872 property->getLocation();
1873
1874 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001875 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001876 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001877 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00001878 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001879 /*isImplicitlyDeclared=*/true,
1880 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001881 (property->getPropertyImplementation() ==
1882 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001883 ObjCMethodDecl::Optional :
1884 ObjCMethodDecl::Required);
1885
Ted Kremenek9d64c152010-03-12 00:38:38 +00001886 // Invent the arguments for the setter. We don't bother making a
1887 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001888 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1889 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001890 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001891 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001892 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001893 SC_None,
1894 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001895 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001896 SetterMethod->setMethodParams(Context, Argument,
1897 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001898
1899 AddPropertyAttrs(*this, SetterMethod, property);
1900
Ted Kremenek9d64c152010-03-12 00:38:38 +00001901 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001902 // FIXME: Eventually this shouldn't be needed, as the lexical context
1903 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001904 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001905 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001906 } else
1907 // A user declared setter will be synthesize when @synthesize of
1908 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00001909 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001910 property->setSetterMethodDecl(SetterMethod);
1911 }
1912 // Add any synthesized methods to the global pool. This allows us to
1913 // handle the following, which is supported by GCC (and part of the design).
1914 //
1915 // @interface Foo
1916 // @property double bar;
1917 // @end
1918 //
1919 // void thisIsUnfortunate() {
1920 // id foo;
1921 // double bar = [foo bar];
1922 // }
1923 //
1924 if (GetterMethod)
1925 AddInstanceMethodToGlobalPool(GetterMethod);
1926 if (SetterMethod)
1927 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00001928
1929 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
1930 if (!CurrentClass) {
1931 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
1932 CurrentClass = Cat->getClassInterface();
1933 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
1934 CurrentClass = Impl->getClassInterface();
1935 }
1936 if (GetterMethod)
1937 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
1938 if (SetterMethod)
1939 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001940}
1941
John McCalld226f652010-08-21 09:40:31 +00001942void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001943 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00001944 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001945 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001946 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001947 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001948 return;
1949
1950 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001951 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001952
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00001954 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001955 PropertyTy->isObjCRetainableType()) {
1956 // 'readonly' property with no obvious lifetime.
1957 // its life time will be determined by its backing ivar.
1958 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
1959 ObjCDeclSpec::DQ_PR_copy |
1960 ObjCDeclSpec::DQ_PR_retain |
1961 ObjCDeclSpec::DQ_PR_strong |
1962 ObjCDeclSpec::DQ_PR_weak |
1963 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00001964 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00001965 return;
1966 }
1967
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001968 if (propertyInPrimaryClass) {
1969 // we postpone most property diagnosis until class's implementation
1970 // because, its readonly attribute may be overridden in its class
1971 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001972 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1973 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00001974 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1975 << "readonly" << "readwrite";
1976 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001977 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001978 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1979 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001980 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001981 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001982 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001983 ObjCDeclSpec::DQ_PR_retain |
1984 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00001985 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001986 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001987 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001988 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001989 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00001990 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00001991 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001992 "copy" : "retain";
1993
Bill Wendlingad017fa2012-12-20 19:22:21 +00001994 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00001995 diag::err_objc_property_attr_mutually_exclusive :
1996 diag::warn_objc_property_attr_mutually_exclusive)
1997 << "readonly" << which;
1998 }
1999
2000 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002001 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002002 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2003 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002004 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002005 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002006 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2007 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2008 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002009 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002010 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002011 }
2012
2013 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002014 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2015 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002016 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2017 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002018 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002019 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002020 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002021 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2022 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002023 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002024 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002025 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002026 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2027 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002028 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002029 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002030 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002031 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002032 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2033 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002034 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002035 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002036 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2037 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002038 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2039 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002040 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002041 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002042 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002043 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2044 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002045 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002046 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002047 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002048 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2049 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002050 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002051 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002052 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002053 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002054 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2055 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002056 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002057 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002058 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2059 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002060 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2061 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002062 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002063 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002064 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002065 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2066 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002067 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002068 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002069 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002070 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2071 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002072 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002073 }
2074 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002075 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2076 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002077 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2078 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002079 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002080 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002081 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2082 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002083 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2084 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002085 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002086 }
2087
Bill Wendlingad017fa2012-12-20 19:22:21 +00002088 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2089 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002090 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2091 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002092 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002093 }
2094
Ted Kremenek9d64c152010-03-12 00:38:38 +00002095 // Warn if user supplied no assignment attribute, property is
2096 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002097 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002098 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2099 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2100 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002101 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002102 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002103 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002104 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002105 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002106 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002107 bool isAnyClassTy =
2108 (PropertyTy->isObjCClassType() ||
2109 PropertyTy->isObjCQualifiedClassType());
2110 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2111 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002112 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002113 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002114 else if (propertyInPrimaryClass) {
2115 // Don't issue warning on property with no life time in class
2116 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002117 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002118 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002119 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002120
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002121 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002122 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002123 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002124 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002125 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002126
2127 // FIXME: Implement warning dependent on NSCopying being
2128 // implemented. See also:
2129 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2130 // (please trim this list while you are at it).
2131 }
2132
Bill Wendlingad017fa2012-12-20 19:22:21 +00002133 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2134 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002135 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002136 && PropertyTy->isBlockPointerType())
2137 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002138 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2139 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2140 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002141 PropertyTy->isBlockPointerType())
2142 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002143
Bill Wendlingad017fa2012-12-20 19:22:21 +00002144 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2145 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002146 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2147
Ted Kremenek9d64c152010-03-12 00:38:38 +00002148}