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