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