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(Sema &S, 1767 ObjCInterfaceDecl *PrimaryClass, 1768 Selector Method, 1769 ObjCImplDecl* IMPDecl, 1770 ObjCContainerDecl *CDecl, 1771 ObjCCategoryDecl *C, 1772 ObjCPropertyDecl *Prop, 1773 Sema::SelectorSet &SMap) { 1774 // When reporting on missing property setter/getter implementation in 1775 // categories, do not report when they are declared in primary class, 1776 // class's protocol, or one of it super classes. This is because, 1777 // the class is going to implement them. 1778 if (!SMap.count(Method) && 1779 (PrimaryClass == nullptr || 1780 !PrimaryClass->lookupPropertyAccessor(Method, C, 1781 Prop->isClassProperty()))) { 1782 unsigned diag = 1783 isa<ObjCCategoryDecl>(CDecl) 1784 ? (Prop->isClassProperty() 1785 ? diag::warn_impl_required_in_category_for_class_property 1786 : diag::warn_setter_getter_impl_required_in_category) 1787 : (Prop->isClassProperty() 1788 ? diag::warn_impl_required_for_class_property 1789 : diag::warn_setter_getter_impl_required); 1790 S.Diag(IMPDecl->getLocation(), diag) << Prop->getDeclName() << Method; 1791 S.Diag(Prop->getLocation(), diag::note_property_declare); 1792 if (S.LangOpts.ObjCDefaultSynthProperties && 1793 S.LangOpts.ObjCRuntime.isNonFragile()) 1794 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1795 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1796 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1797 } 1798 } 1799 1800 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, 1801 ObjCContainerDecl *CDecl, 1802 bool SynthesizeProperties) { 1803 ObjCContainerDecl::PropertyMap PropMap; 1804 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl); 1805 1806 // Since we don't synthesize class properties, we should emit diagnose even 1807 // if SynthesizeProperties is true. 1808 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; 1809 // Gather properties which need not be implemented in this class 1810 // or category. 1811 if (!IDecl) 1812 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1813 // For categories, no need to implement properties declared in 1814 // its primary class (and its super classes) if property is 1815 // declared in one of those containers. 1816 if ((IDecl = C->getClassInterface())) { 1817 ObjCInterfaceDecl::PropertyDeclOrder PO; 1818 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); 1819 } 1820 } 1821 if (IDecl) 1822 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); 1823 1824 // When SynthesizeProperties is true, we only check class properties. 1825 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap, 1826 SynthesizeProperties/*CollectClassPropsOnly*/); 1827 1828 // Scan the @interface to see if any of the protocols it adopts 1829 // require an explicit implementation, via attribute 1830 // 'objc_protocol_requires_explicit_implementation'. 1831 if (IDecl) { 1832 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap; 1833 1834 for (auto *PDecl : IDecl->all_referenced_protocols()) { 1835 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) 1836 continue; 1837 // Lazily construct a set of all the properties in the @interface 1838 // of the class, without looking at the superclass. We cannot 1839 // use the call to CollectImmediateProperties() above as that 1840 // utilizes information from the super class's properties as well 1841 // as scans the adopted protocols. This work only triggers for protocols 1842 // with the attribute, which is very rare, and only occurs when 1843 // analyzing the @implementation. 1844 if (!LazyMap) { 1845 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; 1846 LazyMap.reset(new ObjCContainerDecl::PropertyMap()); 1847 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap, 1848 /* CollectClassPropsOnly */ false, 1849 /* IncludeProtocols */ false); 1850 } 1851 // Add the properties of 'PDecl' to the list of properties that 1852 // need to be implemented. 1853 for (auto *PropDecl : PDecl->properties()) { 1854 if ((*LazyMap)[std::make_pair(PropDecl->getIdentifier(), 1855 PropDecl->isClassProperty())]) 1856 continue; 1857 PropMap[std::make_pair(PropDecl->getIdentifier(), 1858 PropDecl->isClassProperty())] = PropDecl; 1859 } 1860 } 1861 } 1862 1863 if (PropMap.empty()) 1864 return; 1865 1866 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; 1867 for (const auto *I : IMPDecl->property_impls()) 1868 PropImplMap.insert(I->getPropertyDecl()); 1869 1870 SelectorSet InsMap; 1871 // Collect property accessors implemented in current implementation. 1872 for (const auto *I : IMPDecl->methods()) 1873 InsMap.insert(I->getSelector()); 1874 1875 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); 1876 ObjCInterfaceDecl *PrimaryClass = nullptr; 1877 if (C && !C->IsClassExtension()) 1878 if ((PrimaryClass = C->getClassInterface())) 1879 // Report unimplemented properties in the category as well. 1880 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { 1881 // When reporting on missing setter/getters, do not report when 1882 // setter/getter is implemented in category's primary class 1883 // implementation. 1884 for (const auto *I : IMP->methods()) 1885 InsMap.insert(I->getSelector()); 1886 } 1887 1888 for (ObjCContainerDecl::PropertyMap::iterator 1889 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { 1890 ObjCPropertyDecl *Prop = P->second; 1891 // Is there a matching property synthesize/dynamic? 1892 if (Prop->isInvalidDecl() || 1893 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || 1894 PropImplMap.count(Prop) || 1895 Prop->getAvailability() == AR_Unavailable) 1896 continue; 1897 1898 // Diagnose unimplemented getters and setters. 1899 DiagnoseUnimplementedAccessor(*this, 1900 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap); 1901 if (!Prop->isReadOnly()) 1902 DiagnoseUnimplementedAccessor(*this, 1903 PrimaryClass, Prop->getSetterName(), 1904 IMPDecl, CDecl, C, Prop, InsMap); 1905 } 1906 } 1907 1908 void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) { 1909 for (const auto *propertyImpl : impDecl->property_impls()) { 1910 const auto *property = propertyImpl->getPropertyDecl(); 1911 1912 // Warn about null_resettable properties with synthesized setters, 1913 // because the setter won't properly handle nil. 1914 if (propertyImpl->getPropertyImplementation() 1915 == ObjCPropertyImplDecl::Synthesize && 1916 (property->getPropertyAttributes() & 1917 ObjCPropertyDecl::OBJC_PR_null_resettable) && 1918 property->getGetterMethodDecl() && 1919 property->getSetterMethodDecl()) { 1920 auto *getterMethod = property->getGetterMethodDecl(); 1921 auto *setterMethod = property->getSetterMethodDecl(); 1922 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) && 1923 !impDecl->getInstanceMethod(getterMethod->getSelector())) { 1924 SourceLocation loc = propertyImpl->getLocation(); 1925 if (loc.isInvalid()) 1926 loc = impDecl->getLocStart(); 1927 1928 Diag(loc, diag::warn_null_resettable_setter) 1929 << setterMethod->getSelector() << property->getDeclName(); 1930 } 1931 } 1932 } 1933 } 1934 1935 void 1936 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, 1937 ObjCInterfaceDecl* IDecl) { 1938 // Rules apply in non-GC mode only 1939 if (getLangOpts().getGC() != LangOptions::NonGC) 1940 return; 1941 ObjCContainerDecl::PropertyMap PM; 1942 for (auto *Prop : IDecl->properties()) 1943 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; 1944 for (const auto *Ext : IDecl->known_extensions()) 1945 for (auto *Prop : Ext->properties()) 1946 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop; 1947 1948 for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end(); 1949 I != E; ++I) { 1950 const ObjCPropertyDecl *Property = I->second; 1951 ObjCMethodDecl *GetterMethod = nullptr; 1952 ObjCMethodDecl *SetterMethod = nullptr; 1953 bool LookedUpGetterSetter = false; 1954 1955 unsigned Attributes = Property->getPropertyAttributes(); 1956 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); 1957 1958 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && 1959 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { 1960 GetterMethod = Property->isClassProperty() ? 1961 IMPDecl->getClassMethod(Property->getGetterName()) : 1962 IMPDecl->getInstanceMethod(Property->getGetterName()); 1963 SetterMethod = Property->isClassProperty() ? 1964 IMPDecl->getClassMethod(Property->getSetterName()) : 1965 IMPDecl->getInstanceMethod(Property->getSetterName()); 1966 LookedUpGetterSetter = true; 1967 if (GetterMethod) { 1968 Diag(GetterMethod->getLocation(), 1969 diag::warn_default_atomic_custom_getter_setter) 1970 << Property->getIdentifier() << 0; 1971 Diag(Property->getLocation(), diag::note_property_declare); 1972 } 1973 if (SetterMethod) { 1974 Diag(SetterMethod->getLocation(), 1975 diag::warn_default_atomic_custom_getter_setter) 1976 << Property->getIdentifier() << 1; 1977 Diag(Property->getLocation(), diag::note_property_declare); 1978 } 1979 } 1980 1981 // We only care about readwrite atomic property. 1982 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || 1983 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) 1984 continue; 1985 if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl( 1986 Property->getIdentifier(), Property->getQueryKind())) { 1987 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) 1988 continue; 1989 if (!LookedUpGetterSetter) { 1990 GetterMethod = Property->isClassProperty() ? 1991 IMPDecl->getClassMethod(Property->getGetterName()) : 1992 IMPDecl->getInstanceMethod(Property->getGetterName()); 1993 SetterMethod = Property->isClassProperty() ? 1994 IMPDecl->getClassMethod(Property->getSetterName()) : 1995 IMPDecl->getInstanceMethod(Property->getSetterName()); 1996 } 1997 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { 1998 SourceLocation MethodLoc = 1999 (GetterMethod ? GetterMethod->getLocation() 2000 : SetterMethod->getLocation()); 2001 Diag(MethodLoc, diag::warn_atomic_property_rule) 2002 << Property->getIdentifier() << (GetterMethod != nullptr) 2003 << (SetterMethod != nullptr); 2004 // fixit stuff. 2005 if (Property->getLParenLoc().isValid() && 2006 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { 2007 // @property () ... case. 2008 SourceLocation AfterLParen = 2009 getLocForEndOfToken(Property->getLParenLoc()); 2010 StringRef NonatomicStr = AttributesAsWritten? "nonatomic, " 2011 : "nonatomic"; 2012 Diag(Property->getLocation(), 2013 diag::note_atomic_property_fixup_suggest) 2014 << FixItHint::CreateInsertion(AfterLParen, NonatomicStr); 2015 } else if (Property->getLParenLoc().isInvalid()) { 2016 //@property id etc. 2017 SourceLocation startLoc = 2018 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); 2019 Diag(Property->getLocation(), 2020 diag::note_atomic_property_fixup_suggest) 2021 << FixItHint::CreateInsertion(startLoc, "(nonatomic) "); 2022 } 2023 else 2024 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); 2025 Diag(Property->getLocation(), diag::note_property_declare); 2026 } 2027 } 2028 } 2029 } 2030 2031 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { 2032 if (getLangOpts().getGC() == LangOptions::GCOnly) 2033 return; 2034 2035 for (const auto *PID : D->property_impls()) { 2036 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 2037 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && 2038 !PD->isClassProperty() && 2039 !D->getInstanceMethod(PD->getGetterName())) { 2040 ObjCMethodDecl *method = PD->getGetterMethodDecl(); 2041 if (!method) 2042 continue; 2043 ObjCMethodFamily family = method->getMethodFamily(); 2044 if (family == OMF_alloc || family == OMF_copy || 2045 family == OMF_mutableCopy || family == OMF_new) { 2046 if (getLangOpts().ObjCAutoRefCount) 2047 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule); 2048 else 2049 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule); 2050 2051 // Look for a getter explicitly declared alongside the property. 2052 // If we find one, use its location for the note. 2053 SourceLocation noteLoc = PD->getLocation(); 2054 SourceLocation fixItLoc; 2055 for (auto *getterRedecl : method->redecls()) { 2056 if (getterRedecl->isImplicit()) 2057 continue; 2058 if (getterRedecl->getDeclContext() != PD->getDeclContext()) 2059 continue; 2060 noteLoc = getterRedecl->getLocation(); 2061 fixItLoc = getterRedecl->getLocEnd(); 2062 } 2063 2064 Preprocessor &PP = getPreprocessor(); 2065 TokenValue tokens[] = { 2066 tok::kw___attribute, tok::l_paren, tok::l_paren, 2067 PP.getIdentifierInfo("objc_method_family"), tok::l_paren, 2068 PP.getIdentifierInfo("none"), tok::r_paren, 2069 tok::r_paren, tok::r_paren 2070 }; 2071 StringRef spelling = "__attribute__((objc_method_family(none)))"; 2072 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens); 2073 if (!macroName.empty()) 2074 spelling = macroName; 2075 2076 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family) 2077 << method->getDeclName() << spelling; 2078 if (fixItLoc.isValid()) { 2079 SmallString<64> fixItText(" "); 2080 fixItText += spelling; 2081 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText); 2082 } 2083 } 2084 } 2085 } 2086 } 2087 2088 void Sema::DiagnoseMissingDesignatedInitOverrides( 2089 const ObjCImplementationDecl *ImplD, 2090 const ObjCInterfaceDecl *IFD) { 2091 assert(IFD->hasDesignatedInitializers()); 2092 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass(); 2093 if (!SuperD) 2094 return; 2095 2096 SelectorSet InitSelSet; 2097 for (const auto *I : ImplD->instance_methods()) 2098 if (I->getMethodFamily() == OMF_init) 2099 InitSelSet.insert(I->getSelector()); 2100 2101 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits; 2102 SuperD->getDesignatedInitializers(DesignatedInits); 2103 for (SmallVector<const ObjCMethodDecl *, 8>::iterator 2104 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) { 2105 const ObjCMethodDecl *MD = *I; 2106 if (!InitSelSet.count(MD->getSelector())) { 2107 bool Ignore = false; 2108 if (auto *IMD = IFD->getInstanceMethod(MD->getSelector())) { 2109 Ignore = IMD->isUnavailable(); 2110 } 2111 if (!Ignore) { 2112 Diag(ImplD->getLocation(), 2113 diag::warn_objc_implementation_missing_designated_init_override) 2114 << MD->getSelector(); 2115 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here); 2116 } 2117 } 2118 } 2119 } 2120 2121 /// AddPropertyAttrs - Propagates attributes from a property to the 2122 /// implicitly-declared getter or setter for that property. 2123 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, 2124 ObjCPropertyDecl *Property) { 2125 // Should we just clone all attributes over? 2126 for (const auto *A : Property->attrs()) { 2127 if (isa<DeprecatedAttr>(A) || 2128 isa<UnavailableAttr>(A) || 2129 isa<AvailabilityAttr>(A)) 2130 PropertyMethod->addAttr(A->clone(S.Context)); 2131 } 2132 } 2133 2134 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 2135 /// have the property type and issue diagnostics if they don't. 2136 /// Also synthesize a getter/setter method if none exist (and update the 2137 /// appropriate lookup tables. 2138 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property) { 2139 ObjCMethodDecl *GetterMethod, *SetterMethod; 2140 ObjCContainerDecl *CD = cast<ObjCContainerDecl>(property->getDeclContext()); 2141 if (CD->isInvalidDecl()) 2142 return; 2143 2144 bool IsClassProperty = property->isClassProperty(); 2145 GetterMethod = IsClassProperty ? 2146 CD->getClassMethod(property->getGetterName()) : 2147 CD->getInstanceMethod(property->getGetterName()); 2148 2149 // if setter or getter is not found in class extension, it might be 2150 // in the primary class. 2151 if (!GetterMethod) 2152 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) 2153 if (CatDecl->IsClassExtension()) 2154 GetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> 2155 getClassMethod(property->getGetterName()) : 2156 CatDecl->getClassInterface()-> 2157 getInstanceMethod(property->getGetterName()); 2158 2159 SetterMethod = IsClassProperty ? 2160 CD->getClassMethod(property->getSetterName()) : 2161 CD->getInstanceMethod(property->getSetterName()); 2162 if (!SetterMethod) 2163 if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CD)) 2164 if (CatDecl->IsClassExtension()) 2165 SetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> 2166 getClassMethod(property->getSetterName()) : 2167 CatDecl->getClassInterface()-> 2168 getInstanceMethod(property->getSetterName()); 2169 DiagnosePropertyAccessorMismatch(property, GetterMethod, 2170 property->getLocation()); 2171 2172 if (SetterMethod) { 2173 ObjCPropertyDecl::PropertyAttributeKind CAttr = 2174 property->getPropertyAttributes(); 2175 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && 2176 Context.getCanonicalType(SetterMethod->getReturnType()) != 2177 Context.VoidTy) 2178 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 2179 if (SetterMethod->param_size() != 1 || 2180 !Context.hasSameUnqualifiedType( 2181 (*SetterMethod->param_begin())->getType().getNonReferenceType(), 2182 property->getType().getNonReferenceType())) { 2183 Diag(property->getLocation(), 2184 diag::warn_accessor_property_type_mismatch) 2185 << property->getDeclName() 2186 << SetterMethod->getSelector(); 2187 Diag(SetterMethod->getLocation(), diag::note_declared_at); 2188 } 2189 } 2190 2191 // Synthesize getter/setter methods if none exist. 2192 // Find the default getter and if one not found, add one. 2193 // FIXME: The synthesized property we set here is misleading. We almost always 2194 // synthesize these methods unless the user explicitly provided prototypes 2195 // (which is odd, but allowed). Sema should be typechecking that the 2196 // declarations jive in that situation (which it is not currently). 2197 if (!GetterMethod) { 2198 // No instance/class method of same name as property getter name was found. 2199 // Declare a getter method and add it to the list of methods 2200 // for this class. 2201 SourceLocation Loc = property->getLocation(); 2202 2203 // If the property is null_resettable, the getter returns nonnull. 2204 QualType resultTy = property->getType(); 2205 if (property->getPropertyAttributes() & 2206 ObjCPropertyDecl::OBJC_PR_null_resettable) { 2207 QualType modifiedTy = resultTy; 2208 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) { 2209 if (*nullability == NullabilityKind::Unspecified) 2210 resultTy = Context.getAttributedType(AttributedType::attr_nonnull, 2211 modifiedTy, modifiedTy); 2212 } 2213 } 2214 2215 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, 2216 property->getGetterName(), 2217 resultTy, nullptr, CD, 2218 !IsClassProperty, /*isVariadic=*/false, 2219 /*isPropertyAccessor=*/true, 2220 /*isImplicitlyDeclared=*/true, /*isDefined=*/false, 2221 (property->getPropertyImplementation() == 2222 ObjCPropertyDecl::Optional) ? 2223 ObjCMethodDecl::Optional : 2224 ObjCMethodDecl::Required); 2225 CD->addDecl(GetterMethod); 2226 2227 AddPropertyAttrs(*this, GetterMethod, property); 2228 2229 if (property->hasAttr<NSReturnsNotRetainedAttr>()) 2230 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context, 2231 Loc)); 2232 2233 if (property->hasAttr<ObjCReturnsInnerPointerAttr>()) 2234 GetterMethod->addAttr( 2235 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc)); 2236 2237 if (const SectionAttr *SA = property->getAttr<SectionAttr>()) 2238 GetterMethod->addAttr( 2239 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, 2240 SA->getName(), Loc)); 2241 2242 if (getLangOpts().ObjCAutoRefCount) 2243 CheckARCMethodDecl(GetterMethod); 2244 } else 2245 // A user declared getter will be synthesize when @synthesize of 2246 // the property with the same name is seen in the @implementation 2247 GetterMethod->setPropertyAccessor(true); 2248 property->setGetterMethodDecl(GetterMethod); 2249 2250 // Skip setter if property is read-only. 2251 if (!property->isReadOnly()) { 2252 // Find the default setter and if one not found, add one. 2253 if (!SetterMethod) { 2254 // No instance/class method of same name as property setter name was 2255 // found. 2256 // Declare a setter method and add it to the list of methods 2257 // for this class. 2258 SourceLocation Loc = property->getLocation(); 2259 2260 SetterMethod = 2261 ObjCMethodDecl::Create(Context, Loc, Loc, 2262 property->getSetterName(), Context.VoidTy, 2263 nullptr, CD, !IsClassProperty, 2264 /*isVariadic=*/false, 2265 /*isPropertyAccessor=*/true, 2266 /*isImplicitlyDeclared=*/true, 2267 /*isDefined=*/false, 2268 (property->getPropertyImplementation() == 2269 ObjCPropertyDecl::Optional) ? 2270 ObjCMethodDecl::Optional : 2271 ObjCMethodDecl::Required); 2272 2273 // If the property is null_resettable, the setter accepts a 2274 // nullable value. 2275 QualType paramTy = property->getType().getUnqualifiedType(); 2276 if (property->getPropertyAttributes() & 2277 ObjCPropertyDecl::OBJC_PR_null_resettable) { 2278 QualType modifiedTy = paramTy; 2279 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){ 2280 if (*nullability == NullabilityKind::Unspecified) 2281 paramTy = Context.getAttributedType(AttributedType::attr_nullable, 2282 modifiedTy, modifiedTy); 2283 } 2284 } 2285 2286 // Invent the arguments for the setter. We don't bother making a 2287 // nice name for the argument. 2288 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 2289 Loc, Loc, 2290 property->getIdentifier(), 2291 paramTy, 2292 /*TInfo=*/nullptr, 2293 SC_None, 2294 nullptr); 2295 SetterMethod->setMethodParams(Context, Argument, None); 2296 2297 AddPropertyAttrs(*this, SetterMethod, property); 2298 2299 CD->addDecl(SetterMethod); 2300 if (const SectionAttr *SA = property->getAttr<SectionAttr>()) 2301 SetterMethod->addAttr( 2302 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section, 2303 SA->getName(), Loc)); 2304 // It's possible for the user to have set a very odd custom 2305 // setter selector that causes it to have a method family. 2306 if (getLangOpts().ObjCAutoRefCount) 2307 CheckARCMethodDecl(SetterMethod); 2308 } else 2309 // A user declared setter will be synthesize when @synthesize of 2310 // the property with the same name is seen in the @implementation 2311 SetterMethod->setPropertyAccessor(true); 2312 property->setSetterMethodDecl(SetterMethod); 2313 } 2314 // Add any synthesized methods to the global pool. This allows us to 2315 // handle the following, which is supported by GCC (and part of the design). 2316 // 2317 // @interface Foo 2318 // @property double bar; 2319 // @end 2320 // 2321 // void thisIsUnfortunate() { 2322 // id foo; 2323 // double bar = [foo bar]; 2324 // } 2325 // 2326 if (!IsClassProperty) { 2327 if (GetterMethod) 2328 AddInstanceMethodToGlobalPool(GetterMethod); 2329 if (SetterMethod) 2330 AddInstanceMethodToGlobalPool(SetterMethod); 2331 } else { 2332 if (GetterMethod) 2333 AddFactoryMethodToGlobalPool(GetterMethod); 2334 if (SetterMethod) 2335 AddFactoryMethodToGlobalPool(SetterMethod); 2336 } 2337 2338 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); 2339 if (!CurrentClass) { 2340 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) 2341 CurrentClass = Cat->getClassInterface(); 2342 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) 2343 CurrentClass = Impl->getClassInterface(); 2344 } 2345 if (GetterMethod) 2346 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); 2347 if (SetterMethod) 2348 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); 2349 } 2350 2351 void Sema::CheckObjCPropertyAttributes(Decl *PDecl, 2352 SourceLocation Loc, 2353 unsigned &Attributes, 2354 bool propertyInPrimaryClass) { 2355 // FIXME: Improve the reported location. 2356 if (!PDecl || PDecl->isInvalidDecl()) 2357 return; 2358 2359 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2360 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) 2361 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2362 << "readonly" << "readwrite"; 2363 2364 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); 2365 QualType PropertyTy = PropertyDecl->getType(); 2366 2367 // Check for copy or retain on non-object types. 2368 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2369 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && 2370 !PropertyTy->isObjCRetainableType() && 2371 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) { 2372 Diag(Loc, diag::err_objc_property_requires_object) 2373 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : 2374 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); 2375 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2376 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); 2377 PropertyDecl->setInvalidDecl(); 2378 } 2379 2380 // Check for more than one of { assign, copy, retain }. 2381 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 2382 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2383 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2384 << "assign" << "copy"; 2385 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2386 } 2387 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2388 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2389 << "assign" << "retain"; 2390 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2391 } 2392 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2393 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2394 << "assign" << "strong"; 2395 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2396 } 2397 if (getLangOpts().ObjCAutoRefCount && 2398 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2399 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2400 << "assign" << "weak"; 2401 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2402 } 2403 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>()) 2404 Diag(Loc, diag::warn_iboutletcollection_property_assign); 2405 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { 2406 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2407 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2408 << "unsafe_unretained" << "copy"; 2409 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2410 } 2411 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2412 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2413 << "unsafe_unretained" << "retain"; 2414 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2415 } 2416 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2417 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2418 << "unsafe_unretained" << "strong"; 2419 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2420 } 2421 if (getLangOpts().ObjCAutoRefCount && 2422 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2423 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2424 << "unsafe_unretained" << "weak"; 2425 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2426 } 2427 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2428 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2429 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2430 << "copy" << "retain"; 2431 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2432 } 2433 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2434 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2435 << "copy" << "strong"; 2436 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2437 } 2438 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 2439 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2440 << "copy" << "weak"; 2441 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2442 } 2443 } 2444 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2445 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2446 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2447 << "retain" << "weak"; 2448 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2449 } 2450 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && 2451 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2452 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2453 << "strong" << "weak"; 2454 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2455 } 2456 2457 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 2458 // 'weak' and 'nonnull' are mutually exclusive. 2459 if (auto nullability = PropertyTy->getNullability(Context)) { 2460 if (*nullability == NullabilityKind::NonNull) 2461 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2462 << "nonnull" << "weak"; 2463 } 2464 } 2465 2466 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && 2467 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { 2468 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2469 << "atomic" << "nonatomic"; 2470 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; 2471 } 2472 2473 // Warn if user supplied no assignment attribute, property is 2474 // readwrite, and this is an object type. 2475 if (!getOwnershipRule(Attributes) && PropertyTy->isObjCRetainableType()) { 2476 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { 2477 // do nothing 2478 } else if (getLangOpts().ObjCAutoRefCount) { 2479 // With arc, @property definitions should default to strong when 2480 // not specified. 2481 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 2482 } else if (PropertyTy->isObjCObjectPointerType()) { 2483 bool isAnyClassTy = 2484 (PropertyTy->isObjCClassType() || 2485 PropertyTy->isObjCQualifiedClassType()); 2486 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to 2487 // issue any warning. 2488 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) 2489 ; 2490 else if (propertyInPrimaryClass) { 2491 // Don't issue warning on property with no life time in class 2492 // extension as it is inherited from property in primary class. 2493 // Skip this warning in gc-only mode. 2494 if (getLangOpts().getGC() != LangOptions::GCOnly) 2495 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 2496 2497 // If non-gc code warn that this is likely inappropriate. 2498 if (getLangOpts().getGC() == LangOptions::NonGC) 2499 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 2500 } 2501 } 2502 2503 // FIXME: Implement warning dependent on NSCopying being 2504 // implemented. See also: 2505 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 2506 // (please trim this list while you are at it). 2507 } 2508 2509 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 2510 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) 2511 && getLangOpts().getGC() == LangOptions::GCOnly 2512 && PropertyTy->isBlockPointerType()) 2513 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 2514 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2515 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2516 !(Attributes & ObjCDeclSpec::DQ_PR_strong) && 2517 PropertyTy->isBlockPointerType()) 2518 Diag(Loc, diag::warn_objc_property_retain_of_block); 2519 2520 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2521 (Attributes & ObjCDeclSpec::DQ_PR_setter)) 2522 Diag(Loc, diag::warn_objc_readonly_property_has_setter); 2523 } 2524