blob: c8358bccb6f76a4b281e033f97698c35018e1dbf [file] [log] [blame]
Ted Kremenek7a7a0802010-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 McCall83024632010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +000016#include "clang/AST/ASTMutationListener.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +000020#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/Lexer.h"
22#include "clang/Lex/Preprocessor.h"
23#include "clang/Sema/Initialization.h"
John McCalla1e130b2010-08-25 07:03:20 +000024#include "llvm/ADT/DenseSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Ted Kremenek7a7a0802010-03-12 00:38:38 +000026
27using namespace clang;
28
Ted Kremenekac597f32010-03-12 00:46:40 +000029//===----------------------------------------------------------------------===//
30// Grammar actions.
31//===----------------------------------------------------------------------===//
32
John McCall43192862011-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 McCalld8561f02012-08-20 23:36:59 +000047 return Qualifiers::OCL_Strong;
John McCall43192862011-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 McCall31168b02011-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 McCall43192862011-09-13 18:31:23 +000076 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
John McCall31168b02011-06-15 23:02:42 +000079 // We have a lifetime qualifier but no dominating property
John McCall43192862011-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 McCall31168b02011-06-15 23:02:42 +000093 return;
94 }
95
96 if (propertyLifetime == expectedLifetime) return;
97
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +0000100 diag::err_arc_inconsistent_property_ownership)
John McCall31168b02011-06-15 23:02:42 +0000101 << property->getDeclName()
John McCall43192862011-09-13 18:31:23 +0000102 << expectedLifetime
John McCall31168b02011-06-15 23:02:42 +0000103 << propertyLifetime;
104}
105
Fariborz Jahanian8d1ca5a12012-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 Gregorb8982092013-01-21 19:42:21 +0000115/// \brief Check this Objective-C property against a property declared in the
116/// given protocol.
117static void
118CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
119 ObjCProtocolDecl *Proto,
120 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> &Known) {
121 // Have we seen this protocol before?
122 if (!Known.insert(Proto))
123 return;
124
125 // Look for a property with the same name.
126 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
127 for (unsigned I = 0, N = R.size(); I != N; ++I) {
128 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000129 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
Douglas Gregorb8982092013-01-21 19:42:21 +0000130 return;
131 }
132 }
133
134 // Check this property against any protocols we inherit.
135 for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
136 PEnd = Proto->protocol_end();
137 P != PEnd; ++P) {
138 CheckPropertyAgainstProtocol(S, Prop, *P, Known);
139 }
140}
141
John McCall48871652010-08-21 09:40:31 +0000142Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000143 SourceLocation LParenLoc,
John McCall48871652010-08-21 09:40:31 +0000144 FieldDeclarator &FD,
145 ObjCDeclSpec &ODS,
146 Selector GetterSel,
147 Selector SetterSel,
John McCall48871652010-08-21 09:40:31 +0000148 bool *isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000149 tok::ObjCKeywordKind MethodImplKind,
150 DeclContext *lexicalDC) {
Bill Wendling44426052012-12-20 19:22:21 +0000151 unsigned Attributes = ODS.getPropertyAttributes();
John McCall31168b02011-06-15 23:02:42 +0000152 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
153 QualType T = TSI->getType();
Bill Wendling44426052012-12-20 19:22:21 +0000154 Attributes |= deduceWeakPropertyFromType(*this, T);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000155
Bill Wendling44426052012-12-20 19:22:21 +0000156 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000157 // default is readwrite!
Bill Wendling44426052012-12-20 19:22:21 +0000158 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
Ted Kremenekac597f32010-03-12 00:46:40 +0000159 // property is defaulted to 'assign' if it is readwrite and is
160 // not retain or copy
Bill Wendling44426052012-12-20 19:22:21 +0000161 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
Ted Kremenekac597f32010-03-12 00:46:40 +0000162 (isReadWrite &&
Bill Wendling44426052012-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 Jahanianb24b5682011-03-28 23:47:18 +0000168
Douglas Gregor90d34422013-01-21 19:05:22 +0000169 // Proceed with constructing the ObjCPropertyDecls.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000170 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Douglas Gregor90d34422013-01-21 19:05:22 +0000171 ObjCPropertyDecl *Res = 0;
172 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000173 if (CDecl->IsClassExtension()) {
Douglas Gregor90d34422013-01-21 19:05:22 +0000174 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000175 FD, GetterSel, SetterSel,
176 isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000177 Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000178 ODS.getPropertyAttributes(),
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000179 isOverridingProperty, TSI,
180 MethodImplKind);
Douglas Gregor90d34422013-01-21 19:05:22 +0000181 if (!Res)
182 return 0;
Fariborz Jahanian5848d332010-07-13 22:04:56 +0000183 }
Douglas Gregor90d34422013-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 Kremenekcba58492010-09-23 21:18:05 +0000194
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000195 // Validate the attributes on the @property.
Bill Wendling44426052012-12-20 19:22:21 +0000196 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +0000197 (isa<ObjCInterfaceDecl>(ClassDecl) ||
198 isa<ObjCProtocolDecl>(ClassDecl)));
John McCall31168b02011-06-15 23:02:42 +0000199
David Blaikiebbafb8a2012-03-11 07:00:24 +0000200 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +0000201 checkARCPropertyDecl(*this, Res);
202
Douglas Gregorb8982092013-01-21 19:42:21 +0000203 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
Douglas Gregor90d34422013-01-21 19:05:22 +0000204 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Douglas Gregorb8982092013-01-21 19:42:21 +0000205 // For a class, compare the property against a property in our superclass.
206 bool FoundInSuper = false;
Douglas Gregor90d34422013-01-21 19:05:22 +0000207 if (ObjCInterfaceDecl *Super = IFace->getSuperClass()) {
208 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
Douglas Gregorb8982092013-01-21 19:42:21 +0000209 for (unsigned I = 0, N = R.size(); I != N; ++I) {
210 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +0000211 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
Douglas Gregorb8982092013-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 Gregor90d34422013-01-21 19:05:22 +0000246 }
247 }
248
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +0000249 ActOnDocumentableDecl(Res);
Fariborz Jahaniandf586032010-03-30 22:40:11 +0000250 return Res;
Ted Kremenek959e8302010-03-12 02:31:10 +0000251}
Ted Kremenek90e2fc22010-03-12 00:49:00 +0000252
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000253static ObjCPropertyDecl::PropertyAttributeKind
Bill Wendling44426052012-12-20 19:22:21 +0000254makePropertyAttributesAsWritten(unsigned Attributes) {
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000255 unsigned attributesAsWritten = 0;
Bill Wendling44426052012-12-20 19:22:21 +0000256 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
Bill Wendling44426052012-12-20 19:22:21 +0000258 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
Bill Wendling44426052012-12-20 19:22:21 +0000260 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
Bill Wendling44426052012-12-20 19:22:21 +0000262 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000263 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
Bill Wendling44426052012-12-20 19:22:21 +0000264 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000265 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
Bill Wendling44426052012-12-20 19:22:21 +0000266 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000267 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
Bill Wendling44426052012-12-20 19:22:21 +0000268 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000269 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
Bill Wendling44426052012-12-20 19:22:21 +0000270 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000271 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
Bill Wendling44426052012-12-20 19:22:21 +0000272 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000273 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
Bill Wendling44426052012-12-20 19:22:21 +0000274 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000275 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
Bill Wendling44426052012-12-20 19:22:21 +0000276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000277 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
Bill Wendling44426052012-12-20 19:22:21 +0000278 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000279 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
280
281 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
282}
283
Fariborz Jahanian19e09cb2012-05-21 17:10:28 +0000284static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
Fariborz Jahanianb52d8d22012-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 Jahanian199a9b52012-05-19 18:17:17 +0000313}
314
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000315static unsigned getOwnershipRule(unsigned attr) {
Fariborz Jahanian8d1ca5a12012-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 Gregor90d34422013-01-21 19:05:22 +0000324ObjCPropertyDecl *
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000325Sema::HandlePropertyInClassExtension(Scope *S,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000326 SourceLocation AtLoc,
327 SourceLocation LParenLoc,
328 FieldDeclarator &FD,
Ted Kremenek959e8302010-03-12 02:31:10 +0000329 Selector GetterSel, Selector SetterSel,
330 const bool isAssign,
331 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000332 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000333 const unsigned AttributesAsWritten,
Ted Kremenek959e8302010-03-12 02:31:10 +0000334 bool *isOverridingProperty,
John McCall339bb662010-06-04 20:50:08 +0000335 TypeSourceInfo *T,
Ted Kremenek959e8302010-03-12 02:31:10 +0000336 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +0000337 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremenek959e8302010-03-12 02:31:10 +0000338 // Diagnose if this property is already in continuation class.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000339 DeclContext *DC = CurContext;
Ted Kremenek959e8302010-03-12 02:31:10 +0000340 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000341 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
342
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000343 if (CCPrimary) {
Fariborz Jahaniana0a9d852010-11-10 18:01:36 +0000344 // Check for duplicate declaration of this property in current and
345 // other class extensions.
Douglas Gregor048fbfa2013-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 Jahaniana0a9d852010-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 Gregor048fbfa2013-01-16 23:00:23 +0000357 }
358
Ted Kremenek959e8302010-03-12 02:31:10 +0000359 // Create a new ObjCPropertyDecl with the DeclContext being
360 // the class extension.
Fariborz Jahanianc21f5432010-12-10 23:36:33 +0000361 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremenek959e8302010-03-12 02:31:10 +0000362 ObjCPropertyDecl *PDecl =
363 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000364 PropertyId, AtLoc, LParenLoc, T);
Argyrios Kyrtzidisb51684d2011-10-18 19:49:16 +0000365 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000366 makePropertyAttributesAsWritten(AttributesAsWritten));
Bill Wendling44426052012-12-20 19:22:21 +0000367 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
Bill Wendling44426052012-12-20 19:22:21 +0000369 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
Fariborz Jahanian00c291b2010-03-22 23:25:52 +0000370 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian234c00d2013-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 Jahanianc21f5432010-12-10 23:36:33 +0000375 // Set setter/getter selector name. Needed later.
376 PDecl->setGetterName(GetterSel);
377 PDecl->setSetterName(SetterSel);
Douglas Gregor397745e2011-07-15 15:30:21 +0000378 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremenek959e8302010-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 Kremenek959e8302010-03-12 02:31:10 +0000383 if (!CCPrimary) {
384 Diag(CDecl->getLocation(), diag::err_continuation_class);
385 *isOverridingProperty = true;
John McCall48871652010-08-21 09:40:31 +0000386 return 0;
Ted Kremenek959e8302010-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 Kyrtzidisceeb19c2012-02-28 17:50:28 +0000396 ObjCPropertyDecl *PrimaryPDecl =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000397 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000398 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000399 Attributes,AttributesAsWritten, T, MethodImplKind, DC);
Ted Kremenek959e8302010-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 Kyrtzidisceeb19c2012-02-28 17:50:28 +0000404 ProcessPropertyDecl(PrimaryPDecl, CCPrimary, /* redeclaredProperty = */ 0,
Ted Kremenek2f075632010-09-21 20:52:59 +0000405 /* lexicalDC = */ CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000406 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
407 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000408 if (ASTMutationListener *L = Context.getASTMutationListener())
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000409 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/0, CDecl);
410 return PrimaryPDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000411 }
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000412 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
413 bool IncompatibleObjC = false;
414 QualType ConvertedType;
Fariborz Jahanian24c2ccc2012-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 Jahanian57539cf2012-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 Jahanian6a733842012-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 Jahanianb14a3b22012-09-17 20:57:19 +0000429 return 0;
Fariborz Jahanian6a733842012-02-02 18:54:58 +0000430 }
Fariborz Jahanian11ee2832011-09-24 00:56:59 +0000431 }
432
Ted Kremenek959e8302010-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 Jahanian3bf0ded2010-06-22 23:20:40 +0000435 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremenek959e8302010-03-12 02:31:10 +0000436 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +0000437 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
438 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000439 PIkind |= deduceWeakPropertyFromType(*this, PIDecl->getType());
Bill Wendling44426052012-12-20 19:22:21 +0000440 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
Fariborz Jahanianea262f72012-08-21 21:52:02 +0000441 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
Fariborz Jahanian8d1ca5a12012-08-21 21:45:58 +0000442 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
443 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
Ted Kremenek959e8302010-03-12 02:31:10 +0000444 Diag(AtLoc, diag::warn_property_attr_mismatch);
445 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000446 }
Fariborz Jahanian71964872013-10-26 00:35:39 +0000447 else if (getLangOpts().ObjCAutoRefCount) {
448 QualType PrimaryPropertyQT =
449 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
450 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000451 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
Fariborz Jahanian71964872013-10-26 00:35:39 +0000452 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
453 PrimaryPropertyQT.getObjCLifetime();
454 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
Fariborz Jahanian3b659822013-11-19 19:26:30 +0000455 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
456 !PropertyIsWeak) {
Fariborz Jahanian71964872013-10-26 00:35:39 +0000457 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
458 Diag(PIDecl->getLocation(), diag::note_property_declare);
459 }
460 }
461 }
462
Ted Kremenek1bc22f72010-03-18 01:22:36 +0000463 DeclContext *DC = cast<DeclContext>(CCPrimary);
464 if (!ObjCPropertyDecl::findPropertyDecl(DC,
465 PIDecl->getDeclName().getAsIdentifierInfo())) {
Fariborz Jahanian369a9c32014-01-27 19:14:49 +0000466 // In mrr mode, 'readwrite' property must have an explicit
467 // memory attribute. If none specified, select the default (assign).
468 if (!getLangOpts().ObjCAutoRefCount) {
469 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
470 ObjCDeclSpec::DQ_PR_retain |
471 ObjCDeclSpec::DQ_PR_strong |
472 ObjCDeclSpec::DQ_PR_copy |
473 ObjCDeclSpec::DQ_PR_unsafe_unretained |
474 ObjCDeclSpec::DQ_PR_weak)))
475 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
476 }
477
Ted Kremenek959e8302010-03-12 02:31:10 +0000478 // Protocol is not in the primary class. Must build one for it.
479 ObjCDeclSpec ProtocolPropertyODS;
480 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
481 // and ObjCPropertyDecl::PropertyAttributeKind have identical
482 // values. Should consolidate both into one enum type.
483 ProtocolPropertyODS.
484 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
485 PIkind);
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000486 // Must re-establish the context from class extension to primary
487 // class context.
Fariborz Jahaniana6460842011-08-22 20:15:24 +0000488 ContextRAII SavedContext(*this, CCPrimary);
489
John McCall48871652010-08-21 09:40:31 +0000490 Decl *ProtocolPtrTy =
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000491 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
Ted Kremenek959e8302010-03-12 02:31:10 +0000492 PIDecl->getGetterName(),
493 PIDecl->getSetterName(),
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +0000494 isOverridingProperty,
Ted Kremenekcba58492010-09-23 21:18:05 +0000495 MethodImplKind,
496 /* lexicalDC = */ CDecl);
John McCall48871652010-08-21 09:40:31 +0000497 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremenek959e8302010-03-12 02:31:10 +0000498 }
499 PIDecl->makeitReadWriteAttribute();
Bill Wendling44426052012-12-20 19:22:21 +0000500 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenek959e8302010-03-12 02:31:10 +0000501 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
Bill Wendling44426052012-12-20 19:22:21 +0000502 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000503 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +0000504 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenek959e8302010-03-12 02:31:10 +0000505 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
506 PIDecl->setSetterName(SetterSel);
507 } else {
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000508 // Tailor the diagnostics for the common case where a readwrite
509 // property is declared both in the @interface and the continuation.
510 // This is a common error where the user often intended the original
511 // declaration to be readonly.
512 unsigned diag =
Bill Wendling44426052012-12-20 19:22:21 +0000513 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
Ted Kremenek5ef9ad92010-10-21 18:49:42 +0000514 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
515 ? diag::err_use_continuation_class_redeclaration_readwrite
516 : diag::err_use_continuation_class;
517 Diag(AtLoc, diag)
Ted Kremenek959e8302010-03-12 02:31:10 +0000518 << CCPrimary->getDeclName();
519 Diag(PIDecl->getLocation(), diag::note_property_declare);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000520 return 0;
Ted Kremenek959e8302010-03-12 02:31:10 +0000521 }
522 *isOverridingProperty = true;
523 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +0000524 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
Argyrios Kyrtzidisceeb19c2012-02-28 17:50:28 +0000525 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
526 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
Argyrios Kyrtzidis846e61a2011-11-14 04:52:29 +0000527 if (ASTMutationListener *L = Context.getASTMutationListener())
528 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
Fariborz Jahanianb14a3b22012-09-17 20:57:19 +0000529 return PDecl;
Ted Kremenek959e8302010-03-12 02:31:10 +0000530}
531
532ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
533 ObjCContainerDecl *CDecl,
534 SourceLocation AtLoc,
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000535 SourceLocation LParenLoc,
Ted Kremenek959e8302010-03-12 02:31:10 +0000536 FieldDeclarator &FD,
537 Selector GetterSel,
538 Selector SetterSel,
539 const bool isAssign,
540 const bool isReadWrite,
Bill Wendling44426052012-12-20 19:22:21 +0000541 const unsigned Attributes,
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000542 const unsigned AttributesAsWritten,
John McCall339bb662010-06-04 20:50:08 +0000543 TypeSourceInfo *TInfo,
Ted Kremenek49be9e02010-05-18 21:09:07 +0000544 tok::ObjCKeywordKind MethodImplKind,
545 DeclContext *lexicalDC){
Ted Kremenek959e8302010-03-12 02:31:10 +0000546 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall339bb662010-06-04 20:50:08 +0000547 QualType T = TInfo->getType();
Ted Kremenekac597f32010-03-12 00:46:40 +0000548
549 // Issue a warning if property is 'assign' as default and its object, which is
550 // gc'able conforms to NSCopying protocol
David Blaikiebbafb8a2012-03-11 07:00:24 +0000551 if (getLangOpts().getGC() != LangOptions::NonGC &&
Bill Wendling44426052012-12-20 19:22:21 +0000552 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCall8b07ec22010-05-15 11:32:37 +0000553 if (const ObjCObjectPointerType *ObjPtrTy =
554 T->getAs<ObjCObjectPointerType>()) {
555 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
556 if (IDecl)
557 if (ObjCProtocolDecl* PNSCopying =
558 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
559 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
560 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +0000561 }
Eli Friedman999af7b2013-07-09 01:38:07 +0000562
563 if (T->isObjCObjectType()) {
564 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
565 StarLoc = PP.getLocForEndOfToken(StarLoc);
566 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
567 << FixItHint::CreateInsertion(StarLoc, "*");
568 T = Context.getObjCObjectPointerType(T);
569 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
570 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
571 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000572
Ted Kremenek959e8302010-03-12 02:31:10 +0000573 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenekac597f32010-03-12 00:46:40 +0000574 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
575 FD.D.getIdentifierLoc(),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +0000576 PropertyId, AtLoc, LParenLoc, TInfo);
Ted Kremenek959e8302010-03-12 02:31:10 +0000577
Ted Kremenek4fb821e2010-03-15 20:11:46 +0000578 if (ObjCPropertyDecl *prevDecl =
579 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenekac597f32010-03-12 00:46:40 +0000580 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek679708e2010-03-15 18:47:25 +0000581 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenekac597f32010-03-12 00:46:40 +0000582 PDecl->setInvalidDecl();
583 }
Ted Kremenek49be9e02010-05-18 21:09:07 +0000584 else {
Ted Kremenekac597f32010-03-12 00:46:40 +0000585 DC->addDecl(PDecl);
Ted Kremenek49be9e02010-05-18 21:09:07 +0000586 if (lexicalDC)
587 PDecl->setLexicalDeclContext(lexicalDC);
588 }
Ted Kremenekac597f32010-03-12 00:46:40 +0000589
590 if (T->isArrayType() || T->isFunctionType()) {
591 Diag(AtLoc, diag::err_property_type) << T;
592 PDecl->setInvalidDecl();
593 }
594
595 ProcessDeclAttributes(S, PDecl, FD.D);
596
597 // Regardless of setter/getter attribute, we save the default getter/setter
598 // selector names in anticipation of declaration of setter/getter methods.
599 PDecl->setGetterName(GetterSel);
600 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000601 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb458ffb2011-11-06 18:58:12 +0000602 makePropertyAttributesAsWritten(AttributesAsWritten));
Argyrios Kyrtzidis52bfc2b2011-07-12 04:30:16 +0000603
Bill Wendling44426052012-12-20 19:22:21 +0000604 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenekac597f32010-03-12 00:46:40 +0000605 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
606
Bill Wendling44426052012-12-20 19:22:21 +0000607 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000608 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
609
Bill Wendling44426052012-12-20 19:22:21 +0000610 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
Ted Kremenekac597f32010-03-12 00:46:40 +0000611 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
612
613 if (isReadWrite)
614 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
615
Bill Wendling44426052012-12-20 19:22:21 +0000616 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
Ted Kremenekac597f32010-03-12 00:46:40 +0000617 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
618
Bill Wendling44426052012-12-20 19:22:21 +0000619 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
John McCall31168b02011-06-15 23:02:42 +0000620 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
621
Bill Wendling44426052012-12-20 19:22:21 +0000622 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
John McCall31168b02011-06-15 23:02:42 +0000623 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
624
Bill Wendling44426052012-12-20 19:22:21 +0000625 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
Ted Kremenekac597f32010-03-12 00:46:40 +0000626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
627
Bill Wendling44426052012-12-20 19:22:21 +0000628 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000629 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
630
Ted Kremenekac597f32010-03-12 00:46:40 +0000631 if (isAssign)
632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
633
John McCall43192862011-09-13 18:31:23 +0000634 // In the semantic attributes, one of nonatomic or atomic is always set.
Bill Wendling44426052012-12-20 19:22:21 +0000635 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
Ted Kremenekac597f32010-03-12 00:46:40 +0000636 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall43192862011-09-13 18:31:23 +0000637 else
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000638 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenekac597f32010-03-12 00:46:40 +0000639
John McCall31168b02011-06-15 23:02:42 +0000640 // 'unsafe_unretained' is alias for 'assign'.
Bill Wendling44426052012-12-20 19:22:21 +0000641 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
John McCall31168b02011-06-15 23:02:42 +0000642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
643 if (isAssign)
644 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
645
Ted Kremenekac597f32010-03-12 00:46:40 +0000646 if (MethodImplKind == tok::objc_required)
647 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
648 else if (MethodImplKind == tok::objc_optional)
649 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenekac597f32010-03-12 00:46:40 +0000650
Ted Kremenek959e8302010-03-12 02:31:10 +0000651 return PDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +0000652}
653
John McCall31168b02011-06-15 23:02:42 +0000654static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
655 ObjCPropertyDecl *property,
656 ObjCIvarDecl *ivar) {
657 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
658
John McCall31168b02011-06-15 23:02:42 +0000659 QualType ivarType = ivar->getType();
660 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCall31168b02011-06-15 23:02:42 +0000661
John McCall43192862011-09-13 18:31:23 +0000662 // The lifetime implied by the property's attributes.
663 Qualifiers::ObjCLifetime propertyLifetime =
664 getImpliedARCOwnership(property->getPropertyAttributes(),
665 property->getType());
John McCall31168b02011-06-15 23:02:42 +0000666
John McCall43192862011-09-13 18:31:23 +0000667 // We're fine if they match.
668 if (propertyLifetime == ivarLifetime) return;
John McCall31168b02011-06-15 23:02:42 +0000669
John McCall43192862011-09-13 18:31:23 +0000670 // These aren't valid lifetimes for object ivars; don't diagnose twice.
671 if (ivarLifetime == Qualifiers::OCL_None ||
672 ivarLifetime == Qualifiers::OCL_Autoreleasing)
673 return;
John McCall31168b02011-06-15 23:02:42 +0000674
John McCalld8561f02012-08-20 23:36:59 +0000675 // If the ivar is private, and it's implicitly __unsafe_unretained
676 // becaues of its type, then pretend it was actually implicitly
677 // __strong. This is only sound because we're processing the
678 // property implementation before parsing any method bodies.
679 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
680 propertyLifetime == Qualifiers::OCL_Strong &&
681 ivar->getAccessControl() == ObjCIvarDecl::Private) {
682 SplitQualType split = ivarType.split();
683 if (split.Quals.hasObjCLifetime()) {
684 assert(ivarType->isObjCARCImplicitlyUnretainedType());
685 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
686 ivarType = S.Context.getQualifiedType(split);
687 ivar->setType(ivarType);
688 return;
689 }
690 }
691
John McCall43192862011-09-13 18:31:23 +0000692 switch (propertyLifetime) {
693 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000694 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000695 << property->getDeclName()
696 << ivar->getDeclName()
697 << ivarLifetime;
698 break;
John McCall31168b02011-06-15 23:02:42 +0000699
John McCall43192862011-09-13 18:31:23 +0000700 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000701 S.Diag(ivar->getLocation(), diag::error_weak_property)
John McCall43192862011-09-13 18:31:23 +0000702 << property->getDeclName()
703 << ivar->getDeclName();
704 break;
John McCall31168b02011-06-15 23:02:42 +0000705
John McCall43192862011-09-13 18:31:23 +0000706 case Qualifiers::OCL_ExplicitNone:
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000707 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
John McCall43192862011-09-13 18:31:23 +0000708 << property->getDeclName()
709 << ivar->getDeclName()
710 << ((property->getPropertyAttributesAsWritten()
711 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
712 break;
John McCall31168b02011-06-15 23:02:42 +0000713
John McCall43192862011-09-13 18:31:23 +0000714 case Qualifiers::OCL_Autoreleasing:
715 llvm_unreachable("properties cannot be autoreleasing");
John McCall31168b02011-06-15 23:02:42 +0000716
John McCall43192862011-09-13 18:31:23 +0000717 case Qualifiers::OCL_None:
718 // Any other property should be ignored.
John McCall31168b02011-06-15 23:02:42 +0000719 return;
720 }
721
722 S.Diag(property->getLocation(), diag::note_property_declare);
Argyrios Kyrtzidisf5b993f2012-12-12 22:48:25 +0000723 if (propertyImplLoc.isValid())
724 S.Diag(propertyImplLoc, diag::note_property_synthesize);
John McCall31168b02011-06-15 23:02:42 +0000725}
726
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000727/// setImpliedPropertyAttributeForReadOnlyProperty -
728/// This routine evaludates life-time attributes for a 'readonly'
729/// property with no known lifetime of its own, using backing
730/// 'ivar's attribute, if any. If no backing 'ivar', property's
731/// life-time is assumed 'strong'.
732static void setImpliedPropertyAttributeForReadOnlyProperty(
733 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
734 Qualifiers::ObjCLifetime propertyLifetime =
735 getImpliedARCOwnership(property->getPropertyAttributes(),
736 property->getType());
737 if (propertyLifetime != Qualifiers::OCL_None)
738 return;
739
740 if (!ivar) {
741 // if no backing ivar, make property 'strong'.
742 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
743 return;
744 }
745 // property assumes owenership of backing ivar.
746 QualType ivarType = ivar->getType();
747 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
748 if (ivarLifetime == Qualifiers::OCL_Strong)
749 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
750 else if (ivarLifetime == Qualifiers::OCL_Weak)
751 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
752 return;
753}
Ted Kremenekac597f32010-03-12 00:46:40 +0000754
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000755/// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
756/// in inherited protocols with mismatched types. Since any of them can
757/// be candidate for synthesis.
Benjamin Kramerbf8d2542013-05-23 15:53:44 +0000758static void
759DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
760 ObjCInterfaceDecl *ClassDecl,
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000761 ObjCPropertyDecl *Property) {
762 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
763 for (ObjCInterfaceDecl::all_protocol_iterator
764 PI = ClassDecl->all_referenced_protocol_begin(),
765 E = ClassDecl->all_referenced_protocol_end(); PI != E; ++PI) {
766 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
767 PDecl->collectInheritedProtocolProperties(Property, PropMap);
768 }
769 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
770 while (SDecl) {
771 for (ObjCInterfaceDecl::all_protocol_iterator
772 PI = SDecl->all_referenced_protocol_begin(),
773 E = SDecl->all_referenced_protocol_end(); PI != E; ++PI) {
774 if (const ObjCProtocolDecl *PDecl = (*PI)->getDefinition())
775 PDecl->collectInheritedProtocolProperties(Property, PropMap);
776 }
777 SDecl = SDecl->getSuperClass();
778 }
779
780 if (PropMap.empty())
781 return;
782
783 QualType RHSType = S.Context.getCanonicalType(Property->getType());
784 bool FirsTime = true;
785 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
786 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
787 ObjCPropertyDecl *Prop = I->second;
788 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
789 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
790 bool IncompatibleObjC = false;
791 QualType ConvertedType;
792 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
793 || IncompatibleObjC) {
794 if (FirsTime) {
795 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
796 << Property->getType();
797 FirsTime = false;
798 }
799 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
800 << Prop->getType();
801 }
802 }
803 }
804 if (!FirsTime && AtLoc.isValid())
805 S.Diag(AtLoc, diag::note_property_synthesize);
806}
807
Ted Kremenekac597f32010-03-12 00:46:40 +0000808/// ActOnPropertyImplDecl - This routine performs semantic checks and
809/// builds the AST node for a property implementation declaration; declared
James Dennett2a4d13c2012-06-15 07:13:21 +0000810/// as \@synthesize or \@dynamic.
Ted Kremenekac597f32010-03-12 00:46:40 +0000811///
John McCall48871652010-08-21 09:40:31 +0000812Decl *Sema::ActOnPropertyImplDecl(Scope *S,
813 SourceLocation AtLoc,
814 SourceLocation PropertyLoc,
815 bool Synthesize,
John McCall48871652010-08-21 09:40:31 +0000816 IdentifierInfo *PropertyId,
Douglas Gregorb1b71e52010-11-17 01:03:52 +0000817 IdentifierInfo *PropertyIvar,
818 SourceLocation PropertyIvarLoc) {
Ted Kremenek273c4f52010-04-05 23:45:09 +0000819 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahaniana195a512011-09-19 16:32:32 +0000820 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenekac597f32010-03-12 00:46:40 +0000821 // Make sure we have a context for the property implementation declaration.
822 if (!ClassImpDecl) {
823 Diag(AtLoc, diag::error_missing_property_context);
John McCall48871652010-08-21 09:40:31 +0000824 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000825 }
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +0000826 if (PropertyIvarLoc.isInvalid())
827 PropertyIvarLoc = PropertyLoc;
Eli Friedman169ec352012-05-01 22:26:06 +0000828 SourceLocation PropertyDiagLoc = PropertyLoc;
829 if (PropertyDiagLoc.isInvalid())
830 PropertyDiagLoc = ClassImpDecl->getLocStart();
Ted Kremenekac597f32010-03-12 00:46:40 +0000831 ObjCPropertyDecl *property = 0;
832 ObjCInterfaceDecl* IDecl = 0;
833 // Find the class or category class where this property must have
834 // a declaration.
835 ObjCImplementationDecl *IC = 0;
836 ObjCCategoryImplDecl* CatImplClass = 0;
837 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
838 IDecl = IC->getClassInterface();
839 // We always synthesize an interface for an implementation
840 // without an interface decl. So, IDecl is always non-zero.
841 assert(IDecl &&
842 "ActOnPropertyImplDecl - @implementation without @interface");
843
844 // Look for this property declaration in the @implementation's @interface
845 property = IDecl->FindPropertyDeclaration(PropertyId);
846 if (!property) {
847 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000848 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000849 }
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000850 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000851 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
852 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000853 if (AtLoc.isValid())
854 Diag(AtLoc, diag::warn_implicit_atomic_property);
855 else
856 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
857 Diag(property->getLocation(), diag::note_property_declare);
858 }
859
Ted Kremenekac597f32010-03-12 00:46:40 +0000860 if (const ObjCCategoryDecl *CD =
861 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
862 if (!CD->IsClassExtension()) {
863 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
864 Diag(property->getLocation(), diag::note_property_declare);
John McCall48871652010-08-21 09:40:31 +0000865 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000866 }
867 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000868 if (Synthesize&&
869 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
870 property->hasAttr<IBOutletAttr>() &&
871 !AtLoc.isValid()) {
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000872 bool ReadWriteProperty = false;
873 // Search into the class extensions and see if 'readonly property is
874 // redeclared 'readwrite', then no warning is to be issued.
875 for (ObjCInterfaceDecl::known_extensions_iterator
876 Ext = IDecl->known_extensions_begin(),
877 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) {
878 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
879 if (!R.empty())
880 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
881 PIkind = ExtProp->getPropertyAttributesAsWritten();
882 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
883 ReadWriteProperty = true;
884 break;
885 }
886 }
887 }
888
889 if (!ReadWriteProperty) {
Ted Kremenek7ee25672013-02-09 07:13:16 +0000890 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
Aaron Ballman1fb39552014-01-03 14:23:03 +0000891 << property;
Fariborz Jahanianf3c171e2013-02-08 23:32:30 +0000892 SourceLocation readonlyLoc;
893 if (LocPropertyAttribute(Context, "readonly",
894 property->getLParenLoc(), readonlyLoc)) {
895 SourceLocation endLoc =
896 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
897 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
898 Diag(property->getLocation(),
899 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
900 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
901 }
Fariborz Jahanianb52d8d22012-05-21 17:02:43 +0000902 }
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000903 }
Fariborz Jahanian0ebf8792013-05-20 21:20:24 +0000904 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
905 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
Fariborz Jahanian199a9b52012-05-19 18:17:17 +0000906
Ted Kremenekac597f32010-03-12 00:46:40 +0000907 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
908 if (Synthesize) {
909 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCall48871652010-08-21 09:40:31 +0000910 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000911 }
912 IDecl = CatImplClass->getClassInterface();
913 if (!IDecl) {
914 Diag(AtLoc, diag::error_missing_property_interface);
John McCall48871652010-08-21 09:40:31 +0000915 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000916 }
917 ObjCCategoryDecl *Category =
918 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
919
920 // If category for this implementation not found, it is an error which
921 // has already been reported eralier.
922 if (!Category)
John McCall48871652010-08-21 09:40:31 +0000923 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000924 // Look for this property declaration in @implementation's category
925 property = Category->FindPropertyDeclaration(PropertyId);
926 if (!property) {
927 Diag(PropertyLoc, diag::error_bad_category_property_decl)
928 << Category->getDeclName();
John McCall48871652010-08-21 09:40:31 +0000929 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000930 }
931 } else {
932 Diag(AtLoc, diag::error_bad_property_context);
John McCall48871652010-08-21 09:40:31 +0000933 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +0000934 }
935 ObjCIvarDecl *Ivar = 0;
Eli Friedman169ec352012-05-01 22:26:06 +0000936 bool CompleteTypeErr = false;
Fariborz Jahanian3da77752012-05-15 18:12:51 +0000937 bool compat = true;
Ted Kremenekac597f32010-03-12 00:46:40 +0000938 // Check that we have a valid, previously declared ivar for @synthesize
939 if (Synthesize) {
940 // @synthesize
941 if (!PropertyIvar)
942 PropertyIvar = PropertyId;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000943 // Check that this is a previously declared 'ivar' in 'IDecl' interface
944 ObjCInterfaceDecl *ClassDeclared;
945 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
946 QualType PropType = property->getType();
947 QualType PropertyIvarType = PropType.getNonReferenceType();
Eli Friedman169ec352012-05-01 22:26:06 +0000948
949 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000950 diag::err_incomplete_synthesized_property,
951 property->getDeclName())) {
Eli Friedman169ec352012-05-01 22:26:06 +0000952 Diag(property->getLocation(), diag::note_property_declare);
953 CompleteTypeErr = true;
954 }
955
David Blaikiebbafb8a2012-03-11 07:00:24 +0000956 if (getLangOpts().ObjCAutoRefCount &&
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000957 (property->getPropertyAttributesAsWritten() &
Fariborz Jahaniana230dea2012-01-11 19:48:08 +0000958 ObjCPropertyDecl::OBJC_PR_readonly) &&
959 PropertyIvarType->isObjCRetainableType()) {
Fariborz Jahanian39ba6392012-01-11 18:26:06 +0000960 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
961 }
962
John McCall31168b02011-06-15 23:02:42 +0000963 ObjCPropertyDecl::PropertyAttributeKind kind
964 = property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +0000965
966 // Add GC __weak to the ivar type if the property is weak.
967 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
David Blaikiebbafb8a2012-03-11 07:00:24 +0000968 getLangOpts().getGC() != LangOptions::NonGC) {
969 assert(!getLangOpts().ObjCAutoRefCount);
John McCall43192862011-09-13 18:31:23 +0000970 if (PropertyIvarType.isObjCGCStrong()) {
Eli Friedman169ec352012-05-01 22:26:06 +0000971 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
John McCall43192862011-09-13 18:31:23 +0000972 Diag(property->getLocation(), diag::note_property_declare);
973 } else {
974 PropertyIvarType =
975 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000976 }
Fariborz Jahanianeebdb672011-09-07 16:24:21 +0000977 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000978 if (AtLoc.isInvalid()) {
979 // Check when default synthesizing a property that there is
980 // an ivar matching property name and issue warning; since this
981 // is the most common case of not using an ivar used for backing
982 // property in non-default synthesis case.
983 ObjCInterfaceDecl *ClassDeclared=0;
984 ObjCIvarDecl *originalIvar =
985 IDecl->lookupInstanceVariable(property->getIdentifier(),
986 ClassDeclared);
987 if (originalIvar) {
988 Diag(PropertyDiagLoc,
989 diag::warn_autosynthesis_property_ivar_match)
Fariborz Jahanian9699c1e2012-06-29 19:05:11 +0000990 << PropertyId << (Ivar == 0) << PropertyIvar
Fariborz Jahanian1db30fc2012-06-29 18:43:30 +0000991 << originalIvar->getIdentifier();
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000992 Diag(property->getLocation(), diag::note_property_declare);
993 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
Fariborz Jahanian63d40202012-06-19 22:51:22 +0000994 }
Fariborz Jahanianedc29712012-06-20 17:18:31 +0000995 }
996
997 if (!Ivar) {
John McCall43192862011-09-13 18:31:23 +0000998 // In ARC, give the ivar a lifetime qualifier based on the
John McCall31168b02011-06-15 23:02:42 +0000999 // property attributes.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001000 if (getLangOpts().ObjCAutoRefCount &&
John McCall43192862011-09-13 18:31:23 +00001001 !PropertyIvarType.getObjCLifetime() &&
1002 PropertyIvarType->isObjCRetainableType()) {
John McCall31168b02011-06-15 23:02:42 +00001003
John McCall43192862011-09-13 18:31:23 +00001004 // It's an error if we have to do this and the user didn't
1005 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001006 if (!property->hasWrittenStorageAttribute() &&
John McCall43192862011-09-13 18:31:23 +00001007 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001008 Diag(PropertyDiagLoc,
Argyrios Kyrtzidise3be9792011-07-26 21:48:26 +00001009 diag::err_arc_objc_property_default_assign_on_object);
1010 Diag(property->getLocation(), diag::note_property_declare);
John McCall43192862011-09-13 18:31:23 +00001011 } else {
1012 Qualifiers::ObjCLifetime lifetime =
1013 getImpliedARCOwnership(kind, PropertyIvarType);
1014 assert(lifetime && "no lifetime for property?");
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001015 if (lifetime == Qualifiers::OCL_Weak) {
1016 bool err = false;
1017 if (const ObjCObjectPointerType *ObjT =
Richard Smith802c4b72012-08-23 06:16:52 +00001018 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1019 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1020 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
Fariborz Jahanian6a413372013-04-24 19:13:05 +00001021 Diag(property->getLocation(),
1022 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1023 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1024 << ClassImpDecl->getName();
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001025 err = true;
1026 }
Richard Smith802c4b72012-08-23 06:16:52 +00001027 }
John McCall3deb1ad2012-08-21 02:47:43 +00001028 if (!err && !getLangOpts().ObjCARCWeak) {
Eli Friedman169ec352012-05-01 22:26:06 +00001029 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001030 Diag(property->getLocation(), diag::note_property_declare);
1031 }
John McCall31168b02011-06-15 23:02:42 +00001032 }
Fariborz Jahaniane2833462011-12-09 19:55:11 +00001033
John McCall31168b02011-06-15 23:02:42 +00001034 Qualifiers qs;
John McCall43192862011-09-13 18:31:23 +00001035 qs.addObjCLifetime(lifetime);
John McCall31168b02011-06-15 23:02:42 +00001036 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1037 }
John McCall31168b02011-06-15 23:02:42 +00001038 }
1039
1040 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001041 !getLangOpts().ObjCAutoRefCount &&
1042 getLangOpts().getGC() == LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001043 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
John McCall31168b02011-06-15 23:02:42 +00001044 Diag(property->getLocation(), diag::note_property_declare);
1045 }
1046
Abramo Bagnaradff19302011-03-08 08:55:46 +00001047 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001048 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001049 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian522eb7b2010-12-15 23:29:04 +00001050 ObjCIvarDecl::Private,
Fariborz Jahanian18722982010-07-17 00:59:30 +00001051 (Expr *)0, true);
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001052 if (RequireNonAbstractType(PropertyIvarLoc,
1053 PropertyIvarType,
1054 diag::err_abstract_type_in_decl,
1055 AbstractSynthesizedIvarType)) {
1056 Diag(property->getLocation(), diag::note_property_declare);
Eli Friedman169ec352012-05-01 22:26:06 +00001057 Ivar->setInvalidDecl();
Fariborz Jahanian873bae72013-07-05 17:18:11 +00001058 } else if (CompleteTypeErr)
1059 Ivar->setInvalidDecl();
Daniel Dunbarab5d7ae2010-04-02 19:44:54 +00001060 ClassImpDecl->addDecl(Ivar);
Richard Smith05afe5e2012-03-13 03:12:56 +00001061 IDecl->makeDeclVisibleInContext(Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001062
John McCall5fb5df92012-06-20 06:18:46 +00001063 if (getLangOpts().ObjCRuntime.isFragile())
Eli Friedman169ec352012-05-01 22:26:06 +00001064 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1065 << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001066 // Note! I deliberately want it to fall thru so, we have a
1067 // a property implementation and to avoid future warnings.
John McCall5fb5df92012-06-20 06:18:46 +00001068 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001069 !declaresSameEntity(ClassDeclared, IDecl)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001070 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001071 << property->getDeclName() << Ivar->getDeclName()
1072 << ClassDeclared->getDeclName();
1073 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar56df9772010-08-17 22:39:59 +00001074 << Ivar << Ivar->getName();
Ted Kremenekac597f32010-03-12 00:46:40 +00001075 // Note! I deliberately want it to fall thru so more errors are caught.
1076 }
Anna Zaks9802f9f2012-09-26 18:55:16 +00001077 property->setPropertyIvarDecl(Ivar);
1078
Ted Kremenekac597f32010-03-12 00:46:40 +00001079 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1080
1081 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001082 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001083 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCall31168b02011-06-15 23:02:42 +00001084 && isa<ObjCObjectPointerType>(IvarType))
Richard Smithda837032012-09-14 18:27:01 +00001085 compat =
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001086 Context.canAssignObjCInterfaces(
Fariborz Jahanianb24b5682011-03-28 23:47:18 +00001087 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001088 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorc03a1082011-01-28 02:26:04 +00001089 else {
Argyrios Kyrtzidis34608802012-02-28 17:50:39 +00001090 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1091 IvarType)
John McCall8cb679e2010-11-15 09:13:47 +00001092 == Compatible);
Douglas Gregorc03a1082011-01-28 02:26:04 +00001093 }
Fariborz Jahanian9f963c22010-05-18 23:04:17 +00001094 if (!compat) {
Eli Friedman169ec352012-05-01 22:26:06 +00001095 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
Ted Kremenek5921b832010-03-23 19:02:22 +00001096 << property->getDeclName() << PropType
1097 << Ivar->getDeclName() << IvarType;
1098 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001099 // Note! I deliberately want it to fall thru so, we have a
1100 // a property implementation and to avoid future warnings.
1101 }
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001102 else {
1103 // FIXME! Rules for properties are somewhat different that those
1104 // for assignments. Use a new routine to consolidate all cases;
1105 // specifically for property redeclarations as well as for ivars.
1106 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1107 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1108 if (lhsType != rhsType &&
1109 lhsType->isArithmeticType()) {
1110 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1111 << property->getDeclName() << PropType
1112 << Ivar->getDeclName() << IvarType;
1113 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1114 // Fall thru - see previous comment
1115 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001116 }
1117 // __weak is explicit. So it works on Canonical type.
John McCall31168b02011-06-15 23:02:42 +00001118 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001119 getLangOpts().getGC() != LangOptions::NonGC)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001120 Diag(PropertyDiagLoc, diag::error_weak_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001121 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianeebdb672011-09-07 16:24:21 +00001122 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenekac597f32010-03-12 00:46:40 +00001123 // Fall thru - see previous comment
1124 }
John McCall31168b02011-06-15 23:02:42 +00001125 // Fall thru - see previous comment
Ted Kremenekac597f32010-03-12 00:46:40 +00001126 if ((property->getType()->isObjCObjectPointerType() ||
1127 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001128 getLangOpts().getGC() != LangOptions::NonGC) {
Eli Friedman169ec352012-05-01 22:26:06 +00001129 Diag(PropertyDiagLoc, diag::error_strong_property)
Ted Kremenekac597f32010-03-12 00:46:40 +00001130 << property->getDeclName() << Ivar->getDeclName();
1131 // Fall thru - see previous comment
1132 }
1133 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001134 if (getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00001135 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenekac597f32010-03-12 00:46:40 +00001136 } else if (PropertyIvar)
1137 // @dynamic
Eli Friedman169ec352012-05-01 22:26:06 +00001138 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
John McCall31168b02011-06-15 23:02:42 +00001139
Ted Kremenekac597f32010-03-12 00:46:40 +00001140 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1141 ObjCPropertyImplDecl *PIDecl =
1142 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1143 property,
1144 (Synthesize ?
1145 ObjCPropertyImplDecl::Synthesize
1146 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001147 Ivar, PropertyIvarLoc);
Eli Friedman169ec352012-05-01 22:26:06 +00001148
Fariborz Jahanian3da77752012-05-15 18:12:51 +00001149 if (CompleteTypeErr || !compat)
Eli Friedman169ec352012-05-01 22:26:06 +00001150 PIDecl->setInvalidDecl();
1151
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001152 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1153 getterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001154 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
Fariborz Jahaniandddf1582010-10-15 22:42:59 +00001155 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001156 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1157 // returned by the getter as it must conform to C++'s copy-return rules.
1158 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001159 SynthesizedFunctionScope Scope(*this, getterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001160 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1161 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001162 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001163 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001164 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001165 Expr *LoadSelfExpr =
1166 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1167 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001168 Expr *IvarRefExpr =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001169 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001170 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001171 LoadSelfExpr, true, true);
Alp Toker314cc812014-01-25 16:55:45 +00001172 ExprResult Res = PerformCopyInitialization(
1173 InitializedEntity::InitializeResult(PropertyDiagLoc,
1174 getterMethod->getReturnType(),
1175 /*NRVO=*/false),
1176 PropertyDiagLoc, Owned(IvarRefExpr));
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001177 if (!Res.isInvalid()) {
1178 Expr *ResExpr = Res.takeAs<Expr>();
1179 if (ResExpr)
John McCall5d413782010-12-06 08:20:24 +00001180 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001181 PIDecl->setGetterCXXConstructor(ResExpr);
1182 }
1183 }
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001184 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1185 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1186 Diag(getterMethod->getLocation(),
1187 diag::warn_property_getter_owning_mismatch);
1188 Diag(property->getLocation(), diag::note_property_declare);
1189 }
Fariborz Jahanian39d1c422013-05-16 19:08:44 +00001190 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1191 switch (getterMethod->getMethodFamily()) {
1192 case OMF_retain:
1193 case OMF_retainCount:
1194 case OMF_release:
1195 case OMF_autorelease:
1196 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1197 << 1 << getterMethod->getSelector();
1198 break;
1199 default:
1200 break;
1201 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001202 }
1203 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1204 setterMethod->createImplicitParams(Context, IDecl);
Eli Friedman169ec352012-05-01 22:26:06 +00001205 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1206 Ivar->getType()->isRecordType()) {
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001207 // FIXME. Eventually we want to do this for Objective-C as well.
Eli Friedmaneaf34142012-10-18 20:14:08 +00001208 SynthesizedFunctionScope Scope(*this, setterMethod);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001209 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1210 DeclRefExpr *SelfExpr =
John McCall113bee02012-03-10 09:33:50 +00001211 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001212 VK_LValue, PropertyDiagLoc);
Eli Friedmaneaf34142012-10-18 20:14:08 +00001213 MarkDeclRefReferenced(SelfExpr);
Jordan Rose31c05a12014-01-14 17:29:00 +00001214 Expr *LoadSelfExpr =
1215 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1216 CK_LValueToRValue, SelfExpr, 0, VK_RValue);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001217 Expr *lhs =
Eli Friedmaneaf34142012-10-18 20:14:08 +00001218 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc,
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001219 Ivar->getLocation(),
Jordan Rose31c05a12014-01-14 17:29:00 +00001220 LoadSelfExpr, true, true);
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001221 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1222 ParmVarDecl *Param = (*P);
John McCall526ab472011-10-25 17:37:35 +00001223 QualType T = Param->getType().getNonReferenceType();
Eli Friedmaneaf34142012-10-18 20:14:08 +00001224 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1225 VK_LValue, PropertyDiagLoc);
1226 MarkDeclRefReferenced(rhs);
1227 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
John McCalle3027922010-08-25 11:45:40 +00001228 BO_Assign, lhs, rhs);
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001229 if (property->getPropertyAttributes() &
1230 ObjCPropertyDecl::OBJC_PR_atomic) {
1231 Expr *callExpr = Res.takeAs<Expr>();
1232 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13d3f862011-10-07 21:08:14 +00001233 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1234 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001235 if (!FuncDecl->isTrivial())
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001236 if (property->getType()->isReferenceType()) {
Eli Friedmaneaf34142012-10-18 20:14:08 +00001237 Diag(PropertyDiagLoc,
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001238 diag::err_atomic_property_nontrivial_assign_op)
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001239 << property->getType();
Fariborz Jahaniana08a7472012-01-10 00:37:01 +00001240 Diag(FuncDecl->getLocStart(),
1241 diag::note_callee_decl) << FuncDecl;
1242 }
Fariborz Jahanian565ed7a2011-10-06 18:38:18 +00001243 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001244 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
1245 }
1246 }
1247
Ted Kremenekac597f32010-03-12 00:46:40 +00001248 if (IC) {
1249 if (Synthesize)
1250 if (ObjCPropertyImplDecl *PPIDecl =
1251 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1252 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1253 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1254 << PropertyIvar;
1255 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1256 }
1257
1258 if (ObjCPropertyImplDecl *PPIDecl
1259 = IC->FindPropertyImplDecl(PropertyId)) {
1260 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1261 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001262 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001263 }
1264 IC->addPropertyImplementation(PIDecl);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001265 if (getLangOpts().ObjCDefaultSynthProperties &&
John McCall5fb5df92012-06-20 06:18:46 +00001266 getLangOpts().ObjCRuntime.isNonFragile() &&
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001267 !IDecl->isObjCRequiresPropertyDefs()) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001268 // Diagnose if an ivar was lazily synthesdized due to a previous
1269 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001270 // but it requires an ivar of different name.
Fariborz Jahanian4ad7afa2011-01-20 23:34:25 +00001271 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanian18722982010-07-17 00:59:30 +00001272 ObjCIvarDecl *Ivar = 0;
1273 if (!Synthesize)
1274 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1275 else {
1276 if (PropertyIvar && PropertyIvar != PropertyId)
1277 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1278 }
Fariborz Jahanian76b35372010-08-24 18:48:05 +00001279 // Issue diagnostics only if Ivar belongs to current class.
1280 if (Ivar && Ivar->getSynthesize() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00001281 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
Fariborz Jahanian18722982010-07-17 00:59:30 +00001282 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1283 << PropertyId;
1284 Ivar->setInvalidDecl();
1285 }
1286 }
Ted Kremenekac597f32010-03-12 00:46:40 +00001287 } else {
1288 if (Synthesize)
1289 if (ObjCPropertyImplDecl *PPIDecl =
1290 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001291 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
Ted Kremenekac597f32010-03-12 00:46:40 +00001292 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1293 << PropertyIvar;
1294 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1295 }
1296
1297 if (ObjCPropertyImplDecl *PPIDecl =
1298 CatImplClass->FindPropertyImplDecl(PropertyId)) {
Eli Friedman169ec352012-05-01 22:26:06 +00001299 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
Ted Kremenekac597f32010-03-12 00:46:40 +00001300 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +00001301 return 0;
Ted Kremenekac597f32010-03-12 00:46:40 +00001302 }
1303 CatImplClass->addPropertyImplementation(PIDecl);
1304 }
1305
John McCall48871652010-08-21 09:40:31 +00001306 return PIDecl;
Ted Kremenekac597f32010-03-12 00:46:40 +00001307}
1308
1309//===----------------------------------------------------------------------===//
1310// Helper methods.
1311//===----------------------------------------------------------------------===//
1312
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001313/// DiagnosePropertyMismatch - Compares two properties for their
1314/// attributes and types and warns on a variety of inconsistencies.
1315///
1316void
1317Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1318 ObjCPropertyDecl *SuperProperty,
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001319 const IdentifierInfo *inheritedName,
1320 bool OverridingProtocolProperty) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001321 ObjCPropertyDecl::PropertyAttributeKind CAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001322 Property->getPropertyAttributes();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001323 ObjCPropertyDecl::PropertyAttributeKind SAttr =
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001324 SuperProperty->getPropertyAttributes();
1325
1326 // We allow readonly properties without an explicit ownership
1327 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1328 // to be overridden by a property with any explicit ownership in the subclass.
1329 if (!OverridingProtocolProperty &&
1330 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1331 ;
1332 else {
1333 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1334 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1335 Diag(Property->getLocation(), diag::warn_readonly_property)
1336 << Property->getDeclName() << inheritedName;
1337 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1338 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
John McCall31168b02011-06-15 23:02:42 +00001339 Diag(Property->getLocation(), diag::warn_property_attribute)
Fariborz Jahanianb809a0e2013-10-04 18:06:08 +00001340 << Property->getDeclName() << "copy" << inheritedName;
1341 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1342 unsigned CAttrRetain =
1343 (CAttr &
1344 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1345 unsigned SAttrRetain =
1346 (SAttr &
1347 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1348 bool CStrong = (CAttrRetain != 0);
1349 bool SStrong = (SAttrRetain != 0);
1350 if (CStrong != SStrong)
1351 Diag(Property->getLocation(), diag::warn_property_attribute)
1352 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1353 }
John McCall31168b02011-06-15 23:02:42 +00001354 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001355
1356 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001357 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001358 Diag(Property->getLocation(), diag::warn_property_attribute)
1359 << Property->getDeclName() << "atomic" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001360 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1361 }
1362 if (Property->getSetterName() != SuperProperty->getSetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001363 Diag(Property->getLocation(), diag::warn_property_attribute)
1364 << Property->getDeclName() << "setter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001365 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1366 }
1367 if (Property->getGetterName() != SuperProperty->getGetterName()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001368 Diag(Property->getLocation(), diag::warn_property_attribute)
1369 << Property->getDeclName() << "getter" << inheritedName;
Fariborz Jahanian234c00d2013-02-10 00:16:04 +00001370 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1371 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001372
1373 QualType LHSType =
1374 Context.getCanonicalType(SuperProperty->getType());
1375 QualType RHSType =
1376 Context.getCanonicalType(Property->getType());
1377
Fariborz Jahanianc0f6af22011-07-12 22:05:16 +00001378 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001379 // Do cases not handled in above.
1380 // FIXME. For future support of covariant property types, revisit this.
1381 bool IncompatibleObjC = false;
1382 QualType ConvertedType;
1383 if (!isObjCPointerConversion(RHSType, LHSType,
1384 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001385 IncompatibleObjC) {
Fariborz Jahanian17585e72011-07-13 17:55:01 +00001386 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1387 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanianfa643c82011-10-12 00:00:57 +00001388 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1389 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001390 }
1391}
1392
1393bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1394 ObjCMethodDecl *GetterMethod,
1395 SourceLocation Loc) {
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001396 if (!GetterMethod)
1397 return false;
Alp Toker314cc812014-01-25 16:55:45 +00001398 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001399 QualType PropertyIvarType = property->getType().getNonReferenceType();
1400 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1401 if (!compat) {
1402 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1403 isa<ObjCObjectPointerType>(GetterType))
1404 compat =
1405 Context.canAssignObjCInterfaces(
Fariborz Jahanianb5dd2cb2012-05-29 19:56:01 +00001406 GetterType->getAs<ObjCObjectPointerType>(),
1407 PropertyIvarType->getAs<ObjCObjectPointerType>());
1408 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001409 != Compatible) {
1410 Diag(Loc, diag::error_property_accessor_type)
1411 << property->getDeclName() << PropertyIvarType
1412 << GetterMethod->getSelector() << GetterType;
1413 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1414 return true;
1415 } else {
1416 compat = true;
1417 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1418 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1419 if (lhsType != rhsType && lhsType->isArithmeticType())
1420 compat = false;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001421 }
1422 }
Fariborz Jahanian0ebc0fa2012-05-15 22:37:04 +00001423
1424 if (!compat) {
1425 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1426 << property->getDeclName()
1427 << GetterMethod->getSelector();
1428 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1429 return true;
1430 }
1431
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001432 return false;
1433}
1434
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001435/// CollectImmediateProperties - This routine collects all properties in
Douglas Gregora669ab92013-01-21 18:35:55 +00001436/// the class and its conforming protocols; but not those in its super class.
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001437void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001438 ObjCContainerDecl::PropertyMap &PropMap,
1439 ObjCContainerDecl::PropertyMap &SuperPropMap) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001440 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1441 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1442 E = IDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001443 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001444 PropMap[Prop->getIdentifier()] = Prop;
1445 }
1446 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001447 for (ObjCInterfaceDecl::all_protocol_iterator
1448 PI = IDecl->all_referenced_protocol_begin(),
1449 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001450 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001451 }
1452 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1453 if (!CATDecl->IsClassExtension())
1454 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1455 E = CATDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001456 ObjCPropertyDecl *Prop = *P;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001457 PropMap[Prop->getIdentifier()] = Prop;
1458 }
1459 // scan through class's protocols.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001460 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001461 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001462 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001463 }
1464 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1465 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1466 E = PDecl->prop_end(); P != E; ++P) {
David Blaikie40ed2972012-06-06 20:45:41 +00001467 ObjCPropertyDecl *Prop = *P;
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001468 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1469 // Exclude property for protocols which conform to class's super-class,
1470 // as super-class has to implement the property.
Fariborz Jahanian698bd312011-09-27 00:23:52 +00001471 if (!PropertyFromSuper ||
1472 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001473 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1474 if (!PropEntry)
1475 PropEntry = Prop;
1476 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001477 }
1478 // scan through protocol's protocols.
1479 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1480 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001481 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001482 }
1483}
1484
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001485/// CollectSuperClassPropertyImplementations - This routine collects list of
1486/// properties to be implemented in super class(s) and also coming from their
1487/// conforming protocols.
1488static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
Anna Zaks408f7d02012-10-31 01:18:22 +00001489 ObjCInterfaceDecl::PropertyMap &PropMap) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001490 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001491 ObjCInterfaceDecl::PropertyDeclOrder PO;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001492 while (SDecl) {
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001493 SDecl->collectPropertiesToImplement(PropMap, PO);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001494 SDecl = SDecl->getSuperClass();
1495 }
1496 }
1497}
1498
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001499/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1500/// an ivar synthesized for 'Method' and 'Method' is a property accessor
1501/// declared in class 'IFace'.
1502bool
1503Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1504 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1505 if (!IV->getSynthesize())
1506 return false;
1507 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1508 Method->isInstanceMethod());
1509 if (!IMD || !IMD->isPropertyAccessor())
1510 return false;
1511
1512 // look up a property declaration whose one of its accessors is implemented
1513 // by this method.
1514 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
1515 E = IFace->prop_end(); P != E; ++P) {
1516 ObjCPropertyDecl *property = *P;
1517 if ((property->getGetterName() == IMD->getSelector() ||
1518 property->getSetterName() == IMD->getSelector()) &&
1519 (property->getPropertyIvarDecl() == IV))
1520 return true;
1521 }
1522 return false;
1523}
1524
1525
James Dennett2a4d13c2012-06-15 07:13:21 +00001526/// \brief Default synthesizes all properties which must be synthesized
1527/// in class's \@implementation.
Ted Kremenekab2dcc82011-09-27 23:39:40 +00001528void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1529 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001530
Anna Zaks673d76b2012-10-18 19:17:53 +00001531 ObjCInterfaceDecl::PropertyMap PropMap;
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001532 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1533 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001534 if (PropMap.empty())
1535 return;
Anna Zaks673d76b2012-10-18 19:17:53 +00001536 ObjCInterfaceDecl::PropertyMap SuperPropMap;
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001537 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1538
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001539 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1540 ObjCPropertyDecl *Prop = PropertyOrder[i];
Fariborz Jahanianbbc126e2013-06-07 20:26:51 +00001541 // Is there a matching property synthesize/dynamic?
1542 if (Prop->isInvalidDecl() ||
1543 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1544 continue;
1545 // Property may have been synthesized by user.
1546 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1547 continue;
1548 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1549 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1550 continue;
1551 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1552 continue;
1553 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001554 // If property to be implemented in the super class, ignore.
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001555 if (SuperPropMap[Prop->getIdentifier()]) {
1556 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1557 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1558 (PropInSuperClass->getPropertyAttributes() &
Fariborz Jahanianb0df66b2013-03-12 22:22:38 +00001559 ObjCPropertyDecl::OBJC_PR_readonly) &&
Fariborz Jahanian1446b342013-03-21 20:50:53 +00001560 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1561 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001562 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001563 << Prop->getIdentifier();
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001564 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1565 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001566 continue;
Fariborz Jahanian9d25a482013-03-12 19:46:17 +00001567 }
Fariborz Jahanian46145242013-06-07 18:32:55 +00001568 if (ObjCPropertyImplDecl *PID =
1569 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1570 if (PID->getPropertyDecl() != Prop) {
1571 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
Aaron Ballman5dff61d2014-01-03 14:06:37 +00001572 << Prop->getIdentifier();
Fariborz Jahanian46145242013-06-07 18:32:55 +00001573 if (!PID->getLocation().isInvalid())
1574 Diag(PID->getLocation(), diag::note_property_synthesize);
1575 }
1576 continue;
1577 }
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001578 if (ObjCProtocolDecl *Proto =
1579 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001580 // We won't auto-synthesize properties declared in protocols.
1581 Diag(IMPDecl->getLocation(),
Ted Kremenek6d69ac82013-12-12 23:40:14 +00001582 diag::warn_auto_synthesizing_protocol_property)
1583 << Prop << Proto;
Fariborz Jahanian9e49b6a2011-12-15 01:03:18 +00001584 Diag(Prop->getLocation(), diag::note_property_declare);
1585 continue;
1586 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001587
1588 // We use invalid SourceLocations for the synthesized ivars since they
1589 // aren't really synthesized at a particular location; they just exist.
1590 // Saying that they are located at the @implementation isn't really going
1591 // to help users.
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001592 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1593 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1594 true,
1595 /* property = */ Prop->getIdentifier(),
Anna Zaks454477c2012-09-27 19:45:11 +00001596 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
Argyrios Kyrtzidis31afb952012-06-08 02:16:11 +00001597 Prop->getLocation()));
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001598 if (PIDecl) {
1599 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
Fariborz Jahaniand6886e72012-05-08 18:03:39 +00001600 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
Fariborz Jahaniand5f34f92012-05-03 16:43:30 +00001601 }
Ted Kremenek74a9f982010-09-24 01:23:01 +00001602 }
Fariborz Jahanianbdb1b0d2010-05-14 18:35:57 +00001603}
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001604
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001605void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
John McCall5fb5df92012-06-20 06:18:46 +00001606 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001607 return;
1608 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1609 if (!IC)
1610 return;
1611 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001612 if (!IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001613 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian97d744b2011-08-31 22:24:06 +00001614}
1615
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001616void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001617 ObjCContainerDecl *CDecl) {
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001618 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1619 ObjCInterfaceDecl *IDecl;
1620 // Gather properties which need not be implemented in this class
1621 // or category.
1622 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)))
1623 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1624 // For categories, no need to implement properties declared in
1625 // its primary class (and its super classes) if property is
1626 // declared in one of those containers.
Fariborz Jahanianaedaaa42013-02-14 22:33:34 +00001627 if ((IDecl = C->getClassInterface())) {
1628 ObjCInterfaceDecl::PropertyDeclOrder PO;
1629 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1630 }
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001631 }
1632 if (IDecl)
1633 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
Fariborz Jahanian66f9a652010-06-29 18:12:32 +00001634
Anna Zaks673d76b2012-10-18 19:17:53 +00001635 ObjCContainerDecl::PropertyMap PropMap;
Fariborz Jahaniana1f85712012-12-19 18:58:55 +00001636 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001637 if (PropMap.empty())
1638 return;
1639
1640 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1641 for (ObjCImplDecl::propimpl_iterator
1642 I = IMPDecl->propimpl_begin(),
1643 EI = IMPDecl->propimpl_end(); I != EI; ++I)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001644 PropImplMap.insert(I->getPropertyDecl());
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001645
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001646 SelectorSet InsMap;
1647 // Collect property accessors implemented in current implementation.
1648 for (ObjCImplementationDecl::instmeth_iterator
1649 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
1650 InsMap.insert((*I)->getSelector());
1651
1652 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1653 ObjCInterfaceDecl *PrimaryClass = 0;
1654 if (C && !C->IsClassExtension())
1655 if ((PrimaryClass = C->getClassInterface()))
1656 // Report unimplemented properties in the category as well.
1657 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1658 // When reporting on missing setter/getters, do not report when
1659 // setter/getter is implemented in category's primary class
1660 // implementation.
1661 for (ObjCImplementationDecl::instmeth_iterator
1662 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1663 InsMap.insert((*I)->getSelector());
1664 }
1665
Anna Zaks673d76b2012-10-18 19:17:53 +00001666 for (ObjCContainerDecl::PropertyMap::iterator
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001667 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1668 ObjCPropertyDecl *Prop = P->second;
1669 // Is there a matching propery synthesize/dynamic?
1670 if (Prop->isInvalidDecl() ||
1671 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Douglas Gregorc4c1fb32013-01-08 18:16:18 +00001672 PropImplMap.count(Prop) ||
1673 Prop->getAvailability() == AR_Unavailable)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001674 continue;
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001675 // When reporting on missing property getter implementation in
1676 // categories, do not report when they are declared in primary class,
1677 // class's protocol, or one of it super classes. This is because,
1678 // the class is going to implement them.
1679 if (!InsMap.count(Prop->getGetterName()) &&
1680 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001681 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001682 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001683 isa<ObjCCategoryDecl>(CDecl) ?
1684 diag::warn_setter_getter_impl_required_in_category :
1685 diag::warn_setter_getter_impl_required)
1686 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001687 Diag(Prop->getLocation(),
1688 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001689 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001690 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001691 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001692 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1693
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001694 }
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001695 // When reporting on missing property setter implementation in
1696 // categories, do not report when they are declared in primary class,
1697 // class's protocol, or one of it super classes. This is because,
1698 // the class is going to implement them.
1699 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) &&
1700 (PrimaryClass == 0 ||
Fariborz Jahanian73e244a2013-04-25 21:59:34 +00001701 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) {
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001702 Diag(IMPDecl->getLocation(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001703 isa<ObjCCategoryDecl>(CDecl) ?
1704 diag::warn_setter_getter_impl_required_in_category :
1705 diag::warn_setter_getter_impl_required)
1706 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanian83aa8ab2011-08-27 21:55:47 +00001707 Diag(Prop->getLocation(),
1708 diag::note_property_declare);
John McCall5fb5df92012-06-20 06:18:46 +00001709 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001710 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
Ted Kremenek0c2c90b2012-01-05 22:47:47 +00001711 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
Fariborz Jahanian783ffde2012-01-04 23:16:13 +00001712 Diag(RID->getLocation(), diag::note_suppressed_class_declare);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001713 }
1714 }
1715}
1716
1717void
1718Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1719 ObjCContainerDecl* IDecl) {
1720 // Rules apply in non-GC mode only
David Blaikiebbafb8a2012-03-11 07:00:24 +00001721 if (getLangOpts().getGC() != LangOptions::NonGC)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001722 return;
1723 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1724 E = IDecl->prop_end();
1725 I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00001726 ObjCPropertyDecl *Property = *I;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001727 ObjCMethodDecl *GetterMethod = 0;
1728 ObjCMethodDecl *SetterMethod = 0;
1729 bool LookedUpGetterSetter = false;
1730
Bill Wendling44426052012-12-20 19:22:21 +00001731 unsigned Attributes = Property->getPropertyAttributes();
John McCall43192862011-09-13 18:31:23 +00001732 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001733
John McCall43192862011-09-13 18:31:23 +00001734 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1735 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001736 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1737 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1738 LookedUpGetterSetter = true;
1739 if (GetterMethod) {
1740 Diag(GetterMethod->getLocation(),
1741 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001742 << Property->getIdentifier() << 0;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001743 Diag(Property->getLocation(), diag::note_property_declare);
1744 }
1745 if (SetterMethod) {
1746 Diag(SetterMethod->getLocation(),
1747 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidisb5b5a592011-01-31 23:20:03 +00001748 << Property->getIdentifier() << 1;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001749 Diag(Property->getLocation(), diag::note_property_declare);
1750 }
1751 }
1752
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001753 // We only care about readwrite atomic property.
Bill Wendling44426052012-12-20 19:22:21 +00001754 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1755 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001756 continue;
1757 if (const ObjCPropertyImplDecl *PIDecl
1758 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1759 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1760 continue;
Argyrios Kyrtzidisdd88dbf2011-01-31 21:34:11 +00001761 if (!LookedUpGetterSetter) {
1762 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1763 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1764 LookedUpGetterSetter = true;
1765 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001766 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1767 SourceLocation MethodLoc =
1768 (GetterMethod ? GetterMethod->getLocation()
1769 : SetterMethod->getLocation());
1770 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian9cd57a72011-10-06 23:47:58 +00001771 << Property->getIdentifier() << (GetterMethod != 0)
1772 << (SetterMethod != 0);
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00001773 // fixit stuff.
1774 if (!AttributesAsWritten) {
1775 if (Property->getLParenLoc().isValid()) {
1776 // @property () ... case.
1777 SourceRange PropSourceRange(Property->getAtLoc(),
1778 Property->getLParenLoc());
1779 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1780 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1781 }
1782 else {
1783 //@property id etc.
1784 SourceLocation endLoc =
1785 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1786 endLoc = endLoc.getLocWithOffset(-1);
1787 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1788 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1789 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1790 }
1791 }
1792 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1793 // @property () ... case.
1794 SourceLocation endLoc = Property->getLParenLoc();
1795 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1796 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1797 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1798 }
1799 else
1800 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001801 Diag(Property->getLocation(), diag::note_property_declare);
1802 }
1803 }
1804 }
1805}
1806
John McCall31168b02011-06-15 23:02:42 +00001807void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001808 if (getLangOpts().getGC() == LangOptions::GCOnly)
John McCall31168b02011-06-15 23:02:42 +00001809 return;
1810
1811 for (ObjCImplementationDecl::propimpl_iterator
1812 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00001813 ObjCPropertyImplDecl *PID = *i;
John McCall31168b02011-06-15 23:02:42 +00001814 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001815 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1816 !D->getInstanceMethod(PD->getGetterName())) {
John McCall31168b02011-06-15 23:02:42 +00001817 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1818 if (!method)
1819 continue;
1820 ObjCMethodFamily family = method->getMethodFamily();
1821 if (family == OMF_alloc || family == OMF_copy ||
1822 family == OMF_mutableCopy || family == OMF_new) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001823 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001824 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001825 else
Fariborz Jahanian65b13772014-01-10 00:53:48 +00001826 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
John McCall31168b02011-06-15 23:02:42 +00001827 }
1828 }
1829 }
1830}
1831
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00001832void Sema::DiagnoseMissingDesignatedInitOverrides(
1833 const ObjCImplementationDecl *ImplD,
1834 const ObjCInterfaceDecl *IFD) {
1835 assert(IFD->hasDesignatedInitializers());
1836 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1837 if (!SuperD)
1838 return;
1839
1840 SelectorSet InitSelSet;
1841 for (ObjCImplementationDecl::instmeth_iterator
1842 I = ImplD->instmeth_begin(), E = ImplD->instmeth_end(); I!=E; ++I)
1843 if ((*I)->getMethodFamily() == OMF_init)
1844 InitSelSet.insert((*I)->getSelector());
1845
1846 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1847 SuperD->getDesignatedInitializers(DesignatedInits);
1848 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1849 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1850 const ObjCMethodDecl *MD = *I;
1851 if (!InitSelSet.count(MD->getSelector())) {
1852 Diag(ImplD->getLocation(),
1853 diag::warn_objc_implementation_missing_designated_init_override)
1854 << MD->getSelector();
1855 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1856 }
1857 }
1858}
1859
John McCallad31b5f2010-11-10 07:01:40 +00001860/// AddPropertyAttrs - Propagates attributes from a property to the
1861/// implicitly-declared getter or setter for that property.
1862static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1863 ObjCPropertyDecl *Property) {
1864 // Should we just clone all attributes over?
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001865 for (Decl::attr_iterator A = Property->attr_begin(),
1866 AEnd = Property->attr_end();
1867 A != AEnd; ++A) {
1868 if (isa<DeprecatedAttr>(*A) ||
1869 isa<UnavailableAttr>(*A) ||
1870 isa<AvailabilityAttr>(*A))
1871 PropertyMethod->addAttr((*A)->clone(S.Context));
1872 }
John McCallad31b5f2010-11-10 07:01:40 +00001873}
1874
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001875/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1876/// have the property type and issue diagnostics if they don't.
1877/// Also synthesize a getter/setter method if none exist (and update the
1878/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1879/// methods is the "right" thing to do.
1880void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001881 ObjCContainerDecl *CD,
1882 ObjCPropertyDecl *redeclaredProperty,
1883 ObjCContainerDecl *lexicalDC) {
1884
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001885 ObjCMethodDecl *GetterMethod, *SetterMethod;
1886
1887 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1888 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1889 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1890 property->getLocation());
1891
1892 if (SetterMethod) {
1893 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1894 property->getPropertyAttributes();
1895 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
Alp Toker314cc812014-01-25 16:55:45 +00001896 Context.getCanonicalType(SetterMethod->getReturnType()) !=
1897 Context.VoidTy)
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001898 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1899 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian0ee58d62011-09-26 22:59:09 +00001900 !Context.hasSameUnqualifiedType(
Fariborz Jahanian7c386f82011-10-15 17:36:49 +00001901 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1902 property->getType().getNonReferenceType())) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001903 Diag(property->getLocation(),
1904 diag::warn_accessor_property_type_mismatch)
1905 << property->getDeclName()
1906 << SetterMethod->getSelector();
1907 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1908 }
1909 }
1910
1911 // Synthesize getter/setter methods if none exist.
1912 // Find the default getter and if one not found, add one.
1913 // FIXME: The synthesized property we set here is misleading. We almost always
1914 // synthesize these methods unless the user explicitly provided prototypes
1915 // (which is odd, but allowed). Sema should be typechecking that the
1916 // declarations jive in that situation (which it is not currently).
1917 if (!GetterMethod) {
1918 // No instance method of same name as property getter name was found.
1919 // Declare a getter method and add it to the list of methods
1920 // for this class.
Ted Kremenek2f075632010-09-21 20:52:59 +00001921 SourceLocation Loc = redeclaredProperty ?
1922 redeclaredProperty->getLocation() :
1923 property->getLocation();
1924
1925 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1926 property->getGetterName(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001927 property->getType(), 0, CD, /*isInstance=*/true,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001928 /*isVariadic=*/false, /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001929 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001930 (property->getPropertyImplementation() ==
1931 ObjCPropertyDecl::Optional) ?
1932 ObjCMethodDecl::Optional :
1933 ObjCMethodDecl::Required);
1934 CD->addDecl(GetterMethod);
John McCallad31b5f2010-11-10 07:01:40 +00001935
1936 AddPropertyAttrs(*this, GetterMethod, property);
1937
Ted Kremenek49be9e02010-05-18 21:09:07 +00001938 // FIXME: Eventually this shouldn't be needed, as the lexical context
1939 // and the real context should be the same.
Ted Kremenek2f075632010-09-21 20:52:59 +00001940 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00001941 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00001942 if (property->hasAttr<NSReturnsNotRetainedAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001943 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
1944 Loc));
Fariborz Jahanian8a5e9472013-09-19 16:37:20 +00001945
1946 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
1947 GetterMethod->addAttr(
Aaron Ballman36a53502014-01-16 13:03:14 +00001948 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00001949
1950 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00001951 GetterMethod->addAttr(SectionAttr::CreateImplicit(Context, SA->getName(),
1952 Loc));
John McCalle48f3892013-04-04 01:38:37 +00001953
1954 if (getLangOpts().ObjCAutoRefCount)
1955 CheckARCMethodDecl(GetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001956 } else
1957 // A user declared getter will be synthesize when @synthesize of
1958 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00001959 GetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001960 property->setGetterMethodDecl(GetterMethod);
1961
1962 // Skip setter if property is read-only.
1963 if (!property->isReadOnly()) {
1964 // Find the default setter and if one not found, add one.
1965 if (!SetterMethod) {
1966 // No instance method of same name as property setter name was found.
1967 // Declare a setter method and add it to the list of methods
1968 // for this class.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001969 SourceLocation Loc = redeclaredProperty ?
1970 redeclaredProperty->getLocation() :
1971 property->getLocation();
1972
1973 SetterMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001974 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001975 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001976 CD, /*isInstance=*/true, /*isVariadic=*/false,
Jordan Rosed01e83a2012-10-10 16:42:25 +00001977 /*isPropertyAccessor=*/true,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00001978 /*isImplicitlyDeclared=*/true,
1979 /*isDefined=*/false,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001980 (property->getPropertyImplementation() ==
1981 ObjCPropertyDecl::Optional) ?
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00001982 ObjCMethodDecl::Optional :
1983 ObjCMethodDecl::Required);
1984
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001985 // Invent the arguments for the setter. We don't bother making a
1986 // nice name for the argument.
Abramo Bagnaradff19302011-03-08 08:55:46 +00001987 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1988 Loc, Loc,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001989 property->getIdentifier(),
John McCall31168b02011-06-15 23:02:42 +00001990 property->getType().getUnqualifiedType(),
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001991 /*TInfo=*/0,
John McCall8e7d6562010-08-26 03:08:43 +00001992 SC_None,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001993 0);
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +00001994 SetterMethod->setMethodParams(Context, Argument, None);
John McCallad31b5f2010-11-10 07:01:40 +00001995
1996 AddPropertyAttrs(*this, SetterMethod, property);
1997
Ted Kremenek7a7a0802010-03-12 00:38:38 +00001998 CD->addDecl(SetterMethod);
Ted Kremenek49be9e02010-05-18 21:09:07 +00001999 // FIXME: Eventually this shouldn't be needed, as the lexical context
2000 // and the real context should be the same.
Ted Kremeneke3a7d1b2010-09-21 18:28:43 +00002001 if (lexicalDC)
Ted Kremenek49be9e02010-05-18 21:09:07 +00002002 SetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahaniancb8c7da2013-12-18 23:09:57 +00002003 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
Aaron Ballman36a53502014-01-16 13:03:14 +00002004 SetterMethod->addAttr(SectionAttr::CreateImplicit(Context,
2005 SA->getName(), Loc));
John McCalle48f3892013-04-04 01:38:37 +00002006 // It's possible for the user to have set a very odd custom
2007 // setter selector that causes it to have a method family.
2008 if (getLangOpts().ObjCAutoRefCount)
2009 CheckARCMethodDecl(SetterMethod);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002010 } else
2011 // A user declared setter will be synthesize when @synthesize of
2012 // the property with the same name is seen in the @implementation
Jordan Rosed01e83a2012-10-10 16:42:25 +00002013 SetterMethod->setPropertyAccessor(true);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002014 property->setSetterMethodDecl(SetterMethod);
2015 }
2016 // Add any synthesized methods to the global pool. This allows us to
2017 // handle the following, which is supported by GCC (and part of the design).
2018 //
2019 // @interface Foo
2020 // @property double bar;
2021 // @end
2022 //
2023 // void thisIsUnfortunate() {
2024 // id foo;
2025 // double bar = [foo bar];
2026 // }
2027 //
2028 if (GetterMethod)
2029 AddInstanceMethodToGlobalPool(GetterMethod);
2030 if (SetterMethod)
2031 AddInstanceMethodToGlobalPool(SetterMethod);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002032
2033 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2034 if (!CurrentClass) {
2035 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2036 CurrentClass = Cat->getClassInterface();
2037 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2038 CurrentClass = Impl->getClassInterface();
2039 }
2040 if (GetterMethod)
2041 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2042 if (SetterMethod)
2043 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002044}
2045
John McCall48871652010-08-21 09:40:31 +00002046void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002047 SourceLocation Loc,
Bill Wendling44426052012-12-20 19:22:21 +00002048 unsigned &Attributes,
Fariborz Jahanian876cc652012-06-20 22:57:42 +00002049 bool propertyInPrimaryClass) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002050 // FIXME: Improve the reported location.
John McCall31168b02011-06-15 23:02:42 +00002051 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenekb5357822010-04-05 22:39:42 +00002052 return;
Fariborz Jahanian39ba6392012-01-11 18:26:06 +00002053
Fariborz Jahanian88ff20e2013-10-07 17:20:02 +00002054 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2055 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2056 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2057 << "readonly" << "readwrite";
2058
2059 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2060 QualType PropertyTy = PropertyDecl->getType();
2061 unsigned PropertyOwnership = getOwnershipRule(Attributes);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002062
Fariborz Jahanian059021a2013-12-13 18:19:59 +00002063 // 'readonly' property with no obvious lifetime.
2064 // its life time will be determined by its backing ivar.
2065 if (getLangOpts().ObjCAutoRefCount &&
2066 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2067 PropertyTy->isObjCRetainableType() &&
2068 !PropertyOwnership)
2069 return;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002070
2071 // Check for copy or retain on non-object types.
Bill Wendling44426052012-12-20 19:22:21 +00002072 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002073 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2074 !PropertyTy->isObjCRetainableType() &&
Aaron Ballman9ead1242013-12-19 02:39:40 +00002075 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002076 Diag(Loc, diag::err_objc_property_requires_object)
Bill Wendling44426052012-12-20 19:22:21 +00002077 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2078 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2079 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002080 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
John McCall24992372012-02-21 21:48:05 +00002081 PropertyDecl->setInvalidDecl();
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002082 }
2083
2084 // Check for more than one of { assign, copy, retain }.
Bill Wendling44426052012-12-20 19:22:21 +00002085 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2086 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002087 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2088 << "assign" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002089 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002090 }
Bill Wendling44426052012-12-20 19:22:21 +00002091 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002092 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2093 << "assign" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002094 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002095 }
Bill Wendling44426052012-12-20 19:22:21 +00002096 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002097 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2098 << "assign" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002099 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002100 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002101 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002102 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002103 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2104 << "assign" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002105 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002106 }
Aaron Ballman9ead1242013-12-19 02:39:40 +00002107 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
Fariborz Jahanianf030d162013-06-25 17:34:50 +00002108 Diag(Loc, diag::warn_iboutletcollection_property_assign);
Bill Wendling44426052012-12-20 19:22:21 +00002109 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2110 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
John McCall31168b02011-06-15 23:02:42 +00002111 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2112 << "unsafe_unretained" << "copy";
Bill Wendling44426052012-12-20 19:22:21 +00002113 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
John McCall31168b02011-06-15 23:02:42 +00002114 }
Bill Wendling44426052012-12-20 19:22:21 +00002115 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
John McCall31168b02011-06-15 23:02:42 +00002116 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2117 << "unsafe_unretained" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002118 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002119 }
Bill Wendling44426052012-12-20 19:22:21 +00002120 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002121 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2122 << "unsafe_unretained" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002123 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002124 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00002125 if (getLangOpts().ObjCAutoRefCount &&
Bill Wendling44426052012-12-20 19:22:21 +00002126 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002127 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2128 << "unsafe_unretained" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002129 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002130 }
Bill Wendling44426052012-12-20 19:22:21 +00002131 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2132 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002133 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2134 << "copy" << "retain";
Bill Wendling44426052012-12-20 19:22:21 +00002135 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002136 }
Bill Wendling44426052012-12-20 19:22:21 +00002137 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
John McCall31168b02011-06-15 23:02:42 +00002138 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2139 << "copy" << "strong";
Bill Wendling44426052012-12-20 19:22:21 +00002140 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
John McCall31168b02011-06-15 23:02:42 +00002141 }
Bill Wendling44426052012-12-20 19:22:21 +00002142 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
John McCall31168b02011-06-15 23:02:42 +00002143 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2144 << "copy" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002145 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
John McCall31168b02011-06-15 23:02:42 +00002146 }
2147 }
Bill Wendling44426052012-12-20 19:22:21 +00002148 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2149 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002150 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2151 << "retain" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002152 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCall31168b02011-06-15 23:02:42 +00002153 }
Bill Wendling44426052012-12-20 19:22:21 +00002154 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2155 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
John McCall31168b02011-06-15 23:02:42 +00002156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2157 << "strong" << "weak";
Bill Wendling44426052012-12-20 19:22:21 +00002158 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002159 }
2160
Bill Wendling44426052012-12-20 19:22:21 +00002161 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2162 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002163 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2164 << "atomic" << "nonatomic";
Bill Wendling44426052012-12-20 19:22:21 +00002165 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
Fariborz Jahanian55b4e5c2011-10-10 21:53:24 +00002166 }
2167
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002168 // Warn if user supplied no assignment attribute, property is
2169 // readwrite, and this is an object type.
Bill Wendling44426052012-12-20 19:22:21 +00002170 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCall31168b02011-06-15 23:02:42 +00002171 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2172 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2173 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002174 PropertyTy->isObjCObjectPointerType()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002175 if (getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002176 // With arc, @property definitions should default to (strong) when
Fariborz Jahanianb1ac0812011-11-08 20:58:53 +00002177 // not specified; including when property is 'readonly'.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002178 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Bill Wendling44426052012-12-20 19:22:21 +00002179 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002180 bool isAnyClassTy =
2181 (PropertyTy->isObjCClassType() ||
2182 PropertyTy->isObjCQualifiedClassType());
2183 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2184 // issue any warning.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002185 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002186 ;
Fariborz Jahaniancfb00a42012-09-17 23:57:35 +00002187 else if (propertyInPrimaryClass) {
2188 // Don't issue warning on property with no life time in class
2189 // extension as it is inherited from property in primary class.
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002190 // Skip this warning in gc-only mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002191 if (getLangOpts().getGC() != LangOptions::GCOnly)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002192 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002193
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002194 // If non-gc code warn that this is likely inappropriate.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002195 if (getLangOpts().getGC() == LangOptions::NonGC)
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002196 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Fariborz Jahanian1fc1c6c2012-01-04 00:31:53 +00002197 }
Fariborz Jahanian7e47de32011-08-19 19:28:44 +00002198 }
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002199
2200 // FIXME: Implement warning dependent on NSCopying being
2201 // implemented. See also:
2202 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2203 // (please trim this list while you are at it).
2204 }
2205
Bill Wendling44426052012-12-20 19:22:21 +00002206 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2207 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002208 && getLangOpts().getGC() == LangOptions::GCOnly
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002209 && PropertyTy->isBlockPointerType())
2210 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Bill Wendling44426052012-12-20 19:22:21 +00002211 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2212 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2213 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
Fariborz Jahanian1723e172011-09-14 18:03:46 +00002214 PropertyTy->isBlockPointerType())
2215 Diag(Loc, diag::warn_objc_property_retain_of_block);
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002216
Bill Wendling44426052012-12-20 19:22:21 +00002217 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2218 (Attributes & ObjCDeclSpec::DQ_PR_setter))
Fariborz Jahanian3018b952011-11-01 23:02:16 +00002219 Diag(Loc, diag::warn_objc_readonly_property_has_setter);
2220
Ted Kremenek7a7a0802010-03-12 00:38:38 +00002221}