blob: 7181d58f7fb07cb389d51fc9d83bd2f684251cf1 [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 McCalld226f652010-08-21 09:40:31 +000027Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
28 FieldDeclarator &FD,
29 ObjCDeclSpec &ODS,
30 Selector GetterSel,
31 Selector SetterSel,
32 Decl *ClassCategory,
33 bool *isOverridingProperty,
34 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000035 unsigned Attributes = ODS.getPropertyAttributes();
36 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
37 // default is readwrite!
38 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
39 // property is defaulted to 'assign' if it is readwrite and is
40 // not retain or copy
41 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
42 (isReadWrite &&
43 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
44 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000045
John McCallbf1a0282010-06-04 23:28:52 +000046 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
47 QualType T = TSI->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +000048 if (T->isReferenceType()) {
49 Diag(AtLoc, diag::error_reference_property);
John McCalld226f652010-08-21 09:40:31 +000050 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +000051 }
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000052 // Proceed with constructing the ObjCPropertDecls.
53 ObjCContainerDecl *ClassDecl =
John McCalld226f652010-08-21 09:40:31 +000054 cast<ObjCContainerDecl>(ClassCategory);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000055
Ted Kremenek28685ab2010-03-12 00:46:40 +000056 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000057 if (CDecl->IsClassExtension()) {
John McCalld226f652010-08-21 09:40:31 +000058 Decl *Res = HandlePropertyInClassExtension(S, CDecl, AtLoc,
Fariborz Jahanianae415dc2010-07-13 22:04:56 +000059 FD, GetterSel, SetterSel,
60 isAssign, isReadWrite,
61 Attributes,
62 isOverridingProperty, TSI,
63 MethodImplKind);
64 if (Res)
65 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
66 return Res;
67 }
68
John McCalld226f652010-08-21 09:40:31 +000069 Decl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
70 GetterSel, SetterSel,
71 isAssign, isReadWrite,
72 Attributes, TSI, MethodImplKind);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +000073 // Validate the attributes on the @property.
74 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
75 return Res;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000076}
Ted Kremenek2d2f9362010-03-12 00:49:00 +000077
John McCalld226f652010-08-21 09:40:31 +000078Decl *
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000079Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
80 SourceLocation AtLoc, FieldDeclarator &FD,
81 Selector GetterSel, Selector SetterSel,
82 const bool isAssign,
83 const bool isReadWrite,
84 const unsigned Attributes,
85 bool *isOverridingProperty,
John McCall83a230c2010-06-04 20:50:08 +000086 TypeSourceInfo *T,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000087 tok::ObjCKeywordKind MethodImplKind) {
Ted Kremenek28685ab2010-03-12 00:46:40 +000088
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000089 // Diagnose if this property is already in continuation class.
90 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000091 IdentifierInfo *PropertyId = FD.D.getIdentifier();
Ted Kremenek894ae6a2010-03-15 18:47:25 +000092
Ted Kremenek9f550ff2010-03-15 20:11:46 +000093 if (ObjCPropertyDecl *prevDecl =
94 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000095 Diag(AtLoc, diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +000096 Diag(prevDecl->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +000097 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +000098 }
99
100 // Create a new ObjCPropertyDecl with the DeclContext being
101 // the class extension.
102 ObjCPropertyDecl *PDecl =
103 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
104 PropertyId, AtLoc, T);
Fariborz Jahanian22f757b2010-03-22 23:25:52 +0000105 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
106 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
107 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
108 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
109
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000110 DC->addDecl(PDecl);
111
112 // We need to look in the @interface to see if the @property was
113 // already declared.
114 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
115 if (!CCPrimary) {
116 Diag(CDecl->getLocation(), diag::err_continuation_class);
117 *isOverridingProperty = true;
John McCalld226f652010-08-21 09:40:31 +0000118 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000119 }
120
121 // Find the property in continuation class's primary class only.
122 ObjCPropertyDecl *PIDecl =
123 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
124
125 if (!PIDecl) {
126 // No matching property found in the primary class. Just fall thru
127 // and add property to continuation class's primary class.
128 ObjCPropertyDecl *PDecl =
129 CreatePropertyDecl(S, CCPrimary, AtLoc,
130 FD, GetterSel, SetterSel, isAssign, isReadWrite,
Ted Kremenek23173d72010-05-18 21:09:07 +0000131 Attributes, T, MethodImplKind, DC);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000132 // Mark written attribute as having no attribute because
133 // this is not a user-written property declaration in primary
134 // class.
135 PDecl->setPropertyAttributesAsWritten(ObjCPropertyDecl::OBJC_PR_noattr);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000136
137 // A case of continuation class adding a new property in the class. This
138 // is not what it was meant for. However, gcc supports it and so should we.
139 // Make sure setter/getters are declared here.
140 ProcessPropertyDecl(PDecl, CCPrimary);
John McCalld226f652010-08-21 09:40:31 +0000141 return PDecl;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000142
143 }
144
145 // The property 'PIDecl's readonly attribute will be over-ridden
146 // with continuation class's readwrite property attribute!
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000147 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000148 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
149 unsigned retainCopyNonatomic =
150 (ObjCPropertyDecl::OBJC_PR_retain |
151 ObjCPropertyDecl::OBJC_PR_copy |
152 ObjCPropertyDecl::OBJC_PR_nonatomic);
153 if ((Attributes & retainCopyNonatomic) !=
154 (PIkind & retainCopyNonatomic)) {
155 Diag(AtLoc, diag::warn_property_attr_mismatch);
156 Diag(PIDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000157 }
Ted Kremenek9944c762010-03-18 01:22:36 +0000158 DeclContext *DC = cast<DeclContext>(CCPrimary);
159 if (!ObjCPropertyDecl::findPropertyDecl(DC,
160 PIDecl->getDeclName().getAsIdentifierInfo())) {
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000161 // Protocol is not in the primary class. Must build one for it.
162 ObjCDeclSpec ProtocolPropertyODS;
163 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
164 // and ObjCPropertyDecl::PropertyAttributeKind have identical
165 // values. Should consolidate both into one enum type.
166 ProtocolPropertyODS.
167 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
168 PIkind);
169
John McCalld226f652010-08-21 09:40:31 +0000170 Decl *ProtocolPtrTy =
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000171 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
172 PIDecl->getGetterName(),
173 PIDecl->getSetterName(),
John McCalld226f652010-08-21 09:40:31 +0000174 CCPrimary, isOverridingProperty,
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000175 MethodImplKind);
John McCalld226f652010-08-21 09:40:31 +0000176 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000177 }
178 PIDecl->makeitReadWriteAttribute();
179 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
180 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
181 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
182 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
183 PIDecl->setSetterName(SetterSel);
184 } else {
185 Diag(AtLoc, diag::err_use_continuation_class)
186 << CCPrimary->getDeclName();
187 Diag(PIDecl->getLocation(), diag::note_property_declare);
188 }
189 *isOverridingProperty = true;
190 // Make sure setter decl is synthesized, and added to primary class's list.
191 ProcessPropertyDecl(PIDecl, CCPrimary);
John McCalld226f652010-08-21 09:40:31 +0000192 return 0;
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000193}
194
195ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
196 ObjCContainerDecl *CDecl,
197 SourceLocation AtLoc,
198 FieldDeclarator &FD,
199 Selector GetterSel,
200 Selector SetterSel,
201 const bool isAssign,
202 const bool isReadWrite,
203 const unsigned Attributes,
John McCall83a230c2010-06-04 20:50:08 +0000204 TypeSourceInfo *TInfo,
Ted Kremenek23173d72010-05-18 21:09:07 +0000205 tok::ObjCKeywordKind MethodImplKind,
206 DeclContext *lexicalDC){
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000207 IdentifierInfo *PropertyId = FD.D.getIdentifier();
John McCall83a230c2010-06-04 20:50:08 +0000208 QualType T = TInfo->getType();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000209
210 // Issue a warning if property is 'assign' as default and its object, which is
211 // gc'able conforms to NSCopying protocol
212 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
213 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
John McCallc12c5bb2010-05-15 11:32:37 +0000214 if (const ObjCObjectPointerType *ObjPtrTy =
215 T->getAs<ObjCObjectPointerType>()) {
216 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
217 if (IDecl)
218 if (ObjCProtocolDecl* PNSCopying =
219 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
220 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
221 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000222 }
John McCallc12c5bb2010-05-15 11:32:37 +0000223 if (T->isObjCObjectType())
Ted Kremenek28685ab2010-03-12 00:46:40 +0000224 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
225
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000226 DeclContext *DC = cast<DeclContext>(CDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000227 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
228 FD.D.getIdentifierLoc(),
John McCall83a230c2010-06-04 20:50:08 +0000229 PropertyId, AtLoc, TInfo);
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000230
Ted Kremenek9f550ff2010-03-15 20:11:46 +0000231 if (ObjCPropertyDecl *prevDecl =
232 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000233 Diag(PDecl->getLocation(), diag::err_duplicate_property);
Ted Kremenek894ae6a2010-03-15 18:47:25 +0000234 Diag(prevDecl->getLocation(), diag::note_property_declare);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000235 PDecl->setInvalidDecl();
236 }
Ted Kremenek23173d72010-05-18 21:09:07 +0000237 else {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000238 DC->addDecl(PDecl);
Ted Kremenek23173d72010-05-18 21:09:07 +0000239 if (lexicalDC)
240 PDecl->setLexicalDeclContext(lexicalDC);
241 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000242
243 if (T->isArrayType() || T->isFunctionType()) {
244 Diag(AtLoc, diag::err_property_type) << T;
245 PDecl->setInvalidDecl();
246 }
247
248 ProcessDeclAttributes(S, PDecl, FD.D);
249
250 // Regardless of setter/getter attribute, we save the default getter/setter
251 // selector names in anticipation of declaration of setter/getter methods.
252 PDecl->setGetterName(GetterSel);
253 PDecl->setSetterName(SetterSel);
254
255 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
256 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
257
258 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
259 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
260
261 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
262 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
263
264 if (isReadWrite)
265 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
266
267 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
268 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
269
270 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
271 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
272
273 if (isAssign)
274 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
275
276 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
277 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
278
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000279 PDecl->setPropertyAttributesAsWritten(PDecl->getPropertyAttributes());
280
Ted Kremenek28685ab2010-03-12 00:46:40 +0000281 if (MethodImplKind == tok::objc_required)
282 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
283 else if (MethodImplKind == tok::objc_optional)
284 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000285
Ted Kremeneke3d67bc2010-03-12 02:31:10 +0000286 return PDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000287}
288
289
290/// ActOnPropertyImplDecl - This routine performs semantic checks and
291/// builds the AST node for a property implementation declaration; declared
292/// as @synthesize or @dynamic.
293///
John McCalld226f652010-08-21 09:40:31 +0000294Decl *Sema::ActOnPropertyImplDecl(Scope *S,
295 SourceLocation AtLoc,
296 SourceLocation PropertyLoc,
297 bool Synthesize,
298 Decl *ClassCatImpDecl,
299 IdentifierInfo *PropertyId,
300 IdentifierInfo *PropertyIvar) {
Ted Kremeneke9686572010-04-05 23:45:09 +0000301 ObjCContainerDecl *ClassImpDecl =
John McCalld226f652010-08-21 09:40:31 +0000302 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000303 // Make sure we have a context for the property implementation declaration.
304 if (!ClassImpDecl) {
305 Diag(AtLoc, diag::error_missing_property_context);
John McCalld226f652010-08-21 09:40:31 +0000306 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000307 }
308 ObjCPropertyDecl *property = 0;
309 ObjCInterfaceDecl* IDecl = 0;
310 // Find the class or category class where this property must have
311 // a declaration.
312 ObjCImplementationDecl *IC = 0;
313 ObjCCategoryImplDecl* CatImplClass = 0;
314 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
315 IDecl = IC->getClassInterface();
316 // We always synthesize an interface for an implementation
317 // without an interface decl. So, IDecl is always non-zero.
318 assert(IDecl &&
319 "ActOnPropertyImplDecl - @implementation without @interface");
320
321 // Look for this property declaration in the @implementation's @interface
322 property = IDecl->FindPropertyDeclaration(PropertyId);
323 if (!property) {
324 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000325 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000326 }
327 if (const ObjCCategoryDecl *CD =
328 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
329 if (!CD->IsClassExtension()) {
330 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
331 Diag(property->getLocation(), diag::note_property_declare);
John McCalld226f652010-08-21 09:40:31 +0000332 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000333 }
334 }
335 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
336 if (Synthesize) {
337 Diag(AtLoc, diag::error_synthesize_category_decl);
John McCalld226f652010-08-21 09:40:31 +0000338 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000339 }
340 IDecl = CatImplClass->getClassInterface();
341 if (!IDecl) {
342 Diag(AtLoc, diag::error_missing_property_interface);
John McCalld226f652010-08-21 09:40:31 +0000343 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000344 }
345 ObjCCategoryDecl *Category =
346 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
347
348 // If category for this implementation not found, it is an error which
349 // has already been reported eralier.
350 if (!Category)
John McCalld226f652010-08-21 09:40:31 +0000351 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000352 // Look for this property declaration in @implementation's category
353 property = Category->FindPropertyDeclaration(PropertyId);
354 if (!property) {
355 Diag(PropertyLoc, diag::error_bad_category_property_decl)
356 << Category->getDeclName();
John McCalld226f652010-08-21 09:40:31 +0000357 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000358 }
359 } else {
360 Diag(AtLoc, diag::error_bad_property_context);
John McCalld226f652010-08-21 09:40:31 +0000361 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000362 }
363 ObjCIvarDecl *Ivar = 0;
364 // Check that we have a valid, previously declared ivar for @synthesize
365 if (Synthesize) {
366 // @synthesize
367 if (!PropertyIvar)
368 PropertyIvar = PropertyId;
369 QualType PropType = Context.getCanonicalType(property->getType());
370 // Check that this is a previously declared 'ivar' in 'IDecl' interface
371 ObjCInterfaceDecl *ClassDeclared;
372 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
373 if (!Ivar) {
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000374 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
Ted Kremenek28685ab2010-03-12 00:46:40 +0000375 PropertyIvar, PropType, /*Dinfo=*/0,
Fariborz Jahanian2846b2b2010-04-06 23:36:17 +0000376 ObjCIvarDecl::Protected,
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000377 (Expr *)0, true);
Daniel Dunbar29fa69a2010-04-02 19:44:54 +0000378 ClassImpDecl->addDecl(Ivar);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000379 IDecl->makeDeclVisibleInContext(Ivar, false);
380 property->setPropertyIvarDecl(Ivar);
381
382 if (!getLangOptions().ObjCNonFragileABI)
383 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
384 // Note! I deliberately want it to fall thru so, we have a
385 // a property implementation and to avoid future warnings.
386 } else if (getLangOptions().ObjCNonFragileABI &&
387 ClassDeclared != IDecl) {
388 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
389 << property->getDeclName() << Ivar->getDeclName()
390 << ClassDeclared->getDeclName();
391 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
Daniel Dunbar4087f272010-08-17 22:39:59 +0000392 << Ivar << Ivar->getName();
Ted Kremenek28685ab2010-03-12 00:46:40 +0000393 // Note! I deliberately want it to fall thru so more errors are caught.
394 }
395 QualType IvarType = Context.getCanonicalType(Ivar->getType());
396
397 // Check that type of property and its ivar are type compatible.
398 if (PropType != IvarType) {
Fariborz Jahanian62ac5d02010-05-18 23:04:17 +0000399 bool compat = false;
400 if (isa<ObjCObjectPointerType>(PropType)
401 && isa<ObjCObjectPointerType>(IvarType))
402 compat =
403 Context.canAssignObjCInterfaces(
404 PropType->getAs<ObjCObjectPointerType>(),
405 IvarType->getAs<ObjCObjectPointerType>());
406 else
407 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
408 if (!compat) {
Ted Kremenek28685ab2010-03-12 00:46:40 +0000409 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000410 << property->getDeclName() << PropType
411 << Ivar->getDeclName() << IvarType;
412 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000413 // Note! I deliberately want it to fall thru so, we have a
414 // a property implementation and to avoid future warnings.
415 }
416
417 // FIXME! Rules for properties are somewhat different that those
418 // for assignments. Use a new routine to consolidate all cases;
419 // specifically for property redeclarations as well as for ivars.
420 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
421 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
422 if (lhsType != rhsType &&
423 lhsType->isArithmeticType()) {
424 Diag(PropertyLoc, diag::error_property_ivar_type)
Ted Kremenekf921a482010-03-23 19:02:22 +0000425 << property->getDeclName() << PropType
426 << Ivar->getDeclName() << IvarType;
427 Diag(Ivar->getLocation(), diag::note_ivar_decl);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000428 // Fall thru - see previous comment
429 }
430 // __weak is explicit. So it works on Canonical type.
431 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
432 getLangOptions().getGCMode() != LangOptions::NonGC) {
433 Diag(PropertyLoc, diag::error_weak_property)
434 << property->getDeclName() << Ivar->getDeclName();
435 // Fall thru - see previous comment
436 }
437 if ((property->getType()->isObjCObjectPointerType() ||
438 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
439 getLangOptions().getGCMode() != LangOptions::NonGC) {
440 Diag(PropertyLoc, diag::error_strong_property)
441 << property->getDeclName() << Ivar->getDeclName();
442 // Fall thru - see previous comment
443 }
444 }
445 } else if (PropertyIvar)
446 // @dynamic
447 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
448 assert (property && "ActOnPropertyImplDecl - property declaration missing");
449 ObjCPropertyImplDecl *PIDecl =
450 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
451 property,
452 (Synthesize ?
453 ObjCPropertyImplDecl::Synthesize
454 : ObjCPropertyImplDecl::Dynamic),
455 Ivar);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000456 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
457 getterMethod->createImplicitParams(Context, IDecl);
458 if (getLangOptions().CPlusPlus && Synthesize) {
459 // For Objective-C++, need to synthesize the AST for the IVAR object to be
460 // returned by the getter as it must conform to C++'s copy-return rules.
461 // FIXME. Eventually we want to do this for Objective-C as well.
462 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
463 DeclRefExpr *SelfExpr =
464 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
465 SourceLocation());
466 Expr *IvarRefExpr =
467 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
468 SelfExpr, true, true);
John McCall60d7b3a2010-08-24 06:29:42 +0000469 ExprResult Res =
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000470 PerformCopyInitialization(InitializedEntity::InitializeResult(
Douglas Gregor3c9034c2010-05-15 00:13:29 +0000471 SourceLocation(),
472 getterMethod->getResultType(),
473 /*NRVO=*/false),
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000474 SourceLocation(),
475 Owned(IvarRefExpr));
476 if (!Res.isInvalid()) {
477 Expr *ResExpr = Res.takeAs<Expr>();
478 if (ResExpr)
479 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
480 PIDecl->setGetterCXXConstructor(ResExpr);
481 }
482 }
483 }
484 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
485 setterMethod->createImplicitParams(Context, IDecl);
486 if (getLangOptions().CPlusPlus && Synthesize) {
487 // FIXME. Eventually we want to do this for Objective-C as well.
488 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
489 DeclRefExpr *SelfExpr =
490 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
491 SourceLocation());
492 Expr *lhs =
493 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
494 SelfExpr, true, true);
495 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
496 ParmVarDecl *Param = (*P);
497 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
498 SourceLocation());
John McCall60d7b3a2010-08-24 06:29:42 +0000499 ExprResult Res = BuildBinOp(S, SourceLocation(),
John McCall2de56d12010-08-25 11:45:40 +0000500 BO_Assign, lhs, rhs);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000501 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
502 }
503 }
504
Ted Kremenek28685ab2010-03-12 00:46:40 +0000505 if (IC) {
506 if (Synthesize)
507 if (ObjCPropertyImplDecl *PPIDecl =
508 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
509 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
510 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
511 << PropertyIvar;
512 Diag(PPIDecl->getLocation(), diag::note_previous_use);
513 }
514
515 if (ObjCPropertyImplDecl *PPIDecl
516 = IC->FindPropertyImplDecl(PropertyId)) {
517 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
518 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000519 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000520 }
521 IC->addPropertyImplementation(PIDecl);
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000522 if (getLangOptions().ObjCNonFragileABI2) {
523 // Diagnose if an ivar was lazily synthesdized due to a previous
524 // use and if 1) property is @dynamic or 2) property is synthesized
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000525 // but it requires an ivar of different name.
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000526 ObjCInterfaceDecl *ClassDeclared;
527 ObjCIvarDecl *Ivar = 0;
528 if (!Synthesize)
529 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
530 else {
531 if (PropertyIvar && PropertyIvar != PropertyId)
532 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
533 }
Fariborz Jahaniancdaa6a82010-08-24 18:48:05 +0000534 // Issue diagnostics only if Ivar belongs to current class.
535 if (Ivar && Ivar->getSynthesize() &&
536 IC->getClassInterface() == ClassDeclared) {
Fariborz Jahanianad51e742010-07-17 00:59:30 +0000537 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
538 << PropertyId;
539 Ivar->setInvalidDecl();
540 }
541 }
Ted Kremenek28685ab2010-03-12 00:46:40 +0000542 } else {
543 if (Synthesize)
544 if (ObjCPropertyImplDecl *PPIDecl =
545 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
546 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
547 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
548 << PropertyIvar;
549 Diag(PPIDecl->getLocation(), diag::note_previous_use);
550 }
551
552 if (ObjCPropertyImplDecl *PPIDecl =
553 CatImplClass->FindPropertyImplDecl(PropertyId)) {
554 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
555 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000556 return 0;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000557 }
558 CatImplClass->addPropertyImplementation(PIDecl);
559 }
560
John McCalld226f652010-08-21 09:40:31 +0000561 return PIDecl;
Ted Kremenek28685ab2010-03-12 00:46:40 +0000562}
563
564//===----------------------------------------------------------------------===//
565// Helper methods.
566//===----------------------------------------------------------------------===//
567
Ted Kremenek9d64c152010-03-12 00:38:38 +0000568/// DiagnosePropertyMismatch - Compares two properties for their
569/// attributes and types and warns on a variety of inconsistencies.
570///
571void
572Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
573 ObjCPropertyDecl *SuperProperty,
574 const IdentifierInfo *inheritedName) {
575 ObjCPropertyDecl::PropertyAttributeKind CAttr =
576 Property->getPropertyAttributes();
577 ObjCPropertyDecl::PropertyAttributeKind SAttr =
578 SuperProperty->getPropertyAttributes();
579 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
580 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
581 Diag(Property->getLocation(), diag::warn_readonly_property)
582 << Property->getDeclName() << inheritedName;
583 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
584 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
585 Diag(Property->getLocation(), diag::warn_property_attribute)
586 << Property->getDeclName() << "copy" << inheritedName;
587 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
588 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
589 Diag(Property->getLocation(), diag::warn_property_attribute)
590 << Property->getDeclName() << "retain" << inheritedName;
591
592 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
593 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
594 Diag(Property->getLocation(), diag::warn_property_attribute)
595 << Property->getDeclName() << "atomic" << inheritedName;
596 if (Property->getSetterName() != SuperProperty->getSetterName())
597 Diag(Property->getLocation(), diag::warn_property_attribute)
598 << Property->getDeclName() << "setter" << inheritedName;
599 if (Property->getGetterName() != SuperProperty->getGetterName())
600 Diag(Property->getLocation(), diag::warn_property_attribute)
601 << Property->getDeclName() << "getter" << inheritedName;
602
603 QualType LHSType =
604 Context.getCanonicalType(SuperProperty->getType());
605 QualType RHSType =
606 Context.getCanonicalType(Property->getType());
607
608 if (!Context.typesAreCompatible(LHSType, RHSType)) {
609 // FIXME: Incorporate this test with typesAreCompatible.
610 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
611 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
612 return;
613 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
614 << Property->getType() << SuperProperty->getType() << inheritedName;
615 }
616}
617
618bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
619 ObjCMethodDecl *GetterMethod,
620 SourceLocation Loc) {
621 if (GetterMethod &&
622 GetterMethod->getResultType() != property->getType()) {
623 AssignConvertType result = Incompatible;
624 if (property->getType()->isObjCObjectPointerType())
625 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
626 property->getType());
627 if (result != Compatible) {
628 Diag(Loc, diag::warn_accessor_property_type_mismatch)
629 << property->getDeclName()
630 << GetterMethod->getSelector();
631 Diag(GetterMethod->getLocation(), diag::note_declared_at);
632 return true;
633 }
634 }
635 return false;
636}
637
638/// ComparePropertiesInBaseAndSuper - This routine compares property
639/// declarations in base and its super class, if any, and issues
640/// diagnostics in a variety of inconsistant situations.
641///
642void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
643 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
644 if (!SDecl)
645 return;
646 // FIXME: O(N^2)
647 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
648 E = SDecl->prop_end(); S != E; ++S) {
649 ObjCPropertyDecl *SuperPDecl = (*S);
650 // Does property in super class has declaration in current class?
651 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
652 E = IDecl->prop_end(); I != E; ++I) {
653 ObjCPropertyDecl *PDecl = (*I);
654 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
655 DiagnosePropertyMismatch(PDecl, SuperPDecl,
656 SDecl->getIdentifier());
657 }
658 }
659}
660
661/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
662/// of properties declared in a protocol and compares their attribute against
663/// the same property declared in the class or category.
664void
665Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
666 ObjCProtocolDecl *PDecl) {
667 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
668 if (!IDecl) {
669 // Category
670 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
671 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
672 if (!CatDecl->IsClassExtension())
673 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
674 E = PDecl->prop_end(); P != E; ++P) {
675 ObjCPropertyDecl *Pr = (*P);
676 ObjCCategoryDecl::prop_iterator CP, CE;
677 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000678 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000679 if ((*CP)->getIdentifier() == Pr->getIdentifier())
680 break;
681 if (CP != CE)
682 // Property protocol already exist in class. Diagnose any mismatch.
683 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
684 }
685 return;
686 }
687 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
688 E = PDecl->prop_end(); P != E; ++P) {
689 ObjCPropertyDecl *Pr = (*P);
690 ObjCInterfaceDecl::prop_iterator CP, CE;
691 // Is this property already in class's list of properties?
692 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
693 if ((*CP)->getIdentifier() == Pr->getIdentifier())
694 break;
695 if (CP != CE)
696 // Property protocol already exist in class. Diagnose any mismatch.
697 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
698 }
699}
700
701/// CompareProperties - This routine compares properties
702/// declared in 'ClassOrProtocol' objects (which can be a class or an
703/// inherited protocol with the list of properties for class/category 'CDecl'
704///
John McCalld226f652010-08-21 09:40:31 +0000705void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) {
706 Decl *ClassDecl = ClassOrProtocol;
Ted Kremenek9d64c152010-03-12 00:38:38 +0000707 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
708
709 if (!IDecl) {
710 // Category
711 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
712 assert (CatDecl && "CompareProperties");
713 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
714 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
715 E = MDecl->protocol_end(); P != E; ++P)
716 // Match properties of category with those of protocol (*P)
717 MatchOneProtocolPropertiesInClass(CatDecl, *P);
718
719 // Go thru the list of protocols for this category and recursively match
720 // their properties with those in the category.
721 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
722 E = CatDecl->protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000723 CompareProperties(CatDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000724 } else {
725 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
726 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
727 E = MD->protocol_end(); P != E; ++P)
728 MatchOneProtocolPropertiesInClass(CatDecl, *P);
729 }
730 return;
731 }
732
733 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +0000734 for (ObjCInterfaceDecl::all_protocol_iterator
735 P = MDecl->all_referenced_protocol_begin(),
736 E = MDecl->all_referenced_protocol_end(); P != E; ++P)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000737 // Match properties of class IDecl with those of protocol (*P).
738 MatchOneProtocolPropertiesInClass(IDecl, *P);
739
740 // Go thru the list of protocols for this class and recursively match
741 // their properties with those declared in the class.
Ted Kremenek53b94412010-09-01 01:21:15 +0000742 for (ObjCInterfaceDecl::all_protocol_iterator
743 P = IDecl->all_referenced_protocol_begin(),
744 E = IDecl->all_referenced_protocol_end(); P != E; ++P)
John McCalld226f652010-08-21 09:40:31 +0000745 CompareProperties(IDecl, *P);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000746 } else {
747 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
748 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
749 E = MD->protocol_end(); P != E; ++P)
750 MatchOneProtocolPropertiesInClass(IDecl, *P);
751 }
752}
753
754/// isPropertyReadonly - Return true if property is readonly, by searching
755/// for the property in the class and in its categories and implementations
756///
757bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
758 ObjCInterfaceDecl *IDecl) {
759 // by far the most common case.
760 if (!PDecl->isReadOnly())
761 return false;
762 // Even if property is ready only, if interface has a user defined setter,
763 // it is not considered read only.
764 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
765 return false;
766
767 // Main class has the property as 'readonly'. Must search
768 // through the category list to see if the property's
769 // attribute has been over-ridden to 'readwrite'.
770 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
771 Category; Category = Category->getNextClassCategory()) {
772 // Even if property is ready only, if a category has a user defined setter,
773 // it is not considered read only.
774 if (Category->getInstanceMethod(PDecl->getSetterName()))
775 return false;
776 ObjCPropertyDecl *P =
777 Category->FindPropertyDeclaration(PDecl->getIdentifier());
778 if (P && !P->isReadOnly())
779 return false;
780 }
781
782 // Also, check for definition of a setter method in the implementation if
783 // all else failed.
784 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
785 if (ObjCImplementationDecl *IMD =
786 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
787 if (IMD->getInstanceMethod(PDecl->getSetterName()))
788 return false;
789 } else if (ObjCCategoryImplDecl *CIMD =
790 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
791 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
792 return false;
793 }
794 }
795 // Lastly, look through the implementation (if one is in scope).
796 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
797 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
798 return false;
799 // If all fails, look at the super class.
800 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
801 return isPropertyReadonly(PDecl, SIDecl);
802 return true;
803}
804
805/// CollectImmediateProperties - This routine collects all properties in
806/// the class and its conforming protocols; but not those it its super class.
807void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000808 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
809 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000810 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
811 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
812 E = IDecl->prop_end(); P != E; ++P) {
813 ObjCPropertyDecl *Prop = (*P);
814 PropMap[Prop->getIdentifier()] = Prop;
815 }
816 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000817 for (ObjCInterfaceDecl::all_protocol_iterator
818 PI = IDecl->all_referenced_protocol_begin(),
819 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000820 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000821 }
822 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
823 if (!CATDecl->IsClassExtension())
824 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
825 E = CATDecl->prop_end(); P != E; ++P) {
826 ObjCPropertyDecl *Prop = (*P);
827 PropMap[Prop->getIdentifier()] = Prop;
828 }
829 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000830 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(),
Ted Kremenek9d64c152010-03-12 00:38:38 +0000831 E = CATDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000832 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000833 }
834 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
835 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
836 E = PDecl->prop_end(); P != E; ++P) {
837 ObjCPropertyDecl *Prop = (*P);
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000838 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
839 // Exclude property for protocols which conform to class's super-class,
840 // as super-class has to implement the property.
841 if (!PropertyFromSuper || PropertyFromSuper != Prop) {
842 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
843 if (!PropEntry)
844 PropEntry = Prop;
845 }
Ted Kremenek9d64c152010-03-12 00:38:38 +0000846 }
847 // scan through protocol's protocols.
848 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
849 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000850 CollectImmediateProperties((*PI), PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000851 }
852}
853
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000854/// CollectClassPropertyImplementations - This routine collects list of
855/// properties to be implemented in the class. This includes, class's
856/// and its conforming protocols' properties.
857static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
858 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
859 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
860 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
861 E = IDecl->prop_end(); P != E; ++P) {
862 ObjCPropertyDecl *Prop = (*P);
863 PropMap[Prop->getIdentifier()] = Prop;
864 }
Ted Kremenek53b94412010-09-01 01:21:15 +0000865 for (ObjCInterfaceDecl::all_protocol_iterator
866 PI = IDecl->all_referenced_protocol_begin(),
867 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI)
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000868 CollectClassPropertyImplementations((*PI), PropMap);
869 }
870 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
871 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
872 E = PDecl->prop_end(); P != E; ++P) {
873 ObjCPropertyDecl *Prop = (*P);
874 PropMap[Prop->getIdentifier()] = Prop;
875 }
876 // scan through protocol's protocols.
877 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
878 E = PDecl->protocol_end(); PI != E; ++PI)
879 CollectClassPropertyImplementations((*PI), PropMap);
880 }
881}
882
883/// CollectSuperClassPropertyImplementations - This routine collects list of
884/// properties to be implemented in super class(s) and also coming from their
885/// conforming protocols.
886static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
887 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
888 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
889 while (SDecl) {
890 CollectClassPropertyImplementations(SDecl, PropMap);
891 SDecl = SDecl->getSuperClass();
892 }
893 }
894}
895
Ted Kremenek9d64c152010-03-12 00:38:38 +0000896/// LookupPropertyDecl - Looks up a property in the current class and all
897/// its protocols.
898ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
899 IdentifierInfo *II) {
900 if (const ObjCInterfaceDecl *IDecl =
901 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
902 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
903 E = IDecl->prop_end(); P != E; ++P) {
904 ObjCPropertyDecl *Prop = (*P);
905 if (Prop->getIdentifier() == II)
906 return Prop;
907 }
908 // scan through class's protocols.
Ted Kremenek53b94412010-09-01 01:21:15 +0000909 for (ObjCInterfaceDecl::all_protocol_iterator
910 PI = IDecl->all_referenced_protocol_begin(),
911 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) {
Ted Kremenek9d64c152010-03-12 00:38:38 +0000912 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
913 if (Prop)
914 return Prop;
915 }
916 }
917 else if (const ObjCProtocolDecl *PDecl =
918 dyn_cast<ObjCProtocolDecl>(CDecl)) {
919 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
920 E = PDecl->prop_end(); P != E; ++P) {
921 ObjCPropertyDecl *Prop = (*P);
922 if (Prop->getIdentifier() == II)
923 return Prop;
924 }
925 // scan through protocol's protocols.
926 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
927 E = PDecl->protocol_end(); PI != E; ++PI) {
928 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
929 if (Prop)
930 return Prop;
931 }
932 }
933 return 0;
934}
935
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000936/// DefaultSynthesizeProperties - This routine default synthesizes all
937/// properties which must be synthesized in class's @implementation.
938void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
939 ObjCInterfaceDecl *IDecl) {
940
941 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
942 CollectClassPropertyImplementations(IDecl, PropMap);
943 if (PropMap.empty())
944 return;
945 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
946 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
947
948 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
949 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
950 ObjCPropertyDecl *Prop = P->second;
951 // If property to be implemented in the super class, ignore.
952 if (SuperPropMap[Prop->getIdentifier()])
953 continue;
954 // Is there a matching propery synthesize/dynamic?
955 if (Prop->isInvalidDecl() ||
956 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
957 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
958 continue;
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000959 // Property may have been synthesized by user.
960 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
961 continue;
Fariborz Jahanian95f1b862010-08-25 00:31:58 +0000962 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
963 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
964 continue;
965 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
966 continue;
967 }
Fariborz Jahaniand3635b92010-07-14 18:11:52 +0000968
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000969 ActOnPropertyImplDecl(S, IMPDecl->getLocation(), IMPDecl->getLocation(),
John McCalld226f652010-08-21 09:40:31 +0000970 true, IMPDecl,
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000971 Prop->getIdentifier(), Prop->getIdentifier());
972 }
973}
Ted Kremenek9d64c152010-03-12 00:38:38 +0000974
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000975void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +0000976 ObjCContainerDecl *CDecl,
977 const llvm::DenseSet<Selector>& InsMap) {
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000978 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
979 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
980 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
981
Ted Kremenek9d64c152010-03-12 00:38:38 +0000982 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
Fariborz Jahaniancfa6a272010-06-29 18:12:32 +0000983 CollectImmediateProperties(CDecl, PropMap, SuperPropMap);
Ted Kremenek9d64c152010-03-12 00:38:38 +0000984 if (PropMap.empty())
985 return;
986
987 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
988 for (ObjCImplDecl::propimpl_iterator
989 I = IMPDecl->propimpl_begin(),
990 EI = IMPDecl->propimpl_end(); I != EI; ++I)
991 PropImplMap.insert((*I)->getPropertyDecl());
992
993 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
994 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
995 ObjCPropertyDecl *Prop = P->second;
996 // Is there a matching propery synthesize/dynamic?
997 if (Prop->isInvalidDecl() ||
998 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
999 PropImplMap.count(Prop))
1000 continue;
Ted Kremenek9d64c152010-03-12 00:38:38 +00001001 if (!InsMap.count(Prop->getGetterName())) {
1002 Diag(Prop->getLocation(),
1003 isa<ObjCCategoryDecl>(CDecl) ?
1004 diag::warn_setter_getter_impl_required_in_category :
1005 diag::warn_setter_getter_impl_required)
1006 << Prop->getDeclName() << Prop->getGetterName();
1007 Diag(IMPDecl->getLocation(),
1008 diag::note_property_impl_required);
1009 }
1010
1011 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
1012 Diag(Prop->getLocation(),
1013 isa<ObjCCategoryDecl>(CDecl) ?
1014 diag::warn_setter_getter_impl_required_in_category :
1015 diag::warn_setter_getter_impl_required)
1016 << Prop->getDeclName() << Prop->getSetterName();
1017 Diag(IMPDecl->getLocation(),
1018 diag::note_property_impl_required);
1019 }
1020 }
1021}
1022
1023void
1024Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1025 ObjCContainerDecl* IDecl) {
1026 // Rules apply in non-GC mode only
1027 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1028 return;
1029 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1030 E = IDecl->prop_end();
1031 I != E; ++I) {
1032 ObjCPropertyDecl *Property = (*I);
1033 unsigned Attributes = Property->getPropertyAttributes();
1034 // We only care about readwrite atomic property.
1035 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1036 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1037 continue;
1038 if (const ObjCPropertyImplDecl *PIDecl
1039 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1040 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1041 continue;
1042 ObjCMethodDecl *GetterMethod =
1043 IMPDecl->getInstanceMethod(Property->getGetterName());
1044 ObjCMethodDecl *SetterMethod =
1045 IMPDecl->getInstanceMethod(Property->getSetterName());
1046 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1047 SourceLocation MethodLoc =
1048 (GetterMethod ? GetterMethod->getLocation()
1049 : SetterMethod->getLocation());
1050 Diag(MethodLoc, diag::warn_atomic_property_rule)
1051 << Property->getIdentifier();
1052 Diag(Property->getLocation(), diag::note_property_declare);
1053 }
1054 }
1055 }
1056}
1057
1058/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1059/// have the property type and issue diagnostics if they don't.
1060/// Also synthesize a getter/setter method if none exist (and update the
1061/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1062/// methods is the "right" thing to do.
1063void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1064 ObjCContainerDecl *CD) {
1065 ObjCMethodDecl *GetterMethod, *SetterMethod;
1066
1067 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1068 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1069 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1070 property->getLocation());
1071
1072 if (SetterMethod) {
1073 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1074 property->getPropertyAttributes();
1075 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1076 Context.getCanonicalType(SetterMethod->getResultType()) !=
1077 Context.VoidTy)
1078 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1079 if (SetterMethod->param_size() != 1 ||
1080 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1081 Diag(property->getLocation(),
1082 diag::warn_accessor_property_type_mismatch)
1083 << property->getDeclName()
1084 << SetterMethod->getSelector();
1085 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1086 }
1087 }
1088
1089 // Synthesize getter/setter methods if none exist.
1090 // Find the default getter and if one not found, add one.
1091 // FIXME: The synthesized property we set here is misleading. We almost always
1092 // synthesize these methods unless the user explicitly provided prototypes
1093 // (which is odd, but allowed). Sema should be typechecking that the
1094 // declarations jive in that situation (which it is not currently).
1095 if (!GetterMethod) {
1096 // No instance method of same name as property getter name was found.
1097 // Declare a getter method and add it to the list of methods
1098 // for this class.
1099 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1100 property->getLocation(), property->getGetterName(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001101 property->getType(), 0, CD, true, false, true,
1102 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001103 (property->getPropertyImplementation() ==
1104 ObjCPropertyDecl::Optional) ?
1105 ObjCMethodDecl::Optional :
1106 ObjCMethodDecl::Required);
1107 CD->addDecl(GetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001108 // FIXME: Eventually this shouldn't be needed, as the lexical context
1109 // and the real context should be the same.
1110 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1111 GetterMethod->setLexicalDeclContext(lexicalDC);
1112
Ted Kremenek9d64c152010-03-12 00:38:38 +00001113 } else
1114 // A user declared getter will be synthesize when @synthesize of
1115 // the property with the same name is seen in the @implementation
1116 GetterMethod->setSynthesized(true);
1117 property->setGetterMethodDecl(GetterMethod);
1118
1119 // Skip setter if property is read-only.
1120 if (!property->isReadOnly()) {
1121 // Find the default setter and if one not found, add one.
1122 if (!SetterMethod) {
1123 // No instance method of same name as property setter name was found.
1124 // Declare a setter method and add it to the list of methods
1125 // for this class.
1126 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1127 property->getLocation(),
1128 property->getSetterName(),
1129 Context.VoidTy, 0, CD, true, false, true,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001130 false,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001131 (property->getPropertyImplementation() ==
1132 ObjCPropertyDecl::Optional) ?
1133 ObjCMethodDecl::Optional :
1134 ObjCMethodDecl::Required);
1135 // Invent the arguments for the setter. We don't bother making a
1136 // nice name for the argument.
1137 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1138 property->getLocation(),
1139 property->getIdentifier(),
1140 property->getType(),
1141 /*TInfo=*/0,
John McCalld931b082010-08-26 03:08:43 +00001142 SC_None,
1143 SC_None,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001144 0);
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001145 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001146 CD->addDecl(SetterMethod);
Ted Kremenek23173d72010-05-18 21:09:07 +00001147 // FIXME: Eventually this shouldn't be needed, as the lexical context
1148 // and the real context should be the same.
1149 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1150 SetterMethod->setLexicalDeclContext(lexicalDC);
Ted Kremenek9d64c152010-03-12 00:38:38 +00001151 } else
1152 // A user declared setter will be synthesize when @synthesize of
1153 // the property with the same name is seen in the @implementation
1154 SetterMethod->setSynthesized(true);
1155 property->setSetterMethodDecl(SetterMethod);
1156 }
1157 // Add any synthesized methods to the global pool. This allows us to
1158 // handle the following, which is supported by GCC (and part of the design).
1159 //
1160 // @interface Foo
1161 // @property double bar;
1162 // @end
1163 //
1164 // void thisIsUnfortunate() {
1165 // id foo;
1166 // double bar = [foo bar];
1167 // }
1168 //
1169 if (GetterMethod)
1170 AddInstanceMethodToGlobalPool(GetterMethod);
1171 if (SetterMethod)
1172 AddInstanceMethodToGlobalPool(SetterMethod);
1173}
1174
John McCalld226f652010-08-21 09:40:31 +00001175void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
Ted Kremenek9d64c152010-03-12 00:38:38 +00001176 SourceLocation Loc,
1177 unsigned &Attributes) {
1178 // FIXME: Improve the reported location.
Ted Kremenek5fcd52a2010-04-05 22:39:42 +00001179 if (!PDecl)
1180 return;
1181
1182 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001183 QualType PropertyTy = PropertyDecl->getType();
Ted Kremenek9d64c152010-03-12 00:38:38 +00001184
1185 // readonly and readwrite/assign/retain/copy conflict.
1186 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1187 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1188 ObjCDeclSpec::DQ_PR_assign |
1189 ObjCDeclSpec::DQ_PR_copy |
1190 ObjCDeclSpec::DQ_PR_retain))) {
1191 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1192 "readwrite" :
1193 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1194 "assign" :
1195 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1196 "copy" : "retain";
1197
1198 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1199 diag::err_objc_property_attr_mutually_exclusive :
1200 diag::warn_objc_property_attr_mutually_exclusive)
1201 << "readonly" << which;
1202 }
1203
1204 // Check for copy or retain on non-object types.
1205 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1206 !PropertyTy->isObjCObjectPointerType() &&
1207 !PropertyTy->isBlockPointerType() &&
Fariborz Jahanian842f07b2010-03-30 22:40:11 +00001208 !Context.isObjCNSObjectType(PropertyTy) &&
1209 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
Ted Kremenek9d64c152010-03-12 00:38:38 +00001210 Diag(Loc, diag::err_objc_property_requires_object)
1211 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1212 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1213 }
1214
1215 // Check for more than one of { assign, copy, retain }.
1216 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1217 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1218 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1219 << "assign" << "copy";
1220 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1221 }
1222 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1223 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1224 << "assign" << "retain";
1225 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1226 }
1227 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1228 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1229 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1230 << "copy" << "retain";
1231 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1232 }
1233 }
1234
1235 // Warn if user supplied no assignment attribute, property is
1236 // readwrite, and this is an object type.
1237 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1238 ObjCDeclSpec::DQ_PR_retain)) &&
1239 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1240 PropertyTy->isObjCObjectPointerType()) {
1241 // Skip this warning in gc-only mode.
1242 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1243 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1244
1245 // If non-gc code warn that this is likely inappropriate.
1246 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1247 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1248
1249 // FIXME: Implement warning dependent on NSCopying being
1250 // implemented. See also:
1251 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1252 // (please trim this list while you are at it).
1253 }
1254
1255 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1256 && getLangOptions().getGCMode() == LangOptions::GCOnly
1257 && PropertyTy->isBlockPointerType())
1258 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1259}