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