blob: 3884e01fe3a91241ea3d8913d20d519c7136abd8 [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
15#include "Sema.h"
16
17using namespace clang;
18
Ted Kremenek28685ab2010-03-12 00:46:40 +000019//===----------------------------------------------------------------------===//
20// Grammar actions.
21//===----------------------------------------------------------------------===//
22
23Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
24 FieldDeclarator &FD,
25 ObjCDeclSpec &ODS,
26 Selector GetterSel,
27 Selector SetterSel,
28 DeclPtrTy ClassCategory,
29 bool *isOverridingProperty,
30 tok::ObjCKeywordKind MethodImplKind) {
31 unsigned Attributes = ODS.getPropertyAttributes();
32 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
33 // default is readwrite!
34 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
35 // property is defaulted to 'assign' if it is readwrite and is
36 // not retain or copy
37 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
38 (isReadWrite &&
39 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
40 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
41 QualType T = GetTypeForDeclarator(FD.D, S);
42 if (T->isReferenceType()) {
43 Diag(AtLoc, diag::error_reference_property);
44 return DeclPtrTy();
45 }
46 Decl *ClassDecl = ClassCategory.getAs<Decl>();
47 ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class
48 // May modify Attributes.
49 CheckObjCPropertyAttributes(T, AtLoc, Attributes);
50 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
51 if (CDecl->IsClassExtension()) {
52 // Diagnose if this property is already in continuation class.
53 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
54 assert(DC && "ClassDecl is not a DeclContext");
55 DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier());
56 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
57 Diag(AtLoc, diag::err_duplicate_property);
58 Diag((*Found.first)->getLocation(), diag::note_property_declare);
59 return DeclPtrTy();
60 }
Ted Kremenek2d2f9362010-03-12 00:49:00 +000061 ObjCPropertyDecl *PDecl =
62 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
63 FD.D.getIdentifier(), AtLoc, T);
Ted Kremenek28685ab2010-03-12 00:46:40 +000064 DC->addDecl(PDecl);
65
66 // This is a continuation class. property requires special
67 // handling.
68 if ((CCPrimary = CDecl->getClassInterface())) {
69 // Find the property in continuation class's primary class only.
70 IdentifierInfo *PropertyId = FD.D.getIdentifier();
71 if (ObjCPropertyDecl *PIDecl =
72 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
73 // property 'PIDecl's readonly attribute will be over-ridden
74 // with continuation class's readwrite property attribute!
75 unsigned PIkind = PIDecl->getPropertyAttributes();
76 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
77 unsigned retainCopyNonatomic =
78 (ObjCPropertyDecl::OBJC_PR_retain |
79 ObjCPropertyDecl::OBJC_PR_copy |
80 ObjCPropertyDecl::OBJC_PR_nonatomic);
81 if ((Attributes & retainCopyNonatomic) !=
82 (PIkind & retainCopyNonatomic)) {
83 Diag(AtLoc, diag::warn_property_attr_mismatch);
84 Diag(PIDecl->getLocation(), diag::note_property_declare);
85 }
86 DeclContext *DC = dyn_cast<DeclContext>(CCPrimary);
87 assert(DC && "ClassDecl is not a DeclContext");
88 DeclContext::lookup_result Found =
89 DC->lookup(PIDecl->getDeclName());
90 bool PropertyInPrimaryClass = false;
91 for (; Found.first != Found.second; ++Found.first)
92 if (isa<ObjCPropertyDecl>(*Found.first)) {
93 PropertyInPrimaryClass = true;
94 break;
95 }
96 if (!PropertyInPrimaryClass) {
97 // Protocol is not in the primary class. Must build one for it.
98 ObjCDeclSpec ProtocolPropertyODS;
Ted Kremenek2d2f9362010-03-12 00:49:00 +000099 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
100 // and ObjCPropertyDecl::PropertyAttributeKind have identical
101 // values. Should consolidate both into one enum type.
102 ProtocolPropertyODS.
103 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
104 PIkind);
105
Ted Kremenek28685ab2010-03-12 00:46:40 +0000106 DeclPtrTy ProtocolPtrTy =
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000107 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
108 PIDecl->getGetterName(),
109 PIDecl->getSetterName(),
110 DeclPtrTy::make(CCPrimary), isOverridingProperty,
111 MethodImplKind);
Ted Kremenek28685ab2010-03-12 00:46:40 +0000112 PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>();
113 }
114 PIDecl->makeitReadWriteAttribute();
115 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
116 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
117 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
118 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
119 PIDecl->setSetterName(SetterSel);
120 } else {
121 Diag(AtLoc, diag::err_use_continuation_class)
122 << CCPrimary->getDeclName();
123 Diag(PIDecl->getLocation(), diag::note_property_declare);
124 }
125 *isOverridingProperty = true;
126 // Make sure setter decl is synthesized, and added to primary
127 // class's list.
128 ProcessPropertyDecl(PIDecl, CCPrimary);
129 return DeclPtrTy();
130 }
131
132 // No matching property found in the primary class. Just fall thru
133 // and add property to continuation class's primary class.
134 ClassDecl = CCPrimary;
135 } else {
136 Diag(CDecl->getLocation(), diag::err_continuation_class);
137 *isOverridingProperty = true;
138 return DeclPtrTy();
139 }
140 }
141
142 // Issue a warning if property is 'assign' as default and its object, which is
143 // gc'able conforms to NSCopying protocol
144 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
145 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
146 if (T->isObjCObjectPointerType()) {
147 QualType InterfaceTy = T->getPointeeType();
148 if (const ObjCInterfaceType *OIT =
149 InterfaceTy->getAs<ObjCInterfaceType>()) {
150 ObjCInterfaceDecl *IDecl = OIT->getDecl();
151 if (IDecl)
152 if (ObjCProtocolDecl* PNSCopying =
153 LookupProtocol(&Context.Idents.get("NSCopying")))
154 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
155 Diag(AtLoc, diag::warn_implements_nscopying)
156 << FD.D.getIdentifier();
157 }
158 }
159 if (T->isObjCInterfaceType())
160 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
161
162 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
163 assert(DC && "ClassDecl is not a DeclContext");
164 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
165 FD.D.getIdentifierLoc(),
166 FD.D.getIdentifier(),
167 AtLoc, T);
168 DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName());
169 if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) {
170 Diag(PDecl->getLocation(), diag::err_duplicate_property);
171 Diag((*Found.first)->getLocation(), diag::note_property_declare);
172 PDecl->setInvalidDecl();
173 }
174 else
175 DC->addDecl(PDecl);
176
177 if (T->isArrayType() || T->isFunctionType()) {
178 Diag(AtLoc, diag::err_property_type) << T;
179 PDecl->setInvalidDecl();
180 }
181
182 ProcessDeclAttributes(S, PDecl, FD.D);
183
184 // Regardless of setter/getter attribute, we save the default getter/setter
185 // selector names in anticipation of declaration of setter/getter methods.
186 PDecl->setGetterName(GetterSel);
187 PDecl->setSetterName(SetterSel);
188
189 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
190 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
191
192 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
193 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
194
195 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
196 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
197
198 if (isReadWrite)
199 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
200
201 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
202 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
203
204 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
205 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
206
207 if (isAssign)
208 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
209
210 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
211 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
212
213 if (MethodImplKind == tok::objc_required)
214 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
215 else if (MethodImplKind == tok::objc_optional)
216 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
217 // A case of continuation class adding a new property in the class. This
218 // is not what it was meant for. However, gcc supports it and so should we.
219 // Make sure setter/getters are declared here.
220 if (CCPrimary)
221 ProcessPropertyDecl(PDecl, CCPrimary);
222
223 return DeclPtrTy::make(PDecl);
224}
225
226
227/// ActOnPropertyImplDecl - This routine performs semantic checks and
228/// builds the AST node for a property implementation declaration; declared
229/// as @synthesize or @dynamic.
230///
231Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc,
232 SourceLocation PropertyLoc,
233 bool Synthesize,
234 DeclPtrTy ClassCatImpDecl,
235 IdentifierInfo *PropertyId,
236 IdentifierInfo *PropertyIvar) {
237 Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>();
238 // Make sure we have a context for the property implementation declaration.
239 if (!ClassImpDecl) {
240 Diag(AtLoc, diag::error_missing_property_context);
241 return DeclPtrTy();
242 }
243 ObjCPropertyDecl *property = 0;
244 ObjCInterfaceDecl* IDecl = 0;
245 // Find the class or category class where this property must have
246 // a declaration.
247 ObjCImplementationDecl *IC = 0;
248 ObjCCategoryImplDecl* CatImplClass = 0;
249 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
250 IDecl = IC->getClassInterface();
251 // We always synthesize an interface for an implementation
252 // without an interface decl. So, IDecl is always non-zero.
253 assert(IDecl &&
254 "ActOnPropertyImplDecl - @implementation without @interface");
255
256 // Look for this property declaration in the @implementation's @interface
257 property = IDecl->FindPropertyDeclaration(PropertyId);
258 if (!property) {
259 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
260 return DeclPtrTy();
261 }
262 if (const ObjCCategoryDecl *CD =
263 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
264 if (!CD->IsClassExtension()) {
265 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
266 Diag(property->getLocation(), diag::note_property_declare);
267 return DeclPtrTy();
268 }
269 }
270 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
271 if (Synthesize) {
272 Diag(AtLoc, diag::error_synthesize_category_decl);
273 return DeclPtrTy();
274 }
275 IDecl = CatImplClass->getClassInterface();
276 if (!IDecl) {
277 Diag(AtLoc, diag::error_missing_property_interface);
278 return DeclPtrTy();
279 }
280 ObjCCategoryDecl *Category =
281 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
282
283 // If category for this implementation not found, it is an error which
284 // has already been reported eralier.
285 if (!Category)
286 return DeclPtrTy();
287 // Look for this property declaration in @implementation's category
288 property = Category->FindPropertyDeclaration(PropertyId);
289 if (!property) {
290 Diag(PropertyLoc, diag::error_bad_category_property_decl)
291 << Category->getDeclName();
292 return DeclPtrTy();
293 }
294 } else {
295 Diag(AtLoc, diag::error_bad_property_context);
296 return DeclPtrTy();
297 }
298 ObjCIvarDecl *Ivar = 0;
299 // Check that we have a valid, previously declared ivar for @synthesize
300 if (Synthesize) {
301 // @synthesize
302 if (!PropertyIvar)
303 PropertyIvar = PropertyId;
304 QualType PropType = Context.getCanonicalType(property->getType());
305 // Check that this is a previously declared 'ivar' in 'IDecl' interface
306 ObjCInterfaceDecl *ClassDeclared;
307 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
308 if (!Ivar) {
309 DeclContext *EnclosingContext = cast_or_null<DeclContext>(ClassImpDecl);
310 assert(EnclosingContext &&
311 "null DeclContext for synthesized ivar - ActOnPropertyImplDecl");
312 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc,
313 PropertyIvar, PropType, /*Dinfo=*/0,
314 ObjCIvarDecl::Public,
315 (Expr *)0);
316 EnclosingContext->addDecl(Ivar);
317 IDecl->makeDeclVisibleInContext(Ivar, false);
318 property->setPropertyIvarDecl(Ivar);
319
320 if (!getLangOptions().ObjCNonFragileABI)
321 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
322 // Note! I deliberately want it to fall thru so, we have a
323 // a property implementation and to avoid future warnings.
324 } else if (getLangOptions().ObjCNonFragileABI &&
325 ClassDeclared != IDecl) {
326 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
327 << property->getDeclName() << Ivar->getDeclName()
328 << ClassDeclared->getDeclName();
329 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
330 << Ivar << Ivar->getNameAsCString();
331 // Note! I deliberately want it to fall thru so more errors are caught.
332 }
333 QualType IvarType = Context.getCanonicalType(Ivar->getType());
334
335 // Check that type of property and its ivar are type compatible.
336 if (PropType != IvarType) {
337 if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) {
338 Diag(PropertyLoc, diag::error_property_ivar_type)
339 << property->getDeclName() << Ivar->getDeclName();
340 // Note! I deliberately want it to fall thru so, we have a
341 // a property implementation and to avoid future warnings.
342 }
343
344 // FIXME! Rules for properties are somewhat different that those
345 // for assignments. Use a new routine to consolidate all cases;
346 // specifically for property redeclarations as well as for ivars.
347 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
348 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
349 if (lhsType != rhsType &&
350 lhsType->isArithmeticType()) {
351 Diag(PropertyLoc, diag::error_property_ivar_type)
352 << property->getDeclName() << Ivar->getDeclName();
353 // Fall thru - see previous comment
354 }
355 // __weak is explicit. So it works on Canonical type.
356 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
357 getLangOptions().getGCMode() != LangOptions::NonGC) {
358 Diag(PropertyLoc, diag::error_weak_property)
359 << property->getDeclName() << Ivar->getDeclName();
360 // Fall thru - see previous comment
361 }
362 if ((property->getType()->isObjCObjectPointerType() ||
363 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
364 getLangOptions().getGCMode() != LangOptions::NonGC) {
365 Diag(PropertyLoc, diag::error_strong_property)
366 << property->getDeclName() << Ivar->getDeclName();
367 // Fall thru - see previous comment
368 }
369 }
370 } else if (PropertyIvar)
371 // @dynamic
372 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
373 assert (property && "ActOnPropertyImplDecl - property declaration missing");
374 ObjCPropertyImplDecl *PIDecl =
375 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
376 property,
377 (Synthesize ?
378 ObjCPropertyImplDecl::Synthesize
379 : ObjCPropertyImplDecl::Dynamic),
380 Ivar);
381 if (IC) {
382 if (Synthesize)
383 if (ObjCPropertyImplDecl *PPIDecl =
384 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
385 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
386 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
387 << PropertyIvar;
388 Diag(PPIDecl->getLocation(), diag::note_previous_use);
389 }
390
391 if (ObjCPropertyImplDecl *PPIDecl
392 = IC->FindPropertyImplDecl(PropertyId)) {
393 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
394 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
395 return DeclPtrTy();
396 }
397 IC->addPropertyImplementation(PIDecl);
398 } else {
399 if (Synthesize)
400 if (ObjCPropertyImplDecl *PPIDecl =
401 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
402 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
403 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
404 << PropertyIvar;
405 Diag(PPIDecl->getLocation(), diag::note_previous_use);
406 }
407
408 if (ObjCPropertyImplDecl *PPIDecl =
409 CatImplClass->FindPropertyImplDecl(PropertyId)) {
410 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
411 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
412 return DeclPtrTy();
413 }
414 CatImplClass->addPropertyImplementation(PIDecl);
415 }
416
417 return DeclPtrTy::make(PIDecl);
418}
419
420//===----------------------------------------------------------------------===//
421// Helper methods.
422//===----------------------------------------------------------------------===//
423
Ted Kremenek9d64c152010-03-12 00:38:38 +0000424/// DiagnosePropertyMismatch - Compares two properties for their
425/// attributes and types and warns on a variety of inconsistencies.
426///
427void
428Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
429 ObjCPropertyDecl *SuperProperty,
430 const IdentifierInfo *inheritedName) {
431 ObjCPropertyDecl::PropertyAttributeKind CAttr =
432 Property->getPropertyAttributes();
433 ObjCPropertyDecl::PropertyAttributeKind SAttr =
434 SuperProperty->getPropertyAttributes();
435 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
436 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
437 Diag(Property->getLocation(), diag::warn_readonly_property)
438 << Property->getDeclName() << inheritedName;
439 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
440 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
441 Diag(Property->getLocation(), diag::warn_property_attribute)
442 << Property->getDeclName() << "copy" << inheritedName;
443 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
444 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
445 Diag(Property->getLocation(), diag::warn_property_attribute)
446 << Property->getDeclName() << "retain" << inheritedName;
447
448 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
449 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
450 Diag(Property->getLocation(), diag::warn_property_attribute)
451 << Property->getDeclName() << "atomic" << inheritedName;
452 if (Property->getSetterName() != SuperProperty->getSetterName())
453 Diag(Property->getLocation(), diag::warn_property_attribute)
454 << Property->getDeclName() << "setter" << inheritedName;
455 if (Property->getGetterName() != SuperProperty->getGetterName())
456 Diag(Property->getLocation(), diag::warn_property_attribute)
457 << Property->getDeclName() << "getter" << inheritedName;
458
459 QualType LHSType =
460 Context.getCanonicalType(SuperProperty->getType());
461 QualType RHSType =
462 Context.getCanonicalType(Property->getType());
463
464 if (!Context.typesAreCompatible(LHSType, RHSType)) {
465 // FIXME: Incorporate this test with typesAreCompatible.
466 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
467 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
468 return;
469 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
470 << Property->getType() << SuperProperty->getType() << inheritedName;
471 }
472}
473
474bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
475 ObjCMethodDecl *GetterMethod,
476 SourceLocation Loc) {
477 if (GetterMethod &&
478 GetterMethod->getResultType() != property->getType()) {
479 AssignConvertType result = Incompatible;
480 if (property->getType()->isObjCObjectPointerType())
481 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
482 property->getType());
483 if (result != Compatible) {
484 Diag(Loc, diag::warn_accessor_property_type_mismatch)
485 << property->getDeclName()
486 << GetterMethod->getSelector();
487 Diag(GetterMethod->getLocation(), diag::note_declared_at);
488 return true;
489 }
490 }
491 return false;
492}
493
494/// ComparePropertiesInBaseAndSuper - This routine compares property
495/// declarations in base and its super class, if any, and issues
496/// diagnostics in a variety of inconsistant situations.
497///
498void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
499 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
500 if (!SDecl)
501 return;
502 // FIXME: O(N^2)
503 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
504 E = SDecl->prop_end(); S != E; ++S) {
505 ObjCPropertyDecl *SuperPDecl = (*S);
506 // Does property in super class has declaration in current class?
507 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
508 E = IDecl->prop_end(); I != E; ++I) {
509 ObjCPropertyDecl *PDecl = (*I);
510 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
511 DiagnosePropertyMismatch(PDecl, SuperPDecl,
512 SDecl->getIdentifier());
513 }
514 }
515}
516
517/// MatchOneProtocolPropertiesInClass - This routine goes thru the list
518/// of properties declared in a protocol and compares their attribute against
519/// the same property declared in the class or category.
520void
521Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
522 ObjCProtocolDecl *PDecl) {
523 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
524 if (!IDecl) {
525 // Category
526 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
527 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
528 if (!CatDecl->IsClassExtension())
529 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
530 E = PDecl->prop_end(); P != E; ++P) {
531 ObjCPropertyDecl *Pr = (*P);
532 ObjCCategoryDecl::prop_iterator CP, CE;
533 // Is this property already in category's list of properties?
Ted Kremenek2d2f9362010-03-12 00:49:00 +0000534 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
Ted Kremenek9d64c152010-03-12 00:38:38 +0000535 if ((*CP)->getIdentifier() == Pr->getIdentifier())
536 break;
537 if (CP != CE)
538 // Property protocol already exist in class. Diagnose any mismatch.
539 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
540 }
541 return;
542 }
543 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
544 E = PDecl->prop_end(); P != E; ++P) {
545 ObjCPropertyDecl *Pr = (*P);
546 ObjCInterfaceDecl::prop_iterator CP, CE;
547 // Is this property already in class's list of properties?
548 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
549 if ((*CP)->getIdentifier() == Pr->getIdentifier())
550 break;
551 if (CP != CE)
552 // Property protocol already exist in class. Diagnose any mismatch.
553 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
554 }
555}
556
557/// CompareProperties - This routine compares properties
558/// declared in 'ClassOrProtocol' objects (which can be a class or an
559/// inherited protocol with the list of properties for class/category 'CDecl'
560///
561void Sema::CompareProperties(Decl *CDecl,
562 DeclPtrTy ClassOrProtocol) {
563 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
564 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
565
566 if (!IDecl) {
567 // Category
568 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
569 assert (CatDecl && "CompareProperties");
570 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
571 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
572 E = MDecl->protocol_end(); P != E; ++P)
573 // Match properties of category with those of protocol (*P)
574 MatchOneProtocolPropertiesInClass(CatDecl, *P);
575
576 // Go thru the list of protocols for this category and recursively match
577 // their properties with those in the category.
578 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
579 E = CatDecl->protocol_end(); P != E; ++P)
580 CompareProperties(CatDecl, DeclPtrTy::make(*P));
581 } else {
582 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
583 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
584 E = MD->protocol_end(); P != E; ++P)
585 MatchOneProtocolPropertiesInClass(CatDecl, *P);
586 }
587 return;
588 }
589
590 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
591 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
592 E = MDecl->protocol_end(); P != E; ++P)
593 // Match properties of class IDecl with those of protocol (*P).
594 MatchOneProtocolPropertiesInClass(IDecl, *P);
595
596 // Go thru the list of protocols for this class and recursively match
597 // their properties with those declared in the class.
598 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
599 E = IDecl->protocol_end(); P != E; ++P)
600 CompareProperties(IDecl, DeclPtrTy::make(*P));
601 } else {
602 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
603 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
604 E = MD->protocol_end(); P != E; ++P)
605 MatchOneProtocolPropertiesInClass(IDecl, *P);
606 }
607}
608
609/// isPropertyReadonly - Return true if property is readonly, by searching
610/// for the property in the class and in its categories and implementations
611///
612bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
613 ObjCInterfaceDecl *IDecl) {
614 // by far the most common case.
615 if (!PDecl->isReadOnly())
616 return false;
617 // Even if property is ready only, if interface has a user defined setter,
618 // it is not considered read only.
619 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
620 return false;
621
622 // Main class has the property as 'readonly'. Must search
623 // through the category list to see if the property's
624 // attribute has been over-ridden to 'readwrite'.
625 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
626 Category; Category = Category->getNextClassCategory()) {
627 // Even if property is ready only, if a category has a user defined setter,
628 // it is not considered read only.
629 if (Category->getInstanceMethod(PDecl->getSetterName()))
630 return false;
631 ObjCPropertyDecl *P =
632 Category->FindPropertyDeclaration(PDecl->getIdentifier());
633 if (P && !P->isReadOnly())
634 return false;
635 }
636
637 // Also, check for definition of a setter method in the implementation if
638 // all else failed.
639 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
640 if (ObjCImplementationDecl *IMD =
641 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
642 if (IMD->getInstanceMethod(PDecl->getSetterName()))
643 return false;
644 } else if (ObjCCategoryImplDecl *CIMD =
645 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
646 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
647 return false;
648 }
649 }
650 // Lastly, look through the implementation (if one is in scope).
651 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
652 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
653 return false;
654 // If all fails, look at the super class.
655 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
656 return isPropertyReadonly(PDecl, SIDecl);
657 return true;
658}
659
660/// CollectImmediateProperties - This routine collects all properties in
661/// the class and its conforming protocols; but not those it its super class.
662void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
663 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
664 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
665 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
666 E = IDecl->prop_end(); P != E; ++P) {
667 ObjCPropertyDecl *Prop = (*P);
668 PropMap[Prop->getIdentifier()] = Prop;
669 }
670 // scan through class's protocols.
671 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
672 E = IDecl->protocol_end(); PI != E; ++PI)
673 CollectImmediateProperties((*PI), PropMap);
674 }
675 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
676 if (!CATDecl->IsClassExtension())
677 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
678 E = CATDecl->prop_end(); P != E; ++P) {
679 ObjCPropertyDecl *Prop = (*P);
680 PropMap[Prop->getIdentifier()] = Prop;
681 }
682 // scan through class's protocols.
683 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
684 E = CATDecl->protocol_end(); PI != E; ++PI)
685 CollectImmediateProperties((*PI), PropMap);
686 }
687 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
688 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
689 E = PDecl->prop_end(); P != E; ++P) {
690 ObjCPropertyDecl *Prop = (*P);
691 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
692 if (!PropEntry)
693 PropEntry = Prop;
694 }
695 // scan through protocol's protocols.
696 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
697 E = PDecl->protocol_end(); PI != E; ++PI)
698 CollectImmediateProperties((*PI), PropMap);
699 }
700}
701
702/// LookupPropertyDecl - Looks up a property in the current class and all
703/// its protocols.
704ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
705 IdentifierInfo *II) {
706 if (const ObjCInterfaceDecl *IDecl =
707 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
708 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
709 E = IDecl->prop_end(); P != E; ++P) {
710 ObjCPropertyDecl *Prop = (*P);
711 if (Prop->getIdentifier() == II)
712 return Prop;
713 }
714 // scan through class's protocols.
715 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
716 E = IDecl->protocol_end(); PI != E; ++PI) {
717 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
718 if (Prop)
719 return Prop;
720 }
721 }
722 else if (const ObjCProtocolDecl *PDecl =
723 dyn_cast<ObjCProtocolDecl>(CDecl)) {
724 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
725 E = PDecl->prop_end(); P != E; ++P) {
726 ObjCPropertyDecl *Prop = (*P);
727 if (Prop->getIdentifier() == II)
728 return Prop;
729 }
730 // scan through protocol's protocols.
731 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
732 E = PDecl->protocol_end(); PI != E; ++PI) {
733 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
734 if (Prop)
735 return Prop;
736 }
737 }
738 return 0;
739}
740
741
742void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl,
743 ObjCContainerDecl *CDecl,
744 const llvm::DenseSet<Selector>& InsMap) {
745 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
746 CollectImmediateProperties(CDecl, PropMap);
747 if (PropMap.empty())
748 return;
749
750 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
751 for (ObjCImplDecl::propimpl_iterator
752 I = IMPDecl->propimpl_begin(),
753 EI = IMPDecl->propimpl_end(); I != EI; ++I)
754 PropImplMap.insert((*I)->getPropertyDecl());
755
756 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
757 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
758 ObjCPropertyDecl *Prop = P->second;
759 // Is there a matching propery synthesize/dynamic?
760 if (Prop->isInvalidDecl() ||
761 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
762 PropImplMap.count(Prop))
763 continue;
764 if (LangOpts.ObjCNonFragileABI2) {
765 ActOnPropertyImplDecl(IMPDecl->getLocation(),
766 SourceLocation(),
767 true, DeclPtrTy::make(IMPDecl),
768 Prop->getIdentifier(),
769 Prop->getIdentifier());
770 continue;
771 }
772 if (!InsMap.count(Prop->getGetterName())) {
773 Diag(Prop->getLocation(),
774 isa<ObjCCategoryDecl>(CDecl) ?
775 diag::warn_setter_getter_impl_required_in_category :
776 diag::warn_setter_getter_impl_required)
777 << Prop->getDeclName() << Prop->getGetterName();
778 Diag(IMPDecl->getLocation(),
779 diag::note_property_impl_required);
780 }
781
782 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
783 Diag(Prop->getLocation(),
784 isa<ObjCCategoryDecl>(CDecl) ?
785 diag::warn_setter_getter_impl_required_in_category :
786 diag::warn_setter_getter_impl_required)
787 << Prop->getDeclName() << Prop->getSetterName();
788 Diag(IMPDecl->getLocation(),
789 diag::note_property_impl_required);
790 }
791 }
792}
793
794void
795Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
796 ObjCContainerDecl* IDecl) {
797 // Rules apply in non-GC mode only
798 if (getLangOptions().getGCMode() != LangOptions::NonGC)
799 return;
800 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
801 E = IDecl->prop_end();
802 I != E; ++I) {
803 ObjCPropertyDecl *Property = (*I);
804 unsigned Attributes = Property->getPropertyAttributes();
805 // We only care about readwrite atomic property.
806 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
807 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
808 continue;
809 if (const ObjCPropertyImplDecl *PIDecl
810 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
811 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
812 continue;
813 ObjCMethodDecl *GetterMethod =
814 IMPDecl->getInstanceMethod(Property->getGetterName());
815 ObjCMethodDecl *SetterMethod =
816 IMPDecl->getInstanceMethod(Property->getSetterName());
817 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
818 SourceLocation MethodLoc =
819 (GetterMethod ? GetterMethod->getLocation()
820 : SetterMethod->getLocation());
821 Diag(MethodLoc, diag::warn_atomic_property_rule)
822 << Property->getIdentifier();
823 Diag(Property->getLocation(), diag::note_property_declare);
824 }
825 }
826 }
827}
828
829/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
830/// have the property type and issue diagnostics if they don't.
831/// Also synthesize a getter/setter method if none exist (and update the
832/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
833/// methods is the "right" thing to do.
834void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
835 ObjCContainerDecl *CD) {
836 ObjCMethodDecl *GetterMethod, *SetterMethod;
837
838 GetterMethod = CD->getInstanceMethod(property->getGetterName());
839 SetterMethod = CD->getInstanceMethod(property->getSetterName());
840 DiagnosePropertyAccessorMismatch(property, GetterMethod,
841 property->getLocation());
842
843 if (SetterMethod) {
844 ObjCPropertyDecl::PropertyAttributeKind CAttr =
845 property->getPropertyAttributes();
846 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
847 Context.getCanonicalType(SetterMethod->getResultType()) !=
848 Context.VoidTy)
849 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
850 if (SetterMethod->param_size() != 1 ||
851 ((*SetterMethod->param_begin())->getType() != property->getType())) {
852 Diag(property->getLocation(),
853 diag::warn_accessor_property_type_mismatch)
854 << property->getDeclName()
855 << SetterMethod->getSelector();
856 Diag(SetterMethod->getLocation(), diag::note_declared_at);
857 }
858 }
859
860 // Synthesize getter/setter methods if none exist.
861 // Find the default getter and if one not found, add one.
862 // FIXME: The synthesized property we set here is misleading. We almost always
863 // synthesize these methods unless the user explicitly provided prototypes
864 // (which is odd, but allowed). Sema should be typechecking that the
865 // declarations jive in that situation (which it is not currently).
866 if (!GetterMethod) {
867 // No instance method of same name as property getter name was found.
868 // Declare a getter method and add it to the list of methods
869 // for this class.
870 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
871 property->getLocation(), property->getGetterName(),
872 property->getType(), 0, CD, true, false, true,
873 (property->getPropertyImplementation() ==
874 ObjCPropertyDecl::Optional) ?
875 ObjCMethodDecl::Optional :
876 ObjCMethodDecl::Required);
877 CD->addDecl(GetterMethod);
878 } else
879 // A user declared getter will be synthesize when @synthesize of
880 // the property with the same name is seen in the @implementation
881 GetterMethod->setSynthesized(true);
882 property->setGetterMethodDecl(GetterMethod);
883
884 // Skip setter if property is read-only.
885 if (!property->isReadOnly()) {
886 // Find the default setter and if one not found, add one.
887 if (!SetterMethod) {
888 // No instance method of same name as property setter name was found.
889 // Declare a setter method and add it to the list of methods
890 // for this class.
891 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
892 property->getLocation(),
893 property->getSetterName(),
894 Context.VoidTy, 0, CD, true, false, true,
895 (property->getPropertyImplementation() ==
896 ObjCPropertyDecl::Optional) ?
897 ObjCMethodDecl::Optional :
898 ObjCMethodDecl::Required);
899 // Invent the arguments for the setter. We don't bother making a
900 // nice name for the argument.
901 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
902 property->getLocation(),
903 property->getIdentifier(),
904 property->getType(),
905 /*TInfo=*/0,
906 VarDecl::None,
907 0);
908 SetterMethod->setMethodParams(Context, &Argument, 1);
909 CD->addDecl(SetterMethod);
910 } else
911 // A user declared setter will be synthesize when @synthesize of
912 // the property with the same name is seen in the @implementation
913 SetterMethod->setSynthesized(true);
914 property->setSetterMethodDecl(SetterMethod);
915 }
916 // Add any synthesized methods to the global pool. This allows us to
917 // handle the following, which is supported by GCC (and part of the design).
918 //
919 // @interface Foo
920 // @property double bar;
921 // @end
922 //
923 // void thisIsUnfortunate() {
924 // id foo;
925 // double bar = [foo bar];
926 // }
927 //
928 if (GetterMethod)
929 AddInstanceMethodToGlobalPool(GetterMethod);
930 if (SetterMethod)
931 AddInstanceMethodToGlobalPool(SetterMethod);
932}
933
934void Sema::CheckObjCPropertyAttributes(QualType PropertyTy,
935 SourceLocation Loc,
936 unsigned &Attributes) {
937 // FIXME: Improve the reported location.
938
939 // readonly and readwrite/assign/retain/copy conflict.
940 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
941 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
942 ObjCDeclSpec::DQ_PR_assign |
943 ObjCDeclSpec::DQ_PR_copy |
944 ObjCDeclSpec::DQ_PR_retain))) {
945 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
946 "readwrite" :
947 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
948 "assign" :
949 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
950 "copy" : "retain";
951
952 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
953 diag::err_objc_property_attr_mutually_exclusive :
954 diag::warn_objc_property_attr_mutually_exclusive)
955 << "readonly" << which;
956 }
957
958 // Check for copy or retain on non-object types.
959 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
960 !PropertyTy->isObjCObjectPointerType() &&
961 !PropertyTy->isBlockPointerType() &&
962 !Context.isObjCNSObjectType(PropertyTy)) {
963 Diag(Loc, diag::err_objc_property_requires_object)
964 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
965 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
966 }
967
968 // Check for more than one of { assign, copy, retain }.
969 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
970 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
971 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
972 << "assign" << "copy";
973 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
974 }
975 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
976 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
977 << "assign" << "retain";
978 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
979 }
980 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
981 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
982 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
983 << "copy" << "retain";
984 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
985 }
986 }
987
988 // Warn if user supplied no assignment attribute, property is
989 // readwrite, and this is an object type.
990 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
991 ObjCDeclSpec::DQ_PR_retain)) &&
992 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
993 PropertyTy->isObjCObjectPointerType()) {
994 // Skip this warning in gc-only mode.
995 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
996 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
997
998 // If non-gc code warn that this is likely inappropriate.
999 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1000 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1001
1002 // FIXME: Implement warning dependent on NSCopying being
1003 // implemented. See also:
1004 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1005 // (please trim this list while you are at it).
1006 }
1007
1008 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1009 && getLangOptions().getGCMode() == LangOptions::GCOnly
1010 && PropertyTy->isBlockPointerType())
1011 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
1012}
1013
Ted Kremenek9d64c152010-03-12 00:38:38 +00001014ObjCIvarDecl*
1015Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl,
1016 IdentifierInfo *NameII) {
1017 ObjCIvarDecl *Ivar = 0;
1018 ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII);
1019 if (Prop && !Prop->isInvalidDecl()) {
1020 DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl);
1021 QualType PropType = Context.getCanonicalType(Prop->getType());
1022 assert(EnclosingContext &&
1023 "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar");
1024 Ivar = ObjCIvarDecl::Create(Context, EnclosingContext,
1025 Prop->getLocation(),
1026 NameII, PropType, /*Dinfo=*/0,
1027 ObjCIvarDecl::Public,
1028 (Expr *)0);
1029 Ivar->setLexicalDeclContext(IDecl);
1030 IDecl->addDecl(Ivar);
1031 Prop->setPropertyIvarDecl(Ivar);
1032 }
1033 return Ivar;
1034}
1035