SemaObjCProperty.cpp revision 20abee6b95c4f5a61e471b4b616439280ca4a81b
1//===--- 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 "clang/Sema/SemaInternal.h" 16#include "clang/Sema/Initialization.h" 17#include "clang/AST/DeclObjC.h" 18#include "clang/AST/ExprObjC.h" 19#include "clang/AST/ExprCXX.h" 20#include "clang/AST/ASTMutationListener.h" 21#include "llvm/ADT/DenseSet.h" 22 23using namespace clang; 24 25//===----------------------------------------------------------------------===// 26// Grammar actions. 27//===----------------------------------------------------------------------===// 28 29/// getImpliedARCOwnership - Given a set of property attributes and a 30/// type, infer an expected lifetime. The type's ownership qualification 31/// is not considered. 32/// 33/// Returns OCL_None if the attributes as stated do not imply an ownership. 34/// Never returns OCL_Autoreleasing. 35static Qualifiers::ObjCLifetime getImpliedARCOwnership( 36 ObjCPropertyDecl::PropertyAttributeKind attrs, 37 QualType type) { 38 // retain, strong, copy, weak, and unsafe_unretained are only legal 39 // on properties of retainable pointer type. 40 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain | 41 ObjCPropertyDecl::OBJC_PR_strong | 42 ObjCPropertyDecl::OBJC_PR_copy)) { 43 return type->getObjCARCImplicitLifetime(); 44 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) { 45 return Qualifiers::OCL_Weak; 46 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { 47 return Qualifiers::OCL_ExplicitNone; 48 } 49 50 // assign can appear on other types, so we have to check the 51 // property type. 52 if (attrs & ObjCPropertyDecl::OBJC_PR_assign && 53 type->isObjCRetainableType()) { 54 return Qualifiers::OCL_ExplicitNone; 55 } 56 57 return Qualifiers::OCL_None; 58} 59 60/// Check the internal consistency of a property declaration. 61static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) { 62 if (property->isInvalidDecl()) return; 63 64 ObjCPropertyDecl::PropertyAttributeKind propertyKind 65 = property->getPropertyAttributes(); 66 Qualifiers::ObjCLifetime propertyLifetime 67 = property->getType().getObjCLifetime(); 68 69 // Nothing to do if we don't have a lifetime. 70 if (propertyLifetime == Qualifiers::OCL_None) return; 71 72 Qualifiers::ObjCLifetime expectedLifetime 73 = getImpliedARCOwnership(propertyKind, property->getType()); 74 if (!expectedLifetime) { 75 // We have a lifetime qualifier but no dominating property 76 // attribute. That's okay, but restore reasonable invariants by 77 // setting the property attribute according to the lifetime 78 // qualifier. 79 ObjCPropertyDecl::PropertyAttributeKind attr; 80 if (propertyLifetime == Qualifiers::OCL_Strong) { 81 attr = ObjCPropertyDecl::OBJC_PR_strong; 82 } else if (propertyLifetime == Qualifiers::OCL_Weak) { 83 attr = ObjCPropertyDecl::OBJC_PR_weak; 84 } else { 85 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); 86 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; 87 } 88 property->setPropertyAttributes(attr); 89 return; 90 } 91 92 if (propertyLifetime == expectedLifetime) return; 93 94 property->setInvalidDecl(); 95 S.Diag(property->getLocation(), 96 diag::err_arc_inconsistent_property_ownership) 97 << property->getDeclName() 98 << expectedLifetime 99 << propertyLifetime; 100} 101 102Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 103 FieldDeclarator &FD, 104 ObjCDeclSpec &ODS, 105 Selector GetterSel, 106 Selector SetterSel, 107 bool *isOverridingProperty, 108 tok::ObjCKeywordKind MethodImplKind, 109 DeclContext *lexicalDC) { 110 unsigned Attributes = ODS.getPropertyAttributes(); 111 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); 112 QualType T = TSI->getType(); 113 if ((getLangOptions().getGC() != LangOptions::NonGC && 114 T.isObjCGCWeak()) || 115 (getLangOptions().ObjCAutoRefCount && 116 T.getObjCLifetime() == Qualifiers::OCL_Weak)) 117 Attributes |= ObjCDeclSpec::DQ_PR_weak; 118 119 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 120 // default is readwrite! 121 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 122 // property is defaulted to 'assign' if it is readwrite and is 123 // not retain or copy 124 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || 125 (isReadWrite && 126 !(Attributes & ObjCDeclSpec::DQ_PR_retain) && 127 !(Attributes & ObjCDeclSpec::DQ_PR_strong) && 128 !(Attributes & ObjCDeclSpec::DQ_PR_copy) && 129 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) && 130 !(Attributes & ObjCDeclSpec::DQ_PR_weak))); 131 132 // Proceed with constructing the ObjCPropertDecls. 133 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); 134 135 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) 136 if (CDecl->IsClassExtension()) { 137 Decl *Res = HandlePropertyInClassExtension(S, AtLoc, 138 FD, GetterSel, SetterSel, 139 isAssign, isReadWrite, 140 Attributes, 141 ODS.getPropertyAttributes(), 142 isOverridingProperty, TSI, 143 MethodImplKind); 144 if (Res) { 145 CheckObjCPropertyAttributes(Res, AtLoc, Attributes); 146 if (getLangOptions().ObjCAutoRefCount) 147 checkARCPropertyDecl(*this, cast<ObjCPropertyDecl>(Res)); 148 } 149 return Res; 150 } 151 152 ObjCPropertyDecl *Res = CreatePropertyDecl(S, ClassDecl, AtLoc, FD, 153 GetterSel, SetterSel, 154 isAssign, isReadWrite, 155 Attributes, 156 ODS.getPropertyAttributes(), 157 TSI, MethodImplKind); 158 if (lexicalDC) 159 Res->setLexicalDeclContext(lexicalDC); 160 161 // Validate the attributes on the @property. 162 CheckObjCPropertyAttributes(Res, AtLoc, Attributes); 163 164 if (getLangOptions().ObjCAutoRefCount) 165 checkARCPropertyDecl(*this, Res); 166 167 return Res; 168} 169 170static ObjCPropertyDecl::PropertyAttributeKind 171makePropertyAttributesAsWritten(unsigned Attributes) { 172 unsigned attributesAsWritten = 0; 173 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 174 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; 175 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) 176 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; 177 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 178 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; 179 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 180 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; 181 if (Attributes & ObjCDeclSpec::DQ_PR_assign) 182 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; 183 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 184 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; 185 if (Attributes & ObjCDeclSpec::DQ_PR_strong) 186 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; 187 if (Attributes & ObjCDeclSpec::DQ_PR_weak) 188 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; 189 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 190 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; 191 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 192 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; 193 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 194 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; 195 if (Attributes & ObjCDeclSpec::DQ_PR_atomic) 196 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; 197 198 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; 199} 200 201Decl * 202Sema::HandlePropertyInClassExtension(Scope *S, 203 SourceLocation AtLoc, FieldDeclarator &FD, 204 Selector GetterSel, Selector SetterSel, 205 const bool isAssign, 206 const bool isReadWrite, 207 const unsigned Attributes, 208 const unsigned AttributesAsWritten, 209 bool *isOverridingProperty, 210 TypeSourceInfo *T, 211 tok::ObjCKeywordKind MethodImplKind) { 212 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); 213 // Diagnose if this property is already in continuation class. 214 DeclContext *DC = CurContext; 215 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 216 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); 217 218 if (CCPrimary) 219 // Check for duplicate declaration of this property in current and 220 // other class extensions. 221 for (const ObjCCategoryDecl *ClsExtDecl = 222 CCPrimary->getFirstClassExtension(); 223 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) { 224 if (ObjCPropertyDecl *prevDecl = 225 ObjCPropertyDecl::findPropertyDecl(ClsExtDecl, PropertyId)) { 226 Diag(AtLoc, diag::err_duplicate_property); 227 Diag(prevDecl->getLocation(), diag::note_property_declare); 228 return 0; 229 } 230 } 231 232 // Create a new ObjCPropertyDecl with the DeclContext being 233 // the class extension. 234 // FIXME. We should really be using CreatePropertyDecl for this. 235 ObjCPropertyDecl *PDecl = 236 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(), 237 PropertyId, AtLoc, T); 238 PDecl->setPropertyAttributesAsWritten( 239 makePropertyAttributesAsWritten(AttributesAsWritten)); 240 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 241 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 242 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) 243 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 244 // Set setter/getter selector name. Needed later. 245 PDecl->setGetterName(GetterSel); 246 PDecl->setSetterName(SetterSel); 247 ProcessDeclAttributes(S, PDecl, FD.D); 248 DC->addDecl(PDecl); 249 250 // We need to look in the @interface to see if the @property was 251 // already declared. 252 if (!CCPrimary) { 253 Diag(CDecl->getLocation(), diag::err_continuation_class); 254 *isOverridingProperty = true; 255 return 0; 256 } 257 258 // Find the property in continuation class's primary class only. 259 ObjCPropertyDecl *PIDecl = 260 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId); 261 262 if (!PIDecl) { 263 // No matching property found in the primary class. Just fall thru 264 // and add property to continuation class's primary class. 265 ObjCPropertyDecl *PDecl = 266 CreatePropertyDecl(S, CCPrimary, AtLoc, 267 FD, GetterSel, SetterSel, isAssign, isReadWrite, 268 Attributes,AttributesAsWritten, T, MethodImplKind, DC); 269 270 // A case of continuation class adding a new property in the class. This 271 // is not what it was meant for. However, gcc supports it and so should we. 272 // Make sure setter/getters are declared here. 273 ProcessPropertyDecl(PDecl, CCPrimary, /* redeclaredProperty = */ 0, 274 /* lexicalDC = */ CDecl); 275 if (ASTMutationListener *L = Context.getASTMutationListener()) 276 L->AddedObjCPropertyInClassExtension(PDecl, /*OrigProp=*/0, CDecl); 277 return PDecl; 278 } 279 if (PIDecl->getType().getCanonicalType() 280 != PDecl->getType().getCanonicalType()) { 281 Diag(AtLoc, 282 diag::err_type_mismatch_continuation_class) << PDecl->getType(); 283 Diag(PIDecl->getLocation(), diag::note_property_declare); 284 } 285 286 // The property 'PIDecl's readonly attribute will be over-ridden 287 // with continuation class's readwrite property attribute! 288 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten(); 289 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { 290 unsigned retainCopyNonatomic = 291 (ObjCPropertyDecl::OBJC_PR_retain | 292 ObjCPropertyDecl::OBJC_PR_strong | 293 ObjCPropertyDecl::OBJC_PR_copy | 294 ObjCPropertyDecl::OBJC_PR_nonatomic); 295 if ((Attributes & retainCopyNonatomic) != 296 (PIkind & retainCopyNonatomic)) { 297 Diag(AtLoc, diag::warn_property_attr_mismatch); 298 Diag(PIDecl->getLocation(), diag::note_property_declare); 299 } 300 DeclContext *DC = cast<DeclContext>(CCPrimary); 301 if (!ObjCPropertyDecl::findPropertyDecl(DC, 302 PIDecl->getDeclName().getAsIdentifierInfo())) { 303 // Protocol is not in the primary class. Must build one for it. 304 ObjCDeclSpec ProtocolPropertyODS; 305 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind 306 // and ObjCPropertyDecl::PropertyAttributeKind have identical 307 // values. Should consolidate both into one enum type. 308 ProtocolPropertyODS. 309 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind) 310 PIkind); 311 // Must re-establish the context from class extension to primary 312 // class context. 313 ContextRAII SavedContext(*this, CCPrimary); 314 315 Decl *ProtocolPtrTy = 316 ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS, 317 PIDecl->getGetterName(), 318 PIDecl->getSetterName(), 319 isOverridingProperty, 320 MethodImplKind, 321 /* lexicalDC = */ CDecl); 322 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy); 323 } 324 PIDecl->makeitReadWriteAttribute(); 325 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 326 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 327 if (Attributes & ObjCDeclSpec::DQ_PR_strong) 328 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 329 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 330 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 331 PIDecl->setSetterName(SetterSel); 332 } else { 333 // Tailor the diagnostics for the common case where a readwrite 334 // property is declared both in the @interface and the continuation. 335 // This is a common error where the user often intended the original 336 // declaration to be readonly. 337 unsigned diag = 338 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && 339 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) 340 ? diag::err_use_continuation_class_redeclaration_readwrite 341 : diag::err_use_continuation_class; 342 Diag(AtLoc, diag) 343 << CCPrimary->getDeclName(); 344 Diag(PIDecl->getLocation(), diag::note_property_declare); 345 } 346 *isOverridingProperty = true; 347 // Make sure setter decl is synthesized, and added to primary class's list. 348 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl); 349 if (ASTMutationListener *L = Context.getASTMutationListener()) 350 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl); 351 return 0; 352} 353 354ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, 355 ObjCContainerDecl *CDecl, 356 SourceLocation AtLoc, 357 FieldDeclarator &FD, 358 Selector GetterSel, 359 Selector SetterSel, 360 const bool isAssign, 361 const bool isReadWrite, 362 const unsigned Attributes, 363 const unsigned AttributesAsWritten, 364 TypeSourceInfo *TInfo, 365 tok::ObjCKeywordKind MethodImplKind, 366 DeclContext *lexicalDC){ 367 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 368 QualType T = TInfo->getType(); 369 370 // Issue a warning if property is 'assign' as default and its object, which is 371 // gc'able conforms to NSCopying protocol 372 if (getLangOptions().getGC() != LangOptions::NonGC && 373 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) 374 if (const ObjCObjectPointerType *ObjPtrTy = 375 T->getAs<ObjCObjectPointerType>()) { 376 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); 377 if (IDecl) 378 if (ObjCProtocolDecl* PNSCopying = 379 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) 380 if (IDecl->ClassImplementsProtocol(PNSCopying, true)) 381 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; 382 } 383 if (T->isObjCObjectType()) 384 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); 385 386 DeclContext *DC = cast<DeclContext>(CDecl); 387 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, 388 FD.D.getIdentifierLoc(), 389 PropertyId, AtLoc, TInfo); 390 391 if (ObjCPropertyDecl *prevDecl = 392 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) { 393 Diag(PDecl->getLocation(), diag::err_duplicate_property); 394 Diag(prevDecl->getLocation(), diag::note_property_declare); 395 PDecl->setInvalidDecl(); 396 } 397 else { 398 DC->addDecl(PDecl); 399 if (lexicalDC) 400 PDecl->setLexicalDeclContext(lexicalDC); 401 } 402 403 if (T->isArrayType() || T->isFunctionType()) { 404 Diag(AtLoc, diag::err_property_type) << T; 405 PDecl->setInvalidDecl(); 406 } 407 408 ProcessDeclAttributes(S, PDecl, FD.D); 409 410 // Regardless of setter/getter attribute, we save the default getter/setter 411 // selector names in anticipation of declaration of setter/getter methods. 412 PDecl->setGetterName(GetterSel); 413 PDecl->setSetterName(SetterSel); 414 PDecl->setPropertyAttributesAsWritten( 415 makePropertyAttributesAsWritten(AttributesAsWritten)); 416 417 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 418 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 419 420 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 421 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 422 423 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 424 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 425 426 if (isReadWrite) 427 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 428 429 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 430 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 431 432 if (Attributes & ObjCDeclSpec::DQ_PR_strong) 433 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 434 435 if (Attributes & ObjCDeclSpec::DQ_PR_weak) 436 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); 437 438 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 439 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 440 441 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 442 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 443 444 if (isAssign) 445 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 446 447 // In the semantic attributes, one of nonatomic or atomic is always set. 448 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 449 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 450 else 451 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); 452 453 // 'unsafe_unretained' is alias for 'assign'. 454 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 455 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 456 if (isAssign) 457 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 458 459 if (MethodImplKind == tok::objc_required) 460 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 461 else if (MethodImplKind == tok::objc_optional) 462 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 463 464 return PDecl; 465} 466 467static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, 468 ObjCPropertyDecl *property, 469 ObjCIvarDecl *ivar) { 470 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; 471 472 QualType ivarType = ivar->getType(); 473 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); 474 475 // The lifetime implied by the property's attributes. 476 Qualifiers::ObjCLifetime propertyLifetime = 477 getImpliedARCOwnership(property->getPropertyAttributes(), 478 property->getType()); 479 480 // We're fine if they match. 481 if (propertyLifetime == ivarLifetime) return; 482 483 // These aren't valid lifetimes for object ivars; don't diagnose twice. 484 if (ivarLifetime == Qualifiers::OCL_None || 485 ivarLifetime == Qualifiers::OCL_Autoreleasing) 486 return; 487 488 switch (propertyLifetime) { 489 case Qualifiers::OCL_Strong: 490 S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership) 491 << property->getDeclName() 492 << ivar->getDeclName() 493 << ivarLifetime; 494 break; 495 496 case Qualifiers::OCL_Weak: 497 S.Diag(propertyImplLoc, diag::error_weak_property) 498 << property->getDeclName() 499 << ivar->getDeclName(); 500 break; 501 502 case Qualifiers::OCL_ExplicitNone: 503 S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership) 504 << property->getDeclName() 505 << ivar->getDeclName() 506 << ((property->getPropertyAttributesAsWritten() 507 & ObjCPropertyDecl::OBJC_PR_assign) != 0); 508 break; 509 510 case Qualifiers::OCL_Autoreleasing: 511 llvm_unreachable("properties cannot be autoreleasing"); 512 513 case Qualifiers::OCL_None: 514 // Any other property should be ignored. 515 return; 516 } 517 518 S.Diag(property->getLocation(), diag::note_property_declare); 519} 520 521 522/// ActOnPropertyImplDecl - This routine performs semantic checks and 523/// builds the AST node for a property implementation declaration; declared 524/// as @synthesize or @dynamic. 525/// 526Decl *Sema::ActOnPropertyImplDecl(Scope *S, 527 SourceLocation AtLoc, 528 SourceLocation PropertyLoc, 529 bool Synthesize, 530 IdentifierInfo *PropertyId, 531 IdentifierInfo *PropertyIvar, 532 SourceLocation PropertyIvarLoc) { 533 ObjCContainerDecl *ClassImpDecl = 534 dyn_cast<ObjCContainerDecl>(CurContext); 535 // Make sure we have a context for the property implementation declaration. 536 if (!ClassImpDecl) { 537 Diag(AtLoc, diag::error_missing_property_context); 538 return 0; 539 } 540 ObjCPropertyDecl *property = 0; 541 ObjCInterfaceDecl* IDecl = 0; 542 // Find the class or category class where this property must have 543 // a declaration. 544 ObjCImplementationDecl *IC = 0; 545 ObjCCategoryImplDecl* CatImplClass = 0; 546 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 547 IDecl = IC->getClassInterface(); 548 // We always synthesize an interface for an implementation 549 // without an interface decl. So, IDecl is always non-zero. 550 assert(IDecl && 551 "ActOnPropertyImplDecl - @implementation without @interface"); 552 553 // Look for this property declaration in the @implementation's @interface 554 property = IDecl->FindPropertyDeclaration(PropertyId); 555 if (!property) { 556 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 557 return 0; 558 } 559 unsigned PIkind = property->getPropertyAttributesAsWritten(); 560 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | 561 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { 562 if (AtLoc.isValid()) 563 Diag(AtLoc, diag::warn_implicit_atomic_property); 564 else 565 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); 566 Diag(property->getLocation(), diag::note_property_declare); 567 } 568 569 if (const ObjCCategoryDecl *CD = 570 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { 571 if (!CD->IsClassExtension()) { 572 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); 573 Diag(property->getLocation(), diag::note_property_declare); 574 return 0; 575 } 576 } 577 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 578 if (Synthesize) { 579 Diag(AtLoc, diag::error_synthesize_category_decl); 580 return 0; 581 } 582 IDecl = CatImplClass->getClassInterface(); 583 if (!IDecl) { 584 Diag(AtLoc, diag::error_missing_property_interface); 585 return 0; 586 } 587 ObjCCategoryDecl *Category = 588 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 589 590 // If category for this implementation not found, it is an error which 591 // has already been reported eralier. 592 if (!Category) 593 return 0; 594 // Look for this property declaration in @implementation's category 595 property = Category->FindPropertyDeclaration(PropertyId); 596 if (!property) { 597 Diag(PropertyLoc, diag::error_bad_category_property_decl) 598 << Category->getDeclName(); 599 return 0; 600 } 601 } else { 602 Diag(AtLoc, diag::error_bad_property_context); 603 return 0; 604 } 605 ObjCIvarDecl *Ivar = 0; 606 // Check that we have a valid, previously declared ivar for @synthesize 607 if (Synthesize) { 608 // @synthesize 609 if (!PropertyIvar) 610 PropertyIvar = PropertyId; 611 ObjCPropertyDecl::PropertyAttributeKind kind 612 = property->getPropertyAttributes(); 613 QualType PropType = property->getType(); 614 615 QualType PropertyIvarType = PropType.getNonReferenceType(); 616 617 // Add GC __weak to the ivar type if the property is weak. 618 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && 619 getLangOptions().getGC() != LangOptions::NonGC) { 620 assert(!getLangOptions().ObjCAutoRefCount); 621 if (PropertyIvarType.isObjCGCStrong()) { 622 Diag(PropertyLoc, diag::err_gc_weak_property_strong_type); 623 Diag(property->getLocation(), diag::note_property_declare); 624 } else { 625 PropertyIvarType = 626 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); 627 } 628 } 629 630 // Check that this is a previously declared 'ivar' in 'IDecl' interface 631 ObjCInterfaceDecl *ClassDeclared; 632 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); 633 if (!Ivar) { 634 // In ARC, give the ivar a lifetime qualifier based on the 635 // property attributes. 636 if (getLangOptions().ObjCAutoRefCount && 637 !PropertyIvarType.getObjCLifetime() && 638 PropertyIvarType->isObjCRetainableType()) { 639 640 // It's an error if we have to do this and the user didn't 641 // explicitly write an ownership attribute on the property. 642 if (!property->hasWrittenStorageAttribute() && 643 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { 644 Diag(PropertyLoc, 645 diag::err_arc_objc_property_default_assign_on_object); 646 Diag(property->getLocation(), diag::note_property_declare); 647 } else { 648 Qualifiers::ObjCLifetime lifetime = 649 getImpliedARCOwnership(kind, PropertyIvarType); 650 assert(lifetime && "no lifetime for property?"); 651 if (lifetime == Qualifiers::OCL_Weak) { 652 bool err = false; 653 if (const ObjCObjectPointerType *ObjT = 654 PropertyIvarType->getAs<ObjCObjectPointerType>()) 655 if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) { 656 Diag(PropertyLoc, diag::err_arc_weak_unavailable_property); 657 Diag(property->getLocation(), diag::note_property_declare); 658 err = true; 659 } 660 if (!err && !getLangOptions().ObjCRuntimeHasWeak) { 661 Diag(PropertyLoc, diag::err_arc_weak_no_runtime); 662 Diag(property->getLocation(), diag::note_property_declare); 663 } 664 } 665 666 Qualifiers qs; 667 qs.addObjCLifetime(lifetime); 668 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); 669 } 670 } 671 672 if (kind & ObjCPropertyDecl::OBJC_PR_weak && 673 !getLangOptions().ObjCAutoRefCount && 674 getLangOptions().getGC() == LangOptions::NonGC) { 675 Diag(PropertyLoc, diag::error_synthesize_weak_non_arc_or_gc); 676 Diag(property->getLocation(), diag::note_property_declare); 677 } 678 679 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, 680 PropertyLoc, PropertyLoc, PropertyIvar, 681 PropertyIvarType, /*Dinfo=*/0, 682 ObjCIvarDecl::Private, 683 (Expr *)0, true); 684 ClassImpDecl->addDecl(Ivar); 685 IDecl->makeDeclVisibleInContext(Ivar, false); 686 property->setPropertyIvarDecl(Ivar); 687 688 if (!getLangOptions().ObjCNonFragileABI) 689 Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; 690 // Note! I deliberately want it to fall thru so, we have a 691 // a property implementation and to avoid future warnings. 692 } else if (getLangOptions().ObjCNonFragileABI && 693 !declaresSameEntity(ClassDeclared, IDecl)) { 694 Diag(PropertyLoc, diag::error_ivar_in_superclass_use) 695 << property->getDeclName() << Ivar->getDeclName() 696 << ClassDeclared->getDeclName(); 697 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 698 << Ivar << Ivar->getName(); 699 // Note! I deliberately want it to fall thru so more errors are caught. 700 } 701 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 702 703 // Check that type of property and its ivar are type compatible. 704 if (Context.getCanonicalType(PropertyIvarType) != IvarType) { 705 bool compat = false; 706 if (isa<ObjCObjectPointerType>(PropertyIvarType) 707 && isa<ObjCObjectPointerType>(IvarType)) 708 compat = 709 Context.canAssignObjCInterfaces( 710 PropertyIvarType->getAs<ObjCObjectPointerType>(), 711 IvarType->getAs<ObjCObjectPointerType>()); 712 else { 713 SourceLocation Loc = PropertyIvarLoc; 714 if (Loc.isInvalid()) 715 Loc = PropertyLoc; 716 compat = (CheckAssignmentConstraints(Loc, PropertyIvarType, IvarType) 717 == Compatible); 718 } 719 if (!compat) { 720 Diag(PropertyLoc, diag::error_property_ivar_type) 721 << property->getDeclName() << PropType 722 << Ivar->getDeclName() << IvarType; 723 Diag(Ivar->getLocation(), diag::note_ivar_decl); 724 // Note! I deliberately want it to fall thru so, we have a 725 // a property implementation and to avoid future warnings. 726 } 727 728 // FIXME! Rules for properties are somewhat different that those 729 // for assignments. Use a new routine to consolidate all cases; 730 // specifically for property redeclarations as well as for ivars. 731 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); 732 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 733 if (lhsType != rhsType && 734 lhsType->isArithmeticType()) { 735 Diag(PropertyLoc, diag::error_property_ivar_type) 736 << property->getDeclName() << PropType 737 << Ivar->getDeclName() << IvarType; 738 Diag(Ivar->getLocation(), diag::note_ivar_decl); 739 // Fall thru - see previous comment 740 } 741 // __weak is explicit. So it works on Canonical type. 742 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 743 getLangOptions().getGC() != LangOptions::NonGC)) { 744 Diag(PropertyLoc, diag::error_weak_property) 745 << property->getDeclName() << Ivar->getDeclName(); 746 Diag(Ivar->getLocation(), diag::note_ivar_decl); 747 // Fall thru - see previous comment 748 } 749 // Fall thru - see previous comment 750 if ((property->getType()->isObjCObjectPointerType() || 751 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 752 getLangOptions().getGC() != LangOptions::NonGC) { 753 Diag(PropertyLoc, diag::error_strong_property) 754 << property->getDeclName() << Ivar->getDeclName(); 755 // Fall thru - see previous comment 756 } 757 } 758 if (getLangOptions().ObjCAutoRefCount) 759 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); 760 } else if (PropertyIvar) 761 // @dynamic 762 Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); 763 764 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 765 ObjCPropertyImplDecl *PIDecl = 766 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 767 property, 768 (Synthesize ? 769 ObjCPropertyImplDecl::Synthesize 770 : ObjCPropertyImplDecl::Dynamic), 771 Ivar, PropertyIvarLoc); 772 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { 773 getterMethod->createImplicitParams(Context, IDecl); 774 if (getLangOptions().CPlusPlus && Synthesize && 775 Ivar->getType()->isRecordType()) { 776 // For Objective-C++, need to synthesize the AST for the IVAR object to be 777 // returned by the getter as it must conform to C++'s copy-return rules. 778 // FIXME. Eventually we want to do this for Objective-C as well. 779 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); 780 DeclRefExpr *SelfExpr = 781 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), 782 VK_RValue, SourceLocation()); 783 Expr *IvarRefExpr = 784 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, 785 SelfExpr, true, true); 786 ExprResult Res = 787 PerformCopyInitialization(InitializedEntity::InitializeResult( 788 SourceLocation(), 789 getterMethod->getResultType(), 790 /*NRVO=*/false), 791 SourceLocation(), 792 Owned(IvarRefExpr)); 793 if (!Res.isInvalid()) { 794 Expr *ResExpr = Res.takeAs<Expr>(); 795 if (ResExpr) 796 ResExpr = MaybeCreateExprWithCleanups(ResExpr); 797 PIDecl->setGetterCXXConstructor(ResExpr); 798 } 799 } 800 if (property->hasAttr<NSReturnsNotRetainedAttr>() && 801 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { 802 Diag(getterMethod->getLocation(), 803 diag::warn_property_getter_owning_mismatch); 804 Diag(property->getLocation(), diag::note_property_declare); 805 } 806 } 807 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { 808 setterMethod->createImplicitParams(Context, IDecl); 809 if (getLangOptions().CPlusPlus && Synthesize 810 && Ivar->getType()->isRecordType()) { 811 // FIXME. Eventually we want to do this for Objective-C as well. 812 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); 813 DeclRefExpr *SelfExpr = 814 new (Context) DeclRefExpr(SelfDecl, SelfDecl->getType(), 815 VK_RValue, SourceLocation()); 816 Expr *lhs = 817 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), AtLoc, 818 SelfExpr, true, true); 819 ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); 820 ParmVarDecl *Param = (*P); 821 QualType T = Param->getType().getNonReferenceType(); 822 Expr *rhs = new (Context) DeclRefExpr(Param, T, 823 VK_LValue, SourceLocation()); 824 ExprResult Res = BuildBinOp(S, lhs->getLocEnd(), 825 BO_Assign, lhs, rhs); 826 if (property->getPropertyAttributes() & 827 ObjCPropertyDecl::OBJC_PR_atomic) { 828 Expr *callExpr = Res.takeAs<Expr>(); 829 if (const CXXOperatorCallExpr *CXXCE = 830 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) 831 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) 832 if (!FuncDecl->isTrivial()) 833 if (property->getType()->isReferenceType()) { 834 Diag(PropertyLoc, 835 diag::err_atomic_property_nontrivial_assign_op) 836 << property->getType(); 837 Diag(FuncDecl->getLocStart(), 838 diag::note_callee_decl) << FuncDecl; 839 } 840 } 841 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); 842 } 843 } 844 845 if (IC) { 846 if (Synthesize) 847 if (ObjCPropertyImplDecl *PPIDecl = 848 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 849 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 850 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 851 << PropertyIvar; 852 Diag(PPIDecl->getLocation(), diag::note_previous_use); 853 } 854 855 if (ObjCPropertyImplDecl *PPIDecl 856 = IC->FindPropertyImplDecl(PropertyId)) { 857 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 858 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 859 return 0; 860 } 861 IC->addPropertyImplementation(PIDecl); 862 if (getLangOptions().ObjCDefaultSynthProperties && 863 getLangOptions().ObjCNonFragileABI2 && 864 !IDecl->isObjCRequiresPropertyDefs()) { 865 // Diagnose if an ivar was lazily synthesdized due to a previous 866 // use and if 1) property is @dynamic or 2) property is synthesized 867 // but it requires an ivar of different name. 868 ObjCInterfaceDecl *ClassDeclared=0; 869 ObjCIvarDecl *Ivar = 0; 870 if (!Synthesize) 871 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 872 else { 873 if (PropertyIvar && PropertyIvar != PropertyId) 874 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 875 } 876 // Issue diagnostics only if Ivar belongs to current class. 877 if (Ivar && Ivar->getSynthesize() && 878 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { 879 Diag(Ivar->getLocation(), diag::err_undeclared_var_use) 880 << PropertyId; 881 Ivar->setInvalidDecl(); 882 } 883 } 884 } else { 885 if (Synthesize) 886 if (ObjCPropertyImplDecl *PPIDecl = 887 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 888 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 889 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 890 << PropertyIvar; 891 Diag(PPIDecl->getLocation(), diag::note_previous_use); 892 } 893 894 if (ObjCPropertyImplDecl *PPIDecl = 895 CatImplClass->FindPropertyImplDecl(PropertyId)) { 896 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 897 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 898 return 0; 899 } 900 CatImplClass->addPropertyImplementation(PIDecl); 901 } 902 903 return PIDecl; 904} 905 906//===----------------------------------------------------------------------===// 907// Helper methods. 908//===----------------------------------------------------------------------===// 909 910/// DiagnosePropertyMismatch - Compares two properties for their 911/// attributes and types and warns on a variety of inconsistencies. 912/// 913void 914Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 915 ObjCPropertyDecl *SuperProperty, 916 const IdentifierInfo *inheritedName) { 917 ObjCPropertyDecl::PropertyAttributeKind CAttr = 918 Property->getPropertyAttributes(); 919 ObjCPropertyDecl::PropertyAttributeKind SAttr = 920 SuperProperty->getPropertyAttributes(); 921 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 922 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 923 Diag(Property->getLocation(), diag::warn_readonly_property) 924 << Property->getDeclName() << inheritedName; 925 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 926 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 927 Diag(Property->getLocation(), diag::warn_property_attribute) 928 << Property->getDeclName() << "copy" << inheritedName; 929 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ 930 unsigned CAttrRetain = 931 (CAttr & 932 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 933 unsigned SAttrRetain = 934 (SAttr & 935 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 936 bool CStrong = (CAttrRetain != 0); 937 bool SStrong = (SAttrRetain != 0); 938 if (CStrong != SStrong) 939 Diag(Property->getLocation(), diag::warn_property_attribute) 940 << Property->getDeclName() << "retain (or strong)" << inheritedName; 941 } 942 943 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 944 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) 945 Diag(Property->getLocation(), diag::warn_property_attribute) 946 << Property->getDeclName() << "atomic" << inheritedName; 947 if (Property->getSetterName() != SuperProperty->getSetterName()) 948 Diag(Property->getLocation(), diag::warn_property_attribute) 949 << Property->getDeclName() << "setter" << inheritedName; 950 if (Property->getGetterName() != SuperProperty->getGetterName()) 951 Diag(Property->getLocation(), diag::warn_property_attribute) 952 << Property->getDeclName() << "getter" << inheritedName; 953 954 QualType LHSType = 955 Context.getCanonicalType(SuperProperty->getType()); 956 QualType RHSType = 957 Context.getCanonicalType(Property->getType()); 958 959 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { 960 // Do cases not handled in above. 961 // FIXME. For future support of covariant property types, revisit this. 962 bool IncompatibleObjC = false; 963 QualType ConvertedType; 964 if (!isObjCPointerConversion(RHSType, LHSType, 965 ConvertedType, IncompatibleObjC) || 966 IncompatibleObjC) { 967 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) 968 << Property->getType() << SuperProperty->getType() << inheritedName; 969 Diag(SuperProperty->getLocation(), diag::note_property_declare); 970 } 971 } 972} 973 974bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, 975 ObjCMethodDecl *GetterMethod, 976 SourceLocation Loc) { 977 if (GetterMethod && 978 !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(), 979 property->getType().getNonReferenceType())) { 980 AssignConvertType result = Incompatible; 981 if (property->getType()->isObjCObjectPointerType()) 982 result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(), 983 property->getType()); 984 if (result != Compatible) { 985 Diag(Loc, diag::warn_accessor_property_type_mismatch) 986 << property->getDeclName() 987 << GetterMethod->getSelector(); 988 Diag(GetterMethod->getLocation(), diag::note_declared_at); 989 return true; 990 } 991 } 992 return false; 993} 994 995/// ComparePropertiesInBaseAndSuper - This routine compares property 996/// declarations in base and its super class, if any, and issues 997/// diagnostics in a variety of inconsistent situations. 998/// 999void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { 1000 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 1001 if (!SDecl) 1002 return; 1003 // FIXME: O(N^2) 1004 for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), 1005 E = SDecl->prop_end(); S != E; ++S) { 1006 ObjCPropertyDecl *SuperPDecl = (*S); 1007 // Does property in super class has declaration in current class? 1008 for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), 1009 E = IDecl->prop_end(); I != E; ++I) { 1010 ObjCPropertyDecl *PDecl = (*I); 1011 if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) 1012 DiagnosePropertyMismatch(PDecl, SuperPDecl, 1013 SDecl->getIdentifier()); 1014 } 1015 } 1016} 1017 1018/// MatchOneProtocolPropertiesInClass - This routine goes thru the list 1019/// of properties declared in a protocol and compares their attribute against 1020/// the same property declared in the class or category. 1021void 1022Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, 1023 ObjCProtocolDecl *PDecl) { 1024 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 1025 if (!IDecl) { 1026 // Category 1027 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 1028 assert (CatDecl && "MatchOneProtocolPropertiesInClass"); 1029 if (!CatDecl->IsClassExtension()) 1030 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1031 E = PDecl->prop_end(); P != E; ++P) { 1032 ObjCPropertyDecl *Pr = (*P); 1033 ObjCCategoryDecl::prop_iterator CP, CE; 1034 // Is this property already in category's list of properties? 1035 for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP!=CE; ++CP) 1036 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 1037 break; 1038 if (CP != CE) 1039 // Property protocol already exist in class. Diagnose any mismatch. 1040 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 1041 } 1042 return; 1043 } 1044 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1045 E = PDecl->prop_end(); P != E; ++P) { 1046 ObjCPropertyDecl *Pr = (*P); 1047 ObjCInterfaceDecl::prop_iterator CP, CE; 1048 // Is this property already in class's list of properties? 1049 for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) 1050 if ((*CP)->getIdentifier() == Pr->getIdentifier()) 1051 break; 1052 if (CP != CE) 1053 // Property protocol already exist in class. Diagnose any mismatch. 1054 DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); 1055 } 1056} 1057 1058/// CompareProperties - This routine compares properties 1059/// declared in 'ClassOrProtocol' objects (which can be a class or an 1060/// inherited protocol with the list of properties for class/category 'CDecl' 1061/// 1062void Sema::CompareProperties(Decl *CDecl, Decl *ClassOrProtocol) { 1063 Decl *ClassDecl = ClassOrProtocol; 1064 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); 1065 1066 if (!IDecl) { 1067 // Category 1068 ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); 1069 assert (CatDecl && "CompareProperties"); 1070 if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 1071 for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), 1072 E = MDecl->protocol_end(); P != E; ++P) 1073 // Match properties of category with those of protocol (*P) 1074 MatchOneProtocolPropertiesInClass(CatDecl, *P); 1075 1076 // Go thru the list of protocols for this category and recursively match 1077 // their properties with those in the category. 1078 for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), 1079 E = CatDecl->protocol_end(); P != E; ++P) 1080 CompareProperties(CatDecl, *P); 1081 } else { 1082 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 1083 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 1084 E = MD->protocol_end(); P != E; ++P) 1085 MatchOneProtocolPropertiesInClass(CatDecl, *P); 1086 } 1087 return; 1088 } 1089 1090 if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 1091 for (ObjCInterfaceDecl::all_protocol_iterator 1092 P = MDecl->all_referenced_protocol_begin(), 1093 E = MDecl->all_referenced_protocol_end(); P != E; ++P) 1094 // Match properties of class IDecl with those of protocol (*P). 1095 MatchOneProtocolPropertiesInClass(IDecl, *P); 1096 1097 // Go thru the list of protocols for this class and recursively match 1098 // their properties with those declared in the class. 1099 for (ObjCInterfaceDecl::all_protocol_iterator 1100 P = IDecl->all_referenced_protocol_begin(), 1101 E = IDecl->all_referenced_protocol_end(); P != E; ++P) 1102 CompareProperties(IDecl, *P); 1103 } else { 1104 ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); 1105 for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), 1106 E = MD->protocol_end(); P != E; ++P) 1107 MatchOneProtocolPropertiesInClass(IDecl, *P); 1108 } 1109} 1110 1111/// isPropertyReadonly - Return true if property is readonly, by searching 1112/// for the property in the class and in its categories and implementations 1113/// 1114bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, 1115 ObjCInterfaceDecl *IDecl) { 1116 // by far the most common case. 1117 if (!PDecl->isReadOnly()) 1118 return false; 1119 // Even if property is ready only, if interface has a user defined setter, 1120 // it is not considered read only. 1121 if (IDecl->getInstanceMethod(PDecl->getSetterName())) 1122 return false; 1123 1124 // Main class has the property as 'readonly'. Must search 1125 // through the category list to see if the property's 1126 // attribute has been over-ridden to 'readwrite'. 1127 for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); 1128 Category; Category = Category->getNextClassCategory()) { 1129 // Even if property is ready only, if a category has a user defined setter, 1130 // it is not considered read only. 1131 if (Category->getInstanceMethod(PDecl->getSetterName())) 1132 return false; 1133 ObjCPropertyDecl *P = 1134 Category->FindPropertyDeclaration(PDecl->getIdentifier()); 1135 if (P && !P->isReadOnly()) 1136 return false; 1137 } 1138 1139 // Also, check for definition of a setter method in the implementation if 1140 // all else failed. 1141 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { 1142 if (ObjCImplementationDecl *IMD = 1143 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { 1144 if (IMD->getInstanceMethod(PDecl->getSetterName())) 1145 return false; 1146 } else if (ObjCCategoryImplDecl *CIMD = 1147 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { 1148 if (CIMD->getInstanceMethod(PDecl->getSetterName())) 1149 return false; 1150 } 1151 } 1152 // Lastly, look through the implementation (if one is in scope). 1153 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) 1154 if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) 1155 return false; 1156 // If all fails, look at the super class. 1157 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) 1158 return isPropertyReadonly(PDecl, SIDecl); 1159 return true; 1160} 1161 1162/// CollectImmediateProperties - This routine collects all properties in 1163/// the class and its conforming protocols; but not those it its super class. 1164void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, 1165 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap, 1166 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap) { 1167 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { 1168 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), 1169 E = IDecl->prop_end(); P != E; ++P) { 1170 ObjCPropertyDecl *Prop = (*P); 1171 PropMap[Prop->getIdentifier()] = Prop; 1172 } 1173 // scan through class's protocols. 1174 for (ObjCInterfaceDecl::all_protocol_iterator 1175 PI = IDecl->all_referenced_protocol_begin(), 1176 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) 1177 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1178 } 1179 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1180 if (!CATDecl->IsClassExtension()) 1181 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), 1182 E = CATDecl->prop_end(); P != E; ++P) { 1183 ObjCPropertyDecl *Prop = (*P); 1184 PropMap[Prop->getIdentifier()] = Prop; 1185 } 1186 // scan through class's protocols. 1187 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), 1188 E = CATDecl->protocol_end(); PI != E; ++PI) 1189 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1190 } 1191 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { 1192 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1193 E = PDecl->prop_end(); P != E; ++P) { 1194 ObjCPropertyDecl *Prop = (*P); 1195 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; 1196 // Exclude property for protocols which conform to class's super-class, 1197 // as super-class has to implement the property. 1198 if (!PropertyFromSuper || 1199 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { 1200 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; 1201 if (!PropEntry) 1202 PropEntry = Prop; 1203 } 1204 } 1205 // scan through protocol's protocols. 1206 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 1207 E = PDecl->protocol_end(); PI != E; ++PI) 1208 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1209 } 1210} 1211 1212/// CollectClassPropertyImplementations - This routine collects list of 1213/// properties to be implemented in the class. This includes, class's 1214/// and its conforming protocols' properties. 1215static void CollectClassPropertyImplementations(ObjCContainerDecl *CDecl, 1216 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { 1217 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { 1218 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), 1219 E = IDecl->prop_end(); P != E; ++P) { 1220 ObjCPropertyDecl *Prop = (*P); 1221 PropMap[Prop->getIdentifier()] = Prop; 1222 } 1223 for (ObjCInterfaceDecl::all_protocol_iterator 1224 PI = IDecl->all_referenced_protocol_begin(), 1225 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) 1226 CollectClassPropertyImplementations((*PI), PropMap); 1227 } 1228 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { 1229 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1230 E = PDecl->prop_end(); P != E; ++P) { 1231 ObjCPropertyDecl *Prop = (*P); 1232 PropMap[Prop->getIdentifier()] = Prop; 1233 } 1234 // scan through protocol's protocols. 1235 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 1236 E = PDecl->protocol_end(); PI != E; ++PI) 1237 CollectClassPropertyImplementations((*PI), PropMap); 1238 } 1239} 1240 1241/// CollectSuperClassPropertyImplementations - This routine collects list of 1242/// properties to be implemented in super class(s) and also coming from their 1243/// conforming protocols. 1244static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, 1245 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { 1246 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { 1247 while (SDecl) { 1248 CollectClassPropertyImplementations(SDecl, PropMap); 1249 SDecl = SDecl->getSuperClass(); 1250 } 1251 } 1252} 1253 1254/// LookupPropertyDecl - Looks up a property in the current class and all 1255/// its protocols. 1256ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, 1257 IdentifierInfo *II) { 1258 if (const ObjCInterfaceDecl *IDecl = 1259 dyn_cast<ObjCInterfaceDecl>(CDecl)) { 1260 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), 1261 E = IDecl->prop_end(); P != E; ++P) { 1262 ObjCPropertyDecl *Prop = (*P); 1263 if (Prop->getIdentifier() == II) 1264 return Prop; 1265 } 1266 // scan through class's protocols. 1267 for (ObjCInterfaceDecl::all_protocol_iterator 1268 PI = IDecl->all_referenced_protocol_begin(), 1269 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) { 1270 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); 1271 if (Prop) 1272 return Prop; 1273 } 1274 } 1275 else if (const ObjCProtocolDecl *PDecl = 1276 dyn_cast<ObjCProtocolDecl>(CDecl)) { 1277 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1278 E = PDecl->prop_end(); P != E; ++P) { 1279 ObjCPropertyDecl *Prop = (*P); 1280 if (Prop->getIdentifier() == II) 1281 return Prop; 1282 } 1283 // scan through protocol's protocols. 1284 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 1285 E = PDecl->protocol_end(); PI != E; ++PI) { 1286 ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); 1287 if (Prop) 1288 return Prop; 1289 } 1290 } 1291 return 0; 1292} 1293 1294static IdentifierInfo * getDefaultSynthIvarName(ObjCPropertyDecl *Prop, 1295 ASTContext &Ctx) { 1296 llvm::SmallString<128> ivarName; 1297 { 1298 llvm::raw_svector_ostream os(ivarName); 1299 os << '_' << Prop->getIdentifier()->getName(); 1300 } 1301 return &Ctx.Idents.get(ivarName.str()); 1302} 1303 1304/// DefaultSynthesizeProperties - This routine default synthesizes all 1305/// properties which must be synthesized in class's @implementation. 1306void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, 1307 ObjCInterfaceDecl *IDecl) { 1308 1309 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; 1310 CollectClassPropertyImplementations(IDecl, PropMap); 1311 if (PropMap.empty()) 1312 return; 1313 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; 1314 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); 1315 1316 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator 1317 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { 1318 ObjCPropertyDecl *Prop = P->second; 1319 // If property to be implemented in the super class, ignore. 1320 if (SuperPropMap[Prop->getIdentifier()]) 1321 continue; 1322 // Is there a matching propery synthesize/dynamic? 1323 if (Prop->isInvalidDecl() || 1324 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || 1325 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) 1326 continue; 1327 // Property may have been synthesized by user. 1328 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) 1329 continue; 1330 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { 1331 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) 1332 continue; 1333 if (IMPDecl->getInstanceMethod(Prop->getSetterName())) 1334 continue; 1335 } 1336 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) { 1337 // We won't auto-synthesize properties declared in protocols. 1338 Diag(IMPDecl->getLocation(), 1339 diag::warn_auto_synthesizing_protocol_property); 1340 Diag(Prop->getLocation(), diag::note_property_declare); 1341 continue; 1342 } 1343 1344 // We use invalid SourceLocations for the synthesized ivars since they 1345 // aren't really synthesized at a particular location; they just exist. 1346 // Saying that they are located at the @implementation isn't really going 1347 // to help users. 1348 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), 1349 true, 1350 /* property = */ Prop->getIdentifier(), 1351 /* ivar = */ getDefaultSynthIvarName(Prop, Context), 1352 SourceLocation()); 1353 } 1354} 1355 1356void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { 1357 if (!LangOpts.ObjCDefaultSynthProperties || !LangOpts.ObjCNonFragileABI2) 1358 return; 1359 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); 1360 if (!IC) 1361 return; 1362 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) 1363 if (!IDecl->isObjCRequiresPropertyDefs()) 1364 DefaultSynthesizeProperties(S, IC, IDecl); 1365} 1366 1367void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, 1368 ObjCContainerDecl *CDecl, 1369 const llvm::DenseSet<Selector>& InsMap) { 1370 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> SuperPropMap; 1371 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1372 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); 1373 1374 llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; 1375 CollectImmediateProperties(CDecl, PropMap, SuperPropMap); 1376 if (PropMap.empty()) 1377 return; 1378 1379 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; 1380 for (ObjCImplDecl::propimpl_iterator 1381 I = IMPDecl->propimpl_begin(), 1382 EI = IMPDecl->propimpl_end(); I != EI; ++I) 1383 PropImplMap.insert((*I)->getPropertyDecl()); 1384 1385 for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator 1386 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { 1387 ObjCPropertyDecl *Prop = P->second; 1388 // Is there a matching propery synthesize/dynamic? 1389 if (Prop->isInvalidDecl() || 1390 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || 1391 PropImplMap.count(Prop) || Prop->hasAttr<UnavailableAttr>()) 1392 continue; 1393 if (!InsMap.count(Prop->getGetterName())) { 1394 Diag(IMPDecl->getLocation(), 1395 isa<ObjCCategoryDecl>(CDecl) ? 1396 diag::warn_setter_getter_impl_required_in_category : 1397 diag::warn_setter_getter_impl_required) 1398 << Prop->getDeclName() << Prop->getGetterName(); 1399 Diag(Prop->getLocation(), 1400 diag::note_property_declare); 1401 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) 1402 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1403 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1404 Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1405 1406 } 1407 1408 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { 1409 Diag(IMPDecl->getLocation(), 1410 isa<ObjCCategoryDecl>(CDecl) ? 1411 diag::warn_setter_getter_impl_required_in_category : 1412 diag::warn_setter_getter_impl_required) 1413 << Prop->getDeclName() << Prop->getSetterName(); 1414 Diag(Prop->getLocation(), 1415 diag::note_property_declare); 1416 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) 1417 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1418 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1419 Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1420 } 1421 } 1422} 1423 1424void 1425Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, 1426 ObjCContainerDecl* IDecl) { 1427 // Rules apply in non-GC mode only 1428 if (getLangOptions().getGC() != LangOptions::NonGC) 1429 return; 1430 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), 1431 E = IDecl->prop_end(); 1432 I != E; ++I) { 1433 ObjCPropertyDecl *Property = (*I); 1434 ObjCMethodDecl *GetterMethod = 0; 1435 ObjCMethodDecl *SetterMethod = 0; 1436 bool LookedUpGetterSetter = false; 1437 1438 unsigned Attributes = Property->getPropertyAttributes(); 1439 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); 1440 1441 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && 1442 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { 1443 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); 1444 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); 1445 LookedUpGetterSetter = true; 1446 if (GetterMethod) { 1447 Diag(GetterMethod->getLocation(), 1448 diag::warn_default_atomic_custom_getter_setter) 1449 << Property->getIdentifier() << 0; 1450 Diag(Property->getLocation(), diag::note_property_declare); 1451 } 1452 if (SetterMethod) { 1453 Diag(SetterMethod->getLocation(), 1454 diag::warn_default_atomic_custom_getter_setter) 1455 << Property->getIdentifier() << 1; 1456 Diag(Property->getLocation(), diag::note_property_declare); 1457 } 1458 } 1459 1460 // We only care about readwrite atomic property. 1461 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || 1462 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) 1463 continue; 1464 if (const ObjCPropertyImplDecl *PIDecl 1465 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { 1466 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) 1467 continue; 1468 if (!LookedUpGetterSetter) { 1469 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); 1470 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); 1471 LookedUpGetterSetter = true; 1472 } 1473 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { 1474 SourceLocation MethodLoc = 1475 (GetterMethod ? GetterMethod->getLocation() 1476 : SetterMethod->getLocation()); 1477 Diag(MethodLoc, diag::warn_atomic_property_rule) 1478 << Property->getIdentifier() << (GetterMethod != 0) 1479 << (SetterMethod != 0); 1480 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); 1481 Diag(Property->getLocation(), diag::note_property_declare); 1482 } 1483 } 1484 } 1485} 1486 1487void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { 1488 if (getLangOptions().getGC() == LangOptions::GCOnly) 1489 return; 1490 1491 for (ObjCImplementationDecl::propimpl_iterator 1492 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1493 ObjCPropertyImplDecl *PID = *i; 1494 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) 1495 continue; 1496 1497 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1498 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && 1499 !D->getInstanceMethod(PD->getGetterName())) { 1500 ObjCMethodDecl *method = PD->getGetterMethodDecl(); 1501 if (!method) 1502 continue; 1503 ObjCMethodFamily family = method->getMethodFamily(); 1504 if (family == OMF_alloc || family == OMF_copy || 1505 family == OMF_mutableCopy || family == OMF_new) { 1506 if (getLangOptions().ObjCAutoRefCount) 1507 Diag(PID->getLocation(), diag::err_ownin_getter_rule); 1508 else 1509 Diag(PID->getLocation(), diag::warn_owning_getter_rule); 1510 Diag(PD->getLocation(), diag::note_property_declare); 1511 } 1512 } 1513 } 1514} 1515 1516/// AddPropertyAttrs - Propagates attributes from a property to the 1517/// implicitly-declared getter or setter for that property. 1518static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, 1519 ObjCPropertyDecl *Property) { 1520 // Should we just clone all attributes over? 1521 for (Decl::attr_iterator A = Property->attr_begin(), 1522 AEnd = Property->attr_end(); 1523 A != AEnd; ++A) { 1524 if (isa<DeprecatedAttr>(*A) || 1525 isa<UnavailableAttr>(*A) || 1526 isa<AvailabilityAttr>(*A)) 1527 PropertyMethod->addAttr((*A)->clone(S.Context)); 1528 } 1529} 1530 1531/// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 1532/// have the property type and issue diagnostics if they don't. 1533/// Also synthesize a getter/setter method if none exist (and update the 1534/// appropriate lookup tables. FIXME: Should reconsider if adding synthesized 1535/// methods is the "right" thing to do. 1536void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, 1537 ObjCContainerDecl *CD, 1538 ObjCPropertyDecl *redeclaredProperty, 1539 ObjCContainerDecl *lexicalDC) { 1540 1541 ObjCMethodDecl *GetterMethod, *SetterMethod; 1542 1543 GetterMethod = CD->getInstanceMethod(property->getGetterName()); 1544 SetterMethod = CD->getInstanceMethod(property->getSetterName()); 1545 DiagnosePropertyAccessorMismatch(property, GetterMethod, 1546 property->getLocation()); 1547 1548 if (SetterMethod) { 1549 ObjCPropertyDecl::PropertyAttributeKind CAttr = 1550 property->getPropertyAttributes(); 1551 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && 1552 Context.getCanonicalType(SetterMethod->getResultType()) != 1553 Context.VoidTy) 1554 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 1555 if (SetterMethod->param_size() != 1 || 1556 !Context.hasSameUnqualifiedType( 1557 (*SetterMethod->param_begin())->getType().getNonReferenceType(), 1558 property->getType().getNonReferenceType())) { 1559 Diag(property->getLocation(), 1560 diag::warn_accessor_property_type_mismatch) 1561 << property->getDeclName() 1562 << SetterMethod->getSelector(); 1563 Diag(SetterMethod->getLocation(), diag::note_declared_at); 1564 } 1565 } 1566 1567 // Synthesize getter/setter methods if none exist. 1568 // Find the default getter and if one not found, add one. 1569 // FIXME: The synthesized property we set here is misleading. We almost always 1570 // synthesize these methods unless the user explicitly provided prototypes 1571 // (which is odd, but allowed). Sema should be typechecking that the 1572 // declarations jive in that situation (which it is not currently). 1573 if (!GetterMethod) { 1574 // No instance method of same name as property getter name was found. 1575 // Declare a getter method and add it to the list of methods 1576 // for this class. 1577 SourceLocation Loc = redeclaredProperty ? 1578 redeclaredProperty->getLocation() : 1579 property->getLocation(); 1580 1581 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, 1582 property->getGetterName(), 1583 property->getType(), 0, CD, /*isInstance=*/true, 1584 /*isVariadic=*/false, /*isSynthesized=*/true, 1585 /*isImplicitlyDeclared=*/true, /*isDefined=*/false, 1586 (property->getPropertyImplementation() == 1587 ObjCPropertyDecl::Optional) ? 1588 ObjCMethodDecl::Optional : 1589 ObjCMethodDecl::Required); 1590 CD->addDecl(GetterMethod); 1591 1592 AddPropertyAttrs(*this, GetterMethod, property); 1593 1594 // FIXME: Eventually this shouldn't be needed, as the lexical context 1595 // and the real context should be the same. 1596 if (lexicalDC) 1597 GetterMethod->setLexicalDeclContext(lexicalDC); 1598 if (property->hasAttr<NSReturnsNotRetainedAttr>()) 1599 GetterMethod->addAttr( 1600 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); 1601 } else 1602 // A user declared getter will be synthesize when @synthesize of 1603 // the property with the same name is seen in the @implementation 1604 GetterMethod->setSynthesized(true); 1605 property->setGetterMethodDecl(GetterMethod); 1606 1607 // Skip setter if property is read-only. 1608 if (!property->isReadOnly()) { 1609 // Find the default setter and if one not found, add one. 1610 if (!SetterMethod) { 1611 // No instance method of same name as property setter name was found. 1612 // Declare a setter method and add it to the list of methods 1613 // for this class. 1614 SourceLocation Loc = redeclaredProperty ? 1615 redeclaredProperty->getLocation() : 1616 property->getLocation(); 1617 1618 SetterMethod = 1619 ObjCMethodDecl::Create(Context, Loc, Loc, 1620 property->getSetterName(), Context.VoidTy, 0, 1621 CD, /*isInstance=*/true, /*isVariadic=*/false, 1622 /*isSynthesized=*/true, 1623 /*isImplicitlyDeclared=*/true, 1624 /*isDefined=*/false, 1625 (property->getPropertyImplementation() == 1626 ObjCPropertyDecl::Optional) ? 1627 ObjCMethodDecl::Optional : 1628 ObjCMethodDecl::Required); 1629 1630 // Invent the arguments for the setter. We don't bother making a 1631 // nice name for the argument. 1632 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 1633 Loc, Loc, 1634 property->getIdentifier(), 1635 property->getType().getUnqualifiedType(), 1636 /*TInfo=*/0, 1637 SC_None, 1638 SC_None, 1639 0); 1640 SetterMethod->setMethodParams(Context, Argument, 1641 ArrayRef<SourceLocation>()); 1642 1643 AddPropertyAttrs(*this, SetterMethod, property); 1644 1645 CD->addDecl(SetterMethod); 1646 // FIXME: Eventually this shouldn't be needed, as the lexical context 1647 // and the real context should be the same. 1648 if (lexicalDC) 1649 SetterMethod->setLexicalDeclContext(lexicalDC); 1650 } else 1651 // A user declared setter will be synthesize when @synthesize of 1652 // the property with the same name is seen in the @implementation 1653 SetterMethod->setSynthesized(true); 1654 property->setSetterMethodDecl(SetterMethod); 1655 } 1656 // Add any synthesized methods to the global pool. This allows us to 1657 // handle the following, which is supported by GCC (and part of the design). 1658 // 1659 // @interface Foo 1660 // @property double bar; 1661 // @end 1662 // 1663 // void thisIsUnfortunate() { 1664 // id foo; 1665 // double bar = [foo bar]; 1666 // } 1667 // 1668 if (GetterMethod) 1669 AddInstanceMethodToGlobalPool(GetterMethod); 1670 if (SetterMethod) 1671 AddInstanceMethodToGlobalPool(SetterMethod); 1672} 1673 1674void Sema::CheckObjCPropertyAttributes(Decl *PDecl, 1675 SourceLocation Loc, 1676 unsigned &Attributes) { 1677 // FIXME: Improve the reported location. 1678 if (!PDecl || PDecl->isInvalidDecl()) 1679 return; 1680 1681 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); 1682 QualType PropertyTy = PropertyDecl->getType(); 1683 1684 // readonly and readwrite/assign/retain/copy conflict. 1685 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1686 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 1687 ObjCDeclSpec::DQ_PR_assign | 1688 ObjCDeclSpec::DQ_PR_unsafe_unretained | 1689 ObjCDeclSpec::DQ_PR_copy | 1690 ObjCDeclSpec::DQ_PR_retain | 1691 ObjCDeclSpec::DQ_PR_strong))) { 1692 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 1693 "readwrite" : 1694 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 1695 "assign" : 1696 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? 1697 "unsafe_unretained" : 1698 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 1699 "copy" : "retain"; 1700 1701 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 1702 diag::err_objc_property_attr_mutually_exclusive : 1703 diag::warn_objc_property_attr_mutually_exclusive) 1704 << "readonly" << which; 1705 } 1706 1707 // Check for copy or retain on non-object types. 1708 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 1709 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && 1710 !PropertyTy->isObjCRetainableType() && 1711 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { 1712 Diag(Loc, diag::err_objc_property_requires_object) 1713 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : 1714 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); 1715 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 1716 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); 1717 } 1718 1719 // Check for more than one of { assign, copy, retain }. 1720 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 1721 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1722 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1723 << "assign" << "copy"; 1724 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1725 } 1726 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1727 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1728 << "assign" << "retain"; 1729 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1730 } 1731 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 1732 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1733 << "assign" << "strong"; 1734 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 1735 } 1736 if (getLangOptions().ObjCAutoRefCount && 1737 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 1738 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1739 << "assign" << "weak"; 1740 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 1741 } 1742 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { 1743 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1744 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1745 << "unsafe_unretained" << "copy"; 1746 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 1747 } 1748 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1749 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1750 << "unsafe_unretained" << "retain"; 1751 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1752 } 1753 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 1754 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1755 << "unsafe_unretained" << "strong"; 1756 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 1757 } 1758 if (getLangOptions().ObjCAutoRefCount && 1759 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 1760 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1761 << "unsafe_unretained" << "weak"; 1762 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 1763 } 1764 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 1765 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 1766 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1767 << "copy" << "retain"; 1768 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1769 } 1770 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 1771 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1772 << "copy" << "strong"; 1773 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 1774 } 1775 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 1776 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1777 << "copy" << "weak"; 1778 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 1779 } 1780 } 1781 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 1782 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 1783 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1784 << "retain" << "weak"; 1785 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 1786 } 1787 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && 1788 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 1789 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1790 << "strong" << "weak"; 1791 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 1792 } 1793 1794 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && 1795 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { 1796 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 1797 << "atomic" << "nonatomic"; 1798 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; 1799 } 1800 1801 // Warn if user supplied no assignment attribute, property is 1802 // readwrite, and this is an object type. 1803 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 1804 ObjCDeclSpec::DQ_PR_unsafe_unretained | 1805 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | 1806 ObjCDeclSpec::DQ_PR_weak)) && 1807 PropertyTy->isObjCObjectPointerType()) { 1808 if (getLangOptions().ObjCAutoRefCount) 1809 // With arc, @property definitions should default to (strong) when 1810 // not specified; including when property is 'readonly'. 1811 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 1812 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) { 1813 bool isAnyClassTy = 1814 (PropertyTy->isObjCClassType() || 1815 PropertyTy->isObjCQualifiedClassType()); 1816 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to 1817 // issue any warning. 1818 if (isAnyClassTy && getLangOptions().getGC() == LangOptions::NonGC) 1819 ; 1820 else { 1821 // Skip this warning in gc-only mode. 1822 if (getLangOptions().getGC() != LangOptions::GCOnly) 1823 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 1824 1825 // If non-gc code warn that this is likely inappropriate. 1826 if (getLangOptions().getGC() == LangOptions::NonGC) 1827 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 1828 } 1829 } 1830 1831 // FIXME: Implement warning dependent on NSCopying being 1832 // implemented. See also: 1833 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 1834 // (please trim this list while you are at it). 1835 } 1836 1837 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 1838 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) 1839 && getLangOptions().getGC() == LangOptions::GCOnly 1840 && PropertyTy->isBlockPointerType()) 1841 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 1842 else if (getLangOptions().ObjCAutoRefCount && 1843 (Attributes & ObjCDeclSpec::DQ_PR_retain) && 1844 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1845 !(Attributes & ObjCDeclSpec::DQ_PR_strong) && 1846 PropertyTy->isBlockPointerType()) 1847 Diag(Loc, diag::warn_objc_property_retain_of_block); 1848 1849 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 1850 (Attributes & ObjCDeclSpec::DQ_PR_setter)) 1851 Diag(Loc, diag::warn_objc_readonly_property_has_setter); 1852 1853} 1854