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