blob: e49edc908aa099e3fc7c745f661eec477486b699 [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 Jahanianb7b25652013-02-10 00:16:04 +0000371 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
372 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
373 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000375 // Set setter/getter selector name. Needed later.
376 PDecl->setGetterName(GetterSel);
377 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000378 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000379 DC->addDecl(PDecl);
380
381 // We need to look in the @interface to see if the @property was
382 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000383 if (!CCPrimary) {
384 Diag(CDecl->getLocation(), diag::err_continuation_class);
385 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000386 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000387 }
388
389 // Find the property in continuation class's primary class only.
390 ObjCPropertyDecl *PIDecl =
391 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
392
393 if (!PIDecl) {
394 // No matching property found in the primary class. Just fall thru
395 // and add property to continuation class's primary class.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000396 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000397 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000398 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000399 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000400
401 // A case of continuation class adding a new property in the class. This
402 // is not what it was meant for. However, gcc supports it and so should we.
403 // Make sure setter/getters are declared here.
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000404 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremeneka054fb42010-09-21 20:52:59 +0000405 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000406 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
407 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000408 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000409 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
410 return PrimaryPDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000411 }
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000412 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
413 bool IncompatibleObjC = false;
414 QualType ConvertedType;
Fariborz Jahanianff2a0ec2012-02-02 19:34:05 +0000415 // Relax the strict type matching for property type in continuation class.
416 // Allow property object type of continuation class to be different as long
Fariborz Jahanianad7eff22012-02-02 22:37:48 +0000417 // as it narrows the object type in its primary class property. Note that
418 // this conversion is safe only because the wider type is for a 'readonly'
419 // property in primary class and 'narrowed' type for a 'readwrite' property
420 // in continuation class.
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000421 if (!isa<ObjCObjectPointerType>(PIDecl->getType()) ||
422 !isa<ObjCObjectPointerType>(PDecl->getType()) ||
423 (!isObjCPointerConversion(PDecl->getType(), PIDecl->getType(),
424 ConvertedType, IncompatibleObjC))
425 || IncompatibleObjC) {
426 Diag(AtLoc,
427 diag::err_type_mismatch_continuation_class) << PDecl->getType();
428 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000429 return 0;
Fariborz Jahaniane2351832012-02-02 18:54:58 +0000430 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000431 }
432
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000433 // The property 'PIDecl's readonly attribute will be over-ridden
434 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000435 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000436 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000437 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendlingad017fa2012-12-20 19:22:21 +0000438 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahaniand9f95b32012-08-21 21:52:02 +0000439 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahaniandad633b2012-08-21 21:45:58 +0000440 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
441 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000442 Diag(AtLoc, diag::warn_property_attr_mismatch);
443 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000444 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000445 DeclContext *DC = cast<DeclContext>(CCPrimary);
446 if (!ObjCPropertyDecl::findPropertyDecl(DC,
447 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000448 // Protocol is not in the primary class. Must build one for it.
449 ObjCDeclSpec ProtocolPropertyODS;
450 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
451 // and ObjCPropertyDecl::PropertyAttributeKind have identical
452 // values. Should consolidate both into one enum type.
453 ProtocolPropertyODS.
454 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
455 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000456 // Must re-establish the context from class extension to primary
457 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000458 ContextRAII SavedContext(*this, CCPrimary);
459
John McCalld226f652010-08-21 09:40:31 +0000460 Decl *ProtocolPtrTy =
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000461 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000462 PIDecl->getGetterName(),
463 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000464 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000465 MethodImplKind,
466 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000467 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000468 }
469 PIDecl->makeitReadWriteAttribute();
Bill Wendlingad017fa2012-12-20 19:22:21 +0000470 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000471 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000472 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000473 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000474 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000475 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
476 PIDecl->setSetterName(SetterSel);
477 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000478 // Tailor the diagnostics for the common case where a readwrite
479 // property is declared both in the @interface and the continuation.
480 // This is a common error where the user often intended the original
481 // declaration to be readonly.
482 unsigned diag =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000483 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek788f4892010-10-21 18:49:42 +0000484 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
485 ? diag::err_use_continuation_class_redeclaration_readwrite
486 : diag::err_use_continuation_class;
487 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000488 << CCPrimary->getDeclName();
489 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000490 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000491 }
492 *isOverridingProperty = true;
493 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000494 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisd7c15a62012-02-28 17:50:28 +0000495 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
496 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidisc80553e2011-11-14 04:52:29 +0000497 if (ASTMutationListener *L = Context.getASTMutationListener())
498 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanian6defd9f2012-09-17 20:57:19 +0000499 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000500}
501
502ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
503 ObjCContainerDecl *CDecl,
504 SourceLocation AtLoc,
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000505 SourceLocation LParenLoc,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000506 FieldDeclarator &FD,
507 Selector GetterSel,
508 Selector SetterSel,
509 const bool isAssign,
510 const bool isReadWrite,
Bill Wendlingad017fa2012-12-20 19:22:21 +0000511 const unsigned Attributes,
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000512 const unsigned AttributesAsWritten,
John McCall83a230c2010-06-04 20:50:08 +0000513 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000514 tok::ObjCKeywordKind MethodImplKind,
515 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000516 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000517 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000518
519 // Issue a warning if property is 'assign' as default and its object, which is
520 // gc'able conforms to NSCopying protocol
David Blaikie4e4d0842012-03-11 07:00:24 +0000521 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendlingad017fa2012-12-20 19:22:21 +0000522 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000523 if (const ObjCObjectPointerType *ObjPtrTy =
524 T->getAs<ObjCObjectPointerType>()) {
525 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
526 if (IDecl)
527 if (ObjCProtocolDecl* PNSCopying =
528 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
529 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
530 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000531 }
John McCallc12c5bb2010-05-15 11:32:37 +0000532 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000533 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
534
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000535 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000536 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
537 FD.D.getIdentifierLoc(),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +0000538 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000539
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000540 if (ObjCPropertyDecl *prevDecl =
541 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000542 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000543 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000544 PDecl->setInvalidDecl();
545 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000546 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000547 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000548 if (lexicalDC)
549 PDecl->setLexicalDeclContext(lexicalDC);
550 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000551
552 if (T->isArrayType() || T->isFunctionType()) {
553 Diag(AtLoc, diag::err_property_type) << T;
554 PDecl->setInvalidDecl();
555 }
556
557 ProcessDeclAttributes(S, PDecl, FD.D);
558
559 // Regardless of setter/getter attribute, we save the default getter/setter
560 // selector names in anticipation of declaration of setter/getter methods.
561 PDecl->setGetterName(GetterSel);
562 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000563 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisdbbdec92011-11-06 18:58:12 +0000564 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000565
Bill Wendlingad017fa2012-12-20 19:22:21 +0000566 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000567 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
568
Bill Wendlingad017fa2012-12-20 19:22:21 +0000569 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000570 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
571
Bill Wendlingad017fa2012-12-20 19:22:21 +0000572 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000573 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
574
575 if (isReadWrite)
576 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
577
Bill Wendlingad017fa2012-12-20 19:22:21 +0000578 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000579 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
580
Bill Wendlingad017fa2012-12-20 19:22:21 +0000581 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCallf85e1932011-06-15 23:02:42 +0000582 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
583
Bill Wendlingad017fa2012-12-20 19:22:21 +0000584 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCallf85e1932011-06-15 23:02:42 +0000585 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
586
Bill Wendlingad017fa2012-12-20 19:22:21 +0000587 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000588 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
589
Bill Wendlingad017fa2012-12-20 19:22:21 +0000590 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000591 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
592
Ted Kremenek28685ab2010-03-12 00:46:40 +0000593 if (isAssign)
594 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
595
John McCall265941b2011-09-13 18:31:23 +0000596 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000597 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenek28685ab2010-03-12 00:46:40 +0000598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000599 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000600 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000601
John McCallf85e1932011-06-15 23:02:42 +0000602 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000603 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCallf85e1932011-06-15 23:02:42 +0000604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
605 if (isAssign)
606 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
607
Ted Kremenek28685ab2010-03-12 00:46:40 +0000608 if (MethodImplKind == tok::objc_required)
609 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
610 else if (MethodImplKind == tok::objc_optional)
611 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000612
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000613 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000614}
615
John McCallf85e1932011-06-15 23:02:42 +0000616static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
617 ObjCPropertyDecl *property,
618 ObjCIvarDecl *ivar) {
619 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
620
John McCallf85e1932011-06-15 23:02:42 +0000621 QualType ivarType = ivar->getType();
622 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000623
John McCall265941b2011-09-13 18:31:23 +0000624 // The lifetime implied by the property's attributes.
625 Qualifiers::ObjCLifetime propertyLifetime =
626 getImpliedARCOwnership(property->getPropertyAttributes(),
627 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000628
John McCall265941b2011-09-13 18:31:23 +0000629 // We're fine if they match.
630 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000631
John McCall265941b2011-09-13 18:31:23 +0000632 // These aren't valid lifetimes for object ivars; don't diagnose twice.
633 if (ivarLifetime == Qualifiers::OCL_None ||
634 ivarLifetime == Qualifiers::OCL_Autoreleasing)
635 return;
John McCallf85e1932011-06-15 23:02:42 +0000636
John McCalld64c2eb2012-08-20 23:36:59 +0000637 // If the ivar is private, and it's implicitly __unsafe_unretained
638 // becaues of its type, then pretend it was actually implicitly
639 // __strong. This is only sound because we're processing the
640 // property implementation before parsing any method bodies.
641 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
642 propertyLifetime == Qualifiers::OCL_Strong &&
643 ivar->getAccessControl() == ObjCIvarDecl::Private) {
644 SplitQualType split = ivarType.split();
645 if (split.Quals.hasObjCLifetime()) {
646 assert(ivarType->isObjCARCImplicitlyUnretainedType());
647 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
648 ivarType = S.Context.getQualifiedType(split);
649 ivar->setType(ivarType);
650 return;
651 }
652 }
653
John McCall265941b2011-09-13 18:31:23 +0000654 switch (propertyLifetime) {
655 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000656 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000657 << property->getDeclName()
658 << ivar->getDeclName()
659 << ivarLifetime;
660 break;
John McCallf85e1932011-06-15 23:02:42 +0000661
John McCall265941b2011-09-13 18:31:23 +0000662 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000663 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall265941b2011-09-13 18:31:23 +0000664 << property->getDeclName()
665 << ivar->getDeclName();
666 break;
John McCallf85e1932011-06-15 23:02:42 +0000667
John McCall265941b2011-09-13 18:31:23 +0000668 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000669 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall265941b2011-09-13 18:31:23 +0000670 << property->getDeclName()
671 << ivar->getDeclName()
672 << ((property->getPropertyAttributesAsWritten()
673 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
674 break;
John McCallf85e1932011-06-15 23:02:42 +0000675
John McCall265941b2011-09-13 18:31:23 +0000676 case Qualifiers::OCL_Autoreleasing:
677 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000678
John McCall265941b2011-09-13 18:31:23 +0000679 case Qualifiers::OCL_None:
680 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000681 return;
682 }
683
684 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidis135aa602012-12-12 22:48:25 +0000685 if (propertyImplLoc.isValid())
686 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCallf85e1932011-06-15 23:02:42 +0000687}
688
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000689/// setImpliedPropertyAttributeForReadOnlyProperty -
690/// This routine evaludates life-time attributes for a 'readonly'
691/// property with no known lifetime of its own, using backing
692/// 'ivar's attribute, if any. If no backing 'ivar', property's
693/// life-time is assumed 'strong'.
694static void setImpliedPropertyAttributeForReadOnlyProperty(
695 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
696 Qualifiers::ObjCLifetime propertyLifetime =
697 getImpliedARCOwnership(property->getPropertyAttributes(),
698 property->getType());
699 if (propertyLifetime != Qualifiers::OCL_None)
700 return;
701
702 if (!ivar) {
703 // if no backing ivar, make property 'strong'.
704 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
705 return;
706 }
707 // property assumes owenership of backing ivar.
708 QualType ivarType = ivar->getType();
709 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
710 if (ivarLifetime == Qualifiers::OCL_Strong)
711 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
712 else if (ivarLifetime == Qualifiers::OCL_Weak)
713 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
714 return;
715}
Ted Kremenek28685ab2010-03-12 00:46:40 +0000716
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000717/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
718/// in inherited protocols with mismatched types. Since any of them can
719/// be candidate for synthesis.
720void DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
721 ObjCInterfaceDecl *ClassDecl,
722 ObjCPropertyDecl *Property) {
723 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
724 for (ObjCInterfaceDecl::all_protocol_iterator
725 PI = ClassDecl->all_referenced_protocol_begin(),
726 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
727 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
728 PDecl->collectInheritedProtocolProperties(Property, PropMap);
729 }
730 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
731 while (SDecl) {
732 for (ObjCInterfaceDecl::all_protocol_iterator
733 PI = SDecl->all_referenced_protocol_begin(),
734 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
735 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
736 PDecl->collectInheritedProtocolProperties(Property, PropMap);
737 }
738 SDecl = SDecl->getSuperClass();
739 }
740
741 if (PropMap.empty())
742 return;
743
744 QualType RHSType = S.Context.getCanonicalType(Property->getType());
745 bool FirsTime = true;
746 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
747 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
748 ObjCPropertyDecl *Prop = I->second;
749 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
750 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
751 bool IncompatibleObjC = false;
752 QualType ConvertedType;
753 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
754 || IncompatibleObjC) {
755 if (FirsTime) {
756 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
757 << Property->getType();
758 FirsTime = false;
759 }
760 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
761 << Prop->getType();
762 }
763 }
764 }
765 if (!FirsTime && AtLoc.isValid())
766 S.Diag(AtLoc, diag::note_property_synthesize);
767}
768
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000769/// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property
770/// attribute declared in primary class and attributes overridden in any of its
771/// class extensions.
772static void
773DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl,
774 ObjCPropertyDecl *property) {
Bill Wendlingad017fa2012-12-20 19:22:21 +0000775 unsigned Attributes = property->getPropertyAttributesAsWritten();
776 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly);
Douglas Gregord3297242013-01-16 23:00:23 +0000777 for (ObjCInterfaceDecl::known_extensions_iterator
778 Ext = ClassDecl->known_extensions_begin(),
779 ExtEnd = ClassDecl->known_extensions_end();
780 Ext != ExtEnd; ++Ext) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000781 ObjCPropertyDecl *ClassExtProperty = 0;
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000782 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
783 for (unsigned I = 0, N = R.size(); I != N; ++I) {
784 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]);
785 if (ClassExtProperty)
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000786 break;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000787 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +0000788
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000789 if (ClassExtProperty) {
Fariborz Jahanianc78ff272012-06-20 23:18:57 +0000790 warn = false;
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000791 unsigned classExtPropertyAttr =
792 ClassExtProperty->getPropertyAttributesAsWritten();
793 // We are issuing the warning that we postponed because class extensions
794 // can override readonly->readwrite and 'setter' attributes originally
795 // placed on class's property declaration now make sense in the overridden
796 // property.
Bill Wendlingad017fa2012-12-20 19:22:21 +0000797 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000798 if (!classExtPropertyAttr ||
Fariborz Jahaniana6e28f22012-08-24 20:10:53 +0000799 (classExtPropertyAttr &
800 (ObjCDeclSpec::DQ_PR_readwrite|
801 ObjCDeclSpec::DQ_PR_assign |
802 ObjCDeclSpec::DQ_PR_unsafe_unretained |
803 ObjCDeclSpec::DQ_PR_copy |
804 ObjCDeclSpec::DQ_PR_retain |
805 ObjCDeclSpec::DQ_PR_strong)))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000806 continue;
807 warn = true;
808 break;
809 }
810 }
811 }
812 if (warn) {
813 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign |
814 ObjCDeclSpec::DQ_PR_unsafe_unretained |
815 ObjCDeclSpec::DQ_PR_copy |
816 ObjCDeclSpec::DQ_PR_retain |
817 ObjCDeclSpec::DQ_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +0000818 if (Attributes & setterAttrs) {
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000819 const char * which =
Bill Wendlingad017fa2012-12-20 19:22:21 +0000820 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000821 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000822 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000823 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000824 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000825 "copy" :
Bill Wendlingad017fa2012-12-20 19:22:21 +0000826 (Attributes & ObjCDeclSpec::DQ_PR_retain) ?
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000827 "retain" : "strong";
828
829 S.Diag(property->getLocation(),
830 diag::warn_objc_property_attr_mutually_exclusive)
831 << "readonly" << which;
832 }
833 }
834
835
836}
837
Ted Kremenek28685ab2010-03-12 00:46:40 +0000838/// ActOnPropertyImplDecl - This routine performs semantic checks and
839/// builds the AST node for a property implementation declaration; declared
James Dennett699c9042012-06-15 07:13:21 +0000840/// as \@synthesize or \@dynamic.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000841///
John McCalld226f652010-08-21 09:40:31 +0000842Decl *Sema::ActOnPropertyImplDecl(Scope *S,
843 SourceLocation AtLoc,
844 SourceLocation PropertyLoc,
845 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000846 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000847 IdentifierInfo *PropertyIvar,
848 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000849 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000850 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000851 // Make sure we have a context for the property implementation declaration.
852 if (!ClassImpDecl) {
853 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000854 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000855 }
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +0000856 if (PropertyIvarLoc.isInvalid())
857 PropertyIvarLoc = PropertyLoc;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000858 SourceLocation PropertyDiagLoc = PropertyLoc;
859 if (PropertyDiagLoc.isInvalid())
860 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000861 ObjCPropertyDecl *property = 0;
862 ObjCInterfaceDecl* IDecl = 0;
863 // Find the class or category class where this property must have
864 // a declaration.
865 ObjCImplementationDecl *IC = 0;
866 ObjCCategoryImplDecl* CatImplClass = 0;
867 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
868 IDecl = IC->getClassInterface();
869 // We always synthesize an interface for an implementation
870 // without an interface decl. So, IDecl is always non-zero.
871 assert(IDecl &&
872 "ActOnPropertyImplDecl - @implementation without @interface");
873
874 // Look for this property declaration in the @implementation's @interface
875 property = IDecl->FindPropertyDeclaration(PropertyId);
876 if (!property) {
877 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000878 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000879 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000880 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000881 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
882 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000883 if (AtLoc.isValid())
884 Diag(AtLoc, diag::warn_implicit_atomic_property);
885 else
886 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
887 Diag(property->getLocation(), diag::note_property_declare);
888 }
889
Ted Kremenek28685ab2010-03-12 00:46:40 +0000890 if (const ObjCCategoryDecl *CD =
891 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
892 if (!CD->IsClassExtension()) {
893 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
894 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000895 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000896 }
897 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000898 if (Synthesize&&
899 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
900 property->hasAttr<IBOutletAttr>() &&
901 !AtLoc.isValid()) {
Fariborz Jahanian12564342013-02-08 23:32:30 +0000902 bool ReadWriteProperty = false;
903 // Search into the class extensions and see if 'readonly property is
904 // redeclared 'readwrite', then no warning is to be issued.
905 for (ObjCInterfaceDecl::known_extensions_iterator
906 Ext = IDecl->known_extensions_begin(),
907 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
908 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
909 if (!R.empty())
910 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
911 PIkind = ExtProp->getPropertyAttributesAsWritten();
912 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
913 ReadWriteProperty = true;
914 break;
915 }
916 }
917 }
918
919 if (!ReadWriteProperty) {
Ted Kremeneka4475a62013-02-09 07:13:16 +0000920 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
921 << property->getName();
Fariborz Jahanian12564342013-02-08 23:32:30 +0000922 SourceLocation readonlyLoc;
923 if (LocPropertyAttribute(Context, "readonly",
924 property->getLParenLoc(), readonlyLoc)) {
925 SourceLocation endLoc =
926 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
927 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
928 Diag(property->getLocation(),
929 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
930 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
931 }
Fariborz Jahanianedcc27f2012-05-21 17:02:43 +0000932 }
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000933 }
Fariborz Jahanian8dbda512013-05-20 21:20:24 +0000934 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
935 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahaniancea06d22012-06-20 22:57:42 +0000936
937 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property);
Fariborz Jahanian2b309fb2012-05-19 18:17:17 +0000938
Ted Kremenek28685ab2010-03-12 00:46:40 +0000939 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
940 if (Synthesize) {
941 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000942 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000943 }
944 IDecl = CatImplClass->getClassInterface();
945 if (!IDecl) {
946 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000947 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000948 }
949 ObjCCategoryDecl *Category =
950 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
951
952 // If category for this implementation not found, it is an error which
953 // has already been reported eralier.
954 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000955 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000956 // Look for this property declaration in @implementation's category
957 property = Category->FindPropertyDeclaration(PropertyId);
958 if (!property) {
959 Diag(PropertyLoc, diag::error_bad_category_property_decl)
960 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000961 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000962 }
963 } else {
964 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000965 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000966 }
967 ObjCIvarDecl *Ivar = 0;
Eli Friedmane4c043d2012-05-01 22:26:06 +0000968 bool CompleteTypeErr = false;
Fariborz Jahanian74414712012-05-15 18:12:51 +0000969 bool compat = true;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000970 // Check that we have a valid, previously declared ivar for @synthesize
971 if (Synthesize) {
972 // @synthesize
973 if (!PropertyIvar)
974 PropertyIvar = PropertyId;
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000975 // Check that this is a previously declared 'ivar' in 'IDecl' interface
976 ObjCInterfaceDecl *ClassDeclared;
977 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
978 QualType PropType = property->getType();
979 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedmane4c043d2012-05-01 22:26:06 +0000980
981 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregord10099e2012-05-04 16:32:21 +0000982 diag::err_incomplete_synthesized_property,
983 property->getDeclName())) {
Eli Friedmane4c043d2012-05-01 22:26:06 +0000984 Diag(property->getLocation(), diag::note_property_declare);
985 CompleteTypeErr = true;
986 }
987
David Blaikie4e4d0842012-03-11 07:00:24 +0000988 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000989 (property->getPropertyAttributesAsWritten() &
Fariborz Jahanian3efd3482012-01-11 19:48:08 +0000990 ObjCPropertyDecl::OBJC_PR_readonly) &&
991 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian015f6082012-01-11 18:26:06 +0000992 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
993 }
994
John McCallf85e1932011-06-15 23:02:42 +0000995 ObjCPropertyDecl::PropertyAttributeKind kind
996 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000997
998 // Add GC __weak to the ivar type if the property is weak.
999 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001000 getLangOpts().getGC() != LangOptions::NonGC) {
1001 assert(!getLangOpts().ObjCAutoRefCount);
John McCall265941b2011-09-13 18:31:23 +00001002 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001003 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall265941b2011-09-13 18:31:23 +00001004 Diag(property->getLocation(), diag::note_property_declare);
1005 } else {
1006 PropertyIvarType =
1007 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001008 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001009 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001010 if (AtLoc.isInvalid()) {
1011 // Check when default synthesizing a property that there is
1012 // an ivar matching property name and issue warning; since this
1013 // is the most common case of not using an ivar used for backing
1014 // property in non-default synthesis case.
1015 ObjCInterfaceDecl *ClassDeclared=0;
1016 ObjCIvarDecl *originalIvar =
1017 IDecl->lookupInstanceVariable(property->getIdentifier(),
1018 ClassDeclared);
1019 if (originalIvar) {
1020 Diag(PropertyDiagLoc,
1021 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian25785322012-06-29 19:05:11 +00001022 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian20e7d992012-06-29 18:43:30 +00001023 << originalIvar->getIdentifier();
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001024 Diag(property->getLocation(), diag::note_property_declare);
1025 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahaniandd3284b2012-06-19 22:51:22 +00001026 }
Fariborz Jahaniane95f8ef2012-06-20 17:18:31 +00001027 }
1028
1029 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +00001030 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +00001031 // property attributes.
David Blaikie4e4d0842012-03-11 07:00:24 +00001032 if (getLangOpts().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +00001033 !PropertyIvarType.getObjCLifetime() &&
1034 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +00001035
John McCall265941b2011-09-13 18:31:23 +00001036 // It's an error if we have to do this and the user didn't
1037 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001038 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +00001039 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001040 Diag(PropertyDiagLoc,
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +00001041 diag::err_arc_objc_property_default_assign_on_object);
1042 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +00001043 } else {
1044 Qualifiers::ObjCLifetime lifetime =
1045 getImpliedARCOwnership(kind, PropertyIvarType);
1046 assert(lifetime && "no lifetime for property?");
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001047 if (lifetime == Qualifiers::OCL_Weak) {
1048 bool err = false;
1049 if (const ObjCObjectPointerType *ObjT =
Richard Smitha8eaf002012-08-23 06:16:52 +00001050 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1051 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1052 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian80abce32013-04-24 19:13:05 +00001053 Diag(property->getLocation(),
1054 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1055 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1056 << ClassImpDecl->getName();
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001057 err = true;
1058 }
Richard Smitha8eaf002012-08-23 06:16:52 +00001059 }
John McCall0a7dd782012-08-21 02:47:43 +00001060 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001061 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001062 Diag(property->getLocation(), diag::note_property_declare);
1063 }
John McCallf85e1932011-06-15 23:02:42 +00001064 }
Fariborz Jahanian6dce88d2011-12-09 19:55:11 +00001065
John McCallf85e1932011-06-15 23:02:42 +00001066 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +00001067 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +00001068 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1069 }
John McCallf85e1932011-06-15 23:02:42 +00001070 }
1071
1072 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001073 !getLangOpts().ObjCAutoRefCount &&
1074 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001075 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCallf85e1932011-06-15 23:02:42 +00001076 Diag(property->getLocation(), diag::note_property_declare);
1077 }
1078
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001079 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001080 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +00001081 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +00001082 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001083 (Expr *)0, true);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001084 if (CompleteTypeErr)
1085 Ivar->setInvalidDecl();
Daniel Dunbar29fa69a2010-04-02 19:44:54 +00001086 ClassImpDecl->addDecl(Ivar);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001087 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001088
John McCall260611a2012-06-20 06:18:46 +00001089 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedmane4c043d2012-05-01 22:26:06 +00001090 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1091 << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001092 // Note! I deliberately want it to fall thru so, we have a
1093 // a property implementation and to avoid future warnings.
John McCall260611a2012-06-20 06:18:46 +00001094 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001095 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001096 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001097 << property->getDeclName() << Ivar->getDeclName()
1098 << ClassDeclared->getDeclName();
1099 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +00001100 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +00001101 // Note! I deliberately want it to fall thru so more errors are caught.
1102 }
Anna Zaks5bf5c2e2012-09-26 18:55:16 +00001103 property->setPropertyIvarDecl(Ivar);
1104
Ted Kremenek28685ab2010-03-12 00:46:40 +00001105 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1106
1107 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian74414712012-05-15 18:12:51 +00001108 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanian14086762011-03-28 23:47:18 +00001109 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +00001110 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithb3cd3c02012-09-14 18:27:01 +00001111 compat =
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001112 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +00001113 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001114 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +00001115 else {
Argyrios Kyrtzidisf9112422012-02-28 17:50:39 +00001116 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1117 IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +00001118 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +00001119 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +00001120 if (!compat) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001121 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +00001122 << property->getDeclName() << PropType
1123 << Ivar->getDeclName() << IvarType;
1124 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001125 // Note! I deliberately want it to fall thru so, we have a
1126 // a property implementation and to avoid future warnings.
1127 }
Fariborz Jahanian74414712012-05-15 18:12:51 +00001128 else {
1129 // FIXME! Rules for properties are somewhat different that those
1130 // for assignments. Use a new routine to consolidate all cases;
1131 // specifically for property redeclarations as well as for ivars.
1132 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1133 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1134 if (lhsType != rhsType &&
1135 lhsType->isArithmeticType()) {
1136 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1137 << property->getDeclName() << PropType
1138 << Ivar->getDeclName() << IvarType;
1139 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1140 // Fall thru - see previous comment
1141 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001142 }
1143 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +00001144 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001145 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001146 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001147 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +00001148 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001149 // Fall thru - see previous comment
1150 }
John McCallf85e1932011-06-15 23:02:42 +00001151 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +00001152 if ((property->getType()->isObjCObjectPointerType() ||
1153 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikie4e4d0842012-03-11 07:00:24 +00001154 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001155 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001156 << property->getDeclName() << Ivar->getDeclName();
1157 // Fall thru - see previous comment
1158 }
1159 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001160 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001161 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +00001162 } else if (PropertyIvar)
1163 // @dynamic
Eli Friedmane4c043d2012-05-01 22:26:06 +00001164 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +00001165
Ted Kremenek28685ab2010-03-12 00:46:40 +00001166 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1167 ObjCPropertyImplDecl *PIDecl =
1168 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1169 property,
1170 (Synthesize ?
1171 ObjCPropertyImplDecl::Synthesize
1172 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001173 Ivar, PropertyIvarLoc);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001174
Fariborz Jahanian74414712012-05-15 18:12:51 +00001175 if (CompleteTypeErr || !compat)
Eli Friedmane4c043d2012-05-01 22:26:06 +00001176 PIDecl->setInvalidDecl();
1177
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001178 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1179 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001180 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahanian0313f442010-10-15 22:42:59 +00001181 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001182 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1183 // returned by the getter as it must conform to C++'s copy-return rules.
1184 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001185 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001186 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1187 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001188 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001189 VK_RValue, PropertyDiagLoc);
1190 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001191 Expr *IvarRefExpr =
Eli Friedman9a14db32012-10-18 20:14:08 +00001192 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001193 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001194 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +00001195 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001196 PerformCopyInitialization(InitializedEntity::InitializeResult(
Eli Friedman9a14db32012-10-18 20:14:08 +00001197 PropertyDiagLoc,
Douglas Gregor3c9034c2010-05-15 00:13:29 +00001198 getterMethod->getResultType(),
1199 /*NRVO=*/false),
Eli Friedman9a14db32012-10-18 20:14:08 +00001200 PropertyDiagLoc,
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001201 Owned(IvarRefExpr));
1202 if (!Res.isInvalid()) {
1203 Expr *ResExpr = Res.takeAs<Expr>();
1204 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +00001205 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001206 PIDecl->setGetterCXXConstructor(ResExpr);
1207 }
1208 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001209 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1210 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1211 Diag(getterMethod->getLocation(),
1212 diag::warn_property_getter_owning_mismatch);
1213 Diag(property->getLocation(), diag::note_property_declare);
1214 }
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +00001215 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1216 switch (getterMethod->getMethodFamily()) {
1217 case OMF_retain:
1218 case OMF_retainCount:
1219 case OMF_release:
1220 case OMF_autorelease:
1221 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1222 << 1 << getterMethod->getSelector();
1223 break;
1224 default:
1225 break;
1226 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001227 }
1228 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1229 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedmane4c043d2012-05-01 22:26:06 +00001230 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1231 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001232 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedman9a14db32012-10-18 20:14:08 +00001233 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001234 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1235 DeclRefExpr *SelfExpr =
John McCallf4b88a42012-03-10 09:33:50 +00001236 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Eli Friedman9a14db32012-10-18 20:14:08 +00001237 VK_RValue, PropertyDiagLoc);
1238 MarkDeclRefReferenced(SelfExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001239 Expr *lhs =
Eli Friedman9a14db32012-10-18 20:14:08 +00001240 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanian0c701812013-04-02 18:57:54 +00001241 Ivar->getLocation(),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001242 SelfExpr, true, true);
1243 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1244 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +00001245 QualType T = Param->getType().getNonReferenceType();
Eli Friedman9a14db32012-10-18 20:14:08 +00001246 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1247 VK_LValue, PropertyDiagLoc);
1248 MarkDeclRefReferenced(rhs);
1249 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCall2de56d12010-08-25 11:45:40 +00001250 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001251 if (property->getPropertyAttributes() &
1252 ObjCPropertyDecl::OBJC_PR_atomic) {
1253 Expr *callExpr = Res.takeAs<Expr>();
1254 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +00001255 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1256 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001257 if (!FuncDecl->isTrivial())
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001258 if (property->getType()->isReferenceType()) {
Eli Friedman9a14db32012-10-18 20:14:08 +00001259 Diag(PropertyDiagLoc,
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001260 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001261 << property->getType();
Fariborz Jahanian20abee62012-01-10 00:37:01 +00001262 Diag(FuncDecl->getLocStart(),
1263 diag::note_callee_decl) << FuncDecl;
1264 }
Fariborz Jahanian57e264e2011-10-06 18:38:18 +00001265 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001266 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1267 }
1268 }
1269
Ted Kremenek28685ab2010-03-12 00:46:40 +00001270 if (IC) {
1271 if (Synthesize)
1272 if (ObjCPropertyImplDecl *PPIDecl =
1273 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1274 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1275 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1276 << PropertyIvar;
1277 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1278 }
1279
1280 if (ObjCPropertyImplDecl *PPIDecl
1281 = IC->FindPropertyImplDecl(PropertyId)) {
1282 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1283 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001284 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001285 }
1286 IC->addPropertyImplementation(PIDecl);
David Blaikie4e4d0842012-03-11 07:00:24 +00001287 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall260611a2012-06-20 06:18:46 +00001288 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek71207fc2012-01-05 22:47:47 +00001289 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001290 // Diagnose if an ivar was lazily synthesdized due to a previous
1291 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001292 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +00001293 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001294 ObjCIvarDecl *Ivar = 0;
1295 if (!Synthesize)
1296 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1297 else {
1298 if (PropertyIvar && PropertyIvar != PropertyId)
1299 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1300 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +00001301 // Issue diagnostics only if Ivar belongs to current class.
1302 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00001303 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +00001304 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1305 << PropertyId;
1306 Ivar->setInvalidDecl();
1307 }
1308 }
Ted Kremenek28685ab2010-03-12 00:46:40 +00001309 } else {
1310 if (Synthesize)
1311 if (ObjCPropertyImplDecl *PPIDecl =
1312 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001313 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenek28685ab2010-03-12 00:46:40 +00001314 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1315 << PropertyIvar;
1316 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1317 }
1318
1319 if (ObjCPropertyImplDecl *PPIDecl =
1320 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedmane4c043d2012-05-01 22:26:06 +00001321 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001322 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +00001323 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001324 }
1325 CatImplClass->addPropertyImplementation(PIDecl);
1326 }
1327
John McCalld226f652010-08-21 09:40:31 +00001328 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +00001329}
1330
1331//===----------------------------------------------------------------------===//
1332// Helper methods.
1333//===----------------------------------------------------------------------===//
1334
Ted Kremenek9d64c152010-03-12 00:38:38 +00001335/// DiagnosePropertyMismatch - Compares two properties for their
1336/// attributes and types and warns on a variety of inconsistencies.
1337///
1338void
1339Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1340 ObjCPropertyDecl *SuperProperty,
1341 const IdentifierInfo *inheritedName) {
1342 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1343 Property->getPropertyAttributes();
1344 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1345 SuperProperty->getPropertyAttributes();
1346 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1347 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1348 Diag(Property->getLocation(), diag::warn_readonly_property)
1349 << Property->getDeclName() << inheritedName;
1350 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1351 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1352 Diag(Property->getLocation(), diag::warn_property_attribute)
1353 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +00001354 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +00001355 unsigned CAttrRetain =
1356 (CAttr &
1357 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1358 unsigned SAttrRetain =
1359 (SAttr &
1360 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1361 bool CStrong = (CAttrRetain != 0);
1362 bool SStrong = (SAttrRetain != 0);
1363 if (CStrong != SStrong)
1364 Diag(Property->getLocation(), diag::warn_property_attribute)
1365 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1366 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001367
1368 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001369 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001370 Diag(Property->getLocation(), diag::warn_property_attribute)
1371 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001372 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1373 }
1374 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001375 Diag(Property->getLocation(), diag::warn_property_attribute)
1376 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001377 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1378 }
1379 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001380 Diag(Property->getLocation(), diag::warn_property_attribute)
1381 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanianb7b25652013-02-10 00:16:04 +00001382 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1383 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001384
1385 QualType LHSType =
1386 Context.getCanonicalType(SuperProperty->getType());
1387 QualType RHSType =
1388 Context.getCanonicalType(Property->getType());
1389
Fariborz Jahanianc286f382011-07-12 22:05:16 +00001390 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001391 // Do cases not handled in above.
1392 // FIXME. For future support of covariant property types, revisit this.
1393 bool IncompatibleObjC = false;
1394 QualType ConvertedType;
1395 if (!isObjCPointerConversion(RHSType, LHSType,
1396 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001397 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +00001398 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1399 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +00001400 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1401 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001402 }
1403}
1404
1405bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1406 ObjCMethodDecl *GetterMethod,
1407 SourceLocation Loc) {
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001408 if (!GetterMethod)
1409 return false;
1410 QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
1411 QualType PropertyIvarType = property->getType().getNonReferenceType();
1412 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1413 if (!compat) {
1414 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1415 isa<ObjCObjectPointerType>(GetterType))
1416 compat =
1417 Context.canAssignObjCInterfaces(
Fariborz Jahanian490a52b2012-05-29 19:56:01 +00001418 GetterType->getAs<ObjCObjectPointerType>(),
1419 PropertyIvarType->getAs<ObjCObjectPointerType>());
1420 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001421 != Compatible) {
1422 Diag(Loc, diag::error_property_accessor_type)
1423 << property->getDeclName() << PropertyIvarType
1424 << GetterMethod->getSelector() << GetterType;
1425 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1426 return true;
1427 } else {
1428 compat = true;
1429 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1430 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1431 if (lhsType != rhsType && lhsType->isArithmeticType())
1432 compat = false;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001433 }
1434 }
Fariborz Jahanian9abf88c2012-05-15 22:37:04 +00001435
1436 if (!compat) {
1437 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1438 << property->getDeclName()
1439 << GetterMethod->getSelector();
1440 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1441 return true;
1442 }
1443
Ted Kremenek9d64c152010-03-12 00:38:38 +00001444 return false;
1445}
1446
Ted Kremenek9d64c152010-03-12 00:38:38 +00001447/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
1448/// of properties declared in a protocol and compares their attribute against
1449/// the same property declared in the class or category.
1450void
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001451Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) {
1452 if (!CDecl)
1453 return;
1454
1455 // Category case.
1456 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1457 // FIXME: We should perform this check when the property in the category
1458 // is declared.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001459 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1460 if (!CatDecl->IsClassExtension())
1461 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1462 E = PDecl->prop_end(); P != E; ++P) {
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001463 ObjCPropertyDecl *ProtoProp = *P;
1464 DeclContext::lookup_result R
1465 = CatDecl->lookup(ProtoProp->getDeclName());
1466 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1467 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1468 if (CatProp != ProtoProp) {
1469 // Property protocol already exist in class. Diagnose any mismatch.
1470 DiagnosePropertyMismatch(CatProp, ProtoProp,
1471 PDecl->getIdentifier());
1472 }
1473 }
1474 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001475 }
1476 return;
1477 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001478
1479 // Class
1480 // FIXME: We should perform this check when the property in the class
1481 // is declared.
1482 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001483 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001484 E = PDecl->prop_end(); P != E; ++P) {
1485 ObjCPropertyDecl *ProtoProp = *P;
1486 DeclContext::lookup_result R
Douglas Gregoraabd0942013-01-21 19:05:22 +00001487 = IDecl->lookup(ProtoProp->getDeclName());
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001488 for (unsigned I = 0, N = R.size(); I != N; ++I) {
1489 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
1490 if (ClassProp != ProtoProp) {
1491 // Property protocol already exist in class. Diagnose any mismatch.
1492 DiagnosePropertyMismatch(ClassProp, ProtoProp,
1493 PDecl->getIdentifier());
1494 }
1495 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001496 }
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001497 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001498}
1499
Ted Kremenek9d64c152010-03-12 00:38:38 +00001500/// isPropertyReadonly - Return true if property is readonly, by searching
1501/// for the property in the class and in its categories and implementations
1502///
1503bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1504 ObjCInterfaceDecl *IDecl) {
1505 // by far the most common case.
1506 if (!PDecl->isReadOnly())
1507 return false;
1508 // Even if property is ready only, if interface has a user defined setter,
1509 // it is not considered read only.
1510 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1511 return false;
1512
1513 // Main class has the property as 'readonly'. Must search
1514 // through the category list to see if the property's
1515 // attribute has been over-ridden to 'readwrite'.
Douglas Gregord3297242013-01-16 23:00:23 +00001516 for (ObjCInterfaceDecl::visible_categories_iterator
1517 Cat = IDecl->visible_categories_begin(),
1518 CatEnd = IDecl->visible_categories_end();
1519 Cat != CatEnd; ++Cat) {
1520 if (Cat->getInstanceMethod(PDecl->getSetterName()))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001521 return false;
1522 ObjCPropertyDecl *P =
Douglas Gregord3297242013-01-16 23:00:23 +00001523 Cat->FindPropertyDeclaration(PDecl->getIdentifier());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001524 if (P && !P->isReadOnly())
1525 return false;
1526 }
1527
1528 // Also, check for definition of a setter method in the implementation if
1529 // all else failed.
1530 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1531 if (ObjCImplementationDecl *IMD =
1532 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1533 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1534 return false;
1535 } else if (ObjCCategoryImplDecl *CIMD =
1536 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1537 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1538 return false;
1539 }
1540 }
1541 // Lastly, look through the implementation (if one is in scope).
1542 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1543 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1544 return false;
1545 // If all fails, look at the super class.
1546 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1547 return isPropertyReadonly(PDecl, SIDecl);
1548 return true;
1549}
1550
1551/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregor0dfe23c2013-01-21 18:35:55 +00001552/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek9d64c152010-03-12 00:38:38 +00001553void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001554 ObjCContainerDecl::PropertyMap &PropMap,
1555 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001556 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1557 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1558 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001559 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001560 PropMap[Prop->getIdentifier()] = Prop;
1561 }
1562 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001563 for (ObjCInterfaceDecl::all_protocol_iterator
1564 PI = IDecl->all_referenced_protocol_begin(),
1565 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001566 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001567 }
1568 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1569 if (!CATDecl->IsClassExtension())
1570 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1571 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001572 ObjCPropertyDecl *Prop = *P;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001573 PropMap[Prop->getIdentifier()] = Prop;
1574 }
1575 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001576 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001577 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001578 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001579 }
1580 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1581 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1582 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie581deb32012-06-06 20:45:41 +00001583 ObjCPropertyDecl *Prop = *P;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001584 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1585 // Exclude property for protocols which conform to class's super-class,
1586 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001587 if (!PropertyFromSuper ||
1588 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001589 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1590 if (!PropEntry)
1591 PropEntry = Prop;
1592 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001593 }
1594 // scan through protocol's protocols.
1595 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1596 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001597 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001598 }
1599}
1600
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001601/// CollectSuperClassPropertyImplementations - This routine collects list of
1602/// properties to be implemented in super class(s) and also coming from their
1603/// conforming protocols.
1604static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zakse63aedd2012-10-31 01:18:22 +00001605 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001606 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001607 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001608 while (SDecl) {
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001609 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001610 SDecl = SDecl->getSuperClass();
1611 }
1612 }
1613}
1614
Fariborz Jahanian26202292013-02-14 19:07:19 +00001615/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1616/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1617/// declared in class 'IFace'.
1618bool
1619Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1620 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1621 if (!IV->getSynthesize())
1622 return false;
1623 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1624 Method->isInstanceMethod());
1625 if (!IMD || !IMD->isPropertyAccessor())
1626 return false;
1627
1628 // look up a property declaration whose one of its accessors is implemented
1629 // by this method.
1630 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1631 E = IFace->prop_end(); P != E; ++P) {
1632 ObjCPropertyDecl *property = *P;
1633 if ((property->getGetterName() == IMD->getSelector() ||
1634 property->getSetterName() == IMD->getSelector()) &&
1635 (property->getPropertyIvarDecl() == IV))
1636 return true;
1637 }
1638 return false;
1639}
1640
1641
James Dennett699c9042012-06-15 07:13:21 +00001642/// \brief Default synthesizes all properties which must be synthesized
1643/// in class's \@implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001644void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1645 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001646
Anna Zaksb36ea372012-10-18 19:17:53 +00001647 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001648 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1649 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001650 if (PropMap.empty())
1651 return;
Anna Zaksb36ea372012-10-18 19:17:53 +00001652 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001653 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1654
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001655 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1656 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001657 // If property to be implemented in the super class, ignore.
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001658 if (SuperPropMap[Prop->getIdentifier()]) {
1659 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1660 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1661 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanian1d0d2fe2013-03-12 22:22:38 +00001662 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian5bdaef52013-03-21 20:50:53 +00001663 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1664 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001665 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1666 << Prop->getIdentifier()->getName();
1667 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1668 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001669 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001670 }
Anna Zaksb36ea372012-10-18 19:17:53 +00001671 // Is there a matching property synthesize/dynamic?
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001672 if (Prop->isInvalidDecl() ||
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001673 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001674 continue;
Fariborz Jahanian6114a3c2013-03-12 19:46:17 +00001675 if (ObjCPropertyImplDecl *PID =
1676 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1677 if (PID->getPropertyDecl() != Prop) {
1678 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1679 << Prop->getIdentifier()->getName();
1680 if (!PID->getLocation().isInvalid())
1681 Diag(PID->getLocation(), diag::note_property_synthesize);
1682 }
1683 continue;
1684 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001685 // Property may have been synthesized by user.
1686 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1687 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001688 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1689 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1690 continue;
1691 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1692 continue;
1693 }
Fariborz Jahanianf8aba8c2011-12-15 01:03:18 +00001694 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) {
1695 // We won't auto-synthesize properties declared in protocols.
1696 Diag(IMPDecl->getLocation(),
1697 diag::warn_auto_synthesizing_protocol_property);
1698 Diag(Prop->getLocation(), diag::note_property_declare);
1699 continue;
1700 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001701
1702 // We use invalid SourceLocations for the synthesized ivars since they
1703 // aren't really synthesized at a particular location; they just exist.
1704 // Saying that they are located at the @implementation isn't really going
1705 // to help users.
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001706 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1707 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1708 true,
1709 /* property = */ Prop->getIdentifier(),
Anna Zaksad0ce532012-09-27 19:45:11 +00001710 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis390fff82012-06-08 02:16:11 +00001711 Prop->getLocation()));
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001712 if (PIDecl) {
1713 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahanian5ea66612012-05-08 18:03:39 +00001714 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahanian975eef62012-05-03 16:43:30 +00001715 }
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001716 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001717}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001718
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001719void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall260611a2012-06-20 06:18:46 +00001720 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001721 return;
1722 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1723 if (!IC)
1724 return;
1725 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek71207fc2012-01-05 22:47:47 +00001726 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001727 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001728}
1729
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001730void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001731 ObjCContainerDecl *CDecl) {
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001732 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1733 ObjCInterfaceDecl *IDecl;
1734 // Gather properties which need not be implemented in this class
1735 // or category.
1736 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1737 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1738 // For categories, no need to implement properties declared in
1739 // its primary class (and its super classes) if property is
1740 // declared in one of those containers.
Fariborz Jahaniancfaed8d2013-02-14 22:33:34 +00001741 if ((IDecl = C->getClassInterface())) {
1742 ObjCInterfaceDecl::PropertyDeclOrder PO;
1743 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1744 }
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001745 }
1746 if (IDecl)
1747 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001748
Anna Zaksb36ea372012-10-18 19:17:53 +00001749 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahanian277076a2012-12-19 18:58:55 +00001750 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001751 if (PropMap.empty())
1752 return;
1753
1754 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1755 for (ObjCImplDecl::propimpl_iterator
1756 I = IMPDecl->propimpl_begin(),
1757 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie262bc182012-04-30 02:36:29 +00001758 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek9d64c152010-03-12 00:38:38 +00001759
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001760 SelectorSet InsMap;
1761 // Collect property accessors implemented in current implementation.
1762 for (ObjCImplementationDecl::instmeth_iterator
1763 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1764 InsMap.insert((*I)->getSelector());
1765
1766 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1767 ObjCInterfaceDecl *PrimaryClass = 0;
1768 if (C && !C->IsClassExtension())
1769 if ((PrimaryClass = C->getClassInterface()))
1770 // Report unimplemented properties in the category as well.
1771 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1772 // When reporting on missing setter/getters, do not report when
1773 // setter/getter is implemented in category's primary class
1774 // implementation.
1775 for (ObjCImplementationDecl::instmeth_iterator
1776 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1777 InsMap.insert((*I)->getSelector());
1778 }
1779
Anna Zaksb36ea372012-10-18 19:17:53 +00001780 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek9d64c152010-03-12 00:38:38 +00001781 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1782 ObjCPropertyDecl *Prop = P->second;
1783 // Is there a matching propery synthesize/dynamic?
1784 if (Prop->isInvalidDecl() ||
1785 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregor7cdc4572013-01-08 18:16:18 +00001786 PropImplMap.count(Prop) ||
1787 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001788 continue;
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001789 // When reporting on missing property getter implementation in
1790 // categories, do not report when they are declared in primary class,
1791 // class's protocol, or one of it super classes. This is because,
1792 // the class is going to implement them.
1793 if (!InsMap.count(Prop->getGetterName()) &&
1794 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001795 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001796 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001797 isa<ObjCCategoryDecl>(CDecl) ?
1798 diag::warn_setter_getter_impl_required_in_category :
1799 diag::warn_setter_getter_impl_required)
1800 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001801 Diag(Prop->getLocation(),
1802 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001803 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001804 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001805 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001806 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1807
Ted Kremenek9d64c152010-03-12 00:38:38 +00001808 }
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001809 // When reporting on missing property setter implementation in
1810 // categories, do not report when they are declared in primary class,
1811 // class's protocol, or one of it super classes. This is because,
1812 // the class is going to implement them.
1813 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1814 (PrimaryClass == 0 ||
Fariborz Jahanianf3f0f352013-04-25 21:59:34 +00001815 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001816 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001817 isa<ObjCCategoryDecl>(CDecl) ?
1818 diag::warn_setter_getter_impl_required_in_category :
1819 diag::warn_setter_getter_impl_required)
1820 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001821 Diag(Prop->getLocation(),
1822 diag::note_property_declare);
John McCall260611a2012-06-20 06:18:46 +00001823 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001824 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek71207fc2012-01-05 22:47:47 +00001825 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanianda611a72012-01-04 23:16:13 +00001826 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001827 }
1828 }
1829}
1830
1831void
1832Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1833 ObjCContainerDecl* IDecl) {
1834 // Rules apply in non-GC mode only
David Blaikie4e4d0842012-03-11 07:00:24 +00001835 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001836 return;
1837 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1838 E = IDecl->prop_end();
1839 I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00001840 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001841 ObjCMethodDecl *GetterMethod = 0;
1842 ObjCMethodDecl *SetterMethod = 0;
1843 bool LookedUpGetterSetter = false;
1844
Bill Wendlingad017fa2012-12-20 19:22:21 +00001845 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001846 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001847
John McCall265941b2011-09-13 18:31:23 +00001848 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1849 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001850 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1851 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1852 LookedUpGetterSetter = true;
1853 if (GetterMethod) {
1854 Diag(GetterMethod->getLocation(),
1855 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001856 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001857 Diag(Property->getLocation(), diag::note_property_declare);
1858 }
1859 if (SetterMethod) {
1860 Diag(SetterMethod->getLocation(),
1861 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001862 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001863 Diag(Property->getLocation(), diag::note_property_declare);
1864 }
1865 }
1866
Ted Kremenek9d64c152010-03-12 00:38:38 +00001867 // We only care about readwrite atomic property.
Bill Wendlingad017fa2012-12-20 19:22:21 +00001868 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1869 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek9d64c152010-03-12 00:38:38 +00001870 continue;
1871 if (const ObjCPropertyImplDecl *PIDecl
1872 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1873 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1874 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001875 if (!LookedUpGetterSetter) {
1876 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1877 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1878 LookedUpGetterSetter = true;
1879 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001880 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1881 SourceLocation MethodLoc =
1882 (GetterMethod ? GetterMethod->getLocation()
1883 : SetterMethod->getLocation());
1884 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001885 << Property->getIdentifier() << (GetterMethod != 0)
1886 << (SetterMethod != 0);
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00001887 // fixit stuff.
1888 if (!AttributesAsWritten) {
1889 if (Property->getLParenLoc().isValid()) {
1890 // @property () ... case.
1891 SourceRange PropSourceRange(Property->getAtLoc(),
1892 Property->getLParenLoc());
1893 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1894 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1895 }
1896 else {
1897 //@property id etc.
1898 SourceLocation endLoc =
1899 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1900 endLoc = endLoc.getLocWithOffset(-1);
1901 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1902 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1903 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1904 }
1905 }
1906 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1907 // @property () ... case.
1908 SourceLocation endLoc = Property->getLParenLoc();
1909 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1910 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1911 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1912 }
1913 else
1914 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001915 Diag(Property->getLocation(), diag::note_property_declare);
1916 }
1917 }
1918 }
1919}
1920
John McCallf85e1932011-06-15 23:02:42 +00001921void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001922 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001923 return;
1924
1925 for (ObjCImplementationDecl::propimpl_iterator
1926 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +00001927 ObjCPropertyImplDecl *PID = *i;
John McCallf85e1932011-06-15 23:02:42 +00001928 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1929 continue;
1930
1931 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001932 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1933 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001934 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1935 if (!method)
1936 continue;
1937 ObjCMethodFamily family = method->getMethodFamily();
1938 if (family == OMF_alloc || family == OMF_copy ||
1939 family == OMF_mutableCopy || family == OMF_new) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001940 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00001941 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1942 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001943 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001944 Diag(PD->getLocation(), diag::note_property_declare);
1945 }
1946 }
1947 }
1948}
1949
John McCall5de74d12010-11-10 07:01:40 +00001950/// AddPropertyAttrs - Propagates attributes from a property to the
1951/// implicitly-declared getter or setter for that property.
1952static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1953 ObjCPropertyDecl *Property) {
1954 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001955 for (Decl::attr_iterator A = Property->attr_begin(),
1956 AEnd = Property->attr_end();
1957 A != AEnd; ++A) {
1958 if (isa<DeprecatedAttr>(*A) ||
1959 isa<UnavailableAttr>(*A) ||
1960 isa<AvailabilityAttr>(*A))
1961 PropertyMethod->addAttr((*A)->clone(S.Context));
1962 }
John McCall5de74d12010-11-10 07:01:40 +00001963}
1964
Ted Kremenek9d64c152010-03-12 00:38:38 +00001965/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1966/// have the property type and issue diagnostics if they don't.
1967/// Also synthesize a getter/setter method if none exist (and update the
1968/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1969/// methods is the "right" thing to do.
1970void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001971 ObjCContainerDecl *CD,
1972 ObjCPropertyDecl *redeclaredProperty,
1973 ObjCContainerDecl *lexicalDC) {
1974
Ted Kremenek9d64c152010-03-12 00:38:38 +00001975 ObjCMethodDecl *GetterMethod, *SetterMethod;
1976
1977 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1978 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1979 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1980 property->getLocation());
1981
1982 if (SetterMethod) {
1983 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1984 property->getPropertyAttributes();
1985 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1986 Context.getCanonicalType(SetterMethod->getResultType()) !=
1987 Context.VoidTy)
1988 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1989 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001990 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001991 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1992 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001993 Diag(property->getLocation(),
1994 diag::warn_accessor_property_type_mismatch)
1995 << property->getDeclName()
1996 << SetterMethod->getSelector();
1997 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1998 }
1999 }
2000
2001 // Synthesize getter/setter methods if none exist.
2002 // Find the default getter and if one not found, add one.
2003 // FIXME: The synthesized property we set here is misleading. We almost always
2004 // synthesize these methods unless the user explicitly provided prototypes
2005 // (which is odd, but allowed). Sema should be typechecking that the
2006 // declarations jive in that situation (which it is not currently).
2007 if (!GetterMethod) {
2008 // No instance method of same name as property getter name was found.
2009 // Declare a getter method and add it to the list of methods
2010 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00002011 SourceLocation Loc = redeclaredProperty ?
2012 redeclaredProperty->getLocation() :
2013 property->getLocation();
2014
2015 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2016 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002017 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002018 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002019 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002020 (property->getPropertyImplementation() ==
2021 ObjCPropertyDecl::Optional) ?
2022 ObjCMethodDecl::Optional :
2023 ObjCMethodDecl::Required);
2024 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00002025
2026 AddPropertyAttrs(*this, GetterMethod, property);
2027
Ted Kremenek23173d72010-05-18 21:09:07 +00002028 // FIXME: Eventually this shouldn't be needed, as the lexical context
2029 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00002030 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002031 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00002032 if (property->hasAttr<NSReturnsNotRetainedAttr>())
2033 GetterMethod->addAttr(
2034 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
John McCallb8463812013-04-04 01:38:37 +00002035
2036 if (getLangOpts().ObjCAutoRefCount)
2037 CheckARCMethodDecl(GetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002038 } else
2039 // A user declared getter will be synthesize when @synthesize of
2040 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002041 GetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002042 property->setGetterMethodDecl(GetterMethod);
2043
2044 // Skip setter if property is read-only.
2045 if (!property->isReadOnly()) {
2046 // Find the default setter and if one not found, add one.
2047 if (!SetterMethod) {
2048 // No instance method of same name as property setter name was found.
2049 // Declare a setter method and add it to the list of methods
2050 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002051 SourceLocation Loc = redeclaredProperty ?
2052 redeclaredProperty->getLocation() :
2053 property->getLocation();
2054
2055 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002056 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00002057 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002058 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002059 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002060 /*isImplicitlyDeclared=*/true,
2061 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002062 (property->getPropertyImplementation() ==
2063 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00002064 ObjCMethodDecl::Optional :
2065 ObjCMethodDecl::Required);
2066
Ted Kremenek9d64c152010-03-12 00:38:38 +00002067 // Invent the arguments for the setter. We don't bother making a
2068 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002069 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2070 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002071 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00002072 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00002073 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00002074 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002075 0);
Dmitri Gribenko55431692013-05-05 00:41:58 +00002076 SetterMethod->setMethodParams(Context, Argument, None);
John McCall5de74d12010-11-10 07:01:40 +00002077
2078 AddPropertyAttrs(*this, SetterMethod, property);
2079
Ted Kremenek9d64c152010-03-12 00:38:38 +00002080 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00002081 // FIXME: Eventually this shouldn't be needed, as the lexical context
2082 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00002083 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00002084 SetterMethod->setLexicalDeclContext(lexicalDC);
John McCallb8463812013-04-04 01:38:37 +00002085
2086 // It's possible for the user to have set a very odd custom
2087 // setter selector that causes it to have a method family.
2088 if (getLangOpts().ObjCAutoRefCount)
2089 CheckARCMethodDecl(SetterMethod);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002090 } else
2091 // A user declared setter will be synthesize when @synthesize of
2092 // the property with the same name is seen in the @implementation
Jordan Rose1e4691b2012-10-10 16:42:25 +00002093 SetterMethod->setPropertyAccessor(true);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002094 property->setSetterMethodDecl(SetterMethod);
2095 }
2096 // Add any synthesized methods to the global pool. This allows us to
2097 // handle the following, which is supported by GCC (and part of the design).
2098 //
2099 // @interface Foo
2100 // @property double bar;
2101 // @end
2102 //
2103 // void thisIsUnfortunate() {
2104 // id foo;
2105 // double bar = [foo bar];
2106 // }
2107 //
2108 if (GetterMethod)
2109 AddInstanceMethodToGlobalPool(GetterMethod);
2110 if (SetterMethod)
2111 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002112
2113 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2114 if (!CurrentClass) {
2115 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2116 CurrentClass = Cat->getClassInterface();
2117 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2118 CurrentClass = Impl->getClassInterface();
2119 }
2120 if (GetterMethod)
2121 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2122 if (SetterMethod)
2123 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002124}
2125
John McCalld226f652010-08-21 09:40:31 +00002126void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00002127 SourceLocation Loc,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002128 unsigned &Attributes,
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002129 bool propertyInPrimaryClass) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002130 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00002131 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00002132 return;
2133
2134 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002135 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002136
David Blaikie4e4d0842012-03-11 07:00:24 +00002137 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002138 (Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002139 PropertyTy->isObjCRetainableType()) {
2140 // 'readonly' property with no obvious lifetime.
2141 // its life time will be determined by its backing ivar.
2142 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained |
2143 ObjCDeclSpec::DQ_PR_copy |
2144 ObjCDeclSpec::DQ_PR_retain |
2145 ObjCDeclSpec::DQ_PR_strong |
2146 ObjCDeclSpec::DQ_PR_weak |
2147 ObjCDeclSpec::DQ_PR_assign);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002148 if ((Attributes & rel) == 0)
Fariborz Jahanian015f6082012-01-11 18:26:06 +00002149 return;
2150 }
2151
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002152 if (propertyInPrimaryClass) {
2153 // we postpone most property diagnosis until class's implementation
2154 // because, its readonly attribute may be overridden in its class
2155 // extensions making other attributes, which make no sense, to make sense.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002156 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2157 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
Fariborz Jahaniancea06d22012-06-20 22:57:42 +00002158 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2159 << "readonly" << "readwrite";
2160 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002161 // readonly and readwrite/assign/retain/copy conflict.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002162 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2163 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002164 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00002165 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00002166 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002167 ObjCDeclSpec::DQ_PR_retain |
2168 ObjCDeclSpec::DQ_PR_strong))) {
Bill Wendlingad017fa2012-12-20 19:22:21 +00002169 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002170 "readwrite" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002171 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002172 "assign" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002173 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
John McCallf85e1932011-06-15 23:02:42 +00002174 "unsafe_unretained" :
Bill Wendlingad017fa2012-12-20 19:22:21 +00002175 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002176 "copy" : "retain";
2177
Bill Wendlingad017fa2012-12-20 19:22:21 +00002178 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
Ted Kremenek9d64c152010-03-12 00:38:38 +00002179 diag::err_objc_property_attr_mutually_exclusive :
2180 diag::warn_objc_property_attr_mutually_exclusive)
2181 << "readonly" << which;
2182 }
2183
2184 // Check for copy or retain on non-object types.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002185 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002186 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2187 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00002188 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002189 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendlingad017fa2012-12-20 19:22:21 +00002190 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2191 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2192 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002193 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall977ea782012-02-21 21:48:05 +00002194 PropertyDecl->setInvalidDecl();
Ted Kremenek9d64c152010-03-12 00:38:38 +00002195 }
2196
2197 // Check for more than one of { assign, copy, retain }.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002198 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2199 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002200 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2201 << "assign" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002202 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002203 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002204 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002205 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2206 << "assign" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002207 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002208 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002209 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002210 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2211 << "assign" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002212 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002213 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002214 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002215 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002216 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2217 << "assign" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002218 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002219 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002220 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2221 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCallf85e1932011-06-15 23:02:42 +00002222 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2223 << "unsafe_unretained" << "copy";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002224 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCallf85e1932011-06-15 23:02:42 +00002225 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002226 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCallf85e1932011-06-15 23:02:42 +00002227 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2228 << "unsafe_unretained" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002229 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002230 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002231 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002232 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2233 << "unsafe_unretained" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002234 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002235 }
David Blaikie4e4d0842012-03-11 07:00:24 +00002236 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendlingad017fa2012-12-20 19:22:21 +00002237 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002238 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2239 << "unsafe_unretained" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002240 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002241 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002242 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2243 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00002244 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2245 << "copy" << "retain";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002246 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002247 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002248 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCallf85e1932011-06-15 23:02:42 +00002249 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2250 << "copy" << "strong";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002251 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCallf85e1932011-06-15 23:02:42 +00002252 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002253 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCallf85e1932011-06-15 23:02:42 +00002254 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2255 << "copy" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002256 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCallf85e1932011-06-15 23:02:42 +00002257 }
2258 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002259 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2260 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002261 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2262 << "retain" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002263 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00002264 }
Bill Wendlingad017fa2012-12-20 19:22:21 +00002265 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2266 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCallf85e1932011-06-15 23:02:42 +00002267 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2268 << "strong" << "weak";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002269 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00002270 }
2271
Bill Wendlingad017fa2012-12-20 19:22:21 +00002272 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2273 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002274 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2275 << "atomic" << "nonatomic";
Bill Wendlingad017fa2012-12-20 19:22:21 +00002276 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00002277 }
2278
Ted Kremenek9d64c152010-03-12 00:38:38 +00002279 // Warn if user supplied no assignment attribute, property is
2280 // readwrite, and this is an object type.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002281 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00002282 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2283 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2284 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00002285 PropertyTy->isObjCObjectPointerType()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002286 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002287 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianf21a92d2011-11-08 20:58:53 +00002288 // not specified; including when property is 'readonly'.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002289 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002290 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002291 bool isAnyClassTy =
2292 (PropertyTy->isObjCClassType() ||
2293 PropertyTy->isObjCQualifiedClassType());
2294 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2295 // issue any warning.
David Blaikie4e4d0842012-03-11 07:00:24 +00002296 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002297 ;
Fariborz Jahanianf224fb52012-09-17 23:57:35 +00002298 else if (propertyInPrimaryClass) {
2299 // Don't issue warning on property with no life time in class
2300 // extension as it is inherited from property in primary class.
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002301 // Skip this warning in gc-only mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002302 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002303 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00002304
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002305 // If non-gc code warn that this is likely inappropriate.
David Blaikie4e4d0842012-03-11 07:00:24 +00002306 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002307 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian9f37cd12012-01-04 00:31:53 +00002308 }
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00002309 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00002310
2311 // FIXME: Implement warning dependent on NSCopying being
2312 // implemented. See also:
2313 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2314 // (please trim this list while you are at it).
2315 }
2316
Bill Wendlingad017fa2012-12-20 19:22:21 +00002317 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2318 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikie4e4d0842012-03-11 07:00:24 +00002319 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00002320 && PropertyTy->isBlockPointerType())
2321 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendlingad017fa2012-12-20 19:22:21 +00002322 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2323 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2324 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian528a4992011-09-14 18:03:46 +00002325 PropertyTy->isBlockPointerType())
2326 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002327
Bill Wendlingad017fa2012-12-20 19:22:21 +00002328 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2329 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian48a98c72011-11-01 23:02:16 +00002330 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2331
Ted Kremenek9d64c152010-03-12 00:38:38 +00002332}