blob: 617dbefadafdb2ce0b69639089086dc57ee84161 [file] [log] [blame]
Ted Kremenek9d64c152010-03-12 00:38:38 +00001//===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C @property and
11// @synthesize declarations.
12//
13//===----------------------------------------------------------------------===//
14
John McCall2d887082010-08-25 22:03:47 +000015#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Initialization.h"
John McCall7cd088e2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Fariborz Jahanian17cb3262010-05-05 21:52:17 +000018#include "clang/AST/ExprObjC.h"
Fariborz Jahanian57e264e2011-10-06 18:38:18 +000019#include "clang/AST/ExprCXX.h"
John McCall50df6ae2010-08-25 07:03:20 +000020#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000021
22using namespace clang;
23
Ted Kremenek28685ab2010-03-12 00:46:40 +000024//===----------------------------------------------------------------------===//
25// Grammar actions.
26//===----------------------------------------------------------------------===//
27
John McCall265941b2011-09-13 18:31:23 +000028/// getImpliedARCOwnership - Given a set of property attributes and a
29/// type, infer an expected lifetime. The type's ownership qualification
30/// is not considered.
31///
32/// Returns OCL_None if the attributes as stated do not imply an ownership.
33/// Never returns OCL_Autoreleasing.
34static Qualifiers::ObjCLifetime getImpliedARCOwnership(
35 ObjCPropertyDecl::PropertyAttributeKind attrs,
36 QualType type) {
37 // retain, strong, copy, weak, and unsafe_unretained are only legal
38 // on properties of retainable pointer type.
39 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
40 ObjCPropertyDecl::OBJC_PR_strong |
41 ObjCPropertyDecl::OBJC_PR_copy)) {
Fariborz Jahanian5fa065b2011-10-13 23:45:45 +000042 return type->getObjCARCImplicitLifetime();
John McCall265941b2011-09-13 18:31:23 +000043 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
44 return Qualifiers::OCL_Weak;
45 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
46 return Qualifiers::OCL_ExplicitNone;
47 }
48
49 // assign can appear on other types, so we have to check the
50 // property type.
51 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
52 type->isObjCRetainableType()) {
53 return Qualifiers::OCL_ExplicitNone;
54 }
55
56 return Qualifiers::OCL_None;
57}
58
John McCallf85e1932011-06-15 23:02:42 +000059/// Check the internal consistency of a property declaration.
60static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
61 if (property->isInvalidDecl()) return;
62
63 ObjCPropertyDecl::PropertyAttributeKind propertyKind
64 = property->getPropertyAttributes();
65 Qualifiers::ObjCLifetime propertyLifetime
66 = property->getType().getObjCLifetime();
67
68 // Nothing to do if we don't have a lifetime.
69 if (propertyLifetime == Qualifiers::OCL_None) return;
70
John McCall265941b2011-09-13 18:31:23 +000071 Qualifiers::ObjCLifetime expectedLifetime
72 = getImpliedARCOwnership(propertyKind, property->getType());
73 if (!expectedLifetime) {
John McCallf85e1932011-06-15 23:02:42 +000074 // We have a lifetime qualifier but no dominating property
John McCall265941b2011-09-13 18:31:23 +000075 // attribute. That's okay, but restore reasonable invariants by
76 // setting the property attribute according to the lifetime
77 // qualifier.
78 ObjCPropertyDecl::PropertyAttributeKind attr;
79 if (propertyLifetime == Qualifiers::OCL_Strong) {
80 attr = ObjCPropertyDecl::OBJC_PR_strong;
81 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
82 attr = ObjCPropertyDecl::OBJC_PR_weak;
83 } else {
84 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
85 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
86 }
87 property->setPropertyAttributes(attr);
John McCallf85e1932011-06-15 23:02:42 +000088 return;
89 }
90
91 if (propertyLifetime == expectedLifetime) return;
92
93 property->setInvalidDecl();
94 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000095 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000096 << property->getDeclName()
John McCall265941b2011-09-13 18:31:23 +000097 << expectedLifetime
John McCallf85e1932011-06-15 23:02:42 +000098 << propertyLifetime;
99}
100
John McCalld226f652010-08-21 09:40:31 +0000101Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
102 FieldDeclarator &FD,
103 ObjCDeclSpec &ODS,
104 Selector GetterSel,
105 Selector SetterSel,
John McCalld226f652010-08-21 09:40:31 +0000106 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000107 tok::ObjCKeywordKind MethodImplKind,
108 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000109 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +0000110 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
111 QualType T = TSI->getType();
Douglas Gregore289d812011-09-13 17:21:33 +0000112 if ((getLangOptions().getGC() != LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +0000113 T.isObjCGCWeak()) ||
114 (getLangOptions().ObjCAutoRefCount &&
115 T.getObjCLifetime() == Qualifiers::OCL_Weak))
116 Attributes |= ObjCDeclSpec::DQ_PR_weak;
117
Ted Kremenek28685ab2010-03-12 00:46:40 +0000118 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
119 // default is readwrite!
120 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
121 // property is defaulted to 'assign' if it is readwrite and is
122 // not retain or copy
123 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
124 (isReadWrite &&
125 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +0000126 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
127 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
128 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
129 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000130
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000131 // Proceed with constructing the ObjCPropertDecls.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000132 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000133
Ted Kremenek28685ab2010-03-12 00:46:40 +0000134 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000135 if (CDecl->IsClassExtension()) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000136 Decl *Res = HandlePropertyInClassExtension(S, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000137 FD, GetterSel, SetterSel,
138 isAssign, isReadWrite,
139 Attributes,
140 isOverridingProperty, TSI,
141 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000142 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000143 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000144 if (getLangOptions().ObjCAutoRefCount)
145 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
146 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000147 return Res;
148 }
149
John McCallf85e1932011-06-15 23:02:42 +0000150 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
151 GetterSel, SetterSel,
152 isAssign, isReadWrite,
153 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000154 if (lexicalDC)
155 Res->setLexicalDeclContext(lexicalDC);
156
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000157 // Validate the attributes on the @property.
158 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000159
160 if (getLangOptions().ObjCAutoRefCount)
161 checkARCPropertyDecl(*this, Res);
162
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000163 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000164}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000165
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000166static ObjCPropertyDecl::PropertyAttributeKind
167makePropertyAttributesAsWritten(unsigned Attributes) {
168 unsigned attributesAsWritten = 0;
169 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
170 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
171 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
172 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
173 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
174 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
175 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
176 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
177 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
179 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
181 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
183 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
185 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
187 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
189 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
191 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
193
194 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
195}
196
John McCalld226f652010-08-21 09:40:31 +0000197Decl *
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000198Sema::HandlePropertyInClassExtension(Scope *S,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000199 SourceLocation AtLoc, FieldDeclarator &FD,
200 Selector GetterSel, Selector SetterSel,
201 const bool isAssign,
202 const bool isReadWrite,
203 const unsigned Attributes,
204 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000205 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000206 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +0000207 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000208 // Diagnose if this property is already in continuation class.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000209 DeclContext *DC = CurContext;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000210 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000211 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
212
213 if (CCPrimary)
214 // Check for duplicate declaration of this property in current and
215 // other class extensions.
216 for (const ObjCCategoryDecl *ClsExtDecl =
217 CCPrimary->getFirstClassExtension();
218 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
219 if (ObjCPropertyDecl *prevDecl =
220 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
221 Diag(AtLoc, diag::err_duplicate_property);
222 Diag(prevDecl->getLocation(), diag::note_property_declare);
223 return 0;
224 }
225 }
226
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000227 // Create a new ObjCPropertyDecl with the DeclContext being
228 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000229 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000230 ObjCPropertyDecl *PDecl =
231 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
232 PropertyId, AtLoc, T);
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000233 PDecl->setPropertyAttributesAsWritten(
234 makePropertyAttributesAsWritten(Attributes));
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000235 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
236 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
237 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
238 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000239 // Set setter/getter selector name. Needed later.
240 PDecl->setGetterName(GetterSel);
241 PDecl->setSetterName(SetterSel);
Douglas Gregor91ae6b42011-07-15 15:30:21 +0000242 ProcessDeclAttributes(S, PDecl, FD.D);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000243 DC->addDecl(PDecl);
244
245 // We need to look in the @interface to see if the @property was
246 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000247 if (!CCPrimary) {
248 Diag(CDecl->getLocation(), diag::err_continuation_class);
249 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000250 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000251 }
252
253 // Find the property in continuation class's primary class only.
254 ObjCPropertyDecl *PIDecl =
255 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
256
257 if (!PIDecl) {
258 // No matching property found in the primary class. Just fall thru
259 // and add property to continuation class's primary class.
260 ObjCPropertyDecl *PDecl =
261 CreatePropertyDecl(S, CCPrimary, AtLoc,
262 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000263 Attributes, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000264
265 // A case of continuation class adding a new property in the class. This
266 // is not what it was meant for. However, gcc supports it and so should we.
267 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000268 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
269 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000270 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000271 }
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000272 if (PIDecl->getType().getCanonicalType()
273 != PDecl->getType().getCanonicalType()) {
274 Diag(AtLoc,
Fariborz Jahanian68af5362011-10-04 18:44:26 +0000275 diag::warn_type_mismatch_continuation_class) << PDecl->getType();
Fariborz Jahaniana4b984d2011-09-24 00:56:59 +0000276 Diag(PIDecl->getLocation(), diag::note_property_declare);
277 }
278
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000279 // The property 'PIDecl's readonly attribute will be over-ridden
280 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000281 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000282 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
283 unsigned retainCopyNonatomic =
284 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000285 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000286 ObjCPropertyDecl::OBJC_PR_copy |
287 ObjCPropertyDecl::OBJC_PR_nonatomic);
288 if ((Attributes & retainCopyNonatomic) !=
289 (PIkind & retainCopyNonatomic)) {
290 Diag(AtLoc, diag::warn_property_attr_mismatch);
291 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000292 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000293 DeclContext *DC = cast<DeclContext>(CCPrimary);
294 if (!ObjCPropertyDecl::findPropertyDecl(DC,
295 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000296 // Protocol is not in the primary class. Must build one for it.
297 ObjCDeclSpec ProtocolPropertyODS;
298 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
299 // and ObjCPropertyDecl::PropertyAttributeKind have identical
300 // values. Should consolidate both into one enum type.
301 ProtocolPropertyODS.
302 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
303 PIkind);
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000304 // Must re-establish the context from class extension to primary
305 // class context.
Fariborz Jahanian79394182011-08-22 20:15:24 +0000306 ContextRAII SavedContext(*this, CCPrimary);
307
John McCalld226f652010-08-21 09:40:31 +0000308 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000309 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
310 PIDecl->getGetterName(),
311 PIDecl->getSetterName(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +0000312 isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000313 MethodImplKind,
314 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000315 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000316 }
317 PIDecl->makeitReadWriteAttribute();
318 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
319 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000320 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
321 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000322 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
323 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
324 PIDecl->setSetterName(SetterSel);
325 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000326 // Tailor the diagnostics for the common case where a readwrite
327 // property is declared both in the @interface and the continuation.
328 // This is a common error where the user often intended the original
329 // declaration to be readonly.
330 unsigned diag =
331 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
332 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
333 ? diag::err_use_continuation_class_redeclaration_readwrite
334 : diag::err_use_continuation_class;
335 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000336 << CCPrimary->getDeclName();
337 Diag(PIDecl->getLocation(), diag::note_property_declare);
338 }
339 *isOverridingProperty = true;
340 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000341 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000342 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000343}
344
345ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
346 ObjCContainerDecl *CDecl,
347 SourceLocation AtLoc,
348 FieldDeclarator &FD,
349 Selector GetterSel,
350 Selector SetterSel,
351 const bool isAssign,
352 const bool isReadWrite,
353 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000354 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000355 tok::ObjCKeywordKind MethodImplKind,
356 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000357 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000358 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000359
360 // Issue a warning if property is 'assign' as default and its object, which is
361 // gc'able conforms to NSCopying protocol
Douglas Gregore289d812011-09-13 17:21:33 +0000362 if (getLangOptions().getGC() != LangOptions::NonGC &&
Ted Kremenek28685ab2010-03-12 00:46:40 +0000363 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000364 if (const ObjCObjectPointerType *ObjPtrTy =
365 T->getAs<ObjCObjectPointerType>()) {
366 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
367 if (IDecl)
368 if (ObjCProtocolDecl* PNSCopying =
369 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
370 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
371 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000372 }
John McCallc12c5bb2010-05-15 11:32:37 +0000373 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000374 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
375
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000376 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000377 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
378 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000379 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000380
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000381 if (ObjCPropertyDecl *prevDecl =
382 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000383 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000384 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000385 PDecl->setInvalidDecl();
386 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000387 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000388 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000389 if (lexicalDC)
390 PDecl->setLexicalDeclContext(lexicalDC);
391 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000392
393 if (T->isArrayType() || T->isFunctionType()) {
394 Diag(AtLoc, diag::err_property_type) << T;
395 PDecl->setInvalidDecl();
396 }
397
398 ProcessDeclAttributes(S, PDecl, FD.D);
399
400 // Regardless of setter/getter attribute, we save the default getter/setter
401 // selector names in anticipation of declaration of setter/getter methods.
402 PDecl->setGetterName(GetterSel);
403 PDecl->setSetterName(SetterSel);
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000404 PDecl->setPropertyAttributesAsWritten(
Argyrios Kyrtzidisb98ffde2011-10-18 19:49:16 +0000405 makePropertyAttributesAsWritten(Attributes));
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000406
Ted Kremenek28685ab2010-03-12 00:46:40 +0000407 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
408 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
409
410 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
411 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
412
413 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
414 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
415
416 if (isReadWrite)
417 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
418
419 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
420 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
421
John McCallf85e1932011-06-15 23:02:42 +0000422 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
423 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
424
425 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
426 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
427
Ted Kremenek28685ab2010-03-12 00:46:40 +0000428 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
429 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
430
John McCallf85e1932011-06-15 23:02:42 +0000431 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
432 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
433
Ted Kremenek28685ab2010-03-12 00:46:40 +0000434 if (isAssign)
435 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
436
John McCall265941b2011-09-13 18:31:23 +0000437 // In the semantic attributes, one of nonatomic or atomic is always set.
Ted Kremenek28685ab2010-03-12 00:46:40 +0000438 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
439 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
John McCall265941b2011-09-13 18:31:23 +0000440 else
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000441 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000442
John McCallf85e1932011-06-15 23:02:42 +0000443 // 'unsafe_unretained' is alias for 'assign'.
444 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
445 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
446 if (isAssign)
447 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
448
Ted Kremenek28685ab2010-03-12 00:46:40 +0000449 if (MethodImplKind == tok::objc_required)
450 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
451 else if (MethodImplKind == tok::objc_optional)
452 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000453
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000454 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000455}
456
John McCallf85e1932011-06-15 23:02:42 +0000457static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
458 ObjCPropertyDecl *property,
459 ObjCIvarDecl *ivar) {
460 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
461
John McCallf85e1932011-06-15 23:02:42 +0000462 QualType ivarType = ivar->getType();
463 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
John McCallf85e1932011-06-15 23:02:42 +0000464
John McCall265941b2011-09-13 18:31:23 +0000465 // The lifetime implied by the property's attributes.
466 Qualifiers::ObjCLifetime propertyLifetime =
467 getImpliedARCOwnership(property->getPropertyAttributes(),
468 property->getType());
John McCallf85e1932011-06-15 23:02:42 +0000469
John McCall265941b2011-09-13 18:31:23 +0000470 // We're fine if they match.
471 if (propertyLifetime == ivarLifetime) return;
John McCallf85e1932011-06-15 23:02:42 +0000472
John McCall265941b2011-09-13 18:31:23 +0000473 // These aren't valid lifetimes for object ivars; don't diagnose twice.
474 if (ivarLifetime == Qualifiers::OCL_None ||
475 ivarLifetime == Qualifiers::OCL_Autoreleasing)
476 return;
John McCallf85e1932011-06-15 23:02:42 +0000477
John McCall265941b2011-09-13 18:31:23 +0000478 switch (propertyLifetime) {
479 case Qualifiers::OCL_Strong:
480 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
481 << property->getDeclName()
482 << ivar->getDeclName()
483 << ivarLifetime;
484 break;
John McCallf85e1932011-06-15 23:02:42 +0000485
John McCall265941b2011-09-13 18:31:23 +0000486 case Qualifiers::OCL_Weak:
487 S.Diag(propertyImplLoc, diag::error_weak_property)
488 << property->getDeclName()
489 << ivar->getDeclName();
490 break;
John McCallf85e1932011-06-15 23:02:42 +0000491
John McCall265941b2011-09-13 18:31:23 +0000492 case Qualifiers::OCL_ExplicitNone:
493 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
494 << property->getDeclName()
495 << ivar->getDeclName()
496 << ((property->getPropertyAttributesAsWritten()
497 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
498 break;
John McCallf85e1932011-06-15 23:02:42 +0000499
John McCall265941b2011-09-13 18:31:23 +0000500 case Qualifiers::OCL_Autoreleasing:
501 llvm_unreachable("properties cannot be autoreleasing");
John McCallf85e1932011-06-15 23:02:42 +0000502
John McCall265941b2011-09-13 18:31:23 +0000503 case Qualifiers::OCL_None:
504 // Any other property should be ignored.
John McCallf85e1932011-06-15 23:02:42 +0000505 return;
506 }
507
508 S.Diag(property->getLocation(), diag::note_property_declare);
509}
510
Ted Kremenek28685ab2010-03-12 00:46:40 +0000511
512/// ActOnPropertyImplDecl - This routine performs semantic checks and
513/// builds the AST node for a property implementation declaration; declared
514/// as @synthesize or @dynamic.
515///
John McCalld226f652010-08-21 09:40:31 +0000516Decl *Sema::ActOnPropertyImplDecl(Scope *S,
517 SourceLocation AtLoc,
518 SourceLocation PropertyLoc,
519 bool Synthesize,
John McCalld226f652010-08-21 09:40:31 +0000520 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000521 IdentifierInfo *PropertyIvar,
522 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000523 ObjCContainerDecl *ClassImpDecl =
Fariborz Jahanian84e0ccf2011-09-19 16:32:32 +0000524 dyn_cast<ObjCContainerDecl>(CurContext);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000525 // Make sure we have a context for the property implementation declaration.
526 if (!ClassImpDecl) {
527 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000528 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000529 }
530 ObjCPropertyDecl *property = 0;
531 ObjCInterfaceDecl* IDecl = 0;
532 // Find the class or category class where this property must have
533 // a declaration.
534 ObjCImplementationDecl *IC = 0;
535 ObjCCategoryImplDecl* CatImplClass = 0;
536 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
537 IDecl = IC->getClassInterface();
538 // We always synthesize an interface for an implementation
539 // without an interface decl. So, IDecl is always non-zero.
540 assert(IDecl &&
541 "ActOnPropertyImplDecl - @implementation without @interface");
542
543 // Look for this property declaration in the @implementation's @interface
544 property = IDecl->FindPropertyDeclaration(PropertyId);
545 if (!property) {
546 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000547 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000548 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000549 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000550 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
551 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000552 if (AtLoc.isValid())
553 Diag(AtLoc, diag::warn_implicit_atomic_property);
554 else
555 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
556 Diag(property->getLocation(), diag::note_property_declare);
557 }
558
Ted Kremenek28685ab2010-03-12 00:46:40 +0000559 if (const ObjCCategoryDecl *CD =
560 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
561 if (!CD->IsClassExtension()) {
562 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
563 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000564 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000565 }
566 }
567 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
568 if (Synthesize) {
569 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000570 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000571 }
572 IDecl = CatImplClass->getClassInterface();
573 if (!IDecl) {
574 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000575 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000576 }
577 ObjCCategoryDecl *Category =
578 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
579
580 // If category for this implementation not found, it is an error which
581 // has already been reported eralier.
582 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000583 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 // Look for this property declaration in @implementation's category
585 property = Category->FindPropertyDeclaration(PropertyId);
586 if (!property) {
587 Diag(PropertyLoc, diag::error_bad_category_property_decl)
588 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000589 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000590 }
591 } else {
592 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000593 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000594 }
595 ObjCIvarDecl *Ivar = 0;
596 // Check that we have a valid, previously declared ivar for @synthesize
597 if (Synthesize) {
598 // @synthesize
599 if (!PropertyIvar)
600 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000601 ObjCPropertyDecl::PropertyAttributeKind kind
602 = property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +0000603 QualType PropType = property->getType();
604
605 QualType PropertyIvarType = PropType.getNonReferenceType();
606
607 // Add GC __weak to the ivar type if the property is weak.
608 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
Douglas Gregore289d812011-09-13 17:21:33 +0000609 getLangOptions().getGC() != LangOptions::NonGC) {
John McCall265941b2011-09-13 18:31:23 +0000610 assert(!getLangOptions().ObjCAutoRefCount);
611 if (PropertyIvarType.isObjCGCStrong()) {
612 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type);
613 Diag(property->getLocation(), diag::note_property_declare);
614 } else {
615 PropertyIvarType =
616 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000617 }
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000618 }
John McCall265941b2011-09-13 18:31:23 +0000619
Ted Kremenek28685ab2010-03-12 00:46:40 +0000620 // Check that this is a previously declared 'ivar' in 'IDecl' interface
621 ObjCInterfaceDecl *ClassDeclared;
622 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
623 if (!Ivar) {
John McCall265941b2011-09-13 18:31:23 +0000624 // In ARC, give the ivar a lifetime qualifier based on the
John McCallf85e1932011-06-15 23:02:42 +0000625 // property attributes.
626 if (getLangOptions().ObjCAutoRefCount &&
John McCall265941b2011-09-13 18:31:23 +0000627 !PropertyIvarType.getObjCLifetime() &&
628 PropertyIvarType->isObjCRetainableType()) {
John McCallf85e1932011-06-15 23:02:42 +0000629
John McCall265941b2011-09-13 18:31:23 +0000630 // It's an error if we have to do this and the user didn't
631 // explicitly write an ownership attribute on the property.
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000632 if (!property->hasWrittenStorageAttribute() &&
John McCall265941b2011-09-13 18:31:23 +0000633 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000634 Diag(PropertyLoc,
635 diag::err_arc_objc_property_default_assign_on_object);
636 Diag(property->getLocation(), diag::note_property_declare);
John McCall265941b2011-09-13 18:31:23 +0000637 } else {
638 Qualifiers::ObjCLifetime lifetime =
639 getImpliedARCOwnership(kind, PropertyIvarType);
640 assert(lifetime && "no lifetime for property?");
Argyrios Kyrtzidis473506b2011-07-26 21:48:26 +0000641
John McCall265941b2011-09-13 18:31:23 +0000642 if (lifetime == Qualifiers::OCL_Weak &&
643 !getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000644 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
645 Diag(property->getLocation(), diag::note_property_declare);
646 }
John McCall265941b2011-09-13 18:31:23 +0000647
John McCallf85e1932011-06-15 23:02:42 +0000648 Qualifiers qs;
John McCall265941b2011-09-13 18:31:23 +0000649 qs.addObjCLifetime(lifetime);
John McCallf85e1932011-06-15 23:02:42 +0000650 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
651 }
John McCallf85e1932011-06-15 23:02:42 +0000652 }
653
654 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
655 !getLangOptions().ObjCAutoRefCount &&
Douglas Gregore289d812011-09-13 17:21:33 +0000656 getLangOptions().getGC() == LangOptions::NonGC) {
John McCallf85e1932011-06-15 23:02:42 +0000657 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
658 Diag(property->getLocation(), diag::note_property_declare);
659 }
660
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000661 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
662 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000663 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000664 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000665 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000666 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000667 IDecl->makeDeclVisibleInContext(Ivar, false);
668 property->setPropertyIvarDecl(Ivar);
669
670 if (!getLangOptions().ObjCNonFragileABI)
671 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
672 // Note! I deliberately want it to fall thru so, we have a
673 // a property implementation and to avoid future warnings.
674 } else if (getLangOptions().ObjCNonFragileABI &&
675 ClassDeclared != IDecl) {
676 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
677 << property->getDeclName() << Ivar->getDeclName()
678 << ClassDeclared->getDeclName();
679 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000680 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000681 // Note! I deliberately want it to fall thru so more errors are caught.
682 }
683 QualType IvarType = Context.getCanonicalType(Ivar->getType());
684
685 // Check that type of property and its ivar are type compatible.
John McCall265941b2011-09-13 18:31:23 +0000686 if (Context.getCanonicalType(PropertyIvarType) != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000687 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000688 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000689 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000690 compat =
691 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000692 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000693 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000694 else {
695 SourceLocation Loc = PropertyIvarLoc;
696 if (Loc.isInvalid())
697 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000698 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000699 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000700 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000701 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000702 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000703 << property->getDeclName() << PropType
704 << Ivar->getDeclName() << IvarType;
705 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000706 // Note! I deliberately want it to fall thru so, we have a
707 // a property implementation and to avoid future warnings.
708 }
709
710 // FIXME! Rules for properties are somewhat different that those
711 // for assignments. Use a new routine to consolidate all cases;
712 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000713 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000714 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
715 if (lhsType != rhsType &&
716 lhsType->isArithmeticType()) {
717 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000718 << property->getDeclName() << PropType
719 << Ivar->getDeclName() << IvarType;
720 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000721 // Fall thru - see previous comment
722 }
723 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000724 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000725 getLangOptions().getGC() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000726 Diag(PropertyLoc, diag::error_weak_property)
727 << property->getDeclName() << Ivar->getDeclName();
Fariborz Jahanianedc08822011-09-07 16:24:21 +0000728 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000729 // Fall thru - see previous comment
730 }
John McCallf85e1932011-06-15 23:02:42 +0000731 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000732 if ((property->getType()->isObjCObjectPointerType() ||
733 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
Douglas Gregore289d812011-09-13 17:21:33 +0000734 getLangOptions().getGC() != LangOptions::NonGC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000735 Diag(PropertyLoc, diag::error_strong_property)
736 << property->getDeclName() << Ivar->getDeclName();
737 // Fall thru - see previous comment
738 }
739 }
John McCallf85e1932011-06-15 23:02:42 +0000740 if (getLangOptions().ObjCAutoRefCount)
741 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000742 } else if (PropertyIvar)
743 // @dynamic
744 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000745
Ted Kremenek28685ab2010-03-12 00:46:40 +0000746 assert (property && "ActOnPropertyImplDecl - property declaration missing");
747 ObjCPropertyImplDecl *PIDecl =
748 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
749 property,
750 (Synthesize ?
751 ObjCPropertyImplDecl::Synthesize
752 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000753 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000754 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
755 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000756 if (getLangOptions().CPlusPlus && Synthesize &&
757 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000758 // For Objective-C++, need to synthesize the AST for the IVAR object to be
759 // returned by the getter as it must conform to C++'s copy-return rules.
760 // FIXME. Eventually we want to do this for Objective-C as well.
761 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
762 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000763 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
764 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000765 Expr *IvarRefExpr =
766 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
767 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000768 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000769 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000770 SourceLocation(),
771 getterMethod->getResultType(),
772 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000773 SourceLocation(),
774 Owned(IvarRefExpr));
775 if (!Res.isInvalid()) {
776 Expr *ResExpr = Res.takeAs<Expr>();
777 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000778 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000779 PIDecl->setGetterCXXConstructor(ResExpr);
780 }
781 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000782 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
783 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
784 Diag(getterMethod->getLocation(),
785 diag::warn_property_getter_owning_mismatch);
786 Diag(property->getLocation(), diag::note_property_declare);
787 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000788 }
789 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
790 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000791 if (getLangOptions().CPlusPlus && Synthesize
792 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000793 // FIXME. Eventually we want to do this for Objective-C as well.
794 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
795 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000796 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
797 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000798 Expr *lhs =
799 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
800 SelfExpr, true, true);
801 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
802 ParmVarDecl *Param = (*P);
John McCall3c3b7f92011-10-25 17:37:35 +0000803 QualType T = Param->getType().getNonReferenceType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000804 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000805 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000806 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000807 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000808 if (property->getPropertyAttributes() &
809 ObjCPropertyDecl::OBJC_PR_atomic) {
810 Expr *callExpr = Res.takeAs<Expr>();
811 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000812 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
813 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000814 if (!FuncDecl->isTrivial())
815 Diag(PropertyLoc,
816 diag::warn_atomic_property_nontrivial_assign_op)
817 << property->getType();
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000818 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000819 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
820 }
821 }
822
Ted Kremenek28685ab2010-03-12 00:46:40 +0000823 if (IC) {
824 if (Synthesize)
825 if (ObjCPropertyImplDecl *PPIDecl =
826 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
827 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
828 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
829 << PropertyIvar;
830 Diag(PPIDecl->getLocation(), diag::note_previous_use);
831 }
832
833 if (ObjCPropertyImplDecl *PPIDecl
834 = IC->FindPropertyImplDecl(PropertyId)) {
835 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
836 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000837 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000838 }
839 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000840 if (getLangOptions().ObjCDefaultSynthProperties &&
841 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000842 // Diagnose if an ivar was lazily synthesdized due to a previous
843 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000844 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000845 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000846 ObjCIvarDecl *Ivar = 0;
847 if (!Synthesize)
848 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
849 else {
850 if (PropertyIvar && PropertyIvar != PropertyId)
851 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
852 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000853 // Issue diagnostics only if Ivar belongs to current class.
854 if (Ivar && Ivar->getSynthesize() &&
855 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000856 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
857 << PropertyId;
858 Ivar->setInvalidDecl();
859 }
860 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000861 } else {
862 if (Synthesize)
863 if (ObjCPropertyImplDecl *PPIDecl =
864 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
865 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
866 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
867 << PropertyIvar;
868 Diag(PPIDecl->getLocation(), diag::note_previous_use);
869 }
870
871 if (ObjCPropertyImplDecl *PPIDecl =
872 CatImplClass->FindPropertyImplDecl(PropertyId)) {
873 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
874 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000875 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000876 }
877 CatImplClass->addPropertyImplementation(PIDecl);
878 }
879
John McCalld226f652010-08-21 09:40:31 +0000880 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000881}
882
883//===----------------------------------------------------------------------===//
884// Helper methods.
885//===----------------------------------------------------------------------===//
886
Ted Kremenek9d64c152010-03-12 00:38:38 +0000887/// DiagnosePropertyMismatch - Compares two properties for their
888/// attributes and types and warns on a variety of inconsistencies.
889///
890void
891Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
892 ObjCPropertyDecl *SuperProperty,
893 const IdentifierInfo *inheritedName) {
894 ObjCPropertyDecl::PropertyAttributeKind CAttr =
895 Property->getPropertyAttributes();
896 ObjCPropertyDecl::PropertyAttributeKind SAttr =
897 SuperProperty->getPropertyAttributes();
898 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
899 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
900 Diag(Property->getLocation(), diag::warn_readonly_property)
901 << Property->getDeclName() << inheritedName;
902 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
903 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
904 Diag(Property->getLocation(), diag::warn_property_attribute)
905 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000906 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000907 unsigned CAttrRetain =
908 (CAttr &
909 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
910 unsigned SAttrRetain =
911 (SAttr &
912 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
913 bool CStrong = (CAttrRetain != 0);
914 bool SStrong = (SAttrRetain != 0);
915 if (CStrong != SStrong)
916 Diag(Property->getLocation(), diag::warn_property_attribute)
917 << Property->getDeclName() << "retain (or strong)" << inheritedName;
918 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000919
920 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
921 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
922 Diag(Property->getLocation(), diag::warn_property_attribute)
923 << Property->getDeclName() << "atomic" << inheritedName;
924 if (Property->getSetterName() != SuperProperty->getSetterName())
925 Diag(Property->getLocation(), diag::warn_property_attribute)
926 << Property->getDeclName() << "setter" << inheritedName;
927 if (Property->getGetterName() != SuperProperty->getGetterName())
928 Diag(Property->getLocation(), diag::warn_property_attribute)
929 << Property->getDeclName() << "getter" << inheritedName;
930
931 QualType LHSType =
932 Context.getCanonicalType(SuperProperty->getType());
933 QualType RHSType =
934 Context.getCanonicalType(Property->getType());
935
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000936 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000937 // Do cases not handled in above.
938 // FIXME. For future support of covariant property types, revisit this.
939 bool IncompatibleObjC = false;
940 QualType ConvertedType;
941 if (!isObjCPointerConversion(RHSType, LHSType,
942 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000943 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000944 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
945 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000946 Diag(SuperProperty->getLocation(), diag::note_property_declare);
947 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000948 }
949}
950
951bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
952 ObjCMethodDecl *GetterMethod,
953 SourceLocation Loc) {
954 if (GetterMethod &&
John McCall3c3b7f92011-10-25 17:37:35 +0000955 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
956 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000957 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000958 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000959 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000960 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000961 if (result != Compatible) {
962 Diag(Loc, diag::warn_accessor_property_type_mismatch)
963 << property->getDeclName()
964 << GetterMethod->getSelector();
965 Diag(GetterMethod->getLocation(), diag::note_declared_at);
966 return true;
967 }
968 }
969 return false;
970}
971
972/// ComparePropertiesInBaseAndSuper - This routine compares property
973/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000974/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000975///
976void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
977 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
978 if (!SDecl)
979 return;
980 // FIXME: O(N^2)
981 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
982 E = SDecl->prop_end(); S != E; ++S) {
983 ObjCPropertyDecl *SuperPDecl = (*S);
984 // Does property in super class has declaration in current class?
985 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
986 E = IDecl->prop_end(); I != E; ++I) {
987 ObjCPropertyDecl *PDecl = (*I);
988 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
989 DiagnosePropertyMismatch(PDecl, SuperPDecl,
990 SDecl->getIdentifier());
991 }
992 }
993}
994
995/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
996/// of properties declared in a protocol and compares their attribute against
997/// the same property declared in the class or category.
998void
999Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1000 ObjCProtocolDecl *PDecl) {
1001 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1002 if (!IDecl) {
1003 // Category
1004 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1005 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1006 if (!CatDecl->IsClassExtension())
1007 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1008 E = PDecl->prop_end(); P != E; ++P) {
1009 ObjCPropertyDecl *Pr = (*P);
1010 ObjCCategoryDecl::prop_iterator CP, CE;
1011 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001012 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001013 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1014 break;
1015 if (CP != CE)
1016 // Property protocol already exist in class. Diagnose any mismatch.
1017 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1018 }
1019 return;
1020 }
1021 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1022 E = PDecl->prop_end(); P != E; ++P) {
1023 ObjCPropertyDecl *Pr = (*P);
1024 ObjCInterfaceDecl::prop_iterator CP, CE;
1025 // Is this property already in class's list of properties?
1026 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1027 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1028 break;
1029 if (CP != CE)
1030 // Property protocol already exist in class. Diagnose any mismatch.
1031 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1032 }
1033}
1034
1035/// CompareProperties - This routine compares properties
1036/// declared in 'ClassOrProtocol' objects (which can be a class or an
1037/// inherited protocol with the list of properties for class/category 'CDecl'
1038///
John McCalld226f652010-08-21 09:40:31 +00001039void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1040 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001041 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1042
1043 if (!IDecl) {
1044 // Category
1045 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1046 assert (CatDecl && "CompareProperties");
1047 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1048 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1049 E = MDecl->protocol_end(); P != E; ++P)
1050 // Match properties of category with those of protocol (*P)
1051 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1052
1053 // Go thru the list of protocols for this category and recursively match
1054 // their properties with those in the category.
1055 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1056 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001057 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001058 } else {
1059 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1060 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1061 E = MD->protocol_end(); P != E; ++P)
1062 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1063 }
1064 return;
1065 }
1066
1067 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001068 for (ObjCInterfaceDecl::all_protocol_iterator
1069 P = MDecl->all_referenced_protocol_begin(),
1070 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001071 // Match properties of class IDecl with those of protocol (*P).
1072 MatchOneProtocolPropertiesInClass(IDecl, *P);
1073
1074 // Go thru the list of protocols for this class and recursively match
1075 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001076 for (ObjCInterfaceDecl::all_protocol_iterator
1077 P = IDecl->all_referenced_protocol_begin(),
1078 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001079 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001080 } else {
1081 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1082 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1083 E = MD->protocol_end(); P != E; ++P)
1084 MatchOneProtocolPropertiesInClass(IDecl, *P);
1085 }
1086}
1087
1088/// isPropertyReadonly - Return true if property is readonly, by searching
1089/// for the property in the class and in its categories and implementations
1090///
1091bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1092 ObjCInterfaceDecl *IDecl) {
1093 // by far the most common case.
1094 if (!PDecl->isReadOnly())
1095 return false;
1096 // Even if property is ready only, if interface has a user defined setter,
1097 // it is not considered read only.
1098 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1099 return false;
1100
1101 // Main class has the property as 'readonly'. Must search
1102 // through the category list to see if the property's
1103 // attribute has been over-ridden to 'readwrite'.
1104 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1105 Category; Category = Category->getNextClassCategory()) {
1106 // Even if property is ready only, if a category has a user defined setter,
1107 // it is not considered read only.
1108 if (Category->getInstanceMethod(PDecl->getSetterName()))
1109 return false;
1110 ObjCPropertyDecl *P =
1111 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1112 if (P && !P->isReadOnly())
1113 return false;
1114 }
1115
1116 // Also, check for definition of a setter method in the implementation if
1117 // all else failed.
1118 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1119 if (ObjCImplementationDecl *IMD =
1120 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1121 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1122 return false;
1123 } else if (ObjCCategoryImplDecl *CIMD =
1124 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1125 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1126 return false;
1127 }
1128 }
1129 // Lastly, look through the implementation (if one is in scope).
1130 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1131 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1132 return false;
1133 // If all fails, look at the super class.
1134 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1135 return isPropertyReadonly(PDecl, SIDecl);
1136 return true;
1137}
1138
1139/// CollectImmediateProperties - This routine collects all properties in
1140/// the class and its conforming protocols; but not those it its super class.
1141void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001142 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1143 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001144 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1145 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1146 E = IDecl->prop_end(); P != E; ++P) {
1147 ObjCPropertyDecl *Prop = (*P);
1148 PropMap[Prop->getIdentifier()] = Prop;
1149 }
1150 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001151 for (ObjCInterfaceDecl::all_protocol_iterator
1152 PI = IDecl->all_referenced_protocol_begin(),
1153 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001154 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 }
1156 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1157 if (!CATDecl->IsClassExtension())
1158 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1159 E = CATDecl->prop_end(); P != E; ++P) {
1160 ObjCPropertyDecl *Prop = (*P);
1161 PropMap[Prop->getIdentifier()] = Prop;
1162 }
1163 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001164 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001165 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001166 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167 }
1168 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1169 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1170 E = PDecl->prop_end(); P != E; ++P) {
1171 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001172 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1173 // Exclude property for protocols which conform to class's super-class,
1174 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001175 if (!PropertyFromSuper ||
1176 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001177 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1178 if (!PropEntry)
1179 PropEntry = Prop;
1180 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001181 }
1182 // scan through protocol's protocols.
1183 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1184 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001185 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001186 }
1187}
1188
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001189/// CollectClassPropertyImplementations - This routine collects list of
1190/// properties to be implemented in the class. This includes, class's
1191/// and its conforming protocols' properties.
1192static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1193 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1194 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1195 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1196 E = IDecl->prop_end(); P != E; ++P) {
1197 ObjCPropertyDecl *Prop = (*P);
1198 PropMap[Prop->getIdentifier()] = Prop;
1199 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001200 for (ObjCInterfaceDecl::all_protocol_iterator
1201 PI = IDecl->all_referenced_protocol_begin(),
1202 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001203 CollectClassPropertyImplementations((*PI), PropMap);
1204 }
1205 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1206 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1207 E = PDecl->prop_end(); P != E; ++P) {
1208 ObjCPropertyDecl *Prop = (*P);
1209 PropMap[Prop->getIdentifier()] = Prop;
1210 }
1211 // scan through protocol's protocols.
1212 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1213 E = PDecl->protocol_end(); PI != E; ++PI)
1214 CollectClassPropertyImplementations((*PI), PropMap);
1215 }
1216}
1217
1218/// CollectSuperClassPropertyImplementations - This routine collects list of
1219/// properties to be implemented in super class(s) and also coming from their
1220/// conforming protocols.
1221static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1222 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1223 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1224 while (SDecl) {
1225 CollectClassPropertyImplementations(SDecl, PropMap);
1226 SDecl = SDecl->getSuperClass();
1227 }
1228 }
1229}
1230
Ted Kremenek9d64c152010-03-12 00:38:38 +00001231/// LookupPropertyDecl - Looks up a property in the current class and all
1232/// its protocols.
1233ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1234 IdentifierInfo *II) {
1235 if (const ObjCInterfaceDecl *IDecl =
1236 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1237 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1238 E = IDecl->prop_end(); P != E; ++P) {
1239 ObjCPropertyDecl *Prop = (*P);
1240 if (Prop->getIdentifier() == II)
1241 return Prop;
1242 }
1243 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001244 for (ObjCInterfaceDecl::all_protocol_iterator
1245 PI = IDecl->all_referenced_protocol_begin(),
1246 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001247 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1248 if (Prop)
1249 return Prop;
1250 }
1251 }
1252 else if (const ObjCProtocolDecl *PDecl =
1253 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1254 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1255 E = PDecl->prop_end(); P != E; ++P) {
1256 ObjCPropertyDecl *Prop = (*P);
1257 if (Prop->getIdentifier() == II)
1258 return Prop;
1259 }
1260 // scan through protocol's protocols.
1261 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1262 E = PDecl->protocol_end(); PI != E; ++PI) {
1263 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1264 if (Prop)
1265 return Prop;
1266 }
1267 }
1268 return 0;
1269}
1270
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001271static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1272 ASTContext &Ctx) {
1273 llvm::SmallString<128> ivarName;
1274 {
1275 llvm::raw_svector_ostream os(ivarName);
1276 os << '_' << Prop->getIdentifier()->getName();
1277 }
1278 return &Ctx.Idents.get(ivarName.str());
1279}
1280
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001281/// DefaultSynthesizeProperties - This routine default synthesizes all
1282/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001283void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1284 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001285
1286 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1287 CollectClassPropertyImplementations(IDecl, PropMap);
1288 if (PropMap.empty())
1289 return;
1290 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1291 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1292
1293 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1294 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1295 ObjCPropertyDecl *Prop = P->second;
1296 // If property to be implemented in the super class, ignore.
1297 if (SuperPropMap[Prop->getIdentifier()])
1298 continue;
1299 // Is there a matching propery synthesize/dynamic?
1300 if (Prop->isInvalidDecl() ||
1301 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1302 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1303 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001304 // Property may have been synthesized by user.
1305 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1306 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001307 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1308 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1309 continue;
1310 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1311 continue;
1312 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001313
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001314
1315 // We use invalid SourceLocations for the synthesized ivars since they
1316 // aren't really synthesized at a particular location; they just exist.
1317 // Saying that they are located at the @implementation isn't really going
1318 // to help users.
1319 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001320 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001321 /* property = */ Prop->getIdentifier(),
1322 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001323 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001324 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001325}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001326
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001327void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1328 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1329 return;
1330 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1331 if (!IC)
1332 return;
1333 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1334 DefaultSynthesizeProperties(S, IC, IDecl);
1335}
1336
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001337void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001338 ObjCContainerDecl *CDecl,
1339 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001340 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1341 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1342 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1343
Ted Kremenek9d64c152010-03-12 00:38:38 +00001344 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001345 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001346 if (PropMap.empty())
1347 return;
1348
1349 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1350 for (ObjCImplDecl::propimpl_iterator
1351 I = IMPDecl->propimpl_begin(),
1352 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1353 PropImplMap.insert((*I)->getPropertyDecl());
1354
1355 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1356 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1357 ObjCPropertyDecl *Prop = P->second;
1358 // Is there a matching propery synthesize/dynamic?
1359 if (Prop->isInvalidDecl() ||
1360 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001361 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001362 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001363 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001364 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001365 isa<ObjCCategoryDecl>(CDecl) ?
1366 diag::warn_setter_getter_impl_required_in_category :
1367 diag::warn_setter_getter_impl_required)
1368 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001369 Diag(Prop->getLocation(),
1370 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001371 }
1372
1373 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001374 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001375 isa<ObjCCategoryDecl>(CDecl) ?
1376 diag::warn_setter_getter_impl_required_in_category :
1377 diag::warn_setter_getter_impl_required)
1378 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001379 Diag(Prop->getLocation(),
1380 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001381 }
1382 }
1383}
1384
1385void
1386Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1387 ObjCContainerDecl* IDecl) {
1388 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001389 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001390 return;
1391 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1392 E = IDecl->prop_end();
1393 I != E; ++I) {
1394 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001395 ObjCMethodDecl *GetterMethod = 0;
1396 ObjCMethodDecl *SetterMethod = 0;
1397 bool LookedUpGetterSetter = false;
1398
Ted Kremenek9d64c152010-03-12 00:38:38 +00001399 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001400 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001401
John McCall265941b2011-09-13 18:31:23 +00001402 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1403 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001404 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1405 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1406 LookedUpGetterSetter = true;
1407 if (GetterMethod) {
1408 Diag(GetterMethod->getLocation(),
1409 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001410 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001411 Diag(Property->getLocation(), diag::note_property_declare);
1412 }
1413 if (SetterMethod) {
1414 Diag(SetterMethod->getLocation(),
1415 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001416 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001417 Diag(Property->getLocation(), diag::note_property_declare);
1418 }
1419 }
1420
Ted Kremenek9d64c152010-03-12 00:38:38 +00001421 // We only care about readwrite atomic property.
1422 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1423 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1424 continue;
1425 if (const ObjCPropertyImplDecl *PIDecl
1426 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1427 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1428 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001429 if (!LookedUpGetterSetter) {
1430 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1431 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1432 LookedUpGetterSetter = true;
1433 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001434 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1435 SourceLocation MethodLoc =
1436 (GetterMethod ? GetterMethod->getLocation()
1437 : SetterMethod->getLocation());
1438 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001439 << Property->getIdentifier() << (GetterMethod != 0)
1440 << (SetterMethod != 0);
1441 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001442 Diag(Property->getLocation(), diag::note_property_declare);
1443 }
1444 }
1445 }
1446}
1447
John McCallf85e1932011-06-15 23:02:42 +00001448void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001449 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001450 return;
1451
1452 for (ObjCImplementationDecl::propimpl_iterator
1453 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1454 ObjCPropertyImplDecl *PID = *i;
1455 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1456 continue;
1457
1458 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001459 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1460 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001461 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1462 if (!method)
1463 continue;
1464 ObjCMethodFamily family = method->getMethodFamily();
1465 if (family == OMF_alloc || family == OMF_copy ||
1466 family == OMF_mutableCopy || family == OMF_new) {
1467 if (getLangOptions().ObjCAutoRefCount)
1468 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1469 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001470 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001471 Diag(PD->getLocation(), diag::note_property_declare);
1472 }
1473 }
1474 }
1475}
1476
John McCall5de74d12010-11-10 07:01:40 +00001477/// AddPropertyAttrs - Propagates attributes from a property to the
1478/// implicitly-declared getter or setter for that property.
1479static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1480 ObjCPropertyDecl *Property) {
1481 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001482 for (Decl::attr_iterator A = Property->attr_begin(),
1483 AEnd = Property->attr_end();
1484 A != AEnd; ++A) {
1485 if (isa<DeprecatedAttr>(*A) ||
1486 isa<UnavailableAttr>(*A) ||
1487 isa<AvailabilityAttr>(*A))
1488 PropertyMethod->addAttr((*A)->clone(S.Context));
1489 }
John McCall5de74d12010-11-10 07:01:40 +00001490}
1491
Ted Kremenek9d64c152010-03-12 00:38:38 +00001492/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1493/// have the property type and issue diagnostics if they don't.
1494/// Also synthesize a getter/setter method if none exist (and update the
1495/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1496/// methods is the "right" thing to do.
1497void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001498 ObjCContainerDecl *CD,
1499 ObjCPropertyDecl *redeclaredProperty,
1500 ObjCContainerDecl *lexicalDC) {
1501
Ted Kremenek9d64c152010-03-12 00:38:38 +00001502 ObjCMethodDecl *GetterMethod, *SetterMethod;
1503
1504 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1505 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1506 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1507 property->getLocation());
1508
1509 if (SetterMethod) {
1510 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1511 property->getPropertyAttributes();
1512 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1513 Context.getCanonicalType(SetterMethod->getResultType()) !=
1514 Context.VoidTy)
1515 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1516 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001517 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001518 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1519 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001520 Diag(property->getLocation(),
1521 diag::warn_accessor_property_type_mismatch)
1522 << property->getDeclName()
1523 << SetterMethod->getSelector();
1524 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1525 }
1526 }
1527
1528 // Synthesize getter/setter methods if none exist.
1529 // Find the default getter and if one not found, add one.
1530 // FIXME: The synthesized property we set here is misleading. We almost always
1531 // synthesize these methods unless the user explicitly provided prototypes
1532 // (which is odd, but allowed). Sema should be typechecking that the
1533 // declarations jive in that situation (which it is not currently).
1534 if (!GetterMethod) {
1535 // No instance method of same name as property getter name was found.
1536 // Declare a getter method and add it to the list of methods
1537 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001538 SourceLocation Loc = redeclaredProperty ?
1539 redeclaredProperty->getLocation() :
1540 property->getLocation();
1541
1542 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1543 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001544 property->getType(), 0, CD, /*isInstance=*/true,
1545 /*isVariadic=*/false, /*isSynthesized=*/true,
1546 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001547 (property->getPropertyImplementation() ==
1548 ObjCPropertyDecl::Optional) ?
1549 ObjCMethodDecl::Optional :
1550 ObjCMethodDecl::Required);
1551 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001552
1553 AddPropertyAttrs(*this, GetterMethod, property);
1554
Ted Kremenek23173d72010-05-18 21:09:07 +00001555 // FIXME: Eventually this shouldn't be needed, as the lexical context
1556 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001557 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001558 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001559 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1560 GetterMethod->addAttr(
1561 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001562 } else
1563 // A user declared getter will be synthesize when @synthesize of
1564 // the property with the same name is seen in the @implementation
1565 GetterMethod->setSynthesized(true);
1566 property->setGetterMethodDecl(GetterMethod);
1567
1568 // Skip setter if property is read-only.
1569 if (!property->isReadOnly()) {
1570 // Find the default setter and if one not found, add one.
1571 if (!SetterMethod) {
1572 // No instance method of same name as property setter name was found.
1573 // Declare a setter method and add it to the list of methods
1574 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001575 SourceLocation Loc = redeclaredProperty ?
1576 redeclaredProperty->getLocation() :
1577 property->getLocation();
1578
1579 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001580 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001581 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001582 CD, /*isInstance=*/true, /*isVariadic=*/false,
1583 /*isSynthesized=*/true,
1584 /*isImplicitlyDeclared=*/true,
1585 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001586 (property->getPropertyImplementation() ==
1587 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001588 ObjCMethodDecl::Optional :
1589 ObjCMethodDecl::Required);
1590
Ted Kremenek9d64c152010-03-12 00:38:38 +00001591 // Invent the arguments for the setter. We don't bother making a
1592 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001593 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1594 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001595 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001596 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001597 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001598 SC_None,
1599 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001600 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001601 SetterMethod->setMethodParams(Context, Argument,
1602 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001603
1604 AddPropertyAttrs(*this, SetterMethod, property);
1605
Ted Kremenek9d64c152010-03-12 00:38:38 +00001606 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001607 // FIXME: Eventually this shouldn't be needed, as the lexical context
1608 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001609 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001610 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001611 } else
1612 // A user declared setter will be synthesize when @synthesize of
1613 // the property with the same name is seen in the @implementation
1614 SetterMethod->setSynthesized(true);
1615 property->setSetterMethodDecl(SetterMethod);
1616 }
1617 // Add any synthesized methods to the global pool. This allows us to
1618 // handle the following, which is supported by GCC (and part of the design).
1619 //
1620 // @interface Foo
1621 // @property double bar;
1622 // @end
1623 //
1624 // void thisIsUnfortunate() {
1625 // id foo;
1626 // double bar = [foo bar];
1627 // }
1628 //
1629 if (GetterMethod)
1630 AddInstanceMethodToGlobalPool(GetterMethod);
1631 if (SetterMethod)
1632 AddInstanceMethodToGlobalPool(SetterMethod);
1633}
1634
John McCalld226f652010-08-21 09:40:31 +00001635void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001636 SourceLocation Loc,
1637 unsigned &Attributes) {
1638 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001639 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001640 return;
1641
1642 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001643 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001644
1645 // readonly and readwrite/assign/retain/copy conflict.
1646 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1647 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1648 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001649 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001650 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001651 ObjCDeclSpec::DQ_PR_retain |
1652 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001653 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1654 "readwrite" :
1655 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1656 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001657 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1658 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001659 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1660 "copy" : "retain";
1661
1662 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1663 diag::err_objc_property_attr_mutually_exclusive :
1664 diag::warn_objc_property_attr_mutually_exclusive)
1665 << "readonly" << which;
1666 }
1667
1668 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001669 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1670 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1671 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001672 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001673 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001674 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1675 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1676 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1677 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001678 }
1679
1680 // Check for more than one of { assign, copy, retain }.
1681 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1682 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1684 << "assign" << "copy";
1685 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1686 }
1687 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1688 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1689 << "assign" << "retain";
1690 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1691 }
John McCallf85e1932011-06-15 23:02:42 +00001692 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1693 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1694 << "assign" << "strong";
1695 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1696 }
1697 if (getLangOptions().ObjCAutoRefCount &&
1698 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1699 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1700 << "assign" << "weak";
1701 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1702 }
1703 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1704 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1705 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1706 << "unsafe_unretained" << "copy";
1707 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1708 }
1709 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1710 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1711 << "unsafe_unretained" << "retain";
1712 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1713 }
1714 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1715 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1716 << "unsafe_unretained" << "strong";
1717 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1718 }
1719 if (getLangOptions().ObjCAutoRefCount &&
1720 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1721 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1722 << "unsafe_unretained" << "weak";
1723 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1724 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001725 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1726 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1727 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1728 << "copy" << "retain";
1729 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1730 }
John McCallf85e1932011-06-15 23:02:42 +00001731 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1732 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1733 << "copy" << "strong";
1734 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1735 }
1736 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1737 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1738 << "copy" << "weak";
1739 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1740 }
1741 }
1742 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1743 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1744 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1745 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001746 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001747 }
1748 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1749 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1750 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1751 << "strong" << "weak";
1752 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001753 }
1754
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001755 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1756 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1757 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1758 << "atomic" << "nonatomic";
1759 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1760 }
1761
Ted Kremenek9d64c152010-03-12 00:38:38 +00001762 // Warn if user supplied no assignment attribute, property is
1763 // readwrite, and this is an object type.
1764 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001765 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1766 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1767 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001768 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1769 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001770 if (getLangOptions().ObjCAutoRefCount)
1771 // With arc, @property definitions should default to (strong) when
1772 // not specified
1773 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1774 else {
1775 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001776 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001777 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001778
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001779 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001780 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001781 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1782 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001783
1784 // FIXME: Implement warning dependent on NSCopying being
1785 // implemented. See also:
1786 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1787 // (please trim this list while you are at it).
1788 }
1789
1790 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001791 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001792 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001793 && PropertyTy->isBlockPointerType())
1794 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001795 else if (getLangOptions().ObjCAutoRefCount &&
1796 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1797 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1798 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1799 PropertyTy->isBlockPointerType())
1800 Diag(Loc, diag::warn_objc_property_retain_of_block);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001801}