1 //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for Objective C @property and
11 // @synthesize declarations.
13 //===----------------------------------------------------------------------===//
17 #include "clang/AST/ExprObjC.h"
19 using namespace clang;
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
25 Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
30 DeclPtrTy ClassCategory,
31 bool *isOverridingProperty,
32 tok::ObjCKeywordKind MethodImplKind) {
33 unsigned Attributes = ODS.getPropertyAttributes();
34 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
35 // default is readwrite!
36 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
37 // property is defaulted to 'assign' if it is readwrite and is
39 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
41 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
42 !(Attributes & ObjCDeclSpec::DQ_PR_copy)));
44 QualType T = GetTypeForDeclarator(FD.D, S);
45 if (T->isReferenceType()) {
46 Diag(AtLoc, diag::error_reference_property);
49 // Proceed with constructing the ObjCPropertDecls.
50 ObjCContainerDecl *ClassDecl =
51 cast<ObjCContainerDecl>(ClassCategory.getAs<Decl>());
53 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl))
54 if (CDecl->IsClassExtension())
55 return HandlePropertyInClassExtension(S, CDecl, AtLoc,
56 FD, GetterSel, SetterSel,
57 isAssign, isReadWrite,
59 isOverridingProperty, T,
62 DeclPtrTy Res = DeclPtrTy::make(CreatePropertyDecl(S, ClassDecl, AtLoc, FD,
64 isAssign, isReadWrite,
65 Attributes, T, MethodImplKind));
66 // Validate the attributes on the @property.
67 CheckObjCPropertyAttributes(Res, AtLoc, Attributes);
72 Sema::HandlePropertyInClassExtension(Scope *S, ObjCCategoryDecl *CDecl,
73 SourceLocation AtLoc, FieldDeclarator &FD,
74 Selector GetterSel, Selector SetterSel,
76 const bool isReadWrite,
77 const unsigned Attributes,
78 bool *isOverridingProperty,
80 tok::ObjCKeywordKind MethodImplKind) {
82 // Diagnose if this property is already in continuation class.
83 DeclContext *DC = cast<DeclContext>(CDecl);
84 IdentifierInfo *PropertyId = FD.D.getIdentifier();
86 if (ObjCPropertyDecl *prevDecl =
87 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
88 Diag(AtLoc, diag::err_duplicate_property);
89 Diag(prevDecl->getLocation(), diag::note_property_declare);
93 // Create a new ObjCPropertyDecl with the DeclContext being
94 // the class extension.
95 ObjCPropertyDecl *PDecl =
96 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
97 PropertyId, AtLoc, T);
98 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
99 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
100 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
101 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
105 // We need to look in the @interface to see if the @property was
107 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
109 Diag(CDecl->getLocation(), diag::err_continuation_class);
110 *isOverridingProperty = true;
114 // Find the property in continuation class's primary class only.
115 ObjCPropertyDecl *PIDecl =
116 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
119 // No matching property found in the primary class. Just fall thru
120 // and add property to continuation class's primary class.
121 ObjCPropertyDecl *PDecl =
122 CreatePropertyDecl(S, CCPrimary, AtLoc,
123 FD, GetterSel, SetterSel, isAssign, isReadWrite,
124 Attributes, T, MethodImplKind, DC);
126 // A case of continuation class adding a new property in the class. This
127 // is not what it was meant for. However, gcc supports it and so should we.
128 // Make sure setter/getters are declared here.
129 ProcessPropertyDecl(PDecl, CCPrimary);
130 return DeclPtrTy::make(PDecl);
134 // The property 'PIDecl's readonly attribute will be over-ridden
135 // with continuation class's readwrite property attribute!
136 unsigned PIkind = PIDecl->getPropertyAttributes();
137 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
138 unsigned retainCopyNonatomic =
139 (ObjCPropertyDecl::OBJC_PR_retain |
140 ObjCPropertyDecl::OBJC_PR_copy |
141 ObjCPropertyDecl::OBJC_PR_nonatomic);
142 if ((Attributes & retainCopyNonatomic) !=
143 (PIkind & retainCopyNonatomic)) {
144 Diag(AtLoc, diag::warn_property_attr_mismatch);
145 Diag(PIDecl->getLocation(), diag::note_property_declare);
147 DeclContext *DC = cast<DeclContext>(CCPrimary);
148 if (!ObjCPropertyDecl::findPropertyDecl(DC,
149 PIDecl->getDeclName().getAsIdentifierInfo())) {
150 // Protocol is not in the primary class. Must build one for it.
151 ObjCDeclSpec ProtocolPropertyODS;
152 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
153 // and ObjCPropertyDecl::PropertyAttributeKind have identical
154 // values. Should consolidate both into one enum type.
156 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
159 DeclPtrTy ProtocolPtrTy =
160 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS,
161 PIDecl->getGetterName(),
162 PIDecl->getSetterName(),
163 DeclPtrTy::make(CCPrimary), isOverridingProperty,
165 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy.getAs<Decl>());
167 PIDecl->makeitReadWriteAttribute();
168 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
169 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
170 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
171 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
172 PIDecl->setSetterName(SetterSel);
174 Diag(AtLoc, diag::err_use_continuation_class)
175 << CCPrimary->getDeclName();
176 Diag(PIDecl->getLocation(), diag::note_property_declare);
178 *isOverridingProperty = true;
179 // Make sure setter decl is synthesized, and added to primary class's list.
180 ProcessPropertyDecl(PIDecl, CCPrimary);
184 ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
185 ObjCContainerDecl *CDecl,
186 SourceLocation AtLoc,
191 const bool isReadWrite,
192 const unsigned Attributes,
194 tok::ObjCKeywordKind MethodImplKind,
195 DeclContext *lexicalDC){
197 IdentifierInfo *PropertyId = FD.D.getIdentifier();
199 // Issue a warning if property is 'assign' as default and its object, which is
200 // gc'able conforms to NSCopying protocol
201 if (getLangOptions().getGCMode() != LangOptions::NonGC &&
202 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
203 if (const ObjCObjectPointerType *ObjPtrTy =
204 T->getAs<ObjCObjectPointerType>()) {
205 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
207 if (ObjCProtocolDecl* PNSCopying =
208 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
209 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
210 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
212 if (T->isObjCObjectType())
213 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object);
215 DeclContext *DC = cast<DeclContext>(CDecl);
216 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
217 FD.D.getIdentifierLoc(),
218 PropertyId, AtLoc, T);
220 if (ObjCPropertyDecl *prevDecl =
221 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
222 Diag(PDecl->getLocation(), diag::err_duplicate_property);
223 Diag(prevDecl->getLocation(), diag::note_property_declare);
224 PDecl->setInvalidDecl();
229 PDecl->setLexicalDeclContext(lexicalDC);
232 if (T->isArrayType() || T->isFunctionType()) {
233 Diag(AtLoc, diag::err_property_type) << T;
234 PDecl->setInvalidDecl();
237 ProcessDeclAttributes(S, PDecl, FD.D);
239 // Regardless of setter/getter attribute, we save the default getter/setter
240 // selector names in anticipation of declaration of setter/getter methods.
241 PDecl->setGetterName(GetterSel);
242 PDecl->setSetterName(SetterSel);
244 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
245 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
247 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
248 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
250 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
251 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
254 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
256 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
257 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
259 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
260 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
263 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
265 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
266 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
268 if (MethodImplKind == tok::objc_required)
269 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
270 else if (MethodImplKind == tok::objc_optional)
271 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
277 /// ActOnPropertyImplDecl - This routine performs semantic checks and
278 /// builds the AST node for a property implementation declaration; declared
279 /// as @synthesize or @dynamic.
281 Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(Scope *S,
282 SourceLocation AtLoc,
283 SourceLocation PropertyLoc,
285 DeclPtrTy ClassCatImpDecl,
286 IdentifierInfo *PropertyId,
287 IdentifierInfo *PropertyIvar) {
288 ObjCContainerDecl *ClassImpDecl =
289 cast_or_null<ObjCContainerDecl>(ClassCatImpDecl.getAs<Decl>());
290 // Make sure we have a context for the property implementation declaration.
292 Diag(AtLoc, diag::error_missing_property_context);
295 ObjCPropertyDecl *property = 0;
296 ObjCInterfaceDecl* IDecl = 0;
297 // Find the class or category class where this property must have
299 ObjCImplementationDecl *IC = 0;
300 ObjCCategoryImplDecl* CatImplClass = 0;
301 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
302 IDecl = IC->getClassInterface();
303 // We always synthesize an interface for an implementation
304 // without an interface decl. So, IDecl is always non-zero.
306 "ActOnPropertyImplDecl - @implementation without @interface");
308 // Look for this property declaration in the @implementation's @interface
309 property = IDecl->FindPropertyDeclaration(PropertyId);
311 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
314 if (const ObjCCategoryDecl *CD =
315 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
316 if (!CD->IsClassExtension()) {
317 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
318 Diag(property->getLocation(), diag::note_property_declare);
322 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
324 Diag(AtLoc, diag::error_synthesize_category_decl);
327 IDecl = CatImplClass->getClassInterface();
329 Diag(AtLoc, diag::error_missing_property_interface);
332 ObjCCategoryDecl *Category =
333 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
335 // If category for this implementation not found, it is an error which
336 // has already been reported eralier.
339 // Look for this property declaration in @implementation's category
340 property = Category->FindPropertyDeclaration(PropertyId);
342 Diag(PropertyLoc, diag::error_bad_category_property_decl)
343 << Category->getDeclName();
347 Diag(AtLoc, diag::error_bad_property_context);
350 ObjCIvarDecl *Ivar = 0;
351 // Check that we have a valid, previously declared ivar for @synthesize
355 PropertyIvar = PropertyId;
356 QualType PropType = Context.getCanonicalType(property->getType());
357 // Check that this is a previously declared 'ivar' in 'IDecl' interface
358 ObjCInterfaceDecl *ClassDeclared;
359 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
361 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, PropertyLoc,
362 PropertyIvar, PropType, /*Dinfo=*/0,
363 ObjCIvarDecl::Protected,
365 ClassImpDecl->addDecl(Ivar);
366 IDecl->makeDeclVisibleInContext(Ivar, false);
367 property->setPropertyIvarDecl(Ivar);
369 if (!getLangOptions().ObjCNonFragileABI)
370 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId;
371 // Note! I deliberately want it to fall thru so, we have a
372 // a property implementation and to avoid future warnings.
373 } else if (getLangOptions().ObjCNonFragileABI &&
374 ClassDeclared != IDecl) {
375 Diag(PropertyLoc, diag::error_ivar_in_superclass_use)
376 << property->getDeclName() << Ivar->getDeclName()
377 << ClassDeclared->getDeclName();
378 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
379 << Ivar << Ivar->getNameAsCString();
380 // Note! I deliberately want it to fall thru so more errors are caught.
382 QualType IvarType = Context.getCanonicalType(Ivar->getType());
384 // Check that type of property and its ivar are type compatible.
385 if (PropType != IvarType) {
387 if (isa<ObjCObjectPointerType>(PropType)
388 && isa<ObjCObjectPointerType>(IvarType))
390 Context.canAssignObjCInterfaces(
391 PropType->getAs<ObjCObjectPointerType>(),
392 IvarType->getAs<ObjCObjectPointerType>());
394 compat = (CheckAssignmentConstraints(PropType, IvarType) == Compatible);
396 Diag(PropertyLoc, diag::error_property_ivar_type)
397 << property->getDeclName() << PropType
398 << Ivar->getDeclName() << IvarType;
399 Diag(Ivar->getLocation(), diag::note_ivar_decl);
400 // Note! I deliberately want it to fall thru so, we have a
401 // a property implementation and to avoid future warnings.
404 // FIXME! Rules for properties are somewhat different that those
405 // for assignments. Use a new routine to consolidate all cases;
406 // specifically for property redeclarations as well as for ivars.
407 QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType();
408 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
409 if (lhsType != rhsType &&
410 lhsType->isArithmeticType()) {
411 Diag(PropertyLoc, diag::error_property_ivar_type)
412 << property->getDeclName() << PropType
413 << Ivar->getDeclName() << IvarType;
414 Diag(Ivar->getLocation(), diag::note_ivar_decl);
415 // Fall thru - see previous comment
417 // __weak is explicit. So it works on Canonical type.
418 if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
419 getLangOptions().getGCMode() != LangOptions::NonGC) {
420 Diag(PropertyLoc, diag::error_weak_property)
421 << property->getDeclName() << Ivar->getDeclName();
422 // Fall thru - see previous comment
424 if ((property->getType()->isObjCObjectPointerType() ||
425 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
426 getLangOptions().getGCMode() != LangOptions::NonGC) {
427 Diag(PropertyLoc, diag::error_strong_property)
428 << property->getDeclName() << Ivar->getDeclName();
429 // Fall thru - see previous comment
432 } else if (PropertyIvar)
434 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl);
435 assert (property && "ActOnPropertyImplDecl - property declaration missing");
436 ObjCPropertyImplDecl *PIDecl =
437 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
440 ObjCPropertyImplDecl::Synthesize
441 : ObjCPropertyImplDecl::Dynamic),
443 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
444 getterMethod->createImplicitParams(Context, IDecl);
445 if (getLangOptions().CPlusPlus && Synthesize) {
446 // For Objective-C++, need to synthesize the AST for the IVAR object to be
447 // returned by the getter as it must conform to C++'s copy-return rules.
448 // FIXME. Eventually we want to do this for Objective-C as well.
449 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
450 DeclRefExpr *SelfExpr =
451 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
454 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
455 SelfExpr, true, true);
456 OwningExprResult Res =
457 PerformCopyInitialization(InitializedEntity::InitializeResult(
459 getterMethod->getResultType(),
463 if (!Res.isInvalid()) {
464 Expr *ResExpr = Res.takeAs<Expr>();
466 ResExpr = MaybeCreateCXXExprWithTemporaries(ResExpr);
467 PIDecl->setGetterCXXConstructor(ResExpr);
471 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
472 setterMethod->createImplicitParams(Context, IDecl);
473 if (getLangOptions().CPlusPlus && Synthesize) {
474 // FIXME. Eventually we want to do this for Objective-C as well.
475 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
476 DeclRefExpr *SelfExpr =
477 new (Context) DeclRefExpr(SelfDecl,SelfDecl->getType(),
480 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc,
481 SelfExpr, true, true);
482 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
483 ParmVarDecl *Param = (*P);
484 Expr *rhs = new (Context) DeclRefExpr(Param,Param->getType(),
486 OwningExprResult Res = BuildBinOp(S, SourceLocation(),
487 BinaryOperator::Assign, lhs, rhs);
488 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>());
494 if (ObjCPropertyImplDecl *PPIDecl =
495 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
496 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
497 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
499 Diag(PPIDecl->getLocation(), diag::note_previous_use);
502 if (ObjCPropertyImplDecl *PPIDecl
503 = IC->FindPropertyImplDecl(PropertyId)) {
504 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
505 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
508 IC->addPropertyImplementation(PIDecl);
511 if (ObjCPropertyImplDecl *PPIDecl =
512 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
513 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
514 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
516 Diag(PPIDecl->getLocation(), diag::note_previous_use);
519 if (ObjCPropertyImplDecl *PPIDecl =
520 CatImplClass->FindPropertyImplDecl(PropertyId)) {
521 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
522 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
525 CatImplClass->addPropertyImplementation(PIDecl);
528 return DeclPtrTy::make(PIDecl);
531 //===----------------------------------------------------------------------===//
533 //===----------------------------------------------------------------------===//
535 /// DiagnosePropertyMismatch - Compares two properties for their
536 /// attributes and types and warns on a variety of inconsistencies.
539 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
540 ObjCPropertyDecl *SuperProperty,
541 const IdentifierInfo *inheritedName) {
542 ObjCPropertyDecl::PropertyAttributeKind CAttr =
543 Property->getPropertyAttributes();
544 ObjCPropertyDecl::PropertyAttributeKind SAttr =
545 SuperProperty->getPropertyAttributes();
546 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
547 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
548 Diag(Property->getLocation(), diag::warn_readonly_property)
549 << Property->getDeclName() << inheritedName;
550 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
551 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
552 Diag(Property->getLocation(), diag::warn_property_attribute)
553 << Property->getDeclName() << "copy" << inheritedName;
554 else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain)
555 != (SAttr & ObjCPropertyDecl::OBJC_PR_retain))
556 Diag(Property->getLocation(), diag::warn_property_attribute)
557 << Property->getDeclName() << "retain" << inheritedName;
559 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
560 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic))
561 Diag(Property->getLocation(), diag::warn_property_attribute)
562 << Property->getDeclName() << "atomic" << inheritedName;
563 if (Property->getSetterName() != SuperProperty->getSetterName())
564 Diag(Property->getLocation(), diag::warn_property_attribute)
565 << Property->getDeclName() << "setter" << inheritedName;
566 if (Property->getGetterName() != SuperProperty->getGetterName())
567 Diag(Property->getLocation(), diag::warn_property_attribute)
568 << Property->getDeclName() << "getter" << inheritedName;
571 Context.getCanonicalType(SuperProperty->getType());
573 Context.getCanonicalType(Property->getType());
575 if (!Context.typesAreCompatible(LHSType, RHSType)) {
576 // FIXME: Incorporate this test with typesAreCompatible.
577 if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType())
578 if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false))
580 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
581 << Property->getType() << SuperProperty->getType() << inheritedName;
585 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
586 ObjCMethodDecl *GetterMethod,
587 SourceLocation Loc) {
589 GetterMethod->getResultType() != property->getType()) {
590 AssignConvertType result = Incompatible;
591 if (property->getType()->isObjCObjectPointerType())
592 result = CheckAssignmentConstraints(GetterMethod->getResultType(),
593 property->getType());
594 if (result != Compatible) {
595 Diag(Loc, diag::warn_accessor_property_type_mismatch)
596 << property->getDeclName()
597 << GetterMethod->getSelector();
598 Diag(GetterMethod->getLocation(), diag::note_declared_at);
605 /// ComparePropertiesInBaseAndSuper - This routine compares property
606 /// declarations in base and its super class, if any, and issues
607 /// diagnostics in a variety of inconsistant situations.
609 void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) {
610 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
614 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(),
615 E = SDecl->prop_end(); S != E; ++S) {
616 ObjCPropertyDecl *SuperPDecl = (*S);
617 // Does property in super class has declaration in current class?
618 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(),
619 E = IDecl->prop_end(); I != E; ++I) {
620 ObjCPropertyDecl *PDecl = (*I);
621 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier())
622 DiagnosePropertyMismatch(PDecl, SuperPDecl,
623 SDecl->getIdentifier());
628 /// MatchOneProtocolPropertiesInClass - This routine goes thru the list
629 /// of properties declared in a protocol and compares their attribute against
630 /// the same property declared in the class or category.
632 Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl,
633 ObjCProtocolDecl *PDecl) {
634 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
637 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
638 assert (CatDecl && "MatchOneProtocolPropertiesInClass");
639 if (!CatDecl->IsClassExtension())
640 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
641 E = PDecl->prop_end(); P != E; ++P) {
642 ObjCPropertyDecl *Pr = (*P);
643 ObjCCategoryDecl::prop_iterator CP, CE;
644 // Is this property already in category's list of properties?
645 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP)
646 if ((*CP)->getIdentifier() == Pr->getIdentifier())
649 // Property protocol already exist in class. Diagnose any mismatch.
650 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
654 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
655 E = PDecl->prop_end(); P != E; ++P) {
656 ObjCPropertyDecl *Pr = (*P);
657 ObjCInterfaceDecl::prop_iterator CP, CE;
658 // Is this property already in class's list of properties?
659 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP)
660 if ((*CP)->getIdentifier() == Pr->getIdentifier())
663 // Property protocol already exist in class. Diagnose any mismatch.
664 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier());
668 /// CompareProperties - This routine compares properties
669 /// declared in 'ClassOrProtocol' objects (which can be a class or an
670 /// inherited protocol with the list of properties for class/category 'CDecl'
672 void Sema::CompareProperties(Decl *CDecl,
673 DeclPtrTy ClassOrProtocol) {
674 Decl *ClassDecl = ClassOrProtocol.getAs<Decl>();
675 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl);
679 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl);
680 assert (CatDecl && "CompareProperties");
681 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
682 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(),
683 E = MDecl->protocol_end(); P != E; ++P)
684 // Match properties of category with those of protocol (*P)
685 MatchOneProtocolPropertiesInClass(CatDecl, *P);
687 // Go thru the list of protocols for this category and recursively match
688 // their properties with those in the category.
689 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(),
690 E = CatDecl->protocol_end(); P != E; ++P)
691 CompareProperties(CatDecl, DeclPtrTy::make(*P));
693 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
694 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
695 E = MD->protocol_end(); P != E; ++P)
696 MatchOneProtocolPropertiesInClass(CatDecl, *P);
701 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
702 for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(),
703 E = MDecl->protocol_end(); P != E; ++P)
704 // Match properties of class IDecl with those of protocol (*P).
705 MatchOneProtocolPropertiesInClass(IDecl, *P);
707 // Go thru the list of protocols for this class and recursively match
708 // their properties with those declared in the class.
709 for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(),
710 E = IDecl->protocol_end(); P != E; ++P)
711 CompareProperties(IDecl, DeclPtrTy::make(*P));
713 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl);
714 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(),
715 E = MD->protocol_end(); P != E; ++P)
716 MatchOneProtocolPropertiesInClass(IDecl, *P);
720 /// isPropertyReadonly - Return true if property is readonly, by searching
721 /// for the property in the class and in its categories and implementations
723 bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl,
724 ObjCInterfaceDecl *IDecl) {
725 // by far the most common case.
726 if (!PDecl->isReadOnly())
728 // Even if property is ready only, if interface has a user defined setter,
729 // it is not considered read only.
730 if (IDecl->getInstanceMethod(PDecl->getSetterName()))
733 // Main class has the property as 'readonly'. Must search
734 // through the category list to see if the property's
735 // attribute has been over-ridden to 'readwrite'.
736 for (ObjCCategoryDecl *Category = IDecl->getCategoryList();
737 Category; Category = Category->getNextClassCategory()) {
738 // Even if property is ready only, if a category has a user defined setter,
739 // it is not considered read only.
740 if (Category->getInstanceMethod(PDecl->getSetterName()))
742 ObjCPropertyDecl *P =
743 Category->FindPropertyDeclaration(PDecl->getIdentifier());
744 if (P && !P->isReadOnly())
748 // Also, check for definition of a setter method in the implementation if
750 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) {
751 if (ObjCImplementationDecl *IMD =
752 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) {
753 if (IMD->getInstanceMethod(PDecl->getSetterName()))
755 } else if (ObjCCategoryImplDecl *CIMD =
756 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) {
757 if (CIMD->getInstanceMethod(PDecl->getSetterName()))
761 // Lastly, look through the implementation (if one is in scope).
762 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation())
763 if (ImpDecl->getInstanceMethod(PDecl->getSetterName()))
765 // If all fails, look at the super class.
766 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass())
767 return isPropertyReadonly(PDecl, SIDecl);
771 /// CollectImmediateProperties - This routine collects all properties in
772 /// the class and its conforming protocols; but not those it its super class.
773 void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl,
774 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
775 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
776 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
777 E = IDecl->prop_end(); P != E; ++P) {
778 ObjCPropertyDecl *Prop = (*P);
779 PropMap[Prop->getIdentifier()] = Prop;
781 // scan through class's protocols.
782 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
783 E = IDecl->protocol_end(); PI != E; ++PI)
784 // Exclude property for protocols which conform to class's super-class,
785 // as super-class has to implement the property.
786 if (!ProtocolConformsToSuperClass(IDecl, (*PI)))
787 CollectImmediateProperties((*PI), PropMap);
789 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
790 if (!CATDecl->IsClassExtension())
791 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(),
792 E = CATDecl->prop_end(); P != E; ++P) {
793 ObjCPropertyDecl *Prop = (*P);
794 PropMap[Prop->getIdentifier()] = Prop;
796 // scan through class's protocols.
797 for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(),
798 E = CATDecl->protocol_end(); PI != E; ++PI)
799 CollectImmediateProperties((*PI), PropMap);
801 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
802 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
803 E = PDecl->prop_end(); P != E; ++P) {
804 ObjCPropertyDecl *Prop = (*P);
805 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
809 // scan through protocol's protocols.
810 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
811 E = PDecl->protocol_end(); PI != E; ++PI)
812 CollectImmediateProperties((*PI), PropMap);
816 /// CollectClassPropertyImplementations - This routine collects list of
817 /// properties to be implemented in the class. This includes, class's
818 /// and its conforming protocols' properties.
819 static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl,
820 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
821 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
822 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
823 E = IDecl->prop_end(); P != E; ++P) {
824 ObjCPropertyDecl *Prop = (*P);
825 PropMap[Prop->getIdentifier()] = Prop;
827 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
828 E = IDecl->protocol_end(); PI != E; ++PI)
829 CollectClassPropertyImplementations((*PI), PropMap);
831 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
832 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
833 E = PDecl->prop_end(); P != E; ++P) {
834 ObjCPropertyDecl *Prop = (*P);
835 PropMap[Prop->getIdentifier()] = Prop;
837 // scan through protocol's protocols.
838 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
839 E = PDecl->protocol_end(); PI != E; ++PI)
840 CollectClassPropertyImplementations((*PI), PropMap);
844 /// CollectSuperClassPropertyImplementations - This routine collects list of
845 /// properties to be implemented in super class(s) and also coming from their
846 /// conforming protocols.
847 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
848 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) {
849 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
851 CollectClassPropertyImplementations(SDecl, PropMap);
852 SDecl = SDecl->getSuperClass();
857 /// ProtocolConformsToSuperClass - Returns true if class's given protocol
858 /// conforms to one of its super class's protocols.
859 bool Sema::ProtocolConformsToSuperClass(const ObjCInterfaceDecl *IDecl,
860 const ObjCProtocolDecl *PDecl) {
861 if (const ObjCInterfaceDecl *CDecl = IDecl->getSuperClass()) {
862 for (ObjCInterfaceDecl::protocol_iterator PI = CDecl->protocol_begin(),
863 E = CDecl->protocol_end(); PI != E; ++PI) {
864 if (ProtocolConformsToProtocol((*PI), PDecl))
866 return ProtocolConformsToSuperClass(CDecl, PDecl);
872 bool Sema::ProtocolConformsToProtocol(const ObjCProtocolDecl *NestedProtocol,
873 const ObjCProtocolDecl *PDecl) {
874 if (PDecl->getIdentifier() == NestedProtocol->getIdentifier())
876 // scan through protocol's protocols.
877 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
878 E = PDecl->protocol_end(); PI != E; ++PI)
879 if (ProtocolConformsToProtocol(NestedProtocol, (*PI)))
884 /// LookupPropertyDecl - Looks up a property in the current class and all
886 ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl,
887 IdentifierInfo *II) {
888 if (const ObjCInterfaceDecl *IDecl =
889 dyn_cast<ObjCInterfaceDecl>(CDecl)) {
890 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(),
891 E = IDecl->prop_end(); P != E; ++P) {
892 ObjCPropertyDecl *Prop = (*P);
893 if (Prop->getIdentifier() == II)
896 // scan through class's protocols.
897 for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(),
898 E = IDecl->protocol_end(); PI != E; ++PI) {
899 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
904 else if (const ObjCProtocolDecl *PDecl =
905 dyn_cast<ObjCProtocolDecl>(CDecl)) {
906 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(),
907 E = PDecl->prop_end(); P != E; ++P) {
908 ObjCPropertyDecl *Prop = (*P);
909 if (Prop->getIdentifier() == II)
912 // scan through protocol's protocols.
913 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
914 E = PDecl->protocol_end(); PI != E; ++PI) {
915 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II);
923 /// DefaultSynthesizeProperties - This routine default synthesizes all
924 /// properties which must be synthesized in class's @implementation.
925 void Sema::DefaultSynthesizeProperties (Scope *S, ObjCImplDecl* IMPDecl,
926 ObjCInterfaceDecl *IDecl) {
928 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
929 CollectClassPropertyImplementations(IDecl, PropMap);
932 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap;
933 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
935 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
936 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
937 ObjCPropertyDecl *Prop = P->second;
938 // If property to be implemented in the super class, ignore.
939 if (SuperPropMap[Prop->getIdentifier()])
941 // Is there a matching propery synthesize/dynamic?
942 if (Prop->isInvalidDecl() ||
943 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
944 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier()))
946 ActOnPropertyImplDecl(S, IMPDecl->getLocation(), IMPDecl->getLocation(),
947 true, DeclPtrTy::make(IMPDecl),
948 Prop->getIdentifier(), Prop->getIdentifier());
952 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
953 ObjCContainerDecl *CDecl,
954 const llvm::DenseSet<Selector>& InsMap) {
955 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap;
956 CollectImmediateProperties(CDecl, PropMap);
960 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
961 for (ObjCImplDecl::propimpl_iterator
962 I = IMPDecl->propimpl_begin(),
963 EI = IMPDecl->propimpl_end(); I != EI; ++I)
964 PropImplMap.insert((*I)->getPropertyDecl());
966 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator
967 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
968 ObjCPropertyDecl *Prop = P->second;
969 // Is there a matching propery synthesize/dynamic?
970 if (Prop->isInvalidDecl() ||
971 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
972 PropImplMap.count(Prop))
974 if (!InsMap.count(Prop->getGetterName())) {
975 Diag(Prop->getLocation(),
976 isa<ObjCCategoryDecl>(CDecl) ?
977 diag::warn_setter_getter_impl_required_in_category :
978 diag::warn_setter_getter_impl_required)
979 << Prop->getDeclName() << Prop->getGetterName();
980 Diag(IMPDecl->getLocation(),
981 diag::note_property_impl_required);
984 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) {
985 Diag(Prop->getLocation(),
986 isa<ObjCCategoryDecl>(CDecl) ?
987 diag::warn_setter_getter_impl_required_in_category :
988 diag::warn_setter_getter_impl_required)
989 << Prop->getDeclName() << Prop->getSetterName();
990 Diag(IMPDecl->getLocation(),
991 diag::note_property_impl_required);
997 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
998 ObjCContainerDecl* IDecl) {
999 // Rules apply in non-GC mode only
1000 if (getLangOptions().getGCMode() != LangOptions::NonGC)
1002 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(),
1003 E = IDecl->prop_end();
1005 ObjCPropertyDecl *Property = (*I);
1006 unsigned Attributes = Property->getPropertyAttributes();
1007 // We only care about readwrite atomic property.
1008 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1009 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1011 if (const ObjCPropertyImplDecl *PIDecl
1012 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1013 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1015 ObjCMethodDecl *GetterMethod =
1016 IMPDecl->getInstanceMethod(Property->getGetterName());
1017 ObjCMethodDecl *SetterMethod =
1018 IMPDecl->getInstanceMethod(Property->getSetterName());
1019 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1020 SourceLocation MethodLoc =
1021 (GetterMethod ? GetterMethod->getLocation()
1022 : SetterMethod->getLocation());
1023 Diag(MethodLoc, diag::warn_atomic_property_rule)
1024 << Property->getIdentifier();
1025 Diag(Property->getLocation(), diag::note_property_declare);
1031 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1032 /// have the property type and issue diagnostics if they don't.
1033 /// Also synthesize a getter/setter method if none exist (and update the
1034 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1035 /// methods is the "right" thing to do.
1036 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1037 ObjCContainerDecl *CD) {
1038 ObjCMethodDecl *GetterMethod, *SetterMethod;
1040 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1041 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1042 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1043 property->getLocation());
1046 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1047 property->getPropertyAttributes();
1048 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1049 Context.getCanonicalType(SetterMethod->getResultType()) !=
1051 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
1052 if (SetterMethod->param_size() != 1 ||
1053 ((*SetterMethod->param_begin())->getType() != property->getType())) {
1054 Diag(property->getLocation(),
1055 diag::warn_accessor_property_type_mismatch)
1056 << property->getDeclName()
1057 << SetterMethod->getSelector();
1058 Diag(SetterMethod->getLocation(), diag::note_declared_at);
1062 // Synthesize getter/setter methods if none exist.
1063 // Find the default getter and if one not found, add one.
1064 // FIXME: The synthesized property we set here is misleading. We almost always
1065 // synthesize these methods unless the user explicitly provided prototypes
1066 // (which is odd, but allowed). Sema should be typechecking that the
1067 // declarations jive in that situation (which it is not currently).
1068 if (!GetterMethod) {
1069 // No instance method of same name as property getter name was found.
1070 // Declare a getter method and add it to the list of methods
1072 GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1073 property->getLocation(), property->getGetterName(),
1074 property->getType(), 0, CD, true, false, true,
1075 (property->getPropertyImplementation() ==
1076 ObjCPropertyDecl::Optional) ?
1077 ObjCMethodDecl::Optional :
1078 ObjCMethodDecl::Required);
1079 CD->addDecl(GetterMethod);
1080 // FIXME: Eventually this shouldn't be needed, as the lexical context
1081 // and the real context should be the same.
1082 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1083 GetterMethod->setLexicalDeclContext(lexicalDC);
1086 // A user declared getter will be synthesize when @synthesize of
1087 // the property with the same name is seen in the @implementation
1088 GetterMethod->setSynthesized(true);
1089 property->setGetterMethodDecl(GetterMethod);
1091 // Skip setter if property is read-only.
1092 if (!property->isReadOnly()) {
1093 // Find the default setter and if one not found, add one.
1094 if (!SetterMethod) {
1095 // No instance method of same name as property setter name was found.
1096 // Declare a setter method and add it to the list of methods
1098 SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(),
1099 property->getLocation(),
1100 property->getSetterName(),
1101 Context.VoidTy, 0, CD, true, false, true,
1102 (property->getPropertyImplementation() ==
1103 ObjCPropertyDecl::Optional) ?
1104 ObjCMethodDecl::Optional :
1105 ObjCMethodDecl::Required);
1106 // Invent the arguments for the setter. We don't bother making a
1107 // nice name for the argument.
1108 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
1109 property->getLocation(),
1110 property->getIdentifier(),
1111 property->getType(),
1116 SetterMethod->setMethodParams(Context, &Argument, 1, 1);
1117 CD->addDecl(SetterMethod);
1118 // FIXME: Eventually this shouldn't be needed, as the lexical context
1119 // and the real context should be the same.
1120 if (DeclContext *lexicalDC = property->getLexicalDeclContext())
1121 SetterMethod->setLexicalDeclContext(lexicalDC);
1123 // A user declared setter will be synthesize when @synthesize of
1124 // the property with the same name is seen in the @implementation
1125 SetterMethod->setSynthesized(true);
1126 property->setSetterMethodDecl(SetterMethod);
1128 // Add any synthesized methods to the global pool. This allows us to
1129 // handle the following, which is supported by GCC (and part of the design).
1132 // @property double bar;
1135 // void thisIsUnfortunate() {
1137 // double bar = [foo bar];
1141 AddInstanceMethodToGlobalPool(GetterMethod);
1143 AddInstanceMethodToGlobalPool(SetterMethod);
1146 void Sema::CheckObjCPropertyAttributes(DeclPtrTy PropertyPtrTy,
1148 unsigned &Attributes) {
1149 // FIXME: Improve the reported location.
1150 Decl *PDecl = PropertyPtrTy.getAs<Decl>();
1154 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
1155 QualType PropertyTy = PropertyDecl->getType();
1157 // readonly and readwrite/assign/retain/copy conflict.
1158 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1159 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
1160 ObjCDeclSpec::DQ_PR_assign |
1161 ObjCDeclSpec::DQ_PR_copy |
1162 ObjCDeclSpec::DQ_PR_retain))) {
1163 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ?
1165 (Attributes & ObjCDeclSpec::DQ_PR_assign) ?
1167 (Attributes & ObjCDeclSpec::DQ_PR_copy) ?
1170 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ?
1171 diag::err_objc_property_attr_mutually_exclusive :
1172 diag::warn_objc_property_attr_mutually_exclusive)
1173 << "readonly" << which;
1176 // Check for copy or retain on non-object types.
1177 if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) &&
1178 !PropertyTy->isObjCObjectPointerType() &&
1179 !PropertyTy->isBlockPointerType() &&
1180 !Context.isObjCNSObjectType(PropertyTy) &&
1181 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) {
1182 Diag(Loc, diag::err_objc_property_requires_object)
1183 << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain");
1184 Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain);
1187 // Check for more than one of { assign, copy, retain }.
1188 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
1189 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1190 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1191 << "assign" << "copy";
1192 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
1194 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1195 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1196 << "assign" << "retain";
1197 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1199 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
1200 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
1201 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
1202 << "copy" << "retain";
1203 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
1207 // Warn if user supplied no assignment attribute, property is
1208 // readwrite, and this is an object type.
1209 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
1210 ObjCDeclSpec::DQ_PR_retain)) &&
1211 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
1212 PropertyTy->isObjCObjectPointerType()) {
1213 // Skip this warning in gc-only mode.
1214 if (getLangOptions().getGCMode() != LangOptions::GCOnly)
1215 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
1217 // If non-gc code warn that this is likely inappropriate.
1218 if (getLangOptions().getGCMode() == LangOptions::NonGC)
1219 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
1221 // FIXME: Implement warning dependent on NSCopying being
1222 // implemented. See also:
1223 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
1224 // (please trim this list while you are at it).
1227 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
1228 && getLangOptions().getGCMode() == LangOptions::GCOnly
1229 && PropertyTy->isBlockPointerType())
1230 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);