blob: 27f25c27ad254247d9e39c645757717ba46e714d [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"
John McCall50df6ae2010-08-25 07:03:20 +000019#include "llvm/ADT/DenseSet.h"
Ted Kremenek9d64c152010-03-12 00:38:38 +000020
21using namespace clang;
22
Ted Kremenek28685ab2010-03-12 00:46:40 +000023//===----------------------------------------------------------------------===//
24// Grammar actions.
25//===----------------------------------------------------------------------===//
26
John McCallf85e1932011-06-15 23:02:42 +000027/// Check the internal consistency of a property declaration.
28static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
29 if (property->isInvalidDecl()) return;
30
31 ObjCPropertyDecl::PropertyAttributeKind propertyKind
32 = property->getPropertyAttributes();
33 Qualifiers::ObjCLifetime propertyLifetime
34 = property->getType().getObjCLifetime();
35
36 // Nothing to do if we don't have a lifetime.
37 if (propertyLifetime == Qualifiers::OCL_None) return;
38
39 Qualifiers::ObjCLifetime expectedLifetime;
40 unsigned selector;
41
42 // Strong properties should have either strong or no lifetime.
43 if (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
44 ObjCPropertyDecl::OBJC_PR_strong |
45 ObjCPropertyDecl::OBJC_PR_copy)) {
46 expectedLifetime = Qualifiers::OCL_Strong;
47 selector = 0;
48 } else if (propertyKind & ObjCPropertyDecl::OBJC_PR_weak) {
49 expectedLifetime = Qualifiers::OCL_Weak;
50 selector = 1;
51 } else if (propertyKind & (ObjCPropertyDecl::OBJC_PR_assign |
52 ObjCPropertyDecl::OBJC_PR_unsafe_unretained) &&
53 property->getType()->isObjCRetainableType()) {
54 expectedLifetime = Qualifiers::OCL_ExplicitNone;
55 selector = 2;
56 } else {
57 // We have a lifetime qualifier but no dominating property
58 // attribute. That's okay.
59 return;
60 }
61
62 if (propertyLifetime == expectedLifetime) return;
63
64 property->setInvalidDecl();
65 S.Diag(property->getLocation(),
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +000066 diag::err_arc_inconsistent_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +000067 << property->getDeclName()
68 << selector
69 << propertyLifetime;
70}
71
John McCalld226f652010-08-21 09:40:31 +000072Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
73 FieldDeclarator &FD,
74 ObjCDeclSpec &ODS,
75 Selector GetterSel,
76 Selector SetterSel,
77 Decl *ClassCategory,
78 bool *isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +000079 tok::ObjCKeywordKind MethodImplKind,
80 DeclContext *lexicalDC) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000081 unsigned Attributes = ODS.getPropertyAttributes();
John McCallf85e1932011-06-15 23:02:42 +000082 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
83 QualType T = TSI->getType();
84 if ((getLangOptions().getGCMode() != LangOptions::NonGC &&
85 T.isObjCGCWeak()) ||
86 (getLangOptions().ObjCAutoRefCount &&
87 T.getObjCLifetime() == Qualifiers::OCL_Weak))
88 Attributes |= ObjCDeclSpec::DQ_PR_weak;
89
Ted Kremenek28685ab2010-03-12 00:46:40 +000090 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
91 // default is readwrite!
92 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
93 // property is defaulted to 'assign' if it is readwrite and is
94 // not retain or copy
95 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
96 (isReadWrite &&
97 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
John McCallf85e1932011-06-15 23:02:42 +000098 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
99 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
100 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
101 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
Fariborz Jahanian14086762011-03-28 23:47:18 +0000102
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000103 // Proceed with constructing the ObjCPropertDecls.
104 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +0000105 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000106
Ted Kremenek28685ab2010-03-12 00:46:40 +0000107 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000108 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +0000109 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000110 FD, GetterSel, SetterSel,
111 isAssign, isReadWrite,
112 Attributes,
113 isOverridingProperty, TSI,
114 MethodImplKind);
John McCallf85e1932011-06-15 23:02:42 +0000115 if (Res) {
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000116 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000117 if (getLangOptions().ObjCAutoRefCount)
118 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res));
119 }
Fariborz Jahanianae415dc2010-07-13 22:04:56 +0000120 return Res;
121 }
122
John McCallf85e1932011-06-15 23:02:42 +0000123 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
124 GetterSel, SetterSel,
125 isAssign, isReadWrite,
126 Attributes, TSI, MethodImplKind);
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000127 if (lexicalDC)
128 Res->setLexicalDeclContext(lexicalDC);
129
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000130 // Validate the attributes on the @property.
131 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
John McCallf85e1932011-06-15 23:02:42 +0000132
133 if (getLangOptions().ObjCAutoRefCount)
134 checkARCPropertyDecl(*this, Res);
135
Fariborz Jahanian842f07b2010-03-30 22:40:11 +0000136 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000137}
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000138
John McCalld226f652010-08-21 09:40:31 +0000139Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000140Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
141 SourceLocation AtLoc, FieldDeclarator &FD,
142 Selector GetterSel, Selector SetterSel,
143 const bool isAssign,
144 const bool isReadWrite,
145 const unsigned Attributes,
146 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +0000147 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000149
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000150 // Diagnose if this property is already in continuation class.
151 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000152 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Fariborz Jahanian2a289142010-11-10 18:01:36 +0000153 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
154
155 if (CCPrimary)
156 // Check for duplicate declaration of this property in current and
157 // other class extensions.
158 for (const ObjCCategoryDecl *ClsExtDecl =
159 CCPrimary->getFirstClassExtension();
160 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
161 if (ObjCPropertyDecl *prevDecl =
162 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) {
163 Diag(AtLoc, diag::err_duplicate_property);
164 Diag(prevDecl->getLocation(), diag::note_property_declare);
165 return 0;
166 }
167 }
168
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000169 // Create a new ObjCPropertyDecl with the DeclContext being
170 // the class extension.
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000171 // FIXME. We should really be using CreatePropertyDecl for this.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000172 ObjCPropertyDecl *PDecl =
173 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
174 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000175 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
176 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
177 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
178 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +0000179 // Set setter/getter selector name. Needed later.
180 PDecl->setGetterName(GetterSel);
181 PDecl->setSetterName(SetterSel);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000182 DC->addDecl(PDecl);
183
184 // We need to look in the @interface to see if the @property was
185 // already declared.
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000186 if (!CCPrimary) {
187 Diag(CDecl->getLocation(), diag::err_continuation_class);
188 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000189 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000190 }
191
192 // Find the property in continuation class's primary class only.
193 ObjCPropertyDecl *PIDecl =
194 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
195
196 if (!PIDecl) {
197 // No matching property found in the primary class. Just fall thru
198 // and add property to continuation class's primary class.
199 ObjCPropertyDecl *PDecl =
200 CreatePropertyDecl(S, CCPrimary, AtLoc,
201 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000202 Attributes, T, MethodImplKind, DC);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000203
204 // A case of continuation class adding a new property in the class. This
205 // is not what it was meant for. However, gcc supports it and so should we.
206 // Make sure setter/getters are declared here.
Ted Kremeneka054fb42010-09-21 20:52:59 +0000207 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0,
208 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000209 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000210 }
211
212 // The property 'PIDecl's readonly attribute will be over-ridden
213 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000214 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000215 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
216 unsigned retainCopyNonatomic =
217 (ObjCPropertyDecl::OBJC_PR_retain |
John McCallf85e1932011-06-15 23:02:42 +0000218 ObjCPropertyDecl::OBJC_PR_strong |
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000219 ObjCPropertyDecl::OBJC_PR_copy |
220 ObjCPropertyDecl::OBJC_PR_nonatomic);
221 if ((Attributes & retainCopyNonatomic) !=
222 (PIkind & retainCopyNonatomic)) {
223 Diag(AtLoc, diag::warn_property_attr_mismatch);
224 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000225 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000226 DeclContext *DC = cast<DeclContext>(CCPrimary);
227 if (!ObjCPropertyDecl::findPropertyDecl(DC,
228 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000229 // Protocol is not in the primary class. Must build one for it.
230 ObjCDeclSpec ProtocolPropertyODS;
231 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
232 // and ObjCPropertyDecl::PropertyAttributeKind have identical
233 // values. Should consolidate both into one enum type.
234 ProtocolPropertyODS.
235 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
236 PIkind);
237
John McCalld226f652010-08-21 09:40:31 +0000238 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000239 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
240 PIDecl->getGetterName(),
241 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000242 CCPrimary, isOverridingProperty,
Ted Kremenek4a2e9ea2010-09-23 21:18:05 +0000243 MethodImplKind,
244 /* lexicalDC = */ CDecl);
John McCalld226f652010-08-21 09:40:31 +0000245 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000246 }
247 PIDecl->makeitReadWriteAttribute();
248 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
249 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
John McCallf85e1932011-06-15 23:02:42 +0000250 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
251 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000252 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
253 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
254 PIDecl->setSetterName(SetterSel);
255 } else {
Ted Kremenek788f4892010-10-21 18:49:42 +0000256 // Tailor the diagnostics for the common case where a readwrite
257 // property is declared both in the @interface and the continuation.
258 // This is a common error where the user often intended the original
259 // declaration to be readonly.
260 unsigned diag =
261 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
262 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
263 ? diag::err_use_continuation_class_redeclaration_readwrite
264 : diag::err_use_continuation_class;
265 Diag(AtLoc, diag)
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000266 << CCPrimary->getDeclName();
267 Diag(PIDecl->getLocation(), diag::note_property_declare);
268 }
269 *isOverridingProperty = true;
270 // Make sure setter decl is synthesized, and added to primary class's list.
Ted Kremenek8254aa62010-09-21 18:28:43 +0000271 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
John McCalld226f652010-08-21 09:40:31 +0000272 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000273}
274
275ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
276 ObjCContainerDecl *CDecl,
277 SourceLocation AtLoc,
278 FieldDeclarator &FD,
279 Selector GetterSel,
280 Selector SetterSel,
281 const bool isAssign,
282 const bool isReadWrite,
283 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000284 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000285 tok::ObjCKeywordKind MethodImplKind,
286 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000287 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000288 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000289
290 // Issue a warning if property is 'assign' as default and its object, which is
291 // gc'able conforms to NSCopying protocol
292 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
293 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000294 if (const ObjCObjectPointerType *ObjPtrTy =
295 T->getAs<ObjCObjectPointerType>()) {
296 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
297 if (IDecl)
298 if (ObjCProtocolDecl* PNSCopying =
299 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
300 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
301 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000302 }
John McCallc12c5bb2010-05-15 11:32:37 +0000303 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000304 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
305
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000306 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000307 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
308 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000309 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000310
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000311 if (ObjCPropertyDecl *prevDecl =
312 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000313 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000314 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000315 PDecl->setInvalidDecl();
316 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000317 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000318 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000319 if (lexicalDC)
320 PDecl->setLexicalDeclContext(lexicalDC);
321 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000322
323 if (T->isArrayType() || T->isFunctionType()) {
324 Diag(AtLoc, diag::err_property_type) << T;
325 PDecl->setInvalidDecl();
326 }
327
328 ProcessDeclAttributes(S, PDecl, FD.D);
329
330 // Regardless of setter/getter attribute, we save the default getter/setter
331 // selector names in anticipation of declaration of setter/getter methods.
332 PDecl->setGetterName(GetterSel);
333 PDecl->setSetterName(SetterSel);
334
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000335 unsigned attributesAsWritten = 0;
336 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
337 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
338 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
339 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
340 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
341 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
342 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
343 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
344 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
345 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
346 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
347 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
348 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
349 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
350 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
351 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
352 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
353 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
354 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
355 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
356 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
357 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
358 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
359 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
360
361 PDecl->setPropertyAttributesAsWritten(
362 (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten);
363
Ted Kremenek28685ab2010-03-12 00:46:40 +0000364 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
365 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
366
367 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
368 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
369
370 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
371 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
372
373 if (isReadWrite)
374 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
375
376 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
377 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
378
John McCallf85e1932011-06-15 23:02:42 +0000379 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
380 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
381
382 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
383 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
384
Ted Kremenek28685ab2010-03-12 00:46:40 +0000385 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
386 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
387
John McCallf85e1932011-06-15 23:02:42 +0000388 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
389 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
390
Ted Kremenek28685ab2010-03-12 00:46:40 +0000391 if (isAssign)
392 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
393
394 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
395 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000396 else if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
397 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000398
John McCallf85e1932011-06-15 23:02:42 +0000399 // 'unsafe_unretained' is alias for 'assign'.
400 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
401 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
402 if (isAssign)
403 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
404
Ted Kremenek28685ab2010-03-12 00:46:40 +0000405 if (MethodImplKind == tok::objc_required)
406 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
407 else if (MethodImplKind == tok::objc_optional)
408 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000409
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000410 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000411}
412
John McCallf85e1932011-06-15 23:02:42 +0000413static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
414 ObjCPropertyDecl *property,
415 ObjCIvarDecl *ivar) {
416 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
417
418 QualType propertyType = property->getType();
419 Qualifiers::ObjCLifetime propertyLifetime = propertyType.getObjCLifetime();
420 ObjCPropertyDecl::PropertyAttributeKind propertyKind
421 = property->getPropertyAttributes();
422
423 QualType ivarType = ivar->getType();
424 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
425
426 // Case 1: strong properties.
427 if (propertyLifetime == Qualifiers::OCL_Strong ||
428 (propertyKind & (ObjCPropertyDecl::OBJC_PR_retain |
429 ObjCPropertyDecl::OBJC_PR_strong |
430 ObjCPropertyDecl::OBJC_PR_copy))) {
431 switch (ivarLifetime) {
432 case Qualifiers::OCL_Strong:
433 // Okay.
434 return;
435
436 case Qualifiers::OCL_None:
437 case Qualifiers::OCL_Autoreleasing:
438 // These aren't valid lifetimes for object ivars; don't diagnose twice.
439 return;
440
441 case Qualifiers::OCL_ExplicitNone:
442 case Qualifiers::OCL_Weak:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000443 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000444 << property->getDeclName()
445 << ivar->getDeclName()
446 << ivarLifetime;
447 break;
448 }
449
450 // Case 2: weak properties.
451 } else if (propertyLifetime == Qualifiers::OCL_Weak ||
452 (propertyKind & ObjCPropertyDecl::OBJC_PR_weak)) {
453 switch (ivarLifetime) {
454 case Qualifiers::OCL_Weak:
455 // Okay.
456 return;
457
458 case Qualifiers::OCL_None:
459 case Qualifiers::OCL_Autoreleasing:
460 // These aren't valid lifetimes for object ivars; don't diagnose twice.
461 return;
462
463 case Qualifiers::OCL_ExplicitNone:
464 case Qualifiers::OCL_Strong:
465 S.Diag(propertyImplLoc, diag::error_weak_property)
466 << property->getDeclName()
467 << ivar->getDeclName();
468 break;
469 }
470
471 // Case 3: assign properties.
472 } else if ((propertyKind & ObjCPropertyDecl::OBJC_PR_assign) &&
473 propertyType->isObjCRetainableType()) {
474 switch (ivarLifetime) {
475 case Qualifiers::OCL_ExplicitNone:
476 // Okay.
477 return;
478
479 case Qualifiers::OCL_None:
480 case Qualifiers::OCL_Autoreleasing:
481 // These aren't valid lifetimes for object ivars; don't diagnose twice.
482 return;
483
484 case Qualifiers::OCL_Weak:
485 case Qualifiers::OCL_Strong:
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000486 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
John McCallf85e1932011-06-15 23:02:42 +0000487 << property->getDeclName()
488 << ivar->getDeclName();
489 break;
490 }
491
492 // Any other property should be ignored.
493 } else {
494 return;
495 }
496
497 S.Diag(property->getLocation(), diag::note_property_declare);
498}
499
Ted Kremenek28685ab2010-03-12 00:46:40 +0000500
501/// ActOnPropertyImplDecl - This routine performs semantic checks and
502/// builds the AST node for a property implementation declaration; declared
503/// as @synthesize or @dynamic.
504///
John McCalld226f652010-08-21 09:40:31 +0000505Decl *Sema::ActOnPropertyImplDecl(Scope *S,
506 SourceLocation AtLoc,
507 SourceLocation PropertyLoc,
508 bool Synthesize,
509 Decl *ClassCatImpDecl,
510 IdentifierInfo *PropertyId,
Douglas Gregora4ffd852010-11-17 01:03:52 +0000511 IdentifierInfo *PropertyIvar,
512 SourceLocation PropertyIvarLoc) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000513 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000514 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000515 // Make sure we have a context for the property implementation declaration.
516 if (!ClassImpDecl) {
517 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000518 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000519 }
520 ObjCPropertyDecl *property = 0;
521 ObjCInterfaceDecl* IDecl = 0;
522 // Find the class or category class where this property must have
523 // a declaration.
524 ObjCImplementationDecl *IC = 0;
525 ObjCCategoryImplDecl* CatImplClass = 0;
526 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
527 IDecl = IC->getClassInterface();
528 // We always synthesize an interface for an implementation
529 // without an interface decl. So, IDecl is always non-zero.
530 assert(IDecl &&
531 "ActOnPropertyImplDecl - @implementation without @interface");
532
533 // Look for this property declaration in the @implementation's @interface
534 property = IDecl->FindPropertyDeclaration(PropertyId);
535 if (!property) {
536 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000537 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000538 }
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000539 unsigned PIkind = property->getPropertyAttributesAsWritten();
Fariborz Jahanian45937ae2011-06-11 00:45:12 +0000540 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
541 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
Fariborz Jahaniandd4430e2010-12-17 22:28:16 +0000542 if (AtLoc.isValid())
543 Diag(AtLoc, diag::warn_implicit_atomic_property);
544 else
545 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
546 Diag(property->getLocation(), diag::note_property_declare);
547 }
548
Ted Kremenek28685ab2010-03-12 00:46:40 +0000549 if (const ObjCCategoryDecl *CD =
550 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
551 if (!CD->IsClassExtension()) {
552 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
553 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000554 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000555 }
556 }
557 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
558 if (Synthesize) {
559 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000560 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000561 }
562 IDecl = CatImplClass->getClassInterface();
563 if (!IDecl) {
564 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000565 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000566 }
567 ObjCCategoryDecl *Category =
568 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
569
570 // If category for this implementation not found, it is an error which
571 // has already been reported eralier.
572 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000573 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000574 // Look for this property declaration in @implementation's category
575 property = Category->FindPropertyDeclaration(PropertyId);
576 if (!property) {
577 Diag(PropertyLoc, diag::error_bad_category_property_decl)
578 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000579 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000580 }
581 } else {
582 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000583 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000584 }
585 ObjCIvarDecl *Ivar = 0;
586 // Check that we have a valid, previously declared ivar for @synthesize
587 if (Synthesize) {
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +0000588 if (getLangOptions().ObjCAutoRefCount &&
589 !property->hasWrittenStorageAttribute() &&
590 property->getType()->isObjCRetainableType()) {
591 Diag(PropertyLoc, diag::err_arc_objc_property_default_assign_on_object);
592 Diag(property->getLocation(), diag::note_property_declare);
593 }
594
Ted Kremenek28685ab2010-03-12 00:46:40 +0000595 // @synthesize
596 if (!PropertyIvar)
597 PropertyIvar = PropertyId;
John McCallf85e1932011-06-15 23:02:42 +0000598 ObjCPropertyDecl::PropertyAttributeKind kind
599 = property->getPropertyAttributes();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000600 QualType PropType = Context.getCanonicalType(property->getType());
Fariborz Jahanian14086762011-03-28 23:47:18 +0000601 QualType PropertyIvarType = PropType;
602 if (PropType->isReferenceType())
603 PropertyIvarType = cast<ReferenceType>(PropType)->getPointeeType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000604 // Check that this is a previously declared 'ivar' in 'IDecl' interface
605 ObjCInterfaceDecl *ClassDeclared;
606 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
607 if (!Ivar) {
John McCallf85e1932011-06-15 23:02:42 +0000608 // In ARC, give the ivar a lifetime qualifier based on its
609 // property attributes.
610 if (getLangOptions().ObjCAutoRefCount &&
611 !PropertyIvarType.getObjCLifetime()) {
612
613 // retain/copy have retaining lifetime.
614 if (kind & (ObjCPropertyDecl::OBJC_PR_retain |
615 ObjCPropertyDecl::OBJC_PR_strong |
616 ObjCPropertyDecl::OBJC_PR_copy)) {
617 Qualifiers qs;
618 qs.addObjCLifetime(Qualifiers::OCL_Strong);
619 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
620 }
621 else if (kind & ObjCPropertyDecl::OBJC_PR_weak) {
John McCall9f084a32011-07-06 00:26:06 +0000622 if (!getLangOptions().ObjCRuntimeHasWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000623 Diag(PropertyLoc, diag::err_arc_weak_no_runtime);
624 Diag(property->getLocation(), diag::note_property_declare);
625 }
626 Qualifiers qs;
627 qs.addObjCLifetime(Qualifiers::OCL_Weak);
628 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
629 }
630 else if (kind & ObjCPropertyDecl::OBJC_PR_assign &&
631 PropertyIvarType->isObjCRetainableType()) {
632 // assume that an 'assign' property synthesizes __unsafe_unretained
633 // ivar
634 Qualifiers qs;
635 qs.addObjCLifetime(Qualifiers::OCL_ExplicitNone);
636 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
637 }
638 }
639
640 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
641 !getLangOptions().ObjCAutoRefCount &&
642 getLangOptions().getGCMode() == LangOptions::NonGC) {
643 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc);
644 Diag(property->getLocation(), diag::note_property_declare);
645 }
646
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000647 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
648 PropertyLoc, PropertyLoc, PropertyIvar,
Fariborz Jahanian14086762011-03-28 23:47:18 +0000649 PropertyIvarType, /*Dinfo=*/0,
Fariborz Jahanian75049662010-12-15 23:29:04 +0000650 ObjCIvarDecl::Private,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000651 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000652 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000653 IDecl->makeDeclVisibleInContext(Ivar, false);
654 property->setPropertyIvarDecl(Ivar);
655
656 if (!getLangOptions().ObjCNonFragileABI)
657 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
658 // Note! I deliberately want it to fall thru so, we have a
659 // a property implementation and to avoid future warnings.
660 } else if (getLangOptions().ObjCNonFragileABI &&
661 ClassDeclared != IDecl) {
662 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
663 << property->getDeclName() << Ivar->getDeclName()
664 << ClassDeclared->getDeclName();
665 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000666 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000667 // Note! I deliberately want it to fall thru so more errors are caught.
668 }
669 QualType IvarType = Context.getCanonicalType(Ivar->getType());
670
671 // Check that type of property and its ivar are type compatible.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000672 if (PropertyIvarType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000673 bool compat = false;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000674 if (isa<ObjCObjectPointerType>(PropertyIvarType)
John McCallf85e1932011-06-15 23:02:42 +0000675 && isa<ObjCObjectPointerType>(IvarType))
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000676 compat =
677 Context.canAssignObjCInterfaces(
Fariborz Jahanian14086762011-03-28 23:47:18 +0000678 PropertyIvarType->getAs<ObjCObjectPointerType>(),
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000679 IvarType->getAs<ObjCObjectPointerType>());
Douglas Gregorb608b982011-01-28 02:26:04 +0000680 else {
681 SourceLocation Loc = PropertyIvarLoc;
682 if (Loc.isInvalid())
683 Loc = PropertyLoc;
Fariborz Jahanian14086762011-03-28 23:47:18 +0000684 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType)
John McCalldaa8e4e2010-11-15 09:13:47 +0000685 == Compatible);
Douglas Gregorb608b982011-01-28 02:26:04 +0000686 }
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000687 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000688 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000689 << property->getDeclName() << PropType
690 << Ivar->getDeclName() << IvarType;
691 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000692 // Note! I deliberately want it to fall thru so, we have a
693 // a property implementation and to avoid future warnings.
694 }
695
696 // FIXME! Rules for properties are somewhat different that those
697 // for assignments. Use a new routine to consolidate all cases;
698 // specifically for property redeclarations as well as for ivars.
Fariborz Jahanian14086762011-03-28 23:47:18 +0000699 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000700 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
701 if (lhsType != rhsType &&
702 lhsType->isArithmeticType()) {
703 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000704 << property->getDeclName() << PropType
705 << Ivar->getDeclName() << IvarType;
706 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000707 // Fall thru - see previous comment
708 }
709 // __weak is explicit. So it works on Canonical type.
John McCallf85e1932011-06-15 23:02:42 +0000710 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
711 getLangOptions().getGCMode() != LangOptions::NonGC)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000712 Diag(PropertyLoc, diag::error_weak_property)
713 << property->getDeclName() << Ivar->getDeclName();
714 // Fall thru - see previous comment
715 }
John McCallf85e1932011-06-15 23:02:42 +0000716 // Fall thru - see previous comment
Ted Kremenek28685ab2010-03-12 00:46:40 +0000717 if ((property->getType()->isObjCObjectPointerType() ||
718 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
719 getLangOptions().getGCMode() != LangOptions::NonGC) {
720 Diag(PropertyLoc, diag::error_strong_property)
721 << property->getDeclName() << Ivar->getDeclName();
722 // Fall thru - see previous comment
723 }
724 }
John McCallf85e1932011-06-15 23:02:42 +0000725 if (getLangOptions().ObjCAutoRefCount)
726 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000727 } else if (PropertyIvar)
728 // @dynamic
729 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
John McCallf85e1932011-06-15 23:02:42 +0000730
Ted Kremenek28685ab2010-03-12 00:46:40 +0000731 assert (property && "ActOnPropertyImplDecl - property declaration missing");
732 ObjCPropertyImplDecl *PIDecl =
733 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
734 property,
735 (Synthesize ?
736 ObjCPropertyImplDecl::Synthesize
737 : ObjCPropertyImplDecl::Dynamic),
Douglas Gregora4ffd852010-11-17 01:03:52 +0000738 Ivar, PropertyIvarLoc);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000739 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
740 getterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000741 if (getLangOptions().CPlusPlus && Synthesize &&
742 Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000743 // For Objective-C++, need to synthesize the AST for the IVAR object to be
744 // returned by the getter as it must conform to C++'s copy-return rules.
745 // FIXME. Eventually we want to do this for Objective-C as well.
746 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
747 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000748 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
749 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000750 Expr *IvarRefExpr =
751 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
752 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000753 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000754 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000755 SourceLocation(),
756 getterMethod->getResultType(),
757 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000758 SourceLocation(),
759 Owned(IvarRefExpr));
760 if (!Res.isInvalid()) {
761 Expr *ResExpr = Res.takeAs<Expr>();
762 if (ResExpr)
John McCall4765fa02010-12-06 08:20:24 +0000763 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000764 PIDecl->setGetterCXXConstructor(ResExpr);
765 }
766 }
Fariborz Jahanian831fb962011-06-25 00:17:46 +0000767 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
768 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
769 Diag(getterMethod->getLocation(),
770 diag::warn_property_getter_owning_mismatch);
771 Diag(property->getLocation(), diag::note_property_declare);
772 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000773 }
774 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
775 setterMethod->createImplicitParams(Context, IDecl);
Fariborz Jahanian0313f442010-10-15 22:42:59 +0000776 if (getLangOptions().CPlusPlus && Synthesize
777 && Ivar->getType()->isRecordType()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000778 // FIXME. Eventually we want to do this for Objective-C as well.
779 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
780 DeclRefExpr *SelfExpr =
John McCallf89e55a2010-11-18 06:31:45 +0000781 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(),
782 VK_RValue, SourceLocation());
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000783 Expr *lhs =
784 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
785 SelfExpr, true, true);
786 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
787 ParmVarDecl *Param = (*P);
Fariborz Jahanian14086762011-03-28 23:47:18 +0000788 QualType T = Param->getType();
789 if (T->isReferenceType())
Fariborz Jahanian61750f22011-03-30 16:59:30 +0000790 T = T->getAs<ReferenceType>()->getPointeeType();
Fariborz Jahanian14086762011-03-28 23:47:18 +0000791 Expr *rhs = new (Context) DeclRefExpr(Param, T,
John McCallf89e55a2010-11-18 06:31:45 +0000792 VK_LValue, SourceLocation());
Fariborz Jahanianfa432392010-10-14 21:30:10 +0000793 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(),
John McCall2de56d12010-08-25 11:45:40 +0000794 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000795 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
796 }
797 }
798
Ted Kremenek28685ab2010-03-12 00:46:40 +0000799 if (IC) {
800 if (Synthesize)
801 if (ObjCPropertyImplDecl *PPIDecl =
802 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
803 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
804 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
805 << PropertyIvar;
806 Diag(PPIDecl->getLocation(), diag::note_previous_use);
807 }
808
809 if (ObjCPropertyImplDecl *PPIDecl
810 = IC->FindPropertyImplDecl(PropertyId)) {
811 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
812 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000813 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000814 }
815 IC->addPropertyImplementation(PIDecl);
Fariborz Jahaniane776f882011-01-03 18:08:02 +0000816 if (getLangOptions().ObjCDefaultSynthProperties &&
817 getLangOptions().ObjCNonFragileABI2) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000818 // Diagnose if an ivar was lazily synthesdized due to a previous
819 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000820 // but it requires an ivar of different name.
Fariborz Jahanian411c25c2011-01-20 23:34:25 +0000821 ObjCInterfaceDecl *ClassDeclared=0;
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000822 ObjCIvarDecl *Ivar = 0;
823 if (!Synthesize)
824 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
825 else {
826 if (PropertyIvar && PropertyIvar != PropertyId)
827 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
828 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000829 // Issue diagnostics only if Ivar belongs to current class.
830 if (Ivar && Ivar->getSynthesize() &&
831 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000832 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
833 << PropertyId;
834 Ivar->setInvalidDecl();
835 }
836 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000837 } else {
838 if (Synthesize)
839 if (ObjCPropertyImplDecl *PPIDecl =
840 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
841 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
842 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
843 << PropertyIvar;
844 Diag(PPIDecl->getLocation(), diag::note_previous_use);
845 }
846
847 if (ObjCPropertyImplDecl *PPIDecl =
848 CatImplClass->FindPropertyImplDecl(PropertyId)) {
849 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
850 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000851 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000852 }
853 CatImplClass->addPropertyImplementation(PIDecl);
854 }
855
John McCalld226f652010-08-21 09:40:31 +0000856 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000857}
858
859//===----------------------------------------------------------------------===//
860// Helper methods.
861//===----------------------------------------------------------------------===//
862
Ted Kremenek9d64c152010-03-12 00:38:38 +0000863/// DiagnosePropertyMismatch - Compares two properties for their
864/// attributes and types and warns on a variety of inconsistencies.
865///
866void
867Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
868 ObjCPropertyDecl *SuperProperty,
869 const IdentifierInfo *inheritedName) {
870 ObjCPropertyDecl::PropertyAttributeKind CAttr =
871 Property->getPropertyAttributes();
872 ObjCPropertyDecl::PropertyAttributeKind SAttr =
873 SuperProperty->getPropertyAttributes();
874 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
875 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
876 Diag(Property->getLocation(), diag::warn_readonly_property)
877 << Property->getDeclName() << inheritedName;
878 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
879 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
880 Diag(Property->getLocation(), diag::warn_property_attribute)
881 << Property->getDeclName() << "copy" << inheritedName;
John McCallf85e1932011-06-15 23:02:42 +0000882 else {
883 unsigned CAttrRetain =
884 (CAttr &
885 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
886 unsigned SAttrRetain =
887 (SAttr &
888 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
889 bool CStrong = (CAttrRetain != 0);
890 bool SStrong = (SAttrRetain != 0);
891 if (CStrong != SStrong)
892 Diag(Property->getLocation(), diag::warn_property_attribute)
893 << Property->getDeclName() << "retain (or strong)" << inheritedName;
894 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000895
896 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
897 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
898 Diag(Property->getLocation(), diag::warn_property_attribute)
899 << Property->getDeclName() << "atomic" << inheritedName;
900 if (Property->getSetterName() != SuperProperty->getSetterName())
901 Diag(Property->getLocation(), diag::warn_property_attribute)
902 << Property->getDeclName() << "setter" << inheritedName;
903 if (Property->getGetterName() != SuperProperty->getGetterName())
904 Diag(Property->getLocation(), diag::warn_property_attribute)
905 << Property->getDeclName() << "getter" << inheritedName;
906
907 QualType LHSType =
908 Context.getCanonicalType(SuperProperty->getType());
909 QualType RHSType =
910 Context.getCanonicalType(Property->getType());
911
Fariborz Jahanianc286f382011-07-12 22:05:16 +0000912 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000913 // FIXME: Incorporate this test with typesAreCompatible.
914 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
915 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
916 return;
917 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
918 << Property->getType() << SuperProperty->getType() << inheritedName;
919 }
920}
921
922bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
923 ObjCMethodDecl *GetterMethod,
924 SourceLocation Loc) {
925 if (GetterMethod &&
926 GetterMethod->getResultType() != property->getType()) {
927 AssignConvertType result = Incompatible;
John McCall1c23e912010-11-16 02:32:08 +0000928 if (property->getType()->isObjCObjectPointerType())
Douglas Gregorb608b982011-01-28 02:26:04 +0000929 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
John McCall1c23e912010-11-16 02:32:08 +0000930 property->getType());
Ted Kremenek9d64c152010-03-12 00:38:38 +0000931 if (result != Compatible) {
932 Diag(Loc, diag::warn_accessor_property_type_mismatch)
933 << property->getDeclName()
934 << GetterMethod->getSelector();
935 Diag(GetterMethod->getLocation(), diag::note_declared_at);
936 return true;
937 }
938 }
939 return false;
940}
941
942/// ComparePropertiesInBaseAndSuper - This routine compares property
943/// declarations in base and its super class, if any, and issues
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000944/// diagnostics in a variety of inconsistent situations.
Ted Kremenek9d64c152010-03-12 00:38:38 +0000945///
946void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
947 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
948 if (!SDecl)
949 return;
950 // FIXME: O(N^2)
951 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
952 E = SDecl->prop_end(); S != E; ++S) {
953 ObjCPropertyDecl *SuperPDecl = (*S);
954 // Does property in super class has declaration in current class?
955 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
956 E = IDecl->prop_end(); I != E; ++I) {
957 ObjCPropertyDecl *PDecl = (*I);
958 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
959 DiagnosePropertyMismatch(PDecl, SuperPDecl,
960 SDecl->getIdentifier());
961 }
962 }
963}
964
965/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
966/// of properties declared in a protocol and compares their attribute against
967/// the same property declared in the class or category.
968void
969Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
970 ObjCProtocolDecl *PDecl) {
971 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
972 if (!IDecl) {
973 // Category
974 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
975 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
976 if (!CatDecl->IsClassExtension())
977 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
978 E = PDecl->prop_end(); P != E; ++P) {
979 ObjCPropertyDecl *Pr = (*P);
980 ObjCCategoryDecl::prop_iterator CP, CE;
981 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000982 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000983 if ((*CP)->getIdentifier() == Pr->getIdentifier())
984 break;
985 if (CP != CE)
986 // Property protocol already exist in class. Diagnose any mismatch.
987 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
988 }
989 return;
990 }
991 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
992 E = PDecl->prop_end(); P != E; ++P) {
993 ObjCPropertyDecl *Pr = (*P);
994 ObjCInterfaceDecl::prop_iterator CP, CE;
995 // Is this property already in class's list of properties?
996 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
997 if ((*CP)->getIdentifier() == Pr->getIdentifier())
998 break;
999 if (CP != CE)
1000 // Property protocol already exist in class. Diagnose any mismatch.
1001 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
1002 }
1003}
1004
1005/// CompareProperties - This routine compares properties
1006/// declared in 'ClassOrProtocol' objects (which can be a class or an
1007/// inherited protocol with the list of properties for class/category 'CDecl'
1008///
John McCalld226f652010-08-21 09:40:31 +00001009void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
1010 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001011 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
1012
1013 if (!IDecl) {
1014 // Category
1015 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
1016 assert (CatDecl && "CompareProperties");
1017 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
1018 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
1019 E = MDecl->protocol_end(); P != E; ++P)
1020 // Match properties of category with those of protocol (*P)
1021 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1022
1023 // Go thru the list of protocols for this category and recursively match
1024 // their properties with those in the category.
1025 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
1026 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001027 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001028 } else {
1029 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1030 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1031 E = MD->protocol_end(); P != E; ++P)
1032 MatchOneProtocolPropertiesInClass(CatDecl, *P);
1033 }
1034 return;
1035 }
1036
1037 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001038 for (ObjCInterfaceDecl::all_protocol_iterator
1039 P = MDecl->all_referenced_protocol_begin(),
1040 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001041 // Match properties of class IDecl with those of protocol (*P).
1042 MatchOneProtocolPropertiesInClass(IDecl, *P);
1043
1044 // Go thru the list of protocols for this class and recursively match
1045 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +00001046 for (ObjCInterfaceDecl::all_protocol_iterator
1047 P = IDecl->all_referenced_protocol_begin(),
1048 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +00001049 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001050 } else {
1051 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
1052 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
1053 E = MD->protocol_end(); P != E; ++P)
1054 MatchOneProtocolPropertiesInClass(IDecl, *P);
1055 }
1056}
1057
1058/// isPropertyReadonly - Return true if property is readonly, by searching
1059/// for the property in the class and in its categories and implementations
1060///
1061bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
1062 ObjCInterfaceDecl *IDecl) {
1063 // by far the most common case.
1064 if (!PDecl->isReadOnly())
1065 return false;
1066 // Even if property is ready only, if interface has a user defined setter,
1067 // it is not considered read only.
1068 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
1069 return false;
1070
1071 // Main class has the property as 'readonly'. Must search
1072 // through the category list to see if the property's
1073 // attribute has been over-ridden to 'readwrite'.
1074 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
1075 Category; Category = Category->getNextClassCategory()) {
1076 // Even if property is ready only, if a category has a user defined setter,
1077 // it is not considered read only.
1078 if (Category->getInstanceMethod(PDecl->getSetterName()))
1079 return false;
1080 ObjCPropertyDecl *P =
1081 Category->FindPropertyDeclaration(PDecl->getIdentifier());
1082 if (P && !P->isReadOnly())
1083 return false;
1084 }
1085
1086 // Also, check for definition of a setter method in the implementation if
1087 // all else failed.
1088 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
1089 if (ObjCImplementationDecl *IMD =
1090 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
1091 if (IMD->getInstanceMethod(PDecl->getSetterName()))
1092 return false;
1093 } else if (ObjCCategoryImplDecl *CIMD =
1094 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
1095 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
1096 return false;
1097 }
1098 }
1099 // Lastly, look through the implementation (if one is in scope).
1100 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
1101 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
1102 return false;
1103 // If all fails, look at the super class.
1104 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
1105 return isPropertyReadonly(PDecl, SIDecl);
1106 return true;
1107}
1108
1109/// CollectImmediateProperties - This routine collects all properties in
1110/// the class and its conforming protocols; but not those it its super class.
1111void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001112 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
1113 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001114 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1115 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1116 E = IDecl->prop_end(); P != E; ++P) {
1117 ObjCPropertyDecl *Prop = (*P);
1118 PropMap[Prop->getIdentifier()] = Prop;
1119 }
1120 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001121 for (ObjCInterfaceDecl::all_protocol_iterator
1122 PI = IDecl->all_referenced_protocol_begin(),
1123 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001124 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001125 }
1126 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1127 if (!CATDecl->IsClassExtension())
1128 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
1129 E = CATDecl->prop_end(); P != E; ++P) {
1130 ObjCPropertyDecl *Prop = (*P);
1131 PropMap[Prop->getIdentifier()] = Prop;
1132 }
1133 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001134 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001135 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001136 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001137 }
1138 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1139 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1140 E = PDecl->prop_end(); P != E; ++P) {
1141 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001142 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1143 // Exclude property for protocols which conform to class's super-class,
1144 // as super-class has to implement the property.
1145 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
1146 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1147 if (!PropEntry)
1148 PropEntry = Prop;
1149 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001150 }
1151 // scan through protocol's protocols.
1152 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1153 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001154 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001155 }
1156}
1157
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001158/// CollectClassPropertyImplementations - This routine collects list of
1159/// properties to be implemented in the class. This includes, class's
1160/// and its conforming protocols' properties.
1161static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
1162 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1163 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1164 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1165 E = IDecl->prop_end(); P != E; ++P) {
1166 ObjCPropertyDecl *Prop = (*P);
1167 PropMap[Prop->getIdentifier()] = Prop;
1168 }
Ted Kremenek53b94412010-09-01 01:21:15 +00001169 for (ObjCInterfaceDecl::all_protocol_iterator
1170 PI = IDecl->all_referenced_protocol_begin(),
1171 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001172 CollectClassPropertyImplementations((*PI), PropMap);
1173 }
1174 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1175 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1176 E = PDecl->prop_end(); P != E; ++P) {
1177 ObjCPropertyDecl *Prop = (*P);
1178 PropMap[Prop->getIdentifier()] = Prop;
1179 }
1180 // scan through protocol's protocols.
1181 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1182 E = PDecl->protocol_end(); PI != E; ++PI)
1183 CollectClassPropertyImplementations((*PI), PropMap);
1184 }
1185}
1186
1187/// CollectSuperClassPropertyImplementations - This routine collects list of
1188/// properties to be implemented in super class(s) and also coming from their
1189/// conforming protocols.
1190static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1191 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
1192 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1193 while (SDecl) {
1194 CollectClassPropertyImplementations(SDecl, PropMap);
1195 SDecl = SDecl->getSuperClass();
1196 }
1197 }
1198}
1199
Ted Kremenek9d64c152010-03-12 00:38:38 +00001200/// LookupPropertyDecl - Looks up a property in the current class and all
1201/// its protocols.
1202ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
1203 IdentifierInfo *II) {
1204 if (const ObjCInterfaceDecl *IDecl =
1205 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1206 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
1207 E = IDecl->prop_end(); P != E; ++P) {
1208 ObjCPropertyDecl *Prop = (*P);
1209 if (Prop->getIdentifier() == II)
1210 return Prop;
1211 }
1212 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +00001213 for (ObjCInterfaceDecl::all_protocol_iterator
1214 PI = IDecl->all_referenced_protocol_begin(),
1215 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001216 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1217 if (Prop)
1218 return Prop;
1219 }
1220 }
1221 else if (const ObjCProtocolDecl *PDecl =
1222 dyn_cast<ObjCProtocolDecl>(CDecl)) {
1223 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
1224 E = PDecl->prop_end(); P != E; ++P) {
1225 ObjCPropertyDecl *Prop = (*P);
1226 if (Prop->getIdentifier() == II)
1227 return Prop;
1228 }
1229 // scan through protocol's protocols.
1230 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1231 E = PDecl->protocol_end(); PI != E; ++PI) {
1232 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
1233 if (Prop)
1234 return Prop;
1235 }
1236 }
1237 return 0;
1238}
1239
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001240/// DefaultSynthesizeProperties - This routine default synthesizes all
1241/// properties which must be synthesized in class's @implementation.
1242void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
1243 ObjCInterfaceDecl *IDecl) {
1244
1245 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
1246 CollectClassPropertyImplementations(IDecl, PropMap);
1247 if (PropMap.empty())
1248 return;
1249 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1250 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1251
1252 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1253 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1254 ObjCPropertyDecl *Prop = P->second;
1255 // If property to be implemented in the super class, ignore.
1256 if (SuperPropMap[Prop->getIdentifier()])
1257 continue;
1258 // Is there a matching propery synthesize/dynamic?
1259 if (Prop->isInvalidDecl() ||
1260 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1261 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
1262 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001263 // Property may have been synthesized by user.
1264 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1265 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +00001266 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1267 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1268 continue;
1269 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1270 continue;
1271 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +00001272
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001273
1274 // We use invalid SourceLocations for the synthesized ivars since they
1275 // aren't really synthesized at a particular location; they just exist.
1276 // Saying that they are located at the @implementation isn't really going
1277 // to help users.
1278 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1279 true,IMPDecl,
Douglas Gregora4ffd852010-11-17 01:03:52 +00001280 Prop->getIdentifier(), Prop->getIdentifier(),
1281 SourceLocation());
Ted Kremenek2a6af6b2010-09-24 01:23:01 +00001282 }
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001283}
Ted Kremenek9d64c152010-03-12 00:38:38 +00001284
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001285void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001286 ObjCContainerDecl *CDecl,
1287 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001288 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
1289 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1290 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1291
Ted Kremenek9d64c152010-03-12 00:38:38 +00001292 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +00001293 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001294 if (PropMap.empty())
1295 return;
1296
1297 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1298 for (ObjCImplDecl::propimpl_iterator
1299 I = IMPDecl->propimpl_begin(),
1300 EI = IMPDecl->propimpl_end(); I != EI; ++I)
1301 PropImplMap.insert((*I)->getPropertyDecl());
1302
1303 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
1304 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1305 ObjCPropertyDecl *Prop = P->second;
1306 // Is there a matching propery synthesize/dynamic?
1307 if (Prop->isInvalidDecl() ||
1308 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001309 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>())
Ted Kremenek9d64c152010-03-12 00:38:38 +00001310 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001311 if (!InsMap.count(Prop->getGetterName())) {
1312 Diag(Prop->getLocation(),
1313 isa<ObjCCategoryDecl>(CDecl) ?
1314 diag::warn_setter_getter_impl_required_in_category :
1315 diag::warn_setter_getter_impl_required)
1316 << Prop->getDeclName() << Prop->getGetterName();
1317 Diag(IMPDecl->getLocation(),
1318 diag::note_property_impl_required);
1319 }
1320
1321 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1322 Diag(Prop->getLocation(),
1323 isa<ObjCCategoryDecl>(CDecl) ?
1324 diag::warn_setter_getter_impl_required_in_category :
1325 diag::warn_setter_getter_impl_required)
1326 << Prop->getDeclName() << Prop->getSetterName();
1327 Diag(IMPDecl->getLocation(),
1328 diag::note_property_impl_required);
1329 }
1330 }
1331}
1332
1333void
1334Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1335 ObjCContainerDecl* IDecl) {
1336 // Rules apply in non-GC mode only
1337 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1338 return;
1339 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1340 E = IDecl->prop_end();
1341 I != E; ++I) {
1342 ObjCPropertyDecl *Property = (*I);
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001343 ObjCMethodDecl *GetterMethod = 0;
1344 ObjCMethodDecl *SetterMethod = 0;
1345 bool LookedUpGetterSetter = false;
1346
Ted Kremenek9d64c152010-03-12 00:38:38 +00001347 unsigned Attributes = Property->getPropertyAttributes();
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001348 unsigned AttributesAsWrittern = Property->getPropertyAttributesAsWritten();
1349
Fariborz Jahanian45937ae2011-06-11 00:45:12 +00001350 if (!(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_atomic) &&
1351 !(AttributesAsWrittern & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001352 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1353 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1354 LookedUpGetterSetter = true;
1355 if (GetterMethod) {
1356 Diag(GetterMethod->getLocation(),
1357 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001358 << Property->getIdentifier() << 0;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001359 Diag(Property->getLocation(), diag::note_property_declare);
1360 }
1361 if (SetterMethod) {
1362 Diag(SetterMethod->getLocation(),
1363 diag::warn_default_atomic_custom_getter_setter)
Argyrios Kyrtzidis293a45e2011-01-31 23:20:03 +00001364 << Property->getIdentifier() << 1;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001365 Diag(Property->getLocation(), diag::note_property_declare);
1366 }
1367 }
1368
Ted Kremenek9d64c152010-03-12 00:38:38 +00001369 // We only care about readwrite atomic property.
1370 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1371 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1372 continue;
1373 if (const ObjCPropertyImplDecl *PIDecl
1374 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1375 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1376 continue;
Argyrios Kyrtzidis94659e42011-01-31 21:34:11 +00001377 if (!LookedUpGetterSetter) {
1378 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1379 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1380 LookedUpGetterSetter = true;
1381 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001382 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1383 SourceLocation MethodLoc =
1384 (GetterMethod ? GetterMethod->getLocation()
1385 : SetterMethod->getLocation());
1386 Diag(MethodLoc, diag::warn_atomic_property_rule)
1387 << Property->getIdentifier();
1388 Diag(Property->getLocation(), diag::note_property_declare);
1389 }
1390 }
1391 }
1392}
1393
John McCallf85e1932011-06-15 23:02:42 +00001394void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1395 if (getLangOptions().getGCMode() == LangOptions::GCOnly)
1396 return;
1397
1398 for (ObjCImplementationDecl::propimpl_iterator
1399 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) {
1400 ObjCPropertyImplDecl *PID = *i;
1401 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
1402 continue;
1403
1404 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001405 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1406 !D->getInstanceMethod(PD->getGetterName())) {
John McCallf85e1932011-06-15 23:02:42 +00001407 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1408 if (!method)
1409 continue;
1410 ObjCMethodFamily family = method->getMethodFamily();
1411 if (family == OMF_alloc || family == OMF_copy ||
1412 family == OMF_mutableCopy || family == OMF_new) {
1413 if (getLangOptions().ObjCAutoRefCount)
1414 Diag(PID->getLocation(), diag::err_ownin_getter_rule);
1415 else
1416 Diag(PID->getLocation(), diag::warn_ownin_getter_rule);
1417 Diag(PD->getLocation(), diag::note_property_declare);
1418 }
1419 }
1420 }
1421}
1422
John McCall5de74d12010-11-10 07:01:40 +00001423/// AddPropertyAttrs - Propagates attributes from a property to the
1424/// implicitly-declared getter or setter for that property.
1425static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1426 ObjCPropertyDecl *Property) {
1427 // Should we just clone all attributes over?
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001428 for (Decl::attr_iterator A = Property->attr_begin(),
1429 AEnd = Property->attr_end();
1430 A != AEnd; ++A) {
1431 if (isa<DeprecatedAttr>(*A) ||
1432 isa<UnavailableAttr>(*A) ||
1433 isa<AvailabilityAttr>(*A))
1434 PropertyMethod->addAttr((*A)->clone(S.Context));
1435 }
John McCall5de74d12010-11-10 07:01:40 +00001436}
1437
Ted Kremenek9d64c152010-03-12 00:38:38 +00001438/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1439/// have the property type and issue diagnostics if they don't.
1440/// Also synthesize a getter/setter method if none exist (and update the
1441/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1442/// methods is the "right" thing to do.
1443void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
Ted Kremenek8254aa62010-09-21 18:28:43 +00001444 ObjCContainerDecl *CD,
1445 ObjCPropertyDecl *redeclaredProperty,
1446 ObjCContainerDecl *lexicalDC) {
1447
Ted Kremenek9d64c152010-03-12 00:38:38 +00001448 ObjCMethodDecl *GetterMethod, *SetterMethod;
1449
1450 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1451 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1452 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1453 property->getLocation());
1454
1455 if (SetterMethod) {
1456 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1457 property->getPropertyAttributes();
1458 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1459 Context.getCanonicalType(SetterMethod->getResultType()) !=
1460 Context.VoidTy)
1461 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1462 if (SetterMethod->param_size() != 1 ||
1463 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1464 Diag(property->getLocation(),
1465 diag::warn_accessor_property_type_mismatch)
1466 << property->getDeclName()
1467 << SetterMethod->getSelector();
1468 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1469 }
1470 }
1471
1472 // Synthesize getter/setter methods if none exist.
1473 // Find the default getter and if one not found, add one.
1474 // FIXME: The synthesized property we set here is misleading. We almost always
1475 // synthesize these methods unless the user explicitly provided prototypes
1476 // (which is odd, but allowed). Sema should be typechecking that the
1477 // declarations jive in that situation (which it is not currently).
1478 if (!GetterMethod) {
1479 // No instance method of same name as property getter name was found.
1480 // Declare a getter method and add it to the list of methods
1481 // for this class.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001482 SourceLocation Loc = redeclaredProperty ?
1483 redeclaredProperty->getLocation() :
1484 property->getLocation();
1485
1486 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
1487 property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001488 property->getType(), 0, CD, true, false, true,
1489 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001490 (property->getPropertyImplementation() ==
1491 ObjCPropertyDecl::Optional) ?
1492 ObjCMethodDecl::Optional :
1493 ObjCMethodDecl::Required);
1494 CD->addDecl(GetterMethod);
John McCall5de74d12010-11-10 07:01:40 +00001495
1496 AddPropertyAttrs(*this, GetterMethod, property);
1497
Ted Kremenek23173d72010-05-18 21:09:07 +00001498 // FIXME: Eventually this shouldn't be needed, as the lexical context
1499 // and the real context should be the same.
Ted Kremeneka054fb42010-09-21 20:52:59 +00001500 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001501 GetterMethod->setLexicalDeclContext(lexicalDC);
Fariborz Jahanian831fb962011-06-25 00:17:46 +00001502 if (property->hasAttr<NSReturnsNotRetainedAttr>())
1503 GetterMethod->addAttr(
1504 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context));
Ted Kremenek9d64c152010-03-12 00:38:38 +00001505 } else
1506 // A user declared getter will be synthesize when @synthesize of
1507 // the property with the same name is seen in the @implementation
1508 GetterMethod->setSynthesized(true);
1509 property->setGetterMethodDecl(GetterMethod);
1510
1511 // Skip setter if property is read-only.
1512 if (!property->isReadOnly()) {
1513 // Find the default setter and if one not found, add one.
1514 if (!SetterMethod) {
1515 // No instance method of same name as property setter name was found.
1516 // Declare a setter method and add it to the list of methods
1517 // for this class.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001518 SourceLocation Loc = redeclaredProperty ?
1519 redeclaredProperty->getLocation() :
1520 property->getLocation();
1521
1522 SetterMethod =
1523 ObjCMethodDecl::Create(Context, Loc, Loc,
1524 property->getSetterName(), Context.VoidTy, 0,
1525 CD, true, false, true, false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001526 (property->getPropertyImplementation() ==
1527 ObjCPropertyDecl::Optional) ?
Ted Kremenek8254aa62010-09-21 18:28:43 +00001528 ObjCMethodDecl::Optional :
1529 ObjCMethodDecl::Required);
1530
Ted Kremenek9d64c152010-03-12 00:38:38 +00001531 // Invent the arguments for the setter. We don't bother making a
1532 // nice name for the argument.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001533 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1534 Loc, Loc,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001535 property->getIdentifier(),
John McCallf85e1932011-06-15 23:02:42 +00001536 property->getType().getUnqualifiedType(),
Ted Kremenek9d64c152010-03-12 00:38:38 +00001537 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001538 SC_None,
1539 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001540 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001541 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
John McCall5de74d12010-11-10 07:01:40 +00001542
1543 AddPropertyAttrs(*this, SetterMethod, property);
1544
Ted Kremenek9d64c152010-03-12 00:38:38 +00001545 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001546 // FIXME: Eventually this shouldn't be needed, as the lexical context
1547 // and the real context should be the same.
Ted Kremenek8254aa62010-09-21 18:28:43 +00001548 if (lexicalDC)
Ted Kremenek23173d72010-05-18 21:09:07 +00001549 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001550 } else
1551 // A user declared setter will be synthesize when @synthesize of
1552 // the property with the same name is seen in the @implementation
1553 SetterMethod->setSynthesized(true);
1554 property->setSetterMethodDecl(SetterMethod);
1555 }
1556 // Add any synthesized methods to the global pool. This allows us to
1557 // handle the following, which is supported by GCC (and part of the design).
1558 //
1559 // @interface Foo
1560 // @property double bar;
1561 // @end
1562 //
1563 // void thisIsUnfortunate() {
1564 // id foo;
1565 // double bar = [foo bar];
1566 // }
1567 //
1568 if (GetterMethod)
1569 AddInstanceMethodToGlobalPool(GetterMethod);
1570 if (SetterMethod)
1571 AddInstanceMethodToGlobalPool(SetterMethod);
1572}
1573
John McCalld226f652010-08-21 09:40:31 +00001574void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001575 SourceLocation Loc,
1576 unsigned &Attributes) {
1577 // FIXME: Improve the reported location.
John McCallf85e1932011-06-15 23:02:42 +00001578 if (!PDecl || PDecl->isInvalidDecl())
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001579 return;
1580
1581 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001582 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001583
1584 // readonly and readwrite/assign/retain/copy conflict.
1585 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1586 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1587 ObjCDeclSpec::DQ_PR_assign |
John McCallf85e1932011-06-15 23:02:42 +00001588 ObjCDeclSpec::DQ_PR_unsafe_unretained |
Ted Kremenek9d64c152010-03-12 00:38:38 +00001589 ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001590 ObjCDeclSpec::DQ_PR_retain |
1591 ObjCDeclSpec::DQ_PR_strong))) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001592 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1593 "readwrite" :
1594 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1595 "assign" :
John McCallf85e1932011-06-15 23:02:42 +00001596 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ?
1597 "unsafe_unretained" :
Ted Kremenek9d64c152010-03-12 00:38:38 +00001598 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1599 "copy" : "retain";
1600
1601 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1602 diag::err_objc_property_attr_mutually_exclusive :
1603 diag::warn_objc_property_attr_mutually_exclusive)
1604 << "readonly" << which;
1605 }
1606
1607 // Check for copy or retain on non-object types.
John McCallf85e1932011-06-15 23:02:42 +00001608 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1609 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
1610 !PropertyTy->isObjCRetainableType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001611 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001612 Diag(Loc, diag::err_objc_property_requires_object)
John McCallf85e1932011-06-15 23:02:42 +00001613 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
1614 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
1615 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
1616 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001617 }
1618
1619 // Check for more than one of { assign, copy, retain }.
1620 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1621 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1622 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1623 << "assign" << "copy";
1624 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1625 }
1626 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1627 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1628 << "assign" << "retain";
1629 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1630 }
John McCallf85e1932011-06-15 23:02:42 +00001631 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1632 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1633 << "assign" << "strong";
1634 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1635 }
1636 if (getLangOptions().ObjCAutoRefCount &&
1637 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1638 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1639 << "assign" << "weak";
1640 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1641 }
1642 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
1643 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1644 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1645 << "unsafe_unretained" << "copy";
1646 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1647 }
1648 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1649 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1650 << "unsafe_unretained" << "retain";
1651 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1652 }
1653 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1654 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1655 << "unsafe_unretained" << "strong";
1656 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1657 }
1658 if (getLangOptions().ObjCAutoRefCount &&
1659 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1660 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1661 << "unsafe_unretained" << "weak";
1662 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1663 }
Ted Kremenek9d64c152010-03-12 00:38:38 +00001664 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1665 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1666 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1667 << "copy" << "retain";
1668 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1669 }
John McCallf85e1932011-06-15 23:02:42 +00001670 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
1671 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1672 << "copy" << "strong";
1673 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
1674 }
1675 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
1676 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1677 << "copy" << "weak";
1678 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1679 }
1680 }
1681 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
1682 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1683 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1684 << "retain" << "weak";
1685 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
1686 }
1687 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
1688 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
1689 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1690 << "strong" << "weak";
1691 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001692 }
1693
1694 // Warn if user supplied no assignment attribute, property is
1695 // readwrite, and this is an object type.
1696 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
John McCallf85e1932011-06-15 23:02:42 +00001697 ObjCDeclSpec::DQ_PR_unsafe_unretained |
1698 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
1699 ObjCDeclSpec::DQ_PR_weak)) &&
Ted Kremenek9d64c152010-03-12 00:38:38 +00001700 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1701 PropertyTy->isObjCObjectPointerType()) {
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +00001702 // Skip this warning in gc-only mode.
1703 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1704 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001705
Argyrios Kyrtzidis0a68dc72011-07-12 04:30:16 +00001706 // If non-gc code warn that this is likely inappropriate.
1707 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1708 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001709
1710 // FIXME: Implement warning dependent on NSCopying being
1711 // implemented. See also:
1712 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1713 // (please trim this list while you are at it).
1714 }
1715
1716 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
Fariborz Jahanian2b77cb82011-01-05 23:00:04 +00001717 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
Ted Kremenek9d64c152010-03-12 00:38:38 +00001718 && getLangOptions().getGCMode() == LangOptions::GCOnly
1719 && PropertyTy->isBlockPointerType())
1720 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1721}