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/AST/ASTMutationListener.h" 17 #include "clang/AST/DeclObjC.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/ExprObjC.h" 20 #include "clang/Basic/SourceManager.h" 21 #include "clang/Lex/Lexer.h" 22 #include "clang/Lex/Preprocessor.h" 23 #include "clang/Sema/Initialization.h" 24 #include "llvm/ADT/DenseSet.h" 25 #include "llvm/ADT/SmallString.h" 26 27 using namespace clang; 28 29 //===----------------------------------------------------------------------===// 30 // Grammar actions. 31 //===----------------------------------------------------------------------===// 32 33 /// getImpliedARCOwnership - Given a set of property attributes and a 34 /// type, infer an expected lifetime. The type's ownership qualification 35 /// is not considered. 36 /// 37 /// Returns OCL_None if the attributes as stated do not imply an ownership. 38 /// Never returns OCL_Autoreleasing. 39 static Qualifiers::ObjCLifetime getImpliedARCOwnership( 40 ObjCPropertyDecl::PropertyAttributeKind attrs, 41 QualType type) { 42 // retain, strong, copy, weak, and unsafe_unretained are only legal 43 // on properties of retainable pointer type. 44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain | 45 ObjCPropertyDecl::OBJC_PR_strong | 46 ObjCPropertyDecl::OBJC_PR_copy)) { 47 return Qualifiers::OCL_Strong; 48 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) { 49 return Qualifiers::OCL_Weak; 50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) { 51 return Qualifiers::OCL_ExplicitNone; 52 } 53 54 // assign can appear on other types, so we have to check the 55 // property type. 56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign && 57 type->isObjCRetainableType()) { 58 return Qualifiers::OCL_ExplicitNone; 59 } 60 61 return Qualifiers::OCL_None; 62 } 63 64 /// Check the internal consistency of a property declaration with 65 /// an explicit ownership qualifier. 66 static void checkPropertyDeclWithOwnership(Sema &S, 67 ObjCPropertyDecl *property) { 68 if (property->isInvalidDecl()) return; 69 70 ObjCPropertyDecl::PropertyAttributeKind propertyKind 71 = property->getPropertyAttributes(); 72 Qualifiers::ObjCLifetime propertyLifetime 73 = property->getType().getObjCLifetime(); 74 75 assert(propertyLifetime != Qualifiers::OCL_None); 76 77 Qualifiers::ObjCLifetime expectedLifetime 78 = getImpliedARCOwnership(propertyKind, property->getType()); 79 if (!expectedLifetime) { 80 // We have a lifetime qualifier but no dominating property 81 // attribute. That's okay, but restore reasonable invariants by 82 // setting the property attribute according to the lifetime 83 // qualifier. 84 ObjCPropertyDecl::PropertyAttributeKind attr; 85 if (propertyLifetime == Qualifiers::OCL_Strong) { 86 attr = ObjCPropertyDecl::OBJC_PR_strong; 87 } else if (propertyLifetime == Qualifiers::OCL_Weak) { 88 attr = ObjCPropertyDecl::OBJC_PR_weak; 89 } else { 90 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); 91 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained; 92 } 93 property->setPropertyAttributes(attr); 94 return; 95 } 96 97 if (propertyLifetime == expectedLifetime) return; 98 99 property->setInvalidDecl(); 100 S.Diag(property->getLocation(), 101 diag::err_arc_inconsistent_property_ownership) 102 << property->getDeclName() 103 << expectedLifetime 104 << propertyLifetime; 105 } 106 107 /// \brief Check this Objective-C property against a property declared in the 108 /// given protocol. 109 static void 110 CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop, 111 ObjCProtocolDecl *Proto, 112 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) { 113 // Have we seen this protocol before? 114 if (!Known.insert(Proto).second) 115 return; 116 117 // Look for a property with the same name. 118 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName()); 119 for (unsigned I = 0, N = R.size(); I != N; ++I) { 120 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) { 121 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true); 122 return; 123 } 124 } 125 126 // Check this property against any protocols we inherit. 127 for (auto *P : Proto->protocols()) 128 CheckPropertyAgainstProtocol(S, Prop, P, Known); 129 } 130 131 static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) { 132 // In GC mode, just look for the __weak qualifier. 133 if (S.getLangOpts().getGC() != LangOptions::NonGC) { 134 if (T.isObjCGCWeak()) return ObjCDeclSpec::DQ_PR_weak; 135 136 // In ARC/MRC, look for an explicit ownership qualifier. 137 // For some reason, this only applies to __weak. 138 } else if (auto ownership = T.getObjCLifetime()) { 139 switch (ownership) { 140 case Qualifiers::OCL_Weak: 141 return ObjCDeclSpec::DQ_PR_weak; 142 case Qualifiers::OCL_Strong: 143 return ObjCDeclSpec::DQ_PR_strong; 144 case Qualifiers::OCL_ExplicitNone: 145 return ObjCDeclSpec::DQ_PR_unsafe_unretained; 146 case Qualifiers::OCL_Autoreleasing: 147 case Qualifiers::OCL_None: 148 return 0; 149 } 150 llvm_unreachable("bad qualifier"); 151 } 152 153 return 0; 154 } 155 156 static const unsigned OwnershipMask = 157 (ObjCPropertyDecl::OBJC_PR_assign | 158 ObjCPropertyDecl::OBJC_PR_retain | 159 ObjCPropertyDecl::OBJC_PR_copy | 160 ObjCPropertyDecl::OBJC_PR_weak | 161 ObjCPropertyDecl::OBJC_PR_strong | 162 ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 163 164 static unsigned getOwnershipRule(unsigned attr) { 165 unsigned result = attr & OwnershipMask; 166 167 // From an ownership perspective, assign and unsafe_unretained are 168 // identical; make sure one also implies the other. 169 if (result & (ObjCPropertyDecl::OBJC_PR_assign | 170 ObjCPropertyDecl::OBJC_PR_unsafe_unretained)) { 171 result |= ObjCPropertyDecl::OBJC_PR_assign | 172 ObjCPropertyDecl::OBJC_PR_unsafe_unretained; 173 } 174 175 return result; 176 } 177 178 Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, 179 SourceLocation LParenLoc, 180 FieldDeclarator &FD, 181 ObjCDeclSpec &ODS, 182 Selector GetterSel, 183 Selector SetterSel, 184 tok::ObjCKeywordKind MethodImplKind, 185 DeclContext *lexicalDC) { 186 unsigned Attributes = ODS.getPropertyAttributes(); 187 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0); 188 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S); 189 QualType T = TSI->getType(); 190 if (!getOwnershipRule(Attributes)) { 191 Attributes |= deducePropertyOwnershipFromType(*this, T); 192 } 193 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || 194 // default is readwrite! 195 !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); 196 197 // Proceed with constructing the ObjCPropertyDecls. 198 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext); 199 ObjCPropertyDecl *Res = nullptr; 200 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 201 if (CDecl->IsClassExtension()) { 202 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, 203 FD, GetterSel, SetterSel, 204 isReadWrite, 205 Attributes, 206 ODS.getPropertyAttributes(), 207 T, TSI, MethodImplKind); 208 if (!Res) 209 return nullptr; 210 } 211 } 212 213 if (!Res) { 214 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD, 215 GetterSel, SetterSel, isReadWrite, 216 Attributes, ODS.getPropertyAttributes(), 217 T, TSI, MethodImplKind); 218 if (lexicalDC) 219 Res->setLexicalDeclContext(lexicalDC); 220 } 221 222 // Validate the attributes on the @property. 223 CheckObjCPropertyAttributes(Res, AtLoc, Attributes, 224 (isa<ObjCInterfaceDecl>(ClassDecl) || 225 isa<ObjCProtocolDecl>(ClassDecl))); 226 227 // Check consistency if the type has explicit ownership qualification. 228 if (Res->getType().getObjCLifetime()) 229 checkPropertyDeclWithOwnership(*this, Res); 230 231 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos; 232 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { 233 // For a class, compare the property against a property in our superclass. 234 bool FoundInSuper = false; 235 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace; 236 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) { 237 DeclContext::lookup_result R = Super->lookup(Res->getDeclName()); 238 for (unsigned I = 0, N = R.size(); I != N; ++I) { 239 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) { 240 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false); 241 FoundInSuper = true; 242 break; 243 } 244 } 245 if (FoundInSuper) 246 break; 247 else 248 CurrentInterfaceDecl = Super; 249 } 250 251 if (FoundInSuper) { 252 // Also compare the property against a property in our protocols. 253 for (auto *P : CurrentInterfaceDecl->protocols()) { 254 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); 255 } 256 } else { 257 // Slower path: look in all protocols we referenced. 258 for (auto *P : IFace->all_referenced_protocols()) { 259 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); 260 } 261 } 262 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { 263 // We don't check if class extension. Because properties in class extension 264 // are meant to override some of the attributes and checking has already done 265 // when property in class extension is constructed. 266 if (!Cat->IsClassExtension()) 267 for (auto *P : Cat->protocols()) 268 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); 269 } else { 270 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl); 271 for (auto *P : Proto->protocols()) 272 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos); 273 } 274 275 ActOnDocumentableDecl(Res); 276 return Res; 277 } 278 279 static ObjCPropertyDecl::PropertyAttributeKind 280 makePropertyAttributesAsWritten(unsigned Attributes) { 281 unsigned attributesAsWritten = 0; 282 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 283 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly; 284 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite) 285 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite; 286 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 287 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter; 288 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 289 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter; 290 if (Attributes & ObjCDeclSpec::DQ_PR_assign) 291 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign; 292 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 293 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain; 294 if (Attributes & ObjCDeclSpec::DQ_PR_strong) 295 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong; 296 if (Attributes & ObjCDeclSpec::DQ_PR_weak) 297 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak; 298 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 299 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy; 300 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 301 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained; 302 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 303 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic; 304 if (Attributes & ObjCDeclSpec::DQ_PR_atomic) 305 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic; 306 if (Attributes & ObjCDeclSpec::DQ_PR_class) 307 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_class; 308 309 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten; 310 } 311 312 static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, 313 SourceLocation LParenLoc, SourceLocation &Loc) { 314 if (LParenLoc.isMacroID()) 315 return false; 316 317 SourceManager &SM = Context.getSourceManager(); 318 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc); 319 // Try to load the file buffer. 320 bool invalidTemp = false; 321 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 322 if (invalidTemp) 323 return false; 324 const char *tokenBegin = file.data() + locInfo.second; 325 326 // Lex from the start of the given location. 327 Lexer lexer(SM.getLocForStartOfFile(locInfo.first), 328 Context.getLangOpts(), 329 file.begin(), tokenBegin, file.end()); 330 Token Tok; 331 do { 332 lexer.LexFromRawLexer(Tok); 333 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) { 334 Loc = Tok.getLocation(); 335 return true; 336 } 337 } while (Tok.isNot(tok::r_paren)); 338 return false; 339 } 340 341 /// Check for a mismatch in the atomicity of the given properties. 342 static void checkAtomicPropertyMismatch(Sema &S, 343 ObjCPropertyDecl *OldProperty, 344 ObjCPropertyDecl *NewProperty, 345 bool PropagateAtomicity) { 346 // If the atomicity of both matches, we're done. 347 bool OldIsAtomic = 348 (OldProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 349 == 0; 350 bool NewIsAtomic = 351 (NewProperty->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) 352 == 0; 353 if (OldIsAtomic == NewIsAtomic) return; 354 355 // Determine whether the given property is readonly and implicitly 356 // atomic. 357 auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool { 358 // Is it readonly? 359 auto Attrs = Property->getPropertyAttributes(); 360 if ((Attrs & ObjCPropertyDecl::OBJC_PR_readonly) == 0) return false; 361 362 // Is it nonatomic? 363 if (Attrs & ObjCPropertyDecl::OBJC_PR_nonatomic) return false; 364 365 // Was 'atomic' specified directly? 366 if (Property->getPropertyAttributesAsWritten() & 367 ObjCPropertyDecl::OBJC_PR_atomic) 368 return false; 369 370 return true; 371 }; 372 373 // If we're allowed to propagate atomicity, and the new property did 374 // not specify atomicity at all, propagate. 375 const unsigned AtomicityMask = 376 (ObjCPropertyDecl::OBJC_PR_atomic | ObjCPropertyDecl::OBJC_PR_nonatomic); 377 if (PropagateAtomicity && 378 ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) { 379 unsigned Attrs = NewProperty->getPropertyAttributes(); 380 Attrs = Attrs & ~AtomicityMask; 381 if (OldIsAtomic) 382 Attrs |= ObjCPropertyDecl::OBJC_PR_atomic; 383 else 384 Attrs |= ObjCPropertyDecl::OBJC_PR_nonatomic; 385 386 NewProperty->overwritePropertyAttributes(Attrs); 387 return; 388 } 389 390 // One of the properties is atomic; if it's a readonly property, and 391 // 'atomic' wasn't explicitly specified, we're okay. 392 if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) || 393 (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty))) 394 return; 395 396 // Diagnose the conflict. 397 const IdentifierInfo *OldContextName; 398 auto *OldDC = OldProperty->getDeclContext(); 399 if (auto Category = dyn_cast<ObjCCategoryDecl>(OldDC)) 400 OldContextName = Category->getClassInterface()->getIdentifier(); 401 else 402 OldContextName = cast<ObjCContainerDecl>(OldDC)->getIdentifier(); 403 404 S.Diag(NewProperty->getLocation(), diag::warn_property_attribute) 405 << NewProperty->getDeclName() << "atomic" 406 << OldContextName; 407 S.Diag(OldProperty->getLocation(), diag::note_property_declare); 408 } 409 410 ObjCPropertyDecl * 411 Sema::HandlePropertyInClassExtension(Scope *S, 412 SourceLocation AtLoc, 413 SourceLocation LParenLoc, 414 FieldDeclarator &FD, 415 Selector GetterSel, Selector SetterSel, 416 const bool isReadWrite, 417 unsigned &Attributes, 418 const unsigned AttributesAsWritten, 419 QualType T, 420 TypeSourceInfo *TSI, 421 tok::ObjCKeywordKind MethodImplKind) { 422 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext); 423 // Diagnose if this property is already in continuation class. 424 DeclContext *DC = CurContext; 425 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 426 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); 427 428 // We need to look in the @interface to see if the @property was 429 // already declared. 430 if (!CCPrimary) { 431 Diag(CDecl->getLocation(), diag::err_continuation_class); 432 return nullptr; 433 } 434 435 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) || 436 (Attributes & ObjCDeclSpec::DQ_PR_class); 437 438 // Find the property in the extended class's primary class or 439 // extensions. 440 ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass( 441 PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty)); 442 443 // If we found a property in an extension, complain. 444 if (PIDecl && isa<ObjCCategoryDecl>(PIDecl->getDeclContext())) { 445 Diag(AtLoc, diag::err_duplicate_property); 446 Diag(PIDecl->getLocation(), diag::note_property_declare); 447 return nullptr; 448 } 449 450 // Check for consistency with the previous declaration, if there is one. 451 if (PIDecl) { 452 // A readonly property declared in the primary class can be refined 453 // by adding a readwrite property within an extension. 454 // Anything else is an error. 455 if (!(PIDecl->isReadOnly() && isReadWrite)) { 456 // Tailor the diagnostics for the common case where a readwrite 457 // property is declared both in the @interface and the continuation. 458 // This is a common error where the user often intended the original 459 // declaration to be readonly. 460 unsigned diag = 461 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) && 462 (PIDecl->getPropertyAttributesAsWritten() & 463 ObjCPropertyDecl::OBJC_PR_readwrite) 464 ? diag::err_use_continuation_class_redeclaration_readwrite 465 : diag::err_use_continuation_class; 466 Diag(AtLoc, diag) 467 << CCPrimary->getDeclName(); 468 Diag(PIDecl->getLocation(), diag::note_property_declare); 469 return nullptr; 470 } 471 472 // Check for consistency of getters. 473 if (PIDecl->getGetterName() != GetterSel) { 474 // If the getter was written explicitly, complain. 475 if (AttributesAsWritten & ObjCDeclSpec::DQ_PR_getter) { 476 Diag(AtLoc, diag::warn_property_redecl_getter_mismatch) 477 << PIDecl->getGetterName() << GetterSel; 478 Diag(PIDecl->getLocation(), diag::note_property_declare); 479 } 480 481 // Always adopt the getter from the original declaration. 482 GetterSel = PIDecl->getGetterName(); 483 Attributes |= ObjCDeclSpec::DQ_PR_getter; 484 } 485 486 // Check consistency of ownership. 487 unsigned ExistingOwnership 488 = getOwnershipRule(PIDecl->getPropertyAttributes()); 489 unsigned NewOwnership = getOwnershipRule(Attributes); 490 if (ExistingOwnership && NewOwnership != ExistingOwnership) { 491 // If the ownership was written explicitly, complain. 492 if (getOwnershipRule(AttributesAsWritten)) { 493 Diag(AtLoc, diag::warn_property_attr_mismatch); 494 Diag(PIDecl->getLocation(), diag::note_property_declare); 495 } 496 497 // Take the ownership from the original property. 498 Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership; 499 } 500 501 // If the redeclaration is 'weak' but the original property is not, 502 if ((Attributes & ObjCPropertyDecl::OBJC_PR_weak) && 503 !(PIDecl->getPropertyAttributesAsWritten() 504 & ObjCPropertyDecl::OBJC_PR_weak) && 505 PIDecl->getType()->getAs<ObjCObjectPointerType>() && 506 PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) { 507 Diag(AtLoc, diag::warn_property_implicitly_mismatched); 508 Diag(PIDecl->getLocation(), diag::note_property_declare); 509 } 510 } 511 512 // Create a new ObjCPropertyDecl with the DeclContext being 513 // the class extension. 514 ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc, 515 FD, GetterSel, SetterSel, 516 isReadWrite, 517 Attributes, AttributesAsWritten, 518 T, TSI, MethodImplKind, DC); 519 520 // If there was no declaration of a property with the same name in 521 // the primary class, we're done. 522 if (!PIDecl) { 523 ProcessPropertyDecl(PDecl); 524 return PDecl; 525 } 526 527 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) { 528 bool IncompatibleObjC = false; 529 QualType ConvertedType; 530 // Relax the strict type matching for property type in continuation class. 531 // Allow property object type of continuation class to be different as long 532 // as it narrows the object type in its primary class property. Note that 533 // this conversion is safe only because the wider type is for a 'readonly' 534 // property in primary class and 'narrowed' type for a 'readwrite' property 535 // in continuation class. 536 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType()); 537 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType()); 538 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) || 539 !isa<ObjCObjectPointerType>(ClassExtPropertyT) || 540 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT, 541 ConvertedType, IncompatibleObjC)) 542 || IncompatibleObjC) { 543 Diag(AtLoc, 544 diag::err_type_mismatch_continuation_class) << PDecl->getType(); 545 Diag(PIDecl->getLocation(), diag::note_property_declare); 546 return nullptr; 547 } 548 } 549 550 // Check that atomicity of property in class extension matches the previous 551 // declaration. 552 checkAtomicPropertyMismatch(*this, PIDecl, PDecl, true); 553 554 // Make sure getter/setter are appropriately synthesized. 555 ProcessPropertyDecl(PDecl); 556 return PDecl; 557 } 558 559 ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S, 560 ObjCContainerDecl *CDecl, 561 SourceLocation AtLoc, 562 SourceLocation LParenLoc, 563 FieldDeclarator &FD, 564 Selector GetterSel, 565 Selector SetterSel, 566 const bool isReadWrite, 567 const unsigned Attributes, 568 const unsigned AttributesAsWritten, 569 QualType T, 570 TypeSourceInfo *TInfo, 571 tok::ObjCKeywordKind MethodImplKind, 572 DeclContext *lexicalDC){ 573 IdentifierInfo *PropertyId = FD.D.getIdentifier(); 574 575 // Property defaults to 'assign' if it is readwrite, unless this is ARC 576 // and the type is retainable. 577 bool isAssign; 578 if (Attributes & (ObjCDeclSpec::DQ_PR_assign | 579 ObjCDeclSpec::DQ_PR_unsafe_unretained)) { 580 isAssign = true; 581 } else if (getOwnershipRule(Attributes) || !isReadWrite) { 582 isAssign = false; 583 } else { 584 isAssign = (!getLangOpts().ObjCAutoRefCount || 585 !T->isObjCRetainableType()); 586 } 587 588 // Issue a warning if property is 'assign' as default and its 589 // object, which is gc'able conforms to NSCopying protocol 590 if (getLangOpts().getGC() != LangOptions::NonGC && 591 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) { 592 if (const ObjCObjectPointerType *ObjPtrTy = 593 T->getAs<ObjCObjectPointerType>()) { 594 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); 595 if (IDecl) 596 if (ObjCProtocolDecl* PNSCopying = 597 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc)) 598 if (IDecl->ClassImplementsProtocol(PNSCopying, true)) 599 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId; 600 } 601 } 602 603 if (T->isObjCObjectType()) { 604 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd(); 605 StarLoc = getLocForEndOfToken(StarLoc); 606 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object) 607 << FixItHint::CreateInsertion(StarLoc, "*"); 608 T = Context.getObjCObjectPointerType(T); 609 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart(); 610 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc); 611 } 612 613 DeclContext *DC = cast<DeclContext>(CDecl); 614 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, 615 FD.D.getIdentifierLoc(), 616 PropertyId, AtLoc, 617 LParenLoc, T, TInfo); 618 619 bool isClassProperty = (AttributesAsWritten & ObjCDeclSpec::DQ_PR_class) || 620 (Attributes & ObjCDeclSpec::DQ_PR_class); 621 // Class property and instance property can have the same name. 622 if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl( 623 DC, PropertyId, ObjCPropertyDecl::getQueryKind(isClassProperty))) { 624 Diag(PDecl->getLocation(), diag::err_duplicate_property); 625 Diag(prevDecl->getLocation(), diag::note_property_declare); 626 PDecl->setInvalidDecl(); 627 } 628 else { 629 DC->addDecl(PDecl); 630 if (lexicalDC) 631 PDecl->setLexicalDeclContext(lexicalDC); 632 } 633 634 if (T->isArrayType() || T->isFunctionType()) { 635 Diag(AtLoc, diag::err_property_type) << T; 636 PDecl->setInvalidDecl(); 637 } 638 639 ProcessDeclAttributes(S, PDecl, FD.D); 640 641 // Regardless of setter/getter attribute, we save the default getter/setter 642 // selector names in anticipation of declaration of setter/getter methods. 643 PDecl->setGetterName(GetterSel); 644 PDecl->setSetterName(SetterSel); 645 PDecl->setPropertyAttributesAsWritten( 646 makePropertyAttributesAsWritten(AttributesAsWritten)); 647 648 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) 649 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); 650 651 if (Attributes & ObjCDeclSpec::DQ_PR_getter) 652 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); 653 654 if (Attributes & ObjCDeclSpec::DQ_PR_setter) 655 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); 656 657 if (isReadWrite) 658 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); 659 660 if (Attributes & ObjCDeclSpec::DQ_PR_retain) 661 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); 662 663 if (Attributes & ObjCDeclSpec::DQ_PR_strong) 664 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 665 666 if (Attributes & ObjCDeclSpec::DQ_PR_weak) 667 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); 668 669 if (Attributes & ObjCDeclSpec::DQ_PR_copy) 670 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); 671 672 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 673 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 674 675 if (isAssign) 676 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 677 678 // In the semantic attributes, one of nonatomic or atomic is always set. 679 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) 680 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); 681 else 682 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic); 683 684 // 'unsafe_unretained' is alias for 'assign'. 685 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) 686 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); 687 if (isAssign) 688 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained); 689 690 if (MethodImplKind == tok::objc_required) 691 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); 692 else if (MethodImplKind == tok::objc_optional) 693 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); 694 695 if (Attributes & ObjCDeclSpec::DQ_PR_nullability) 696 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability); 697 698 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable) 699 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable); 700 701 if (Attributes & ObjCDeclSpec::DQ_PR_class) 702 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class); 703 704 return PDecl; 705 } 706 707 static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, 708 ObjCPropertyDecl *property, 709 ObjCIvarDecl *ivar) { 710 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; 711 712 QualType ivarType = ivar->getType(); 713 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); 714 715 // The lifetime implied by the property's attributes. 716 Qualifiers::ObjCLifetime propertyLifetime = 717 getImpliedARCOwnership(property->getPropertyAttributes(), 718 property->getType()); 719 720 // We're fine if they match. 721 if (propertyLifetime == ivarLifetime) return; 722 723 // None isn't a valid lifetime for an object ivar in ARC, and 724 // __autoreleasing is never valid; don't diagnose twice. 725 if ((ivarLifetime == Qualifiers::OCL_None && 726 S.getLangOpts().ObjCAutoRefCount) || 727 ivarLifetime == Qualifiers::OCL_Autoreleasing) 728 return; 729 730 // If the ivar is private, and it's implicitly __unsafe_unretained 731 // becaues of its type, then pretend it was actually implicitly 732 // __strong. This is only sound because we're processing the 733 // property implementation before parsing any method bodies. 734 if (ivarLifetime == Qualifiers::OCL_ExplicitNone && 735 propertyLifetime == Qualifiers::OCL_Strong && 736 ivar->getAccessControl() == ObjCIvarDecl::Private) { 737 SplitQualType split = ivarType.split(); 738 if (split.Quals.hasObjCLifetime()) { 739 assert(ivarType->isObjCARCImplicitlyUnretainedType()); 740 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong); 741 ivarType = S.Context.getQualifiedType(split); 742 ivar->setType(ivarType); 743 return; 744 } 745 } 746 747 switch (propertyLifetime) { 748 case Qualifiers::OCL_Strong: 749 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership) 750 << property->getDeclName() 751 << ivar->getDeclName() 752 << ivarLifetime; 753 break; 754 755 case Qualifiers::OCL_Weak: 756 S.Diag(ivar->getLocation(), diag::error_weak_property) 757 << property->getDeclName() 758 << ivar->getDeclName(); 759 break; 760 761 case Qualifiers::OCL_ExplicitNone: 762 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership) 763 << property->getDeclName() 764 << ivar->getDeclName() 765 << ((property->getPropertyAttributesAsWritten() 766 & ObjCPropertyDecl::OBJC_PR_assign) != 0); 767 break; 768 769 case Qualifiers::OCL_Autoreleasing: 770 llvm_unreachable("properties cannot be autoreleasing"); 771 772 case Qualifiers::OCL_None: 773 // Any other property should be ignored. 774 return; 775 } 776 777 S.Diag(property->getLocation(), diag::note_property_declare); 778 if (propertyImplLoc.isValid()) 779 S.Diag(propertyImplLoc, diag::note_property_synthesize); 780 } 781 782 /// setImpliedPropertyAttributeForReadOnlyProperty - 783 /// This routine evaludates life-time attributes for a 'readonly' 784 /// property with no known lifetime of its own, using backing 785 /// 'ivar's attribute, if any. If no backing 'ivar', property's 786 /// life-time is assumed 'strong'. 787 static void setImpliedPropertyAttributeForReadOnlyProperty( 788 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { 789 Qualifiers::ObjCLifetime propertyLifetime = 790 getImpliedARCOwnership(property->getPropertyAttributes(), 791 property->getType()); 792 if (propertyLifetime != Qualifiers::OCL_None) 793 return; 794 795 if (!ivar) { 796 // if no backing ivar, make property 'strong'. 797 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 798 return; 799 } 800 // property assumes owenership of backing ivar. 801 QualType ivarType = ivar->getType(); 802 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); 803 if (ivarLifetime == Qualifiers::OCL_Strong) 804 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 805 else if (ivarLifetime == Qualifiers::OCL_Weak) 806 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak); 807 } 808 809 /// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared 810 /// in inherited protocols with mismatched types. Since any of them can 811 /// be candidate for synthesis. 812 static void 813 DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc, 814 ObjCInterfaceDecl *ClassDecl, 815 ObjCPropertyDecl *Property) { 816 ObjCInterfaceDecl::ProtocolPropertyMap PropMap; 817 for (const auto *PI : ClassDecl->all_referenced_protocols()) { 818 if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) 819 PDecl->collectInheritedProtocolProperties(Property, PropMap); 820 } 821 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) 822 while (SDecl) { 823 for (const auto *PI : SDecl->all_referenced_protocols()) { 824 if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) 825 PDecl->collectInheritedProtocolProperties(Property, PropMap); 826 } 827 SDecl = SDecl->getSuperClass(); 828 } 829 830 if (PropMap.empty()) 831 return; 832 833 QualType RHSType = S.Context.getCanonicalType(Property->getType()); 834 bool FirsTime = true; 835 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator 836 I = PropMap.begin(), E = PropMap.end(); I != E; I++) { 837 ObjCPropertyDecl *Prop = I->second; 838 QualType LHSType = S.Context.getCanonicalType(Prop->getType()); 839 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) { 840 bool IncompatibleObjC = false; 841 QualType ConvertedType; 842 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC) 843 || IncompatibleObjC) { 844 if (FirsTime) { 845 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch) 846 << Property->getType(); 847 FirsTime = false; 848 } 849 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare) 850 << Prop->getType(); 851 } 852 } 853 } 854 if (!FirsTime && AtLoc.isValid()) 855 S.Diag(AtLoc, diag::note_property_synthesize); 856 } 857 858 /// Determine whether any storage attributes were written on the property. 859 static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop, 860 ObjCPropertyQueryKind QueryKind) { 861 if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true; 862 863 // If this is a readwrite property in a class extension that refines 864 // a readonly property in the original class definition, check it as 865 // well. 866 867 // If it's a readonly property, we're not interested. 868 if (Prop->isReadOnly()) return false; 869 870 // Is it declared in an extension? 871 auto Category = dyn_cast<ObjCCategoryDecl>(Prop->getDeclContext()); 872 if (!Category || !Category->IsClassExtension()) return false; 873 874 // Find the corresponding property in the primary class definition. 875 auto OrigClass = Category->getClassInterface(); 876 for (auto Found : OrigClass->lookup(Prop->getDeclName())) { 877 if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Found)) 878 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; 879 } 880 881 // Look through all of the protocols. 882 for (const auto *Proto : OrigClass->all_referenced_protocols()) { 883 if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration( 884 Prop->getIdentifier(), QueryKind)) 885 return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; 886 } 887 888 return false; 889 } 890 891 /// ActOnPropertyImplDecl - This routine performs semantic checks and 892 /// builds the AST node for a property implementation declaration; declared 893 /// as \@synthesize or \@dynamic. 894 /// 895 Decl *Sema::ActOnPropertyImplDecl(Scope *S, 896 SourceLocation AtLoc, 897 SourceLocation PropertyLoc, 898 bool Synthesize, 899 IdentifierInfo *PropertyId, 900 IdentifierInfo *PropertyIvar, 901 SourceLocation PropertyIvarLoc, 902 ObjCPropertyQueryKind QueryKind) { 903 ObjCContainerDecl *ClassImpDecl = 904 dyn_cast<ObjCContainerDecl>(CurContext); 905 // Make sure we have a context for the property implementation declaration. 906 if (!ClassImpDecl) { 907 Diag(AtLoc, diag::error_missing_property_context); 908 return nullptr; 909 } 910 if (PropertyIvarLoc.isInvalid()) 911 PropertyIvarLoc = PropertyLoc; 912 SourceLocation PropertyDiagLoc = PropertyLoc; 913 if (PropertyDiagLoc.isInvalid()) 914 PropertyDiagLoc = ClassImpDecl->getLocStart(); 915 ObjCPropertyDecl *property = nullptr; 916 ObjCInterfaceDecl *IDecl = nullptr; 917 // Find the class or category class where this property must have 918 // a declaration. 919 ObjCImplementationDecl *IC = nullptr; 920 ObjCCategoryImplDecl *CatImplClass = nullptr; 921 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 922 IDecl = IC->getClassInterface(); 923 // We always synthesize an interface for an implementation 924 // without an interface decl. So, IDecl is always non-zero. 925 assert(IDecl && 926 "ActOnPropertyImplDecl - @implementation without @interface"); 927 928 // Look for this property declaration in the @implementation's @interface 929 property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind); 930 if (!property) { 931 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 932 return nullptr; 933 } 934 if (property->isClassProperty() && Synthesize) { 935 Diag(PropertyLoc, diag::error_synthesize_on_class_property) << PropertyId; 936 return nullptr; 937 } 938 unsigned PIkind = property->getPropertyAttributesAsWritten(); 939 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | 940 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { 941 if (AtLoc.isValid()) 942 Diag(AtLoc, diag::warn_implicit_atomic_property); 943 else 944 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); 945 Diag(property->getLocation(), diag::note_property_declare); 946 } 947 948 if (const ObjCCategoryDecl *CD = 949 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { 950 if (!CD->IsClassExtension()) { 951 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); 952 Diag(property->getLocation(), diag::note_property_declare); 953 return nullptr; 954 } 955 } 956 if (Synthesize&& 957 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && 958 property->hasAttr<IBOutletAttr>() && 959 !AtLoc.isValid()) { 960 bool ReadWriteProperty = false; 961 // Search into the class extensions and see if 'readonly property is 962 // redeclared 'readwrite', then no warning is to be issued. 963 for (auto *Ext : IDecl->known_extensions()) { 964 DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); 965 if (!R.empty()) 966 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) { 967 PIkind = ExtProp->getPropertyAttributesAsWritten(); 968 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) { 969 ReadWriteProperty = true; 970 break; 971 } 972 } 973 } 974 975 if (!ReadWriteProperty) { 976 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property) 977 << property; 978 SourceLocation readonlyLoc; 979 if (LocPropertyAttribute(Context, "readonly", 980 property->getLParenLoc(), readonlyLoc)) { 981 SourceLocation endLoc = 982 readonlyLoc.getLocWithOffset(strlen("readonly")-1); 983 SourceRange ReadonlySourceRange(readonlyLoc, endLoc); 984 Diag(property->getLocation(), 985 diag::note_auto_readonly_iboutlet_fixup_suggest) << 986 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); 987 } 988 } 989 } 990 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext())) 991 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property); 992 993 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 994 if (Synthesize) { 995 Diag(AtLoc, diag::error_synthesize_category_decl); 996 return nullptr; 997 } 998 IDecl = CatImplClass->getClassInterface(); 999 if (!IDecl) { 1000 Diag(AtLoc, diag::error_missing_property_interface); 1001 return nullptr; 1002 } 1003 ObjCCategoryDecl *Category = 1004 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 1005 1006 // If category for this implementation not found, it is an error which 1007 // has already been reported eralier. 1008 if (!Category) 1009 return nullptr; 1010 // Look for this property declaration in @implementation's category 1011 property = Category->FindPropertyDeclaration(PropertyId, QueryKind); 1012 if (!property) { 1013 Diag(PropertyLoc, diag::error_bad_category_property_decl) 1014 << Category->getDeclName(); 1015 return nullptr; 1016 } 1017 } else { 1018 Diag(AtLoc, diag::error_bad_property_context); 1019 return nullptr; 1020 } 1021 ObjCIvarDecl *Ivar = nullptr; 1022 bool CompleteTypeErr = false; 1023 bool compat = true; 1024 // Check that we have a valid, previously declared ivar for @synthesize 1025 if (Synthesize) { 1026 // @synthesize 1027 if (!PropertyIvar) 1028 PropertyIvar = PropertyId; 1029 // Check that this is a previously declared 'ivar' in 'IDecl' interface 1030 ObjCInterfaceDecl *ClassDeclared; 1031 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); 1032 QualType PropType = property->getType(); 1033 QualType PropertyIvarType = PropType.getNonReferenceType(); 1034 1035 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, 1036 diag::err_incomplete_synthesized_property, 1037 property->getDeclName())) { 1038 Diag(property->getLocation(), diag::note_property_declare); 1039 CompleteTypeErr = true; 1040 } 1041 1042 if (getLangOpts().ObjCAutoRefCount && 1043 (property->getPropertyAttributesAsWritten() & 1044 ObjCPropertyDecl::OBJC_PR_readonly) && 1045 PropertyIvarType->isObjCRetainableType()) { 1046 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); 1047 } 1048 1049 ObjCPropertyDecl::PropertyAttributeKind kind 1050 = property->getPropertyAttributes(); 1051 1052 bool isARCWeak = false; 1053 if (kind & ObjCPropertyDecl::OBJC_PR_weak) { 1054 // Add GC __weak to the ivar type if the property is weak. 1055 if (getLangOpts().getGC() != LangOptions::NonGC) { 1056 assert(!getLangOpts().ObjCAutoRefCount); 1057 if (PropertyIvarType.isObjCGCStrong()) { 1058 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); 1059 Diag(property->getLocation(), diag::note_property_declare); 1060 } else { 1061 PropertyIvarType = 1062 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); 1063 } 1064 1065 // Otherwise, check whether ARC __weak is enabled and works with 1066 // the property type. 1067 } else { 1068 if (!getLangOpts().ObjCWeak) { 1069 // Only complain here when synthesizing an ivar. 1070 if (!Ivar) { 1071 Diag(PropertyDiagLoc, 1072 getLangOpts().ObjCWeakRuntime 1073 ? diag::err_synthesizing_arc_weak_property_disabled 1074 : diag::err_synthesizing_arc_weak_property_no_runtime); 1075 Diag(property->getLocation(), diag::note_property_declare); 1076 } 1077 CompleteTypeErr = true; // suppress later diagnostics about the ivar 1078 } else { 1079 isARCWeak = true; 1080 if (const ObjCObjectPointerType *ObjT = 1081 PropertyIvarType->getAs<ObjCObjectPointerType>()) { 1082 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); 1083 if (ObjI && ObjI->isArcWeakrefUnavailable()) { 1084 Diag(property->getLocation(), 1085 diag::err_arc_weak_unavailable_property) 1086 << PropertyIvarType; 1087 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class) 1088 << ClassImpDecl->getName(); 1089 } 1090 } 1091 } 1092 } 1093 } 1094 1095 if (AtLoc.isInvalid()) { 1096 // Check when default synthesizing a property that there is 1097 // an ivar matching property name and issue warning; since this 1098 // is the most common case of not using an ivar used for backing 1099 // property in non-default synthesis case. 1100 ObjCInterfaceDecl *ClassDeclared=nullptr; 1101 ObjCIvarDecl *originalIvar = 1102 IDecl->lookupInstanceVariable(property->getIdentifier(), 1103 ClassDeclared); 1104 if (originalIvar) { 1105 Diag(PropertyDiagLoc, 1106 diag::warn_autosynthesis_property_ivar_match) 1107 << PropertyId << (Ivar == nullptr) << PropertyIvar 1108 << originalIvar->getIdentifier(); 1109 Diag(property->getLocation(), diag::note_property_declare); 1110 Diag(originalIvar->getLocation(), diag::note_ivar_decl); 1111 } 1112 } 1113 1114 if (!Ivar) { 1115 // In ARC, give the ivar a lifetime qualifier based on the 1116 // property attributes. 1117 if ((getLangOpts().ObjCAutoRefCount || isARCWeak) && 1118 !PropertyIvarType.getObjCLifetime() && 1119 PropertyIvarType->isObjCRetainableType()) { 1120 1121 // It's an error if we have to do this and the user didn't 1122 // explicitly write an ownership attribute on the property. 1123 if (!hasWrittenStorageAttribute(property, QueryKind) && 1124 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { 1125 Diag(PropertyDiagLoc, 1126 diag::err_arc_objc_property_default_assign_on_object); 1127 Diag(property->getLocation(), diag::note_property_declare); 1128 } else { 1129 Qualifiers::ObjCLifetime lifetime = 1130 getImpliedARCOwnership(kind, PropertyIvarType); 1131 assert(lifetime && "no lifetime for property?"); 1132 1133 Qualifiers qs; 1134 qs.addObjCLifetime(lifetime); 1135 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); 1136 } 1137 } 1138 1139 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, 1140 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, 1141 PropertyIvarType, /*Dinfo=*/nullptr, 1142 ObjCIvarDecl::Private, 1143 (Expr *)nullptr, true); 1144 if (RequireNonAbstractType(PropertyIvarLoc, 1145 PropertyIvarType, 1146 diag::err_abstract_type_in_decl, 1147 AbstractSynthesizedIvarType)) { 1148 Diag(property->getLocation(), diag::note_property_declare); 1149 Ivar->setInvalidDecl(); 1150 } else if (CompleteTypeErr) 1151 Ivar->setInvalidDecl(); 1152 ClassImpDecl->addDecl(Ivar); 1153 IDecl->makeDeclVisibleInContext(Ivar); 1154 1155 if (getLangOpts().ObjCRuntime.isFragile()) 1156 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl) 1157 << PropertyId; 1158 // Note! I deliberately want it to fall thru so, we have a 1159 // a property implementation and to avoid future warnings. 1160 } else if (getLangOpts().ObjCRuntime.isNonFragile() && 1161 !declaresSameEntity(ClassDeclared, IDecl)) { 1162 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use) 1163 << property->getDeclName() << Ivar->getDeclName() 1164 << ClassDeclared->getDeclName(); 1165 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 1166 << Ivar << Ivar->getName(); 1167 // Note! I deliberately want it to fall thru so more errors are caught. 1168 } 1169 property->setPropertyIvarDecl(Ivar); 1170 1171 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1172 1173 // Check that type of property and its ivar are type compatible. 1174 if (!Context.hasSameType(PropertyIvarType, IvarType)) { 1175 if (isa<ObjCObjectPointerType>(PropertyIvarType) 1176 && isa<ObjCObjectPointerType>(IvarType)) 1177 compat = 1178 Context.canAssignObjCInterfaces( 1179 PropertyIvarType->getAs<ObjCObjectPointerType>(), 1180 IvarType->getAs<ObjCObjectPointerType>()); 1181 else { 1182 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, 1183 IvarType) 1184 == Compatible); 1185 } 1186 if (!compat) { 1187 Diag(PropertyDiagLoc, diag::error_property_ivar_type) 1188 << property->getDeclName() << PropType 1189 << Ivar->getDeclName() << IvarType; 1190 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1191 // Note! I deliberately want it to fall thru so, we have a 1192 // a property implementation and to avoid future warnings. 1193 } 1194 else { 1195 // FIXME! Rules for properties are somewhat different that those 1196 // for assignments. Use a new routine to consolidate all cases; 1197 // specifically for property redeclarations as well as for ivars. 1198 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); 1199 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 1200 if (lhsType != rhsType && 1201 lhsType->isArithmeticType()) { 1202 Diag(PropertyDiagLoc, diag::error_property_ivar_type) 1203 << property->getDeclName() << PropType 1204 << Ivar->getDeclName() << IvarType; 1205 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1206 // Fall thru - see previous comment 1207 } 1208 } 1209 // __weak is explicit. So it works on Canonical type. 1210 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 1211 getLangOpts().getGC() != LangOptions::NonGC)) { 1212 Diag(PropertyDiagLoc, diag::error_weak_property) 1213 << property->getDeclName() << Ivar->getDeclName(); 1214 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1215 // Fall thru - see previous comment 1216 } 1217 // Fall thru - see previous comment 1218 if ((property->getType()->isObjCObjectPointerType() || 1219 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 1220 getLangOpts().getGC() != LangOptions::NonGC) { 1221 Diag(PropertyDiagLoc, diag::error_strong_property) 1222 << property->getDeclName() << Ivar->getDeclName(); 1223 // Fall thru - see previous comment 1224 } 1225 } 1226 if (getLangOpts().ObjCAutoRefCount || isARCWeak || 1227 Ivar->getType().getObjCLifetime()) 1228 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); 1229 } else if (PropertyIvar) 1230 // @dynamic 1231 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl); 1232 1233 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1234 ObjCPropertyImplDecl *PIDecl = 1235 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 1236 property, 1237 (Synthesize ? 1238 ObjCPropertyImplDecl::Synthesize 1239 : ObjCPropertyImplDecl::Dynamic), 1240 Ivar, PropertyIvarLoc); 1241 1242 if (CompleteTypeErr || !compat) 1243 PIDecl->setInvalidDecl(); 1244 1245 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { 1246 getterMethod->createImplicitParams(Context, IDecl); 1247 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && 1248 Ivar->getType()->isRecordType()) { 1249 // For Objective-C++, need to synthesize the AST for the IVAR object to be 1250 // returned by the getter as it must conform to C++'s copy-return rules. 1251 // FIXME. Eventually we want to do this for Objective-C as well. 1252 SynthesizedFunctionScope Scope(*this, getterMethod); 1253 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); 1254 DeclRefExpr *SelfExpr = 1255 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), 1256 VK_LValue, PropertyDiagLoc); 1257 MarkDeclRefReferenced(SelfExpr); 1258 Expr *LoadSelfExpr = 1259 ImplicitCastExpr::Create(Context, SelfDecl->getType(), 1260 CK_LValueToRValue, SelfExpr, nullptr, 1261 VK_RValue); 1262 Expr *IvarRefExpr = 1263 new (Context) ObjCIvarRefExpr(Ivar, 1264 Ivar->getUsageType(SelfDecl->getType()), 1265 PropertyDiagLoc, 1266 Ivar->getLocation(), 1267 LoadSelfExpr, true, true); 1268 ExprResult Res = PerformCopyInitialization( 1269 InitializedEntity::InitializeResult(PropertyDiagLoc, 1270 getterMethod->getReturnType(), 1271 /*NRVO=*/false), 1272 PropertyDiagLoc, IvarRefExpr); 1273 if (!Res.isInvalid()) { 1274 Expr *ResExpr = Res.getAs<Expr>(); 1275 if (ResExpr) 1276 ResExpr = MaybeCreateExprWithCleanups(ResExpr); 1277 PIDecl->setGetterCXXConstructor(ResExpr); 1278 } 1279 } 1280 if (property->hasAttr<NSReturnsNotRetainedAttr>() && 1281 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { 1282 Diag(getterMethod->getLocation(), 1283 diag::warn_property_getter_owning_mismatch); 1284 Diag(property->getLocation(), diag::note_property_declare); 1285 } 1286 if (getLangOpts().ObjCAutoRefCount && Synthesize) 1287 switch (getterMethod->getMethodFamily()) { 1288 case OMF_retain: 1289 case OMF_retainCount: 1290 case OMF_release: 1291 case OMF_autorelease: 1292 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def) 1293 << 1 << getterMethod->getSelector(); 1294 break; 1295 default: 1296 break; 1297 } 1298 } 1299 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { 1300 setterMethod->createImplicitParams(Context, IDecl); 1301 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && 1302 Ivar->getType()->isRecordType()) { 1303 // FIXME. Eventually we want to do this for Objective-C as well. 1304 SynthesizedFunctionScope Scope(*this, setterMethod); 1305 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); 1306 DeclRefExpr *SelfExpr = 1307 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), 1308 VK_LValue, PropertyDiagLoc); 1309 MarkDeclRefReferenced(SelfExpr); 1310 Expr *LoadSelfExpr = 1311 ImplicitCastExpr::Create(Context, SelfDecl->getType(), 1312 CK_LValueToRValue, SelfExpr, nullptr, 1313 VK_RValue); 1314 Expr *lhs = 1315 new (Context) ObjCIvarRefExpr(Ivar, 1316 Ivar->getUsageType(SelfDecl->getType()), 1317 PropertyDiagLoc, 1318 Ivar->getLocation(), 1319 LoadSelfExpr, true, true); 1320 ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); 1321 ParmVarDecl *Param = (*P); 1322 QualType T = Param->getType().getNonReferenceType(); 1323 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T, 1324 VK_LValue, PropertyDiagLoc); 1325 MarkDeclRefReferenced(rhs); 1326 ExprResult Res = BuildBinOp(S, PropertyDiagLoc, 1327 BO_Assign, lhs, rhs); 1328 if (property->getPropertyAttributes() & 1329 ObjCPropertyDecl::OBJC_PR_atomic) { 1330 Expr *callExpr = Res.getAs<Expr>(); 1331 if (const CXXOperatorCallExpr *CXXCE = 1332 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) 1333 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) 1334 if (!FuncDecl->isTrivial()) 1335 if (property->getType()->isReferenceType()) { 1336 Diag(PropertyDiagLoc, 1337 diag::err_atomic_property_nontrivial_assign_op) 1338 << property->getType(); 1339 Diag(FuncDecl->getLocStart(), 1340 diag::note_callee_decl) << FuncDecl; 1341 } 1342 } 1343 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>()); 1344 } 1345 } 1346 1347 if (IC) { 1348 if (Synthesize) 1349 if (ObjCPropertyImplDecl *PPIDecl = 1350 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 1351 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 1352 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1353 << PropertyIvar; 1354 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1355 } 1356 1357 if (ObjCPropertyImplDecl *PPIDecl 1358 = IC->FindPropertyImplDecl(PropertyId, QueryKind)) { 1359 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 1360 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1361 return nullptr; 1362 } 1363 IC->addPropertyImplementation(PIDecl); 1364 if (getLangOpts().ObjCDefaultSynthProperties && 1365 getLangOpts().ObjCRuntime.isNonFragile() && 1366 !IDecl->isObjCRequiresPropertyDefs()) { 1367 // Diagnose if an ivar was lazily synthesdized due to a previous 1368 // use and if 1) property is @dynamic or 2) property is synthesized 1369 // but it requires an ivar of different name. 1370 ObjCInterfaceDecl *ClassDeclared=nullptr; 1371 ObjCIvarDecl *Ivar = nullptr; 1372 if (!Synthesize) 1373 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 1374 else { 1375 if (PropertyIvar && PropertyIvar != PropertyId) 1376 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 1377 } 1378 // Issue diagnostics only if Ivar belongs to current class. 1379 if (Ivar && Ivar->getSynthesize() && 1380 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { 1381 Diag(Ivar->getLocation(), diag::err_undeclared_var_use) 1382 << PropertyId; 1383 Ivar->setInvalidDecl(); 1384 } 1385 } 1386 } else { 1387 if (Synthesize) 1388 if (ObjCPropertyImplDecl *PPIDecl = 1389 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 1390 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use) 1391 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1392 << PropertyIvar; 1393 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1394 } 1395 1396 if (ObjCPropertyImplDecl *PPIDecl = 1397 CatImplClass->FindPropertyImplDecl(PropertyId, QueryKind)) { 1398 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId; 1399 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1400 return nullptr; 1401 } 1402 CatImplClass->addPropertyImplementation(PIDecl); 1403 } 1404 1405 return PIDecl; 1406 } 1407 1408 //===----------------------------------------------------------------------===// 1409 // Helper methods. 1410 //===----------------------------------------------------------------------===// 1411 1412 /// DiagnosePropertyMismatch - Compares two properties for their 1413 /// attributes and types and warns on a variety of inconsistencies. 1414 /// 1415 void 1416 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 1417 ObjCPropertyDecl *SuperProperty, 1418 const IdentifierInfo *inheritedName, 1419 bool OverridingProtocolProperty) { 1420 ObjCPropertyDecl::PropertyAttributeKind CAttr = 1421 Property->getPropertyAttributes(); 1422 ObjCPropertyDecl::PropertyAttributeKind SAttr = 1423 SuperProperty->getPropertyAttributes(); 1424 1425 // We allow readonly properties without an explicit ownership 1426 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class 1427 // to be overridden by a property with any explicit ownership in the subclass. 1428 if (!OverridingProtocolProperty && 1429 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr)) 1430 ; 1431 else { 1432 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 1433 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 1434 Diag(Property->getLocation(), diag::warn_readonly_property) 1435 << Property->getDeclName() << inheritedName; 1436 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 1437 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 1438 Diag(Property->getLocation(), diag::warn_property_attribute) 1439 << Property->getDeclName() << "copy" << inheritedName; 1440 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ 1441 unsigned CAttrRetain = 1442 (CAttr & 1443 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 1444 unsigned SAttrRetain = 1445 (SAttr & 1446 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 1447 bool CStrong = (CAttrRetain != 0); 1448 bool SStrong = (SAttrRetain != 0); 1449 if (CStrong != SStrong) 1450 Diag(Property->getLocation(), diag::warn_property_attribute) 1451 << Property->getDeclName() << "retain (or strong)" << inheritedName; 1452 } 1453 } 1454 1455 // Check for nonatomic; note that nonatomic is effectively 1456 // meaningless for readonly properties, so don't diagnose if the 1457 // atomic property is 'readonly'. 1458 checkAtomicPropertyMismatch(*this, SuperProperty, Property, false); 1459 if (Property->getSetterName() != SuperProperty->getSetterName()) { 1460 Diag(Property->getLocation(), diag::warn_property_attribute) 1461 << Property->getDeclName() << "setter" << inheritedName; 1462 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1463 } 1464 if (Property->getGetterName() != SuperProperty->getGetterName()) { 1465 Diag(Property->getLocation(), diag::warn_property_attribute) 1466 << Property->getDeclName() << "getter" << inheritedName; 1467 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1468 } 1469 1470 QualType LHSType = 1471 Context.getCanonicalType(SuperProperty->getType()); 1472 QualType RHSType = 1473 Context.getCanonicalType(Property->getType()); 1474 1475 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { 1476 // Do cases not handled in above. 1477 // FIXME. For future support of covariant property types, revisit this. 1478 bool IncompatibleObjC = false; 1479 QualType ConvertedType; 1480 if (!isObjCPointerConversion(RHSType, LHSType, 1481 ConvertedType, IncompatibleObjC) || 1482 IncompatibleObjC) { 1483 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) 1484 << Property->getType() << SuperProperty->getType() << inheritedName; 1485 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1486 } 1487 } 1488 } 1489 1490 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, 1491 ObjCMethodDecl *GetterMethod, 1492 SourceLocation Loc) { 1493 if (!GetterMethod) 1494 return false; 1495 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType(); 1496 QualType PropertyIvarType = property->getType().getNonReferenceType(); 1497 bool compat = Context.hasSameType(PropertyIvarType, GetterType); 1498 if (!compat) { 1499 const ObjCObjectPointerType *propertyObjCPtr = nullptr; 1500 const ObjCObjectPointerType *getterObjCPtr = nullptr; 1501 if ((propertyObjCPtr = PropertyIvarType->getAs<ObjCObjectPointerType>()) && 1502 (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>())) 1503 compat = Context.canAssignObjCInterfaces(getterObjCPtr, propertyObjCPtr); 1504 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType) 1505 != Compatible) { 1506 Diag(Loc, diag::error_property_accessor_type) 1507 << property->getDeclName() << PropertyIvarType 1508 << GetterMethod->getSelector() << GetterType; 1509 Diag(GetterMethod->getLocation(), diag::note_declared_at); 1510 return true; 1511 } else { 1512 compat = true; 1513 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); 1514 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); 1515 if (lhsType != rhsType && lhsType->isArithmeticType()) 1516 compat = false; 1517 } 1518 } 1519 1520 if (!compat) { 1521 Diag(Loc, diag::warn_accessor_property_type_mismatch) 1522 << property->getDeclName() 1523 << GetterMethod->getSelector(); 1524 Diag(GetterMethod->getLocation(), diag::note_declared_at); 1525 return true; 1526 } 1527 1528 return false; 1529 } 1530 1531 /// CollectImmediateProperties - This routine collects all properties in 1532 /// the class and its conforming protocols; but not those in its super class. 1533 static void 1534 CollectImmediateProperties(ObjCContainerDecl *CDecl, 1535 ObjCContainerDecl::PropertyMap &PropMap, 1536 ObjCContainerDecl::PropertyMap &SuperPropMap, 1537 bool CollectClassPropsOnly = false, 1538 bool IncludeProtocols = true) { 1539 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { 1540 for (auto *Prop : IDecl->properties()) { 1541 if (CollectClassPropsOnly && !Prop->isClassProperty()) 1542 continue; 1543 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = 1544 Prop; 1545 } 1546 1547 // Collect the properties from visible extensions. 1548 for (auto *Ext : IDecl->visible_extensions()) 1549 CollectImmediateProperties(Ext, PropMap, SuperPropMap, 1550 CollectClassPropsOnly, IncludeProtocols); 1551 1552 if (IncludeProtocols) { 1553 // Scan through class's protocols. 1554 for (auto *PI : IDecl->all_referenced_protocols()) 1555 CollectImmediateProperties(PI, PropMap, SuperPropMap, 1556 CollectClassPropsOnly); 1557 } 1558 } 1559 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1560 for (auto *Prop : CATDecl->properties()) { 1561 if (CollectClassPropsOnly && !Prop->isClassProperty()) 1562 continue; 1563 PropMap[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = 1564 Prop; 1565 } 1566 if (IncludeProtocols) { 1567 // Scan through class's protocols. 1568 for (auto *PI : CATDecl->protocols()) 1569 CollectImmediateProperties(PI, PropMap, SuperPropMap, 1570 CollectClassPropsOnly); 1571 } 1572 } 1573 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { 1574 for (auto *Prop : PDecl->properties()) { 1575 if (CollectClassPropsOnly && !Prop->isClassProperty()) 1576 continue; 1577 ObjCPropertyDecl *PropertyFromSuper = 1578 SuperPropMap[std::make_pair(Prop->getIdentifier(), 1579 Prop->isClassProperty())]; 1580 // Exclude property for protocols which conform to class's super-class, 1581 // as super-class has to implement the property. 1582 if (!PropertyFromSuper || 1583 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { 1584 ObjCPropertyDecl *&PropEntry = 1585 PropMap[std::make_pair(Prop->getIdentifier(), 1586 Prop->isClassProperty())]; 1587 if (!PropEntry) 1588 PropEntry = Prop; 1589 } 1590 } 1591 // Scan through protocol's protocols. 1592 for (auto *PI : PDecl->protocols()) 1593 CollectImmediateProperties(PI, PropMap, SuperPropMap, 1594 CollectClassPropsOnly); 1595 } 1596 } 1597 1598 /// CollectSuperClassPropertyImplementations - This routine collects list of 1599 /// properties to be implemented in super class(s) and also coming from their 1600 /// conforming protocols. 1601 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, 1602 ObjCInterfaceDecl::PropertyMap &PropMap) { 1603 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { 1604 ObjCInterfaceDecl::PropertyDeclOrder PO; 1605 while (SDecl) { 1606 SDecl->collectPropertiesToImplement(PropMap, PO); 1607 SDecl = SDecl->getSuperClass(); 1608 } 1609 } 1610 } 1611 1612 /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is 1613 /// an ivar synthesized for 'Method' and 'Method' is a property accessor 1614 /// declared in class 'IFace'. 1615 bool 1616 Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, 1617 ObjCMethodDecl *Method, ObjCIvarDecl *IV) { 1618 if (!IV->getSynthesize()) 1619 return false; 1620 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(), 1621 Method->isInstanceMethod()); 1622 if (!IMD || !IMD->isPropertyAccessor()) 1623 return false; 1624 1625 // look up a property declaration whose one of its accessors is implemented 1626 // by this method. 1627 for (const auto *Property : IFace->instance_properties()) { 1628 if ((Property->getGetterName() == IMD->getSelector() || 1629 Property->getSetterName() == IMD->getSelector()) && 1630 (Property->getPropertyIvarDecl() == IV)) 1631 return true; 1632 } 1633 // Also look up property declaration in class extension whose one of its 1634 // accessors is implemented by this method. 1635 for (const auto *Ext : IFace->known_extensions()) 1636 for (const auto *Property : Ext->instance_properties()) 1637 if ((Property->getGetterName() == IMD->getSelector() || 1638 Property->getSetterName() == IMD->getSelector()) && 1639 (Property->getPropertyIvarDecl() == IV)) 1640 return true; 1641 return false; 1642 } 1643 1644 static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl, 1645 ObjCPropertyDecl *Prop) { 1646 bool SuperClassImplementsGetter = false; 1647 bool SuperClassImplementsSetter = false; 1648 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) 1649 SuperClassImplementsSetter = true; 1650 1651 while (IDecl->getSuperClass()) { 1652 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); 1653 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName())) 1654 SuperClassImplementsGetter = true; 1655 1656 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName())) 1657 SuperClassImplementsSetter = true; 1658 if (SuperClassImplementsGetter && SuperClassImplementsSetter) 1659 return true; 1660 IDecl = IDecl->getSuperClass(); 1661 } 1662 return false; 1663 } 1664 1665 /// \brief Default synthesizes all properties which must be synthesized 1666 /// in class's \@implementation. 1667 void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, 1668 ObjCInterfaceDecl *IDecl) { 1669 ObjCInterfaceDecl::PropertyMap PropMap; 1670 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder; 1671 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder); 1672 if (PropMap.empty()) 1673 return; 1674 ObjCInterfaceDecl::PropertyMap SuperPropMap; 1675 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); 1676 1677 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) { 1678 ObjCPropertyDecl *Prop = PropertyOrder[i]; 1679 // Is there a matching property synthesize/dynamic? 1680 if (Prop->isInvalidDecl() || 1681 Prop->isClassProperty() || 1682 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1683 continue; 1684 // Property may have been synthesized by user. 1685 if (IMPDecl->FindPropertyImplDecl( 1686 Prop->getIdentifier(), Prop->getQueryKind())) 1687 continue; 1688 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { 1689 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) 1690 continue; 1691 if (IMPDecl->getInstanceMethod(Prop->getSetterName())) 1692 continue; 1693 } 1694 if (ObjCPropertyImplDecl *PID = 1695 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) { 1696 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property) 1697 << Prop->getIdentifier(); 1698 if (PID->getLocation().isValid()) 1699 Diag(PID->getLocation(), diag::note_property_synthesize); 1700 continue; 1701 } 1702 ObjCPropertyDecl *PropInSuperClass = 1703 SuperPropMap[std::make_pair(Prop->getIdentifier(), 1704 Prop->isClassProperty())]; 1705 if (ObjCProtocolDecl *Proto = 1706 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) { 1707 // We won't auto-synthesize properties declared in protocols. 1708 // Suppress the warning if class's superclass implements property's 1709 // getter and implements property's setter (if readwrite property). 1710 // Or, if property is going to be implemented in its super class. 1711 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) { 1712 Diag(IMPDecl->getLocation(), 1713 diag::warn_auto_synthesizing_protocol_property) 1714 << Prop << Proto; 1715 Diag(Prop->getLocation(), diag::note_property_declare); 1716 } 1717 continue; 1718 } 1719 // If property to be implemented in the super class, ignore. 1720 if (PropInSuperClass) { 1721 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) && 1722 (PropInSuperClass->getPropertyAttributes() & 1723 ObjCPropertyDecl::OBJC_PR_readonly) && 1724 !IMPDecl->getInstanceMethod(Prop->getSetterName()) && 1725 !IDecl->HasUserDeclaredSetterMethod(Prop)) { 1726 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property) 1727 << Prop->getIdentifier(); 1728 Diag(PropInSuperClass->getLocation(), diag::note_property_declare); 1729 } 1730 else { 1731 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass) 1732 << Prop->getIdentifier(); 1733 Diag(PropInSuperClass->getLocation(), diag::note_property_declare); 1734 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); 1735 } 1736 continue; 1737 } 1738 // We use invalid SourceLocations for the synthesized ivars since they 1739 // aren't really synthesized at a particular location; they just exist. 1740 // Saying that they are located at the @implementation isn't really going 1741 // to help users. 1742 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( 1743 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), 1744 true, 1745 /* property = */ Prop->getIdentifier(), 1746 /* ivar = */ Prop->getDefaultSynthIvarName(Context), 1747 Prop->getLocation(), Prop->getQueryKind())); 1748 if (PIDecl) { 1749 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); 1750 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); 1751 } 1752 } 1753 } 1754 1755 void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { 1756 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()) 1757 return; 1758 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); 1759 if (!IC) 1760 return; 1761 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) 1762 if (!IDecl->isObjCRequiresPropertyDefs()) 1763 DefaultSynthesizeProperties(S, IC, IDecl); 1764 } 1765 1766 static void DiagnoseUnimplementedAccessor( 1767 Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method, 1768 ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C, 1769 ObjCPropertyDecl *Prop, 1770 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) { 1771 // Check to see if we have a corresponding selector in SMap and with the 1772 // right method type. 1773 auto I = std::find_if(SMap.begin(), SMap.end(), 1774 [&](const ObjCMethodDecl *x) { 1775 return x->getSelector() == Method && 1776 x->isClassMethod() == Prop->isClassProperty(); 1777 }); 1778 // When reporting on missing property setter/getter implementation in 1779 // categories, do not report when they are declared in primary class, 1780 // class's protocol, or one of it super classes. This is because, 1781 // the class is going to implement them. 1782 if (I == SMap.end() && 1783 (PrimaryClass == nullptr || 1784 !PrimaryClass->lookupPropertyAccessor(Method, C, 1785 Prop->isClassProperty()))) { 1786 unsigned diag = 1787 isa<ObjCCategoryDecl>(CDecl) 1788 ? (Prop->isClassProperty() 1789 ? diag::warn_impl_required_in_category_for_class_property 1790 : diag::warn_setter_getter_impl_required_in_category) 1791 : (Prop->isClassProperty() 1792 ? diag::warn_impl_required_for_class_property 1793 : diag::warn_setter_getter_impl_required); 1794 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method; 1795 S.Diag(Prop->getLocation(), diag::note_property_declare); 1796 if (S.LangOpts.ObjCDefaultSynthProperties && 1797 S.LangOpts.ObjCRuntime.isNonFragile()) 1798 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1799 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1800 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1801 } 1802 } 1803 1804 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, 1805 ObjCContainerDecl *CDecl, 1806 bool SynthesizeProperties) { 1807 ObjCContainerDecl::PropertyMap PropMap; 1808 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); 1809 1810 // Since we don't synthesize class properties, we should emit diagnose even 1811 // if SynthesizeProperties is true. 1812 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; 1813 // Gather properties which need not be implemented in this class 1814 // or category. 1815 if (!IDecl) 1816 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1817 // For categories, no need to implement properties declared in 1818 // its primary class (and its super classes) if property is 1819 // declared in one of those containers. 1820 if ((IDecl = C->getClassInterface())) { 1821 ObjCInterfaceDecl::PropertyDeclOrder PO; 1822 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); 1823 } 1824 } 1825 if (IDecl) 1826 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); 1827 1828 // When SynthesizeProperties is true, we only check class properties. 1829 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap, 1830 SynthesizeProperties/*CollectClassPropsOnly*/); 1831 1832 // Scan the @interface to see if any of the protocols it adopts 1833 // require an explicit implementation, via attribute 1834 // 'objc_protocol_requires_explicit_implementation'. 1835 if (IDecl) { 1836 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap; 1837 1838 for (auto *PDecl : IDecl->all_referenced_protocols()) { 1839 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) 1840 continue; 1841 // Lazily construct a set of all the properties in the @interface 1842 // of the class, without looking at the superclass. We cannot 1843 // use the call to CollectImmediateProperties() above as that 1844 // utilizes information from the super class's properties as well 1845 // as scans the adopted protocols. This work only triggers for protocols 1846 // with the attribute, which is very rare, and only occurs when 1847 // analyzing the @implementation. 1848 if (!LazyMap) { 1849 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; 1850 LazyMap.reset(new ObjCContainerDecl::PropertyMap()); 1851 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap, 1852 /* CollectClassPropsOnly */ false, 1853 /* IncludeProtocols */ false); 1854 } 1855 // Add the properties of 'PDecl' to the list of properties that 1856 // need to be implemented. 1857 for (auto *PropDecl : PDecl->properties()) { 1858 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(), 1859 PropDecl->isClassProperty())]) 1860 continue; 1861 PropMap[std::make_pair(PropDecl->getIdentifier(), 1862 PropDecl->isClassProperty())] = PropDecl; 1863 } 1864 } 1865 } 1866 1867 if (PropMap.empty()) 1868 return; 1869 1870 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; 1871 for (const auto *I : IMPDecl->property_impls()) 1872 PropImplMap.insert(I->getPropertyDecl()); 1873 1874 llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap; 1875 // Collect property accessors implemented in current implementation. 1876 for (const auto *I : IMPDecl->methods()) 1877 InsMap.insert(I); 1878 1879 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); 1880 ObjCInterfaceDecl *PrimaryClass = nullptr; 1881 if (C && !C->IsClassExtension()) 1882 if ((PrimaryClass = C->getClassInterface())) 1883 // Report unimplemented properties in the category as well. 1884 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { 1885 // When reporting on missing setter/getters, do not report when 1886 // setter/getter is implemented in category's primary class 1887 // implementation. 1888 for (const auto *I : IMP->methods()) 1889 InsMap.insert(I); 1890 } 1891 1892 for (ObjCContainerDecl::PropertyMap::iterator 1893 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { 1894 ObjCPropertyDecl *Prop = P->second; 1895 // Is there a matching property synthesize/dynamic? 1896 if (Prop->isInvalidDecl() || 1897 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || 1898 PropImplMap.count(Prop) || 1899 Prop->getAvailability() == AR_Unavailable) 1900 continue; 1901 1902 // Diagnose unimplemented getters and setters. 1903 DiagnoseUnimplementedAccessor(*this, 1904 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap); 1905 if (!Prop->isReadOnly()) 1906 DiagnoseUnimplementedAccessor(*this, 1907 PrimaryClass, Prop->getSetterName(), 1908 IMPDecl, CDecl, C, Prop, InsMap); 1909 } 1910 } 1911 1912 void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) { 1913 for (const auto *propertyImpl : impDecl->property_impls()) { 1914 const auto *property = propertyImpl->getPropertyDecl(); 1915 1916 // Warn about null_resettable properties with synthesized setters, 1917 // because the setter won't properly handle nil. 1918 if (propertyImpl->getPropertyImplementation() 1919 == ObjCPropertyImplDecl::Synthesize && 1920 (property->getPropertyAttributes() & 1921 ObjCPropertyDecl::OBJC_PR_null_resettable) && 1922 property->getGetterMethodDecl() && 1923 property->getSetterMethodDecl()) { 1924 auto *getterMethod = property->getGetterMethodDecl(); 1925 auto *setterMethod = property->getSetterMethodDecl(); 1926 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) && 1927 !impDecl->getInstanceMethod(getterMethod->getSelector())) { 1928 SourceLocation loc = propertyImpl->getLocation(); 1929 if (loc.isInvalid()) 1930 loc = impDecl->getLocStart(); 1931 1932 Diag(loc, diag::warn_null_resettable_setter) 1933 << setterMethod->getSelector() << property->getDeclName(); 1934 } 1935 } 1936 } 1937 } 1938 1939 void 1940 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, 1941 ObjCInterfaceDecl* IDecl) { 1942 // Rules apply in non-GC mode only 1943 if (getLangOpts().getGC() != LangOptions::NonGC) 1944 return; 1945 ObjCContainerDecl::PropertyMap PM; 1946 for (auto *Prop : IDecl->properties()) 1947 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; 1948 for (const auto *Ext : IDecl->known_extensions()) 1949 for (auto *Prop : Ext->properties()) 1950 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; 1951 1952 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end(); 1953 I != E; ++I) { 1954 const ObjCPropertyDecl *Property = I->second; 1955 ObjCMethodDecl *GetterMethod = nullptr; 1956 ObjCMethodDecl *SetterMethod = nullptr; 1957 bool LookedUpGetterSetter = false; 1958 1959 unsigned Attributes = Property->getPropertyAttributes(); 1960 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); 1961 1962 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && 1963 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { 1964 GetterMethod = Property->isClassProperty() ? 1965 IMPDecl->getClassMethod(Property->getGetterName()) : 1966 IMPDecl->getInstanceMethod(Property->getGetterName()); 1967 SetterMethod = Property->isClassProperty() ? 1968 IMPDecl->getClassMethod(Property->getSetterName()) : 1969 IMPDecl->getInstanceMethod(Property->getSetterName()); 1970 LookedUpGetterSetter = true; 1971 if (GetterMethod) { 1972 Diag(GetterMethod->getLocation(), 1973 diag::warn_default_atomic_custom_getter_setter) 1974 << Property->getIdentifier() << 0; 1975 Diag(Property->getLocation(), diag::note_property_declare); 1976 } 1977 if (SetterMethod) { 1978 Diag(SetterMethod->getLocation(), 1979 diag::warn_default_atomic_custom_getter_setter) 1980 << Property->getIdentifier() << 1; 1981 Diag(Property->getLocation(), diag::note_property_declare); 1982 } 1983 } 1984 1985 // We only care about readwrite atomic property. 1986 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || 1987 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) 1988 continue; 1989 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl( 1990 Property->getIdentifier(), Property->getQueryKind())) { 1991 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) 1992 continue; 1993 if (!LookedUpGetterSetter) { 1994 GetterMethod = Property->isClassProperty() ? 1995 IMPDecl->getClassMethod(Property->getGetterName()) : 1996 IMPDecl->getInstanceMethod(Property->getGetterName()); 1997 SetterMethod = Property->isClassProperty() ? 1998 IMPDecl->getClassMethod(Property->getSetterName()) : 1999 IMPDecl->getInstanceMethod(Property->getSetterName()); 2000 } 2001 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { 2002 SourceLocation MethodLoc = 2003 (GetterMethod ? GetterMethod->getLocation() 2004 : SetterMethod->getLocation()); 2005 Diag(MethodLoc, diag::warn_atomic_property_rule) 2006 << Property->getIdentifier() << (GetterMethod != nullptr) 2007 << (SetterMethod != nullptr); 2008 // fixit stuff. 2009 if (Property->getLParenLoc().isValid() && 2010 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { 2011 // @property () ... case. 2012 SourceLocation AfterLParen = 2013 getLocForEndOfToken(Property->getLParenLoc()); 2014 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, " 2015 : "nonatomic"; 2016 Diag(Property->getLocation(), 2017 diag::note_atomic_property_fixup_suggest) 2018 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr); 2019 } else if (Property->getLParenLoc().isInvalid()) { 2020 //@property id etc. 2021 SourceLocation startLoc = 2022 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); 2023 Diag(Property->getLocation(), 2024 diag::note_atomic_property_fixup_suggest) 2025 << FixItHint::CreateInsertion(startLoc, "(nonatomic) "); 2026 } 2027 else 2028 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); 2029 Diag(Property->getLocation(), diag::note_property_declare); 2030 } 2031 } 2032 } 2033 } 2034 2035 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { 2036 if (getLangOpts().getGC() == LangOptions::GCOnly) 2037 return; 2038 2039 for (const auto *PID : D->property_impls()) { 2040 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 2041 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && 2042 !PD->isClassProperty() && 2043 !D->getInstanceMethod(PD->getGetterName())) { 2044 ObjCMethodDecl *method = PD->getGetterMethodDecl(); 2045 if (!method) 2046 continue; 2047 ObjCMethodFamily family = method->getMethodFamily(); 2048 if (family == OMF_alloc || family == OMF_copy || 2049 family == OMF_mutableCopy || family == OMF_new) { 2050 if (getLangOpts().ObjCAutoRefCount) 2051 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule); 2052 else 2053 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule); 2054 2055 // Look for a getter explicitly declared alongside the property. 2056 // If we find one, use its location for the note. 2057 SourceLocation noteLoc = PD->getLocation(); 2058 SourceLocation fixItLoc; 2059 for (auto *getterRedecl : method->redecls()) { 2060 if (getterRedecl->isImplicit()) 2061 continue; 2062 if (getterRedecl->getDeclContext() != PD->getDeclContext()) 2063 continue; 2064 noteLoc = getterRedecl->getLocation(); 2065 fixItLoc = getterRedecl->getLocEnd(); 2066 } 2067 2068 Preprocessor &PP = getPreprocessor(); 2069 TokenValue tokens[] = { 2070 tok::kw___attribute, tok::l_paren, tok::l_paren, 2071 PP.getIdentifierInfo("objc_method_family"), tok::l_paren, 2072 PP.getIdentifierInfo("none"), tok::r_paren, 2073 tok::r_paren, tok::r_paren 2074 }; 2075 StringRef spelling = "__attribute__((objc_method_family(none)))"; 2076 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens); 2077 if (!macroName.empty()) 2078 spelling = macroName; 2079 2080 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family) 2081 << method->getDeclName() << spelling; 2082 if (fixItLoc.isValid()) { 2083 SmallString<64> fixItText(" "); 2084 fixItText += spelling; 2085 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText); 2086 } 2087 } 2088 } 2089 } 2090 } 2091 2092 void Sema::DiagnoseMissingDesignatedInitOverrides( 2093 const ObjCImplementationDecl *ImplD, 2094 const ObjCInterfaceDecl *IFD) { 2095 assert(IFD->hasDesignatedInitializers()); 2096 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass(); 2097 if (!SuperD) 2098 return; 2099 2100 SelectorSet InitSelSet; 2101 for (const auto *I : ImplD->instance_methods()) 2102 if (I->getMethodFamily() == OMF_init) 2103 InitSelSet.insert(I->getSelector()); 2104 2105 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits; 2106 SuperD->getDesignatedInitializers(DesignatedInits); 2107 for (SmallVector<const ObjCMethodDecl *, 8>::iterator 2108 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) { 2109 const ObjCMethodDecl *MD = *I; 2110 if (!InitSelSet.count(MD->getSelector())) { 2111 bool Ignore = false; 2112 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) { 2113 Ignore = IMD->isUnavailable(); 2114 } 2115 if (!Ignore) { 2116 Diag(ImplD->getLocation(), 2117 diag::warn_objc_implementation_missing_designated_init_override) 2118 << MD->getSelector(); 2119 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here); 2120 } 2121 } 2122 } 2123 } 2124 2125 /// AddPropertyAttrs - Propagates attributes from a property to the 2126 /// implicitly-declared getter or setter for that property. 2127 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, 2128 ObjCPropertyDecl *Property) { 2129 // Should we just clone all attributes over? 2130 for (const auto *A : Property->attrs()) { 2131 if (isa<DeprecatedAttr>(A) || 2132 isa<UnavailableAttr>(A) || 2133 isa<AvailabilityAttr>(A)) 2134 PropertyMethod->addAttr(A->clone(S.Context)); 2135 } 2136 } 2137 2138 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 2139 /// have the property type and issue diagnostics if they don't. 2140 /// Also synthesize a getter/setter method if none exist (and update the 2141 /// appropriate lookup tables. 2142 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) { 2143 ObjCMethodDecl *GetterMethod, *SetterMethod; 2144 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext()); 2145 if (CD->isInvalidDecl()) 2146 return; 2147 2148 bool IsClassProperty = property->isClassProperty(); 2149 GetterMethod = IsClassProperty ? 2150 CD->getClassMethod(property->getGetterName()) : 2151 CD->getInstanceMethod(property->getGetterName()); 2152 2153 // if setter or getter is not found in class extension, it might be 2154 // in the primary class. 2155 if (!GetterMethod) 2156 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) 2157 if (CatDecl->IsClassExtension()) 2158 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> 2159 getClassMethod(property->getGetterName()) : 2160 CatDecl->getClassInterface()-> 2161 getInstanceMethod(property->getGetterName()); 2162 2163 SetterMethod = IsClassProperty ? 2164 CD->getClassMethod(property->getSetterName()) : 2165 CD->getInstanceMethod(property->getSetterName()); 2166 if (!SetterMethod) 2167 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) 2168 if (CatDecl->IsClassExtension()) 2169 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> 2170 getClassMethod(property->getSetterName()) : 2171 CatDecl->getClassInterface()-> 2172 getInstanceMethod(property->getSetterName()); 2173 DiagnosePropertyAccessorMismatch(property, GetterMethod, 2174 property->getLocation()); 2175 2176 if (SetterMethod) { 2177 ObjCPropertyDecl::PropertyAttributeKind CAttr = 2178 property->getPropertyAttributes(); 2179 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && 2180 Context.getCanonicalType(SetterMethod->getReturnType()) != 2181 Context.VoidTy) 2182 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 2183 if (SetterMethod->param_size() != 1 || 2184 !Context.hasSameUnqualifiedType( 2185 (*SetterMethod->param_begin())->getType().getNonReferenceType(), 2186 property->getType().getNonReferenceType())) { 2187 Diag(property->getLocation(), 2188 diag::warn_accessor_property_type_mismatch) 2189 << property->getDeclName() 2190 << SetterMethod->getSelector(); 2191 Diag(SetterMethod->getLocation(), diag::note_declared_at); 2192 } 2193 } 2194 2195 // Synthesize getter/setter methods if none exist. 2196 // Find the default getter and if one not found, add one. 2197 // FIXME: The synthesized property we set here is misleading. We almost always 2198 // synthesize these methods unless the user explicitly provided prototypes 2199 // (which is odd, but allowed). Sema should be typechecking that the 2200 // declarations jive in that situation (which it is not currently). 2201 if (!GetterMethod) { 2202 // No instance/class method of same name as property getter name was found. 2203 // Declare a getter method and add it to the list of methods 2204 // for this class. 2205 SourceLocation Loc = property->getLocation(); 2206 2207 // If the property is null_resettable, the getter returns nonnull. 2208 QualType resultTy = property->getType(); 2209 if (property->getPropertyAttributes() & 2210 ObjCPropertyDecl::OBJC_PR_null_resettable) { 2211 QualType modifiedTy = resultTy; 2212 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) { 2213 if (*nullability == NullabilityKind::Unspecified) 2214 resultTy = Context.getAttributedType(AttributedType::attr_nonnull, 2215 modifiedTy, modifiedTy); 2216 } 2217 } 2218 2219 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, 2220 property->getGetterName(), 2221 resultTy, nullptr, CD, 2222 !IsClassProperty, /*isVariadic=*/false, 2223 /*isPropertyAccessor=*/true, 2224 /*isImplicitlyDeclared=*/true, /*isDefined=*/false, 2225 (property->getPropertyImplementation() == 2226 ObjCPropertyDecl::Optional) ? 2227 ObjCMethodDecl::Optional : 2228 ObjCMethodDecl::Required); 2229 CD->addDecl(GetterMethod); 2230 2231 AddPropertyAttrs(*this, GetterMethod, property); 2232 2233 if (property->hasAttr<NSReturnsNotRetainedAttr>()) 2234 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context, 2235 Loc)); 2236 2237 if (property->hasAttr<ObjCReturnsInnerPointerAttr>()) 2238 GetterMethod->addAttr( 2239 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc)); 2240 2241 if (const SectionAttr *SA = property->getAttr<SectionAttr>()) 2242 GetterMethod->addAttr( 2243 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, 2244 SA->getName(), Loc)); 2245 2246 if (getLangOpts().ObjCAutoRefCount) 2247 CheckARCMethodDecl(GetterMethod); 2248 } else 2249 // A user declared getter will be synthesize when @synthesize of 2250 // the property with the same name is seen in the @implementation 2251 GetterMethod->setPropertyAccessor(true); 2252 property->setGetterMethodDecl(GetterMethod); 2253 2254 // Skip setter if property is read-only. 2255 if (!property->isReadOnly()) { 2256 // Find the default setter and if one not found, add one. 2257 if (!SetterMethod) { 2258 // No instance/class method of same name as property setter name was 2259 // found. 2260 // Declare a setter method and add it to the list of methods 2261 // for this class. 2262 SourceLocation Loc = property->getLocation(); 2263 2264 SetterMethod = 2265 ObjCMethodDecl::Create(Context, Loc, Loc, 2266 property->getSetterName(), Context.VoidTy, 2267 nullptr, CD, !IsClassProperty, 2268 /*isVariadic=*/false, 2269 /*isPropertyAccessor=*/true, 2270 /*isImplicitlyDeclared=*/true, 2271 /*isDefined=*/false, 2272 (property->getPropertyImplementation() == 2273 ObjCPropertyDecl::Optional) ? 2274 ObjCMethodDecl::Optional : 2275 ObjCMethodDecl::Required); 2276 2277 // If the property is null_resettable, the setter accepts a 2278 // nullable value. 2279 QualType paramTy = property->getType().getUnqualifiedType(); 2280 if (property->getPropertyAttributes() & 2281 ObjCPropertyDecl::OBJC_PR_null_resettable) { 2282 QualType modifiedTy = paramTy; 2283 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){ 2284 if (*nullability == NullabilityKind::Unspecified) 2285 paramTy = Context.getAttributedType(AttributedType::attr_nullable, 2286 modifiedTy, modifiedTy); 2287 } 2288 } 2289 2290 // Invent the arguments for the setter. We don't bother making a 2291 // nice name for the argument. 2292 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 2293 Loc, Loc, 2294 property->getIdentifier(), 2295 paramTy, 2296 /*TInfo=*/nullptr, 2297 SC_None, 2298 nullptr); 2299 SetterMethod->setMethodParams(Context, Argument, None); 2300 2301 AddPropertyAttrs(*this, SetterMethod, property); 2302 2303 CD->addDecl(SetterMethod); 2304 if (const SectionAttr *SA = property->getAttr<SectionAttr>()) 2305 SetterMethod->addAttr( 2306 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, 2307 SA->getName(), Loc)); 2308 // It's possible for the user to have set a very odd custom 2309 // setter selector that causes it to have a method family. 2310 if (getLangOpts().ObjCAutoRefCount) 2311 CheckARCMethodDecl(SetterMethod); 2312 } else 2313 // A user declared setter will be synthesize when @synthesize of 2314 // the property with the same name is seen in the @implementation 2315 SetterMethod->setPropertyAccessor(true); 2316 property->setSetterMethodDecl(SetterMethod); 2317 } 2318 // Add any synthesized methods to the global pool. This allows us to 2319 // handle the following, which is supported by GCC (and part of the design). 2320 // 2321 // @interface Foo 2322 // @property double bar; 2323 // @end 2324 // 2325 // void thisIsUnfortunate() { 2326 // id foo; 2327 // double bar = [foo bar]; 2328 // } 2329 // 2330 if (!IsClassProperty) { 2331 if (GetterMethod) 2332 AddInstanceMethodToGlobalPool(GetterMethod); 2333 if (SetterMethod) 2334 AddInstanceMethodToGlobalPool(SetterMethod); 2335 } else { 2336 if (GetterMethod) 2337 AddFactoryMethodToGlobalPool(GetterMethod); 2338 if (SetterMethod) 2339 AddFactoryMethodToGlobalPool(SetterMethod); 2340 } 2341 2342 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); 2343 if (!CurrentClass) { 2344 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) 2345 CurrentClass = Cat->getClassInterface(); 2346 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) 2347 CurrentClass = Impl->getClassInterface(); 2348 } 2349 if (GetterMethod) 2350 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); 2351 if (SetterMethod) 2352 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); 2353 } 2354 2355 void Sema::CheckObjCPropertyAttributes(Decl *PDecl, 2356 SourceLocation Loc, 2357 unsigned &Attributes, 2358 bool propertyInPrimaryClass) { 2359 // FIXME: Improve the reported location. 2360 if (!PDecl || PDecl->isInvalidDecl()) 2361 return; 2362 2363 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2364 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) 2365 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2366 << "readonly" << "readwrite"; 2367 2368 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); 2369 QualType PropertyTy = PropertyDecl->getType(); 2370 2371 // Check for copy or retain on non-object types. 2372 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2373 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && 2374 !PropertyTy->isObjCRetainableType() && 2375 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) { 2376 Diag(Loc, diag::err_objc_property_requires_object) 2377 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : 2378 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); 2379 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2380 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); 2381 PropertyDecl->setInvalidDecl(); 2382 } 2383 2384 // Check for more than one of { assign, copy, retain }. 2385 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 2386 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2387 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2388 << "assign" << "copy"; 2389 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2390 } 2391 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2392 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2393 << "assign" << "retain"; 2394 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2395 } 2396 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2397 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2398 << "assign" << "strong"; 2399 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2400 } 2401 if (getLangOpts().ObjCAutoRefCount && 2402 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2403 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2404 << "assign" << "weak"; 2405 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2406 } 2407 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>()) 2408 Diag(Loc, diag::warn_iboutletcollection_property_assign); 2409 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { 2410 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2411 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2412 << "unsafe_unretained" << "copy"; 2413 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2414 } 2415 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2416 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2417 << "unsafe_unretained" << "retain"; 2418 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2419 } 2420 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2421 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2422 << "unsafe_unretained" << "strong"; 2423 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2424 } 2425 if (getLangOpts().ObjCAutoRefCount && 2426 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2427 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2428 << "unsafe_unretained" << "weak"; 2429 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2430 } 2431 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2432 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2433 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2434 << "copy" << "retain"; 2435 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2436 } 2437 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2438 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2439 << "copy" << "strong"; 2440 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2441 } 2442 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 2443 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2444 << "copy" << "weak"; 2445 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2446 } 2447 } 2448 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2449 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2450 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2451 << "retain" << "weak"; 2452 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2453 } 2454 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && 2455 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2456 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2457 << "strong" << "weak"; 2458 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2459 } 2460 2461 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 2462 // 'weak' and 'nonnull' are mutually exclusive. 2463 if (auto nullability = PropertyTy->getNullability(Context)) { 2464 if (*nullability == NullabilityKind::NonNull) 2465 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2466 << "nonnull" << "weak"; 2467 } 2468 } 2469 2470 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && 2471 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { 2472 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2473 << "atomic" << "nonatomic"; 2474 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; 2475 } 2476 2477 // Warn if user supplied no assignment attribute, property is 2478 // readwrite, and this is an object type. 2479 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) { 2480 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { 2481 // do nothing 2482 } else if (getLangOpts().ObjCAutoRefCount) { 2483 // With arc, @property definitions should default to strong when 2484 // not specified. 2485 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 2486 } else if (PropertyTy->isObjCObjectPointerType()) { 2487 bool isAnyClassTy = 2488 (PropertyTy->isObjCClassType() || 2489 PropertyTy->isObjCQualifiedClassType()); 2490 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to 2491 // issue any warning. 2492 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) 2493 ; 2494 else if (propertyInPrimaryClass) { 2495 // Don't issue warning on property with no life time in class 2496 // extension as it is inherited from property in primary class. 2497 // Skip this warning in gc-only mode. 2498 if (getLangOpts().getGC() != LangOptions::GCOnly) 2499 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 2500 2501 // If non-gc code warn that this is likely inappropriate. 2502 if (getLangOpts().getGC() == LangOptions::NonGC) 2503 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 2504 } 2505 } 2506 2507 // FIXME: Implement warning dependent on NSCopying being 2508 // implemented. See also: 2509 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 2510 // (please trim this list while you are at it). 2511 } 2512 2513 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 2514 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) 2515 && getLangOpts().getGC() == LangOptions::GCOnly 2516 && PropertyTy->isBlockPointerType()) 2517 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 2518 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2519 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2520 !(Attributes & ObjCDeclSpec::DQ_PR_strong) && 2521 PropertyTy->isBlockPointerType()) 2522 Diag(Loc, diag::warn_objc_property_retain_of_block); 2523 2524 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2525 (Attributes & ObjCDeclSpec::DQ_PR_setter)) 2526 Diag(Loc, diag::warn_objc_readonly_property_has_setter); 2527 } 2528