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