blob: 7e9bb46cd6099bc614f9e5578f9a5259ec602387 [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);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000803 QualType T = Param->getType();
804 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000805 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000806 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000807 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000808 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000809 BO_Assign, lhs, rhs);
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000810 if (property->getPropertyAttributes() &
811 ObjCPropertyDecl::OBJC_PR_atomic) {
812 Expr *callExpr = Res.takeAs<Expr>();
813 if (const CXXOperatorCallExpr *CXXCE =
Fariborz Jahanian13bf6332011-10-07 21:08:14 +0000814 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
815 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000816 if (!FuncDecl->isTrivial())
817 Diag(PropertyLoc,
818 diag::warn_atomic_property_nontrivial_assign_op)
819 << property->getType();
Fariborz Jahanian57e264e2011-10-06 18:38:18 +0000820 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000821 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
822 }
823 }
824
Ted Kremenek28685ab2010-03-12 00:46:40 +0000825 if (IC) {
826 if (Synthesize)
827 if (ObjCPropertyImplDecl *PPIDecl =
828 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
829 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
830 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
831 << PropertyIvar;
832 Diag(PPIDecl->getLocation(), diag::note_previous_use);
833 }
834
835 if (ObjCPropertyImplDecl *PPIDecl
836 = IC->FindPropertyImplDecl(PropertyId)) {
837 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
838 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000839 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000840 }
841 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000842 if (getLangOptions().ObjCDefaultSynthProperties &&
843 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000844 // Diagnose if an ivar was lazily synthesdized due to a previous
845 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000846 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000847 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000848 ObjCIvarDecl *Ivar = 0;
849 if (!Synthesize)
850 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
851 else {
852 if (PropertyIvar && PropertyIvar != PropertyId)
853 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
854 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000855 // Issue diagnostics only if Ivar belongs to current class.
856 if (Ivar && Ivar->getSynthesize() &&
857 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000858 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
859 << PropertyId;
860 Ivar->setInvalidDecl();
861 }
862 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000863 } else {
864 if (Synthesize)
865 if (ObjCPropertyImplDecl *PPIDecl =
866 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
867 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
868 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
869 << PropertyIvar;
870 Diag(PPIDecl->getLocation(), diag::note_previous_use);
871 }
872
873 if (ObjCPropertyImplDecl *PPIDecl =
874 CatImplClass->FindPropertyImplDecl(PropertyId)) {
875 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
876 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000877 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000878 }
879 CatImplClass->addPropertyImplementation(PIDecl);
880 }
881
John McCalld226f652010-08-21 09:40:31 +0000882 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000883}
884
885//===----------------------------------------------------------------------===//
886// Helper methods.
887//===----------------------------------------------------------------------===//
888
Ted Kremenek9d64c152010-03-12 00:38:38 +0000889/// DiagnosePropertyMismatch - Compares two properties for their
890/// attributes and types and warns on a variety of inconsistencies.
891///
892void
893Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
894 ObjCPropertyDecl *SuperProperty,
895 const IdentifierInfo *inheritedName) {
896 ObjCPropertyDecl::PropertyAttributeKind CAttr =
897 Property->getPropertyAttributes();
898 ObjCPropertyDecl::PropertyAttributeKind SAttr =
899 SuperProperty->getPropertyAttributes();
900 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
901 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
902 Diag(Property->getLocation(), diag::warn_readonly_property)
903 << Property->getDeclName() << inheritedName;
904 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
905 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
906 Diag(Property->getLocation(), diag::warn_property_attribute)
907 << Property->getDeclName() << "copy" << inheritedName;
Fariborz Jahanian1b46d8d2011-10-08 17:45:33 +0000908 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
John McCallf85e1932011-06-15 23:02:42 +0000909 unsigned CAttrRetain =
910 (CAttr &
911 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
912 unsigned SAttrRetain =
913 (SAttr &
914 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
915 bool CStrong = (CAttrRetain != 0);
916 bool SStrong = (SAttrRetain != 0);
917 if (CStrong != SStrong)
918 Diag(Property->getLocation(), diag::warn_property_attribute)
919 << Property->getDeclName() << "retain (or strong)" << inheritedName;
920 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000921
922 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
923 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
924 Diag(Property->getLocation(), diag::warn_property_attribute)
925 << Property->getDeclName() << "atomic" << inheritedName;
926 if (Property->getSetterName() != SuperProperty->getSetterName())
927 Diag(Property->getLocation(), diag::warn_property_attribute)
928 << Property->getDeclName() << "setter" << inheritedName;
929 if (Property->getGetterName() != SuperProperty->getGetterName())
930 Diag(Property->getLocation(), diag::warn_property_attribute)
931 << Property->getDeclName() << "getter" << inheritedName;
932
933 QualType LHSType =
934 Context.getCanonicalType(SuperProperty->getType());
935 QualType RHSType =
936 Context.getCanonicalType(Property->getType());
937
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000938 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000939 // Do cases not handled in above.
940 // FIXME. For future support of covariant property types, revisit this.
941 bool IncompatibleObjC = false;
942 QualType ConvertedType;
943 if (!isObjCPointerConversion(RHSType, LHSType,
944 ConvertedType, IncompatibleObjC) ||
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000945 IncompatibleObjC) {
Fariborz Jahanian8beb6a22011-07-13 17:55:01 +0000946 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
947 << Property->getType() << SuperProperty->getType() << inheritedName;
Fariborz Jahanian13546a82011-10-12 00:00:57 +0000948 Diag(SuperProperty->getLocation(), diag::note_property_declare);
949 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000950 }
951}
952
953bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
954 ObjCMethodDecl *GetterMethod,
955 SourceLocation Loc) {
956 if (GetterMethod &&
Fariborz Jahanianbb13c322011-10-15 17:36:49 +0000957 GetterMethod->getResultType().getNonReferenceType()
958 != property->getType().getNonReferenceType()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000959 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000960 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000961 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000962 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000963 if (result != Compatible) {
964 Diag(Loc, diag::warn_accessor_property_type_mismatch)
965 << property->getDeclName()
966 << GetterMethod->getSelector();
967 Diag(GetterMethod->getLocation(), diag::note_declared_at);
968 return true;
969 }
970 }
971 return false;
972}
973
974/// ComparePropertiesInBaseAndSuper - This routine compares property
975/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000976/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000977///
978void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
979 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
980 if (!SDecl)
981 return;
982 // FIXME: O(N^2)
983 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
984 E = SDecl->prop_end(); S != E; ++S) {
985 ObjCPropertyDecl *SuperPDecl = (*S);
986 // Does property in super class has declaration in current class?
987 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
988 E = IDecl->prop_end(); I != E; ++I) {
989 ObjCPropertyDecl *PDecl = (*I);
990 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
991 DiagnosePropertyMismatch(PDecl, SuperPDecl,
992 SDecl->getIdentifier());
993 }
994 }
995}
996
997/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
998/// of properties declared in a protocol and compares their attribute against
999/// the same property declared in the class or category.
1000void
1001Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
1002 ObjCProtocolDecl *PDecl) {
1003 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1004 if (!IDecl) {
1005 // Category
1006 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1007 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
1008 if (!CatDecl->IsClassExtension())
1009 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1010 E = PDecl->prop_end(); P != E; ++P) {
1011 ObjCPropertyDecl *Pr = (*P);
1012 ObjCCategoryDecl::prop_iterator CP, CE;
1013 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +00001014 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001015 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1016 break;
1017 if (CP != CE)
1018 // Property protocol already exist in class. Diagnose any mismatch.
1019 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1020 }
1021 return;
1022 }
1023 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1024 E = PDecl->prop_end(); P != E; ++P) {
1025 ObjCPropertyDecl *Pr = (*P);
1026 ObjCInterfaceDecl::prop_iterator CP, CE;
1027 // Is this property already in class's list of properties?
1028 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
1029 if ((*CP)->getIdentifier() == Pr->getIdentifier())
1030 break;
1031 if (CP != CE)
1032 // Property protocol already exist in class. Diagnose any mismatch.
1033 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1034 }
1035}
1036
1037/// CompareProperties - This routine compares properties
1038/// declared in 'ClassOrProtocol' objects (which can be a class or an
1039/// inherited protocol with the list of properties for class/category 'CDecl'
1040///
John McCalld226f652010-08-21 09:40:31 +00001041void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1042 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001043 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1044
1045 if (!IDecl) {
1046 // Category
1047 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1048 assert (CatDecl && "CompareProperties");
1049 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1050 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1051 E = MDecl->protocol_end(); P != E; ++P)
1052 // Match properties of category with those of protocol (*P)
1053 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1054
1055 // Go thru the list of protocols for this category and recursively match
1056 // their properties with those in the category.
1057 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1058 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001059 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001060 } else {
1061 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1062 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1063 E = MD->protocol_end(); P != E; ++P)
1064 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1065 }
1066 return;
1067 }
1068
1069 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001070 for (ObjCInterfaceDecl::all_protocol_iterator
1071 P = MDecl->all_referenced_protocol_begin(),
1072 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001073 // Match properties of class IDecl with those of protocol (*P).
1074 MatchOneProtocolPropertiesInClass(IDecl, *P);
1075
1076 // Go thru the list of protocols for this class and recursively match
1077 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001078 for (ObjCInterfaceDecl::all_protocol_iterator
1079 P = IDecl->all_referenced_protocol_begin(),
1080 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001081 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001082 } else {
1083 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1084 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1085 E = MD->protocol_end(); P != E; ++P)
1086 MatchOneProtocolPropertiesInClass(IDecl, *P);
1087 }
1088}
1089
1090/// isPropertyReadonly - Return true if property is readonly, by searching
1091/// for the property in the class and in its categories and implementations
1092///
1093bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1094 ObjCInterfaceDecl *IDecl) {
1095 // by far the most common case.
1096 if (!PDecl->isReadOnly())
1097 return false;
1098 // Even if property is ready only, if interface has a user defined setter,
1099 // it is not considered read only.
1100 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1101 return false;
1102
1103 // Main class has the property as 'readonly'. Must search
1104 // through the category list to see if the property's
1105 // attribute has been over-ridden to 'readwrite'.
1106 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1107 Category; Category = Category->getNextClassCategory()) {
1108 // Even if property is ready only, if a category has a user defined setter,
1109 // it is not considered read only.
1110 if (Category->getInstanceMethod(PDecl->getSetterName()))
1111 return false;
1112 ObjCPropertyDecl *P =
1113 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1114 if (P && !P->isReadOnly())
1115 return false;
1116 }
1117
1118 // Also, check for definition of a setter method in the implementation if
1119 // all else failed.
1120 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1121 if (ObjCImplementationDecl *IMD =
1122 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1123 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1124 return false;
1125 } else if (ObjCCategoryImplDecl *CIMD =
1126 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1127 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1128 return false;
1129 }
1130 }
1131 // Lastly, look through the implementation (if one is in scope).
1132 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1133 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1134 return false;
1135 // If all fails, look at the super class.
1136 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1137 return isPropertyReadonly(PDecl, SIDecl);
1138 return true;
1139}
1140
1141/// CollectImmediateProperties - This routine collects all properties in
1142/// the class and its conforming protocols; but not those it its super class.
1143void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001144 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1145 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001146 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1147 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1148 E = IDecl->prop_end(); P != E; ++P) {
1149 ObjCPropertyDecl *Prop = (*P);
1150 PropMap[Prop->getIdentifier()] = Prop;
1151 }
1152 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001153 for (ObjCInterfaceDecl::all_protocol_iterator
1154 PI = IDecl->all_referenced_protocol_begin(),
1155 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001156 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001157 }
1158 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1159 if (!CATDecl->IsClassExtension())
1160 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1161 E = CATDecl->prop_end(); P != E; ++P) {
1162 ObjCPropertyDecl *Prop = (*P);
1163 PropMap[Prop->getIdentifier()] = Prop;
1164 }
1165 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001166 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001167 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001168 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001169 }
1170 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1171 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1172 E = PDecl->prop_end(); P != E; ++P) {
1173 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001174 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1175 // Exclude property for protocols which conform to class's super-class,
1176 // as super-class has to implement the property.
Fariborz Jahaniana929ec72011-09-27 00:23:52 +00001177 if (!PropertyFromSuper ||
1178 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001179 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1180 if (!PropEntry)
1181 PropEntry = Prop;
1182 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001183 }
1184 // scan through protocol's protocols.
1185 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1186 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001187 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001188 }
1189}
1190
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001191/// CollectClassPropertyImplementations - This routine collects list of
1192/// properties to be implemented in the class. This includes, class's
1193/// and its conforming protocols' properties.
1194static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1195 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1196 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1197 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1198 E = IDecl->prop_end(); P != E; ++P) {
1199 ObjCPropertyDecl *Prop = (*P);
1200 PropMap[Prop->getIdentifier()] = Prop;
1201 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001202 for (ObjCInterfaceDecl::all_protocol_iterator
1203 PI = IDecl->all_referenced_protocol_begin(),
1204 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001205 CollectClassPropertyImplementations((*PI), PropMap);
1206 }
1207 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1208 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1209 E = PDecl->prop_end(); P != E; ++P) {
1210 ObjCPropertyDecl *Prop = (*P);
1211 PropMap[Prop->getIdentifier()] = Prop;
1212 }
1213 // scan through protocol's protocols.
1214 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1215 E = PDecl->protocol_end(); PI != E; ++PI)
1216 CollectClassPropertyImplementations((*PI), PropMap);
1217 }
1218}
1219
1220/// CollectSuperClassPropertyImplementations - This routine collects list of
1221/// properties to be implemented in super class(s) and also coming from their
1222/// conforming protocols.
1223static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1224 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1225 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1226 while (SDecl) {
1227 CollectClassPropertyImplementations(SDecl, PropMap);
1228 SDecl = SDecl->getSuperClass();
1229 }
1230 }
1231}
1232
Ted Kremenek9d64c152010-03-12 00:38:38 +00001233/// LookupPropertyDecl - Looks up a property in the current class and all
1234/// its protocols.
1235ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1236 IdentifierInfo *II) {
1237 if (const ObjCInterfaceDecl *IDecl =
1238 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1239 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1240 E = IDecl->prop_end(); P != E; ++P) {
1241 ObjCPropertyDecl *Prop = (*P);
1242 if (Prop->getIdentifier() == II)
1243 return Prop;
1244 }
1245 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001246 for (ObjCInterfaceDecl::all_protocol_iterator
1247 PI = IDecl->all_referenced_protocol_begin(),
1248 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001249 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1250 if (Prop)
1251 return Prop;
1252 }
1253 }
1254 else if (const ObjCProtocolDecl *PDecl =
1255 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1256 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1257 E = PDecl->prop_end(); P != E; ++P) {
1258 ObjCPropertyDecl *Prop = (*P);
1259 if (Prop->getIdentifier() == II)
1260 return Prop;
1261 }
1262 // scan through protocol's protocols.
1263 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1264 E = PDecl->protocol_end(); PI != E; ++PI) {
1265 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1266 if (Prop)
1267 return Prop;
1268 }
1269 }
1270 return 0;
1271}
1272
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001273static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop,
1274 ASTContext &Ctx) {
1275 llvm::SmallString<128> ivarName;
1276 {
1277 llvm::raw_svector_ostream os(ivarName);
1278 os << '_' << Prop->getIdentifier()->getName();
1279 }
1280 return &Ctx.Idents.get(ivarName.str());
1281}
1282
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001283/// DefaultSynthesizeProperties - This routine default synthesizes all
1284/// properties which must be synthesized in class's @implementation.
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001285void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1286 ObjCInterfaceDecl *IDecl) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001287
1288 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1289 CollectClassPropertyImplementations(IDecl, PropMap);
1290 if (PropMap.empty())
1291 return;
1292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1293 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1294
1295 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1296 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1297 ObjCPropertyDecl *Prop = P->second;
1298 // If property to be implemented in the super class, ignore.
1299 if (SuperPropMap[Prop->getIdentifier()])
1300 continue;
1301 // Is there a matching propery synthesize/dynamic?
1302 if (Prop->isInvalidDecl() ||
1303 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1304 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1305 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001306 // Property may have been synthesized by user.
1307 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1308 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001309 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1310 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1311 continue;
1312 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1313 continue;
1314 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001315
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001316
1317 // We use invalid SourceLocations for the synthesized ivars since they
1318 // aren't really synthesized at a particular location; they just exist.
1319 // Saying that they are located at the @implementation isn't really going
1320 // to help users.
1321 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00001322 true,
Ted Kremenekd2ee8092011-09-27 23:39:40 +00001323 /* property = */ Prop->getIdentifier(),
1324 /* ivar = */ getDefaultSynthIvarName(Prop, Context),
Douglas Gregora4ffd852010-11-17 01:03:52 +00001325 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001326 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001327}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001328
Fariborz Jahanian8697d302011-08-31 22:24:06 +00001329void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1330 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2)
1331 return;
1332 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1333 if (!IC)
1334 return;
1335 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1336 DefaultSynthesizeProperties(S, IC, IDecl);
1337}
1338
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001339void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001340 ObjCContainerDecl *CDecl,
1341 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001342 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1343 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1344 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1345
Ted Kremenek9d64c152010-03-12 00:38:38 +00001346 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001347 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001348 if (PropMap.empty())
1349 return;
1350
1351 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1352 for (ObjCImplDecl::propimpl_iterator
1353 I = IMPDecl->propimpl_begin(),
1354 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1355 PropImplMap.insert((*I)->getPropertyDecl());
1356
1357 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1358 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1359 ObjCPropertyDecl *Prop = P->second;
1360 // Is there a matching propery synthesize/dynamic?
1361 if (Prop->isInvalidDecl() ||
1362 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001363 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001364 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001365 if (!InsMap.count(Prop->getGetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001366 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001367 isa<ObjCCategoryDecl>(CDecl) ?
1368 diag::warn_setter_getter_impl_required_in_category :
1369 diag::warn_setter_getter_impl_required)
1370 << Prop->getDeclName() << Prop->getGetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001371 Diag(Prop->getLocation(),
1372 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001373 }
1374
1375 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001376 Diag(IMPDecl->getLocation(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001377 isa<ObjCCategoryDecl>(CDecl) ?
1378 diag::warn_setter_getter_impl_required_in_category :
1379 diag::warn_setter_getter_impl_required)
1380 << Prop->getDeclName() << Prop->getSetterName();
Fariborz Jahanianb8607392011-08-27 21:55:47 +00001381 Diag(Prop->getLocation(),
1382 diag::note_property_declare);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001383 }
1384 }
1385}
1386
1387void
1388Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1389 ObjCContainerDecl* IDecl) {
1390 // Rules apply in non-GC mode only
Douglas Gregore289d812011-09-13 17:21:33 +00001391 if (getLangOptions().getGC() != LangOptions::NonGC)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001392 return;
1393 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1394 E = IDecl->prop_end();
1395 I != E; ++I) {
1396 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001397 ObjCMethodDecl *GetterMethod = 0;
1398 ObjCMethodDecl *SetterMethod = 0;
1399 bool LookedUpGetterSetter = false;
1400
Ted Kremenek9d64c152010-03-12 00:38:38 +00001401 unsigned Attributes = Property->getPropertyAttributes();
John McCall265941b2011-09-13 18:31:23 +00001402 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001403
John McCall265941b2011-09-13 18:31:23 +00001404 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1405 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001406 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1407 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1408 LookedUpGetterSetter = true;
1409 if (GetterMethod) {
1410 Diag(GetterMethod->getLocation(),
1411 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001412 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001413 Diag(Property->getLocation(), diag::note_property_declare);
1414 }
1415 if (SetterMethod) {
1416 Diag(SetterMethod->getLocation(),
1417 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001418 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001419 Diag(Property->getLocation(), diag::note_property_declare);
1420 }
1421 }
1422
Ted Kremenek9d64c152010-03-12 00:38:38 +00001423 // We only care about readwrite atomic property.
1424 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1425 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1426 continue;
1427 if (const ObjCPropertyImplDecl *PIDecl
1428 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1429 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1430 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001431 if (!LookedUpGetterSetter) {
1432 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1433 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1434 LookedUpGetterSetter = true;
1435 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001436 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1437 SourceLocation MethodLoc =
1438 (GetterMethod ? GetterMethod->getLocation()
1439 : SetterMethod->getLocation());
1440 Diag(MethodLoc, diag::warn_atomic_property_rule)
Fariborz Jahanian7d65f692011-10-06 23:47:58 +00001441 << Property->getIdentifier() << (GetterMethod != 0)
1442 << (SetterMethod != 0);
1443 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001444 Diag(Property->getLocation(), diag::note_property_declare);
1445 }
1446 }
1447 }
1448}
1449
John McCallf85e1932011-06-15 23:02:42 +00001450void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
Douglas Gregore289d812011-09-13 17:21:33 +00001451 if (getLangOptions().getGC() == LangOptions::GCOnly)
John McCallf85e1932011-06-15 23:02:42 +00001452 return;
1453
1454 for (ObjCImplementationDecl::propimpl_iterator
1455 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1456 ObjCPropertyImplDecl *PID = *i;
1457 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1458 continue;
1459
1460 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001461 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1462 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001463 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1464 if (!method)
1465 continue;
1466 ObjCMethodFamily family = method->getMethodFamily();
1467 if (family == OMF_alloc || family == OMF_copy ||
1468 family == OMF_mutableCopy || family == OMF_new) {
1469 if (getLangOptions().ObjCAutoRefCount)
1470 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1471 else
Ted Kremenek920c9c12011-10-12 18:03:37 +00001472 Diag(PID->getLocation(), diag::warn_owning_getter_rule);
John McCallf85e1932011-06-15 23:02:42 +00001473 Diag(PD->getLocation(), diag::note_property_declare);
1474 }
1475 }
1476 }
1477}
1478
John McCall5de74d12010-11-10 07:01:40 +00001479/// AddPropertyAttrs - Propagates attributes from a property to the
1480/// implicitly-declared getter or setter for that property.
1481static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1482 ObjCPropertyDecl *Property) {
1483 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001484 for (Decl::attr_iterator A = Property->attr_begin(),
1485 AEnd = Property->attr_end();
1486 A != AEnd; ++A) {
1487 if (isa<DeprecatedAttr>(*A) ||
1488 isa<UnavailableAttr>(*A) ||
1489 isa<AvailabilityAttr>(*A))
1490 PropertyMethod->addAttr((*A)->clone(S.Context));
1491 }
John McCall5de74d12010-11-10 07:01:40 +00001492}
1493
Ted Kremenek9d64c152010-03-12 00:38:38 +00001494/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1495/// have the property type and issue diagnostics if they don't.
1496/// Also synthesize a getter/setter method if none exist (and update the
1497/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1498/// methods is the "right" thing to do.
1499void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001500 ObjCContainerDecl *CD,
1501 ObjCPropertyDecl *redeclaredProperty,
1502 ObjCContainerDecl *lexicalDC) {
1503
Ted Kremenek9d64c152010-03-12 00:38:38 +00001504 ObjCMethodDecl *GetterMethod, *SetterMethod;
1505
1506 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1507 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1508 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1509 property->getLocation());
1510
1511 if (SetterMethod) {
1512 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1513 property->getPropertyAttributes();
1514 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1515 Context.getCanonicalType(SetterMethod->getResultType()) !=
1516 Context.VoidTy)
1517 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1518 if (SetterMethod->param_size() != 1 ||
Fariborz Jahanian2aac0c92011-09-26 22:59:09 +00001519 !Context.hasSameUnqualifiedType(
Fariborz Jahanianbb13c322011-10-15 17:36:49 +00001520 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
1521 property->getType().getNonReferenceType())) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001522 Diag(property->getLocation(),
1523 diag::warn_accessor_property_type_mismatch)
1524 << property->getDeclName()
1525 << SetterMethod->getSelector();
1526 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1527 }
1528 }
1529
1530 // Synthesize getter/setter methods if none exist.
1531 // Find the default getter and if one not found, add one.
1532 // FIXME: The synthesized property we set here is misleading. We almost always
1533 // synthesize these methods unless the user explicitly provided prototypes
1534 // (which is odd, but allowed). Sema should be typechecking that the
1535 // declarations jive in that situation (which it is not currently).
1536 if (!GetterMethod) {
1537 // No instance method of same name as property getter name was found.
1538 // Declare a getter method and add it to the list of methods
1539 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001540 SourceLocation Loc = redeclaredProperty ?
1541 redeclaredProperty->getLocation() :
1542 property->getLocation();
1543
1544 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1545 property->getGetterName(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001546 property->getType(), 0, CD, /*isInstance=*/true,
1547 /*isVariadic=*/false, /*isSynthesized=*/true,
1548 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001549 (property->getPropertyImplementation() ==
1550 ObjCPropertyDecl::Optional) ?
1551 ObjCMethodDecl::Optional :
1552 ObjCMethodDecl::Required);
1553 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001554
1555 AddPropertyAttrs(*this, GetterMethod, property);
1556
Ted Kremenek23173d72010-05-18 21:09:07 +00001557 // FIXME: Eventually this shouldn't be needed, as the lexical context
1558 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001559 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001560 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001561 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1562 GetterMethod->addAttr(
1563 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001564 } else
1565 // A user declared getter will be synthesize when @synthesize of
1566 // the property with the same name is seen in the @implementation
1567 GetterMethod->setSynthesized(true);
1568 property->setGetterMethodDecl(GetterMethod);
1569
1570 // Skip setter if property is read-only.
1571 if (!property->isReadOnly()) {
1572 // Find the default setter and if one not found, add one.
1573 if (!SetterMethod) {
1574 // No instance method of same name as property setter name was found.
1575 // Declare a setter method and add it to the list of methods
1576 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001577 SourceLocation Loc = redeclaredProperty ?
1578 redeclaredProperty->getLocation() :
1579 property->getLocation();
1580
1581 SetterMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001582 ObjCMethodDecl::Create(Context, Loc, Loc,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001583 property->getSetterName(), Context.VoidTy, 0,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00001584 CD, /*isInstance=*/true, /*isVariadic=*/false,
1585 /*isSynthesized=*/true,
1586 /*isImplicitlyDeclared=*/true,
1587 /*isDefined=*/false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001588 (property->getPropertyImplementation() ==
1589 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001590 ObjCMethodDecl::Optional :
1591 ObjCMethodDecl::Required);
1592
Ted Kremenek9d64c152010-03-12 00:38:38 +00001593 // Invent the arguments for the setter. We don't bother making a
1594 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001595 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1596 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001597 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001598 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001599 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001600 SC_None,
1601 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001602 0);
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001603 SetterMethod->setMethodParams(Context, Argument,
1604 ArrayRef<SourceLocation>());
John McCall5de74d12010-11-10 07:01:40 +00001605
1606 AddPropertyAttrs(*this, SetterMethod, property);
1607
Ted Kremenek9d64c152010-03-12 00:38:38 +00001608 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001609 // FIXME: Eventually this shouldn't be needed, as the lexical context
1610 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001611 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001612 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001613 } else
1614 // A user declared setter will be synthesize when @synthesize of
1615 // the property with the same name is seen in the @implementation
1616 SetterMethod->setSynthesized(true);
1617 property->setSetterMethodDecl(SetterMethod);
1618 }
1619 // Add any synthesized methods to the global pool. This allows us to
1620 // handle the following, which is supported by GCC (and part of the design).
1621 //
1622 // @interface Foo
1623 // @property double bar;
1624 // @end
1625 //
1626 // void thisIsUnfortunate() {
1627 // id foo;
1628 // double bar = [foo bar];
1629 // }
1630 //
1631 if (GetterMethod)
1632 AddInstanceMethodToGlobalPool(GetterMethod);
1633 if (SetterMethod)
1634 AddInstanceMethodToGlobalPool(SetterMethod);
1635}
1636
John McCalld226f652010-08-21 09:40:31 +00001637void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001638 SourceLocation Loc,
1639 unsigned &Attributes) {
1640 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001641 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001642 return;
1643
1644 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001645 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001646
1647 // readonly and readwrite/assign/retain/copy conflict.
1648 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1649 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1650 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001651 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001652 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001653 ObjCDeclSpec::DQ_PR_retain |
1654 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001655 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1656 "readwrite" :
1657 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1658 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001659 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1660 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001661 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1662 "copy" : "retain";
1663
1664 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1665 diag::err_objc_property_attr_mutually_exclusive :
1666 diag::warn_objc_property_attr_mutually_exclusive)
1667 << "readonly" << which;
1668 }
1669
1670 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001671 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1672 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1673 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001674 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001675 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001676 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1677 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1678 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1679 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001680 }
1681
1682 // Check for more than one of { assign, copy, retain }.
1683 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1684 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1685 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1686 << "assign" << "copy";
1687 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1688 }
1689 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1690 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1691 << "assign" << "retain";
1692 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1693 }
John McCallf85e1932011-06-15 23:02:42 +00001694 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1695 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1696 << "assign" << "strong";
1697 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1698 }
1699 if (getLangOptions().ObjCAutoRefCount &&
1700 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1701 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1702 << "assign" << "weak";
1703 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1704 }
1705 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1706 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1707 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1708 << "unsafe_unretained" << "copy";
1709 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1710 }
1711 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1712 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1713 << "unsafe_unretained" << "retain";
1714 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1715 }
1716 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1717 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1718 << "unsafe_unretained" << "strong";
1719 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1720 }
1721 if (getLangOptions().ObjCAutoRefCount &&
1722 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1723 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1724 << "unsafe_unretained" << "weak";
1725 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1726 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001727 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1728 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1729 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1730 << "copy" << "retain";
1731 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1732 }
John McCallf85e1932011-06-15 23:02:42 +00001733 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1734 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1735 << "copy" << "strong";
1736 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1737 }
1738 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1739 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1740 << "copy" << "weak";
1741 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1742 }
1743 }
1744 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1745 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1746 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1747 << "retain" << "weak";
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001748 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
John McCallf85e1932011-06-15 23:02:42 +00001749 }
1750 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1751 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1752 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1753 << "strong" << "weak";
1754 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001755 }
1756
Fariborz Jahanian9d1bbea2011-10-10 21:53:24 +00001757 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
1758 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
1759 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1760 << "atomic" << "nonatomic";
1761 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
1762 }
1763
Ted Kremenek9d64c152010-03-12 00:38:38 +00001764 // Warn if user supplied no assignment attribute, property is
1765 // readwrite, and this is an object type.
1766 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001767 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1768 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1769 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001770 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1771 PropertyTy->isObjCObjectPointerType()) {
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001772 if (getLangOptions().ObjCAutoRefCount)
1773 // With arc, @property definitions should default to (strong) when
1774 // not specified
1775 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
1776 else {
1777 // Skip this warning in gc-only mode.
Douglas Gregore289d812011-09-13 17:21:33 +00001778 if (getLangOptions().getGC() != LangOptions::GCOnly)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001779 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001780
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001781 // If non-gc code warn that this is likely inappropriate.
Douglas Gregore289d812011-09-13 17:21:33 +00001782 if (getLangOptions().getGC() == LangOptions::NonGC)
Fariborz Jahanianbc03aea2011-08-19 19:28:44 +00001783 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1784 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001785
1786 // FIXME: Implement warning dependent on NSCopying being
1787 // implemented. See also:
1788 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1789 // (please trim this list while you are at it).
1790 }
1791
1792 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001793 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Douglas Gregore289d812011-09-13 17:21:33 +00001794 && getLangOptions().getGC() == LangOptions::GCOnly
Ted Kremenek9d64c152010-03-12 00:38:38 +00001795 && PropertyTy->isBlockPointerType())
1796 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
Fariborz Jahanian528a4992011-09-14 18:03:46 +00001797 else if (getLangOptions().ObjCAutoRefCount &&
1798 (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1799 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1800 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1801 PropertyTy->isBlockPointerType())
1802 Diag(Loc, diag::warn_objc_property_retain_of_block);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001803}