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 /// DiagnoseClassAndClassExtPropertyMismatch - diagnose inconsistant property 718 /// attribute declared in primary class and attributes overridden in any of its 719 /// class extensions. 720 static void 721 DiagnoseClassAndClassExtPropertyMismatch(Sema &S, ObjCInterfaceDecl *ClassDecl, 722 ObjCPropertyDecl *property) { 723 unsigned Attributes = property->getPropertyAttributesAsWritten(); 724 bool warn = (Attributes & ObjCDeclSpec::DQ_PR_readonly); 725 for (ObjCInterfaceDecl::known_extensions_iterator 726 Ext = ClassDecl->known_extensions_begin(), 727 ExtEnd = ClassDecl->known_extensions_end(); 728 Ext != ExtEnd; ++Ext) { 729 ObjCPropertyDecl *ClassExtProperty = 0; 730 DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); 731 for (unsigned I = 0, N = R.size(); I != N; ++I) { 732 ClassExtProperty = dyn_cast<ObjCPropertyDecl>(R[0]); 733 if (ClassExtProperty) 734 break; 735 } 736 737 if (ClassExtProperty) { 738 warn = false; 739 unsigned classExtPropertyAttr = 740 ClassExtProperty->getPropertyAttributesAsWritten(); 741 // We are issuing the warning that we postponed because class extensions 742 // can override readonly->readwrite and 'setter' attributes originally 743 // placed on class's property declaration now make sense in the overridden 744 // property. 745 if (Attributes & ObjCDeclSpec::DQ_PR_readonly) { 746 if (!classExtPropertyAttr || 747 (classExtPropertyAttr & 748 (ObjCDeclSpec::DQ_PR_readwrite| 749 ObjCDeclSpec::DQ_PR_assign | 750 ObjCDeclSpec::DQ_PR_unsafe_unretained | 751 ObjCDeclSpec::DQ_PR_copy | 752 ObjCDeclSpec::DQ_PR_retain | 753 ObjCDeclSpec::DQ_PR_strong))) 754 continue; 755 warn = true; 756 break; 757 } 758 } 759 } 760 if (warn) { 761 unsigned setterAttrs = (ObjCDeclSpec::DQ_PR_assign | 762 ObjCDeclSpec::DQ_PR_unsafe_unretained | 763 ObjCDeclSpec::DQ_PR_copy | 764 ObjCDeclSpec::DQ_PR_retain | 765 ObjCDeclSpec::DQ_PR_strong); 766 if (Attributes & setterAttrs) { 767 const char * which = 768 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 769 "assign" : 770 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? 771 "unsafe_unretained" : 772 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 773 "copy" : 774 (Attributes & ObjCDeclSpec::DQ_PR_retain) ? 775 "retain" : "strong"; 776 777 S.Diag(property->getLocation(), 778 diag::warn_objc_property_attr_mutually_exclusive) 779 << "readonly" << which; 780 } 781 } 782 783 784 } 785 786 /// ActOnPropertyImplDecl - This routine performs semantic checks and 787 /// builds the AST node for a property implementation declaration; declared 788 /// as \@synthesize or \@dynamic. 789 /// 790 Decl *Sema::ActOnPropertyImplDecl(Scope *S, 791 SourceLocation AtLoc, 792 SourceLocation PropertyLoc, 793 bool Synthesize, 794 IdentifierInfo *PropertyId, 795 IdentifierInfo *PropertyIvar, 796 SourceLocation PropertyIvarLoc) { 797 ObjCContainerDecl *ClassImpDecl = 798 dyn_cast<ObjCContainerDecl>(CurContext); 799 // Make sure we have a context for the property implementation declaration. 800 if (!ClassImpDecl) { 801 Diag(AtLoc, diag::error_missing_property_context); 802 return 0; 803 } 804 if (PropertyIvarLoc.isInvalid()) 805 PropertyIvarLoc = PropertyLoc; 806 SourceLocation PropertyDiagLoc = PropertyLoc; 807 if (PropertyDiagLoc.isInvalid()) 808 PropertyDiagLoc = ClassImpDecl->getLocStart(); 809 ObjCPropertyDecl *property = 0; 810 ObjCInterfaceDecl* IDecl = 0; 811 // Find the class or category class where this property must have 812 // a declaration. 813 ObjCImplementationDecl *IC = 0; 814 ObjCCategoryImplDecl* CatImplClass = 0; 815 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { 816 IDecl = IC->getClassInterface(); 817 // We always synthesize an interface for an implementation 818 // without an interface decl. So, IDecl is always non-zero. 819 assert(IDecl && 820 "ActOnPropertyImplDecl - @implementation without @interface"); 821 822 // Look for this property declaration in the @implementation's @interface 823 property = IDecl->FindPropertyDeclaration(PropertyId); 824 if (!property) { 825 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); 826 return 0; 827 } 828 unsigned PIkind = property->getPropertyAttributesAsWritten(); 829 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic | 830 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) { 831 if (AtLoc.isValid()) 832 Diag(AtLoc, diag::warn_implicit_atomic_property); 833 else 834 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property); 835 Diag(property->getLocation(), diag::note_property_declare); 836 } 837 838 if (const ObjCCategoryDecl *CD = 839 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { 840 if (!CD->IsClassExtension()) { 841 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); 842 Diag(property->getLocation(), diag::note_property_declare); 843 return 0; 844 } 845 } 846 if (Synthesize&& 847 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) && 848 property->hasAttr<IBOutletAttr>() && 849 !AtLoc.isValid()) { 850 bool ReadWriteProperty = false; 851 // Search into the class extensions and see if 'readonly property is 852 // redeclared 'readwrite', then no warning is to be issued. 853 for (ObjCInterfaceDecl::known_extensions_iterator 854 Ext = IDecl->known_extensions_begin(), 855 ExtEnd = IDecl->known_extensions_end(); Ext != ExtEnd; ++Ext) { 856 DeclContext::lookup_result R = Ext->lookup(property->getDeclName()); 857 if (!R.empty()) 858 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) { 859 PIkind = ExtProp->getPropertyAttributesAsWritten(); 860 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) { 861 ReadWriteProperty = true; 862 break; 863 } 864 } 865 } 866 867 if (!ReadWriteProperty) { 868 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property) 869 << property->getName(); 870 SourceLocation readonlyLoc; 871 if (LocPropertyAttribute(Context, "readonly", 872 property->getLParenLoc(), readonlyLoc)) { 873 SourceLocation endLoc = 874 readonlyLoc.getLocWithOffset(strlen("readonly")-1); 875 SourceRange ReadonlySourceRange(readonlyLoc, endLoc); 876 Diag(property->getLocation(), 877 diag::note_auto_readonly_iboutlet_fixup_suggest) << 878 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite"); 879 } 880 } 881 } 882 883 DiagnoseClassAndClassExtPropertyMismatch(*this, IDecl, property); 884 885 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { 886 if (Synthesize) { 887 Diag(AtLoc, diag::error_synthesize_category_decl); 888 return 0; 889 } 890 IDecl = CatImplClass->getClassInterface(); 891 if (!IDecl) { 892 Diag(AtLoc, diag::error_missing_property_interface); 893 return 0; 894 } 895 ObjCCategoryDecl *Category = 896 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); 897 898 // If category for this implementation not found, it is an error which 899 // has already been reported eralier. 900 if (!Category) 901 return 0; 902 // Look for this property declaration in @implementation's category 903 property = Category->FindPropertyDeclaration(PropertyId); 904 if (!property) { 905 Diag(PropertyLoc, diag::error_bad_category_property_decl) 906 << Category->getDeclName(); 907 return 0; 908 } 909 } else { 910 Diag(AtLoc, diag::error_bad_property_context); 911 return 0; 912 } 913 ObjCIvarDecl *Ivar = 0; 914 bool CompleteTypeErr = false; 915 bool compat = true; 916 // Check that we have a valid, previously declared ivar for @synthesize 917 if (Synthesize) { 918 // @synthesize 919 if (!PropertyIvar) 920 PropertyIvar = PropertyId; 921 // Check that this is a previously declared 'ivar' in 'IDecl' interface 922 ObjCInterfaceDecl *ClassDeclared; 923 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); 924 QualType PropType = property->getType(); 925 QualType PropertyIvarType = PropType.getNonReferenceType(); 926 927 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType, 928 diag::err_incomplete_synthesized_property, 929 property->getDeclName())) { 930 Diag(property->getLocation(), diag::note_property_declare); 931 CompleteTypeErr = true; 932 } 933 934 if (getLangOpts().ObjCAutoRefCount && 935 (property->getPropertyAttributesAsWritten() & 936 ObjCPropertyDecl::OBJC_PR_readonly) && 937 PropertyIvarType->isObjCRetainableType()) { 938 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar); 939 } 940 941 ObjCPropertyDecl::PropertyAttributeKind kind 942 = property->getPropertyAttributes(); 943 944 // Add GC __weak to the ivar type if the property is weak. 945 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) && 946 getLangOpts().getGC() != LangOptions::NonGC) { 947 assert(!getLangOpts().ObjCAutoRefCount); 948 if (PropertyIvarType.isObjCGCStrong()) { 949 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type); 950 Diag(property->getLocation(), diag::note_property_declare); 951 } else { 952 PropertyIvarType = 953 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak); 954 } 955 } 956 if (AtLoc.isInvalid()) { 957 // Check when default synthesizing a property that there is 958 // an ivar matching property name and issue warning; since this 959 // is the most common case of not using an ivar used for backing 960 // property in non-default synthesis case. 961 ObjCInterfaceDecl *ClassDeclared=0; 962 ObjCIvarDecl *originalIvar = 963 IDecl->lookupInstanceVariable(property->getIdentifier(), 964 ClassDeclared); 965 if (originalIvar) { 966 Diag(PropertyDiagLoc, 967 diag::warn_autosynthesis_property_ivar_match) 968 << PropertyId << (Ivar == 0) << PropertyIvar 969 << originalIvar->getIdentifier(); 970 Diag(property->getLocation(), diag::note_property_declare); 971 Diag(originalIvar->getLocation(), diag::note_ivar_decl); 972 } 973 } 974 975 if (!Ivar) { 976 // In ARC, give the ivar a lifetime qualifier based on the 977 // property attributes. 978 if (getLangOpts().ObjCAutoRefCount && 979 !PropertyIvarType.getObjCLifetime() && 980 PropertyIvarType->isObjCRetainableType()) { 981 982 // It's an error if we have to do this and the user didn't 983 // explicitly write an ownership attribute on the property. 984 if (!property->hasWrittenStorageAttribute() && 985 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) { 986 Diag(PropertyDiagLoc, 987 diag::err_arc_objc_property_default_assign_on_object); 988 Diag(property->getLocation(), diag::note_property_declare); 989 } else { 990 Qualifiers::ObjCLifetime lifetime = 991 getImpliedARCOwnership(kind, PropertyIvarType); 992 assert(lifetime && "no lifetime for property?"); 993 if (lifetime == Qualifiers::OCL_Weak) { 994 bool err = false; 995 if (const ObjCObjectPointerType *ObjT = 996 PropertyIvarType->getAs<ObjCObjectPointerType>()) { 997 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); 998 if (ObjI && ObjI->isArcWeakrefUnavailable()) { 999 Diag(property->getLocation(), 1000 diag::err_arc_weak_unavailable_property) << PropertyIvarType; 1001 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class) 1002 << ClassImpDecl->getName(); 1003 err = true; 1004 } 1005 } 1006 if (!err && !getLangOpts().ObjCARCWeak) { 1007 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime); 1008 Diag(property->getLocation(), diag::note_property_declare); 1009 } 1010 } 1011 1012 Qualifiers qs; 1013 qs.addObjCLifetime(lifetime); 1014 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs); 1015 } 1016 } 1017 1018 if (kind & ObjCPropertyDecl::OBJC_PR_weak && 1019 !getLangOpts().ObjCAutoRefCount && 1020 getLangOpts().getGC() == LangOptions::NonGC) { 1021 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc); 1022 Diag(property->getLocation(), diag::note_property_declare); 1023 } 1024 1025 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, 1026 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar, 1027 PropertyIvarType, /*Dinfo=*/0, 1028 ObjCIvarDecl::Private, 1029 (Expr *)0, true); 1030 if (CompleteTypeErr) 1031 Ivar->setInvalidDecl(); 1032 ClassImpDecl->addDecl(Ivar); 1033 IDecl->makeDeclVisibleInContext(Ivar); 1034 1035 if (getLangOpts().ObjCRuntime.isFragile()) 1036 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl) 1037 << PropertyId; 1038 // Note! I deliberately want it to fall thru so, we have a 1039 // a property implementation and to avoid future warnings. 1040 } else if (getLangOpts().ObjCRuntime.isNonFragile() && 1041 !declaresSameEntity(ClassDeclared, IDecl)) { 1042 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use) 1043 << property->getDeclName() << Ivar->getDeclName() 1044 << ClassDeclared->getDeclName(); 1045 Diag(Ivar->getLocation(), diag::note_previous_access_declaration) 1046 << Ivar << Ivar->getName(); 1047 // Note! I deliberately want it to fall thru so more errors are caught. 1048 } 1049 property->setPropertyIvarDecl(Ivar); 1050 1051 QualType IvarType = Context.getCanonicalType(Ivar->getType()); 1052 1053 // Check that type of property and its ivar are type compatible. 1054 if (!Context.hasSameType(PropertyIvarType, IvarType)) { 1055 if (isa<ObjCObjectPointerType>(PropertyIvarType) 1056 && isa<ObjCObjectPointerType>(IvarType)) 1057 compat = 1058 Context.canAssignObjCInterfaces( 1059 PropertyIvarType->getAs<ObjCObjectPointerType>(), 1060 IvarType->getAs<ObjCObjectPointerType>()); 1061 else { 1062 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType, 1063 IvarType) 1064 == Compatible); 1065 } 1066 if (!compat) { 1067 Diag(PropertyDiagLoc, diag::error_property_ivar_type) 1068 << property->getDeclName() << PropType 1069 << Ivar->getDeclName() << IvarType; 1070 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1071 // Note! I deliberately want it to fall thru so, we have a 1072 // a property implementation and to avoid future warnings. 1073 } 1074 else { 1075 // FIXME! Rules for properties are somewhat different that those 1076 // for assignments. Use a new routine to consolidate all cases; 1077 // specifically for property redeclarations as well as for ivars. 1078 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); 1079 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); 1080 if (lhsType != rhsType && 1081 lhsType->isArithmeticType()) { 1082 Diag(PropertyDiagLoc, diag::error_property_ivar_type) 1083 << property->getDeclName() << PropType 1084 << Ivar->getDeclName() << IvarType; 1085 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1086 // Fall thru - see previous comment 1087 } 1088 } 1089 // __weak is explicit. So it works on Canonical type. 1090 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && 1091 getLangOpts().getGC() != LangOptions::NonGC)) { 1092 Diag(PropertyDiagLoc, diag::error_weak_property) 1093 << property->getDeclName() << Ivar->getDeclName(); 1094 Diag(Ivar->getLocation(), diag::note_ivar_decl); 1095 // Fall thru - see previous comment 1096 } 1097 // Fall thru - see previous comment 1098 if ((property->getType()->isObjCObjectPointerType() || 1099 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && 1100 getLangOpts().getGC() != LangOptions::NonGC) { 1101 Diag(PropertyDiagLoc, diag::error_strong_property) 1102 << property->getDeclName() << Ivar->getDeclName(); 1103 // Fall thru - see previous comment 1104 } 1105 } 1106 if (getLangOpts().ObjCAutoRefCount) 1107 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar); 1108 } else if (PropertyIvar) 1109 // @dynamic 1110 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl); 1111 1112 assert (property && "ActOnPropertyImplDecl - property declaration missing"); 1113 ObjCPropertyImplDecl *PIDecl = 1114 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, 1115 property, 1116 (Synthesize ? 1117 ObjCPropertyImplDecl::Synthesize 1118 : ObjCPropertyImplDecl::Dynamic), 1119 Ivar, PropertyIvarLoc); 1120 1121 if (CompleteTypeErr || !compat) 1122 PIDecl->setInvalidDecl(); 1123 1124 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { 1125 getterMethod->createImplicitParams(Context, IDecl); 1126 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && 1127 Ivar->getType()->isRecordType()) { 1128 // For Objective-C++, need to synthesize the AST for the IVAR object to be 1129 // returned by the getter as it must conform to C++'s copy-return rules. 1130 // FIXME. Eventually we want to do this for Objective-C as well. 1131 SynthesizedFunctionScope Scope(*this, getterMethod); 1132 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); 1133 DeclRefExpr *SelfExpr = 1134 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), 1135 VK_RValue, PropertyDiagLoc); 1136 MarkDeclRefReferenced(SelfExpr); 1137 Expr *IvarRefExpr = 1138 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, 1139 Ivar->getLocation(), 1140 SelfExpr, true, true); 1141 ExprResult Res = 1142 PerformCopyInitialization(InitializedEntity::InitializeResult( 1143 PropertyDiagLoc, 1144 getterMethod->getResultType(), 1145 /*NRVO=*/false), 1146 PropertyDiagLoc, 1147 Owned(IvarRefExpr)); 1148 if (!Res.isInvalid()) { 1149 Expr *ResExpr = Res.takeAs<Expr>(); 1150 if (ResExpr) 1151 ResExpr = MaybeCreateExprWithCleanups(ResExpr); 1152 PIDecl->setGetterCXXConstructor(ResExpr); 1153 } 1154 } 1155 if (property->hasAttr<NSReturnsNotRetainedAttr>() && 1156 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { 1157 Diag(getterMethod->getLocation(), 1158 diag::warn_property_getter_owning_mismatch); 1159 Diag(property->getLocation(), diag::note_property_declare); 1160 } 1161 } 1162 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { 1163 setterMethod->createImplicitParams(Context, IDecl); 1164 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && 1165 Ivar->getType()->isRecordType()) { 1166 // FIXME. Eventually we want to do this for Objective-C as well. 1167 SynthesizedFunctionScope Scope(*this, setterMethod); 1168 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); 1169 DeclRefExpr *SelfExpr = 1170 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(), 1171 VK_RValue, PropertyDiagLoc); 1172 MarkDeclRefReferenced(SelfExpr); 1173 Expr *lhs = 1174 new (Context) ObjCIvarRefExpr(Ivar, Ivar->getType(), PropertyDiagLoc, 1175 Ivar->getLocation(), 1176 SelfExpr, true, true); 1177 ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); 1178 ParmVarDecl *Param = (*P); 1179 QualType T = Param->getType().getNonReferenceType(); 1180 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T, 1181 VK_LValue, PropertyDiagLoc); 1182 MarkDeclRefReferenced(rhs); 1183 ExprResult Res = BuildBinOp(S, PropertyDiagLoc, 1184 BO_Assign, lhs, rhs); 1185 if (property->getPropertyAttributes() & 1186 ObjCPropertyDecl::OBJC_PR_atomic) { 1187 Expr *callExpr = Res.takeAs<Expr>(); 1188 if (const CXXOperatorCallExpr *CXXCE = 1189 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr)) 1190 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) 1191 if (!FuncDecl->isTrivial()) 1192 if (property->getType()->isReferenceType()) { 1193 Diag(PropertyDiagLoc, 1194 diag::err_atomic_property_nontrivial_assign_op) 1195 << property->getType(); 1196 Diag(FuncDecl->getLocStart(), 1197 diag::note_callee_decl) << FuncDecl; 1198 } 1199 } 1200 PIDecl->setSetterCXXAssignment(Res.takeAs<Expr>()); 1201 } 1202 } 1203 1204 if (IC) { 1205 if (Synthesize) 1206 if (ObjCPropertyImplDecl *PPIDecl = 1207 IC->FindPropertyImplIvarDecl(PropertyIvar)) { 1208 Diag(PropertyLoc, diag::error_duplicate_ivar_use) 1209 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1210 << PropertyIvar; 1211 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1212 } 1213 1214 if (ObjCPropertyImplDecl *PPIDecl 1215 = IC->FindPropertyImplDecl(PropertyId)) { 1216 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; 1217 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1218 return 0; 1219 } 1220 IC->addPropertyImplementation(PIDecl); 1221 if (getLangOpts().ObjCDefaultSynthProperties && 1222 getLangOpts().ObjCRuntime.isNonFragile() && 1223 !IDecl->isObjCRequiresPropertyDefs()) { 1224 // Diagnose if an ivar was lazily synthesdized due to a previous 1225 // use and if 1) property is @dynamic or 2) property is synthesized 1226 // but it requires an ivar of different name. 1227 ObjCInterfaceDecl *ClassDeclared=0; 1228 ObjCIvarDecl *Ivar = 0; 1229 if (!Synthesize) 1230 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 1231 else { 1232 if (PropertyIvar && PropertyIvar != PropertyId) 1233 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared); 1234 } 1235 // Issue diagnostics only if Ivar belongs to current class. 1236 if (Ivar && Ivar->getSynthesize() && 1237 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) { 1238 Diag(Ivar->getLocation(), diag::err_undeclared_var_use) 1239 << PropertyId; 1240 Ivar->setInvalidDecl(); 1241 } 1242 } 1243 } else { 1244 if (Synthesize) 1245 if (ObjCPropertyImplDecl *PPIDecl = 1246 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { 1247 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use) 1248 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() 1249 << PropertyIvar; 1250 Diag(PPIDecl->getLocation(), diag::note_previous_use); 1251 } 1252 1253 if (ObjCPropertyImplDecl *PPIDecl = 1254 CatImplClass->FindPropertyImplDecl(PropertyId)) { 1255 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId; 1256 Diag(PPIDecl->getLocation(), diag::note_previous_declaration); 1257 return 0; 1258 } 1259 CatImplClass->addPropertyImplementation(PIDecl); 1260 } 1261 1262 return PIDecl; 1263 } 1264 1265 //===----------------------------------------------------------------------===// 1266 // Helper methods. 1267 //===----------------------------------------------------------------------===// 1268 1269 /// DiagnosePropertyMismatch - Compares two properties for their 1270 /// attributes and types and warns on a variety of inconsistencies. 1271 /// 1272 void 1273 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, 1274 ObjCPropertyDecl *SuperProperty, 1275 const IdentifierInfo *inheritedName) { 1276 ObjCPropertyDecl::PropertyAttributeKind CAttr = 1277 Property->getPropertyAttributes(); 1278 ObjCPropertyDecl::PropertyAttributeKind SAttr = 1279 SuperProperty->getPropertyAttributes(); 1280 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) 1281 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) 1282 Diag(Property->getLocation(), diag::warn_readonly_property) 1283 << Property->getDeclName() << inheritedName; 1284 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) 1285 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) 1286 Diag(Property->getLocation(), diag::warn_property_attribute) 1287 << Property->getDeclName() << "copy" << inheritedName; 1288 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){ 1289 unsigned CAttrRetain = 1290 (CAttr & 1291 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 1292 unsigned SAttrRetain = 1293 (SAttr & 1294 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong)); 1295 bool CStrong = (CAttrRetain != 0); 1296 bool SStrong = (SAttrRetain != 0); 1297 if (CStrong != SStrong) 1298 Diag(Property->getLocation(), diag::warn_property_attribute) 1299 << Property->getDeclName() << "retain (or strong)" << inheritedName; 1300 } 1301 1302 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) 1303 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) { 1304 Diag(Property->getLocation(), diag::warn_property_attribute) 1305 << Property->getDeclName() << "atomic" << inheritedName; 1306 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1307 } 1308 if (Property->getSetterName() != SuperProperty->getSetterName()) { 1309 Diag(Property->getLocation(), diag::warn_property_attribute) 1310 << Property->getDeclName() << "setter" << inheritedName; 1311 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1312 } 1313 if (Property->getGetterName() != SuperProperty->getGetterName()) { 1314 Diag(Property->getLocation(), diag::warn_property_attribute) 1315 << Property->getDeclName() << "getter" << inheritedName; 1316 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1317 } 1318 1319 QualType LHSType = 1320 Context.getCanonicalType(SuperProperty->getType()); 1321 QualType RHSType = 1322 Context.getCanonicalType(Property->getType()); 1323 1324 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { 1325 // Do cases not handled in above. 1326 // FIXME. For future support of covariant property types, revisit this. 1327 bool IncompatibleObjC = false; 1328 QualType ConvertedType; 1329 if (!isObjCPointerConversion(RHSType, LHSType, 1330 ConvertedType, IncompatibleObjC) || 1331 IncompatibleObjC) { 1332 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) 1333 << Property->getType() << SuperProperty->getType() << inheritedName; 1334 Diag(SuperProperty->getLocation(), diag::note_property_declare); 1335 } 1336 } 1337 } 1338 1339 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, 1340 ObjCMethodDecl *GetterMethod, 1341 SourceLocation Loc) { 1342 if (!GetterMethod) 1343 return false; 1344 QualType GetterType = GetterMethod->getResultType().getNonReferenceType(); 1345 QualType PropertyIvarType = property->getType().getNonReferenceType(); 1346 bool compat = Context.hasSameType(PropertyIvarType, GetterType); 1347 if (!compat) { 1348 if (isa<ObjCObjectPointerType>(PropertyIvarType) && 1349 isa<ObjCObjectPointerType>(GetterType)) 1350 compat = 1351 Context.canAssignObjCInterfaces( 1352 GetterType->getAs<ObjCObjectPointerType>(), 1353 PropertyIvarType->getAs<ObjCObjectPointerType>()); 1354 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType) 1355 != Compatible) { 1356 Diag(Loc, diag::error_property_accessor_type) 1357 << property->getDeclName() << PropertyIvarType 1358 << GetterMethod->getSelector() << GetterType; 1359 Diag(GetterMethod->getLocation(), diag::note_declared_at); 1360 return true; 1361 } else { 1362 compat = true; 1363 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType(); 1364 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType(); 1365 if (lhsType != rhsType && lhsType->isArithmeticType()) 1366 compat = false; 1367 } 1368 } 1369 1370 if (!compat) { 1371 Diag(Loc, diag::warn_accessor_property_type_mismatch) 1372 << property->getDeclName() 1373 << GetterMethod->getSelector(); 1374 Diag(GetterMethod->getLocation(), diag::note_declared_at); 1375 return true; 1376 } 1377 1378 return false; 1379 } 1380 1381 /// MatchOneProtocolPropertiesInClass - This routine goes thru the list 1382 /// of properties declared in a protocol and compares their attribute against 1383 /// the same property declared in the class or category. 1384 void 1385 Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, ObjCProtocolDecl *PDecl) { 1386 if (!CDecl) 1387 return; 1388 1389 // Category case. 1390 if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1391 // FIXME: We should perform this check when the property in the category 1392 // is declared. 1393 assert (CatDecl && "MatchOneProtocolPropertiesInClass"); 1394 if (!CatDecl->IsClassExtension()) 1395 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1396 E = PDecl->prop_end(); P != E; ++P) { 1397 ObjCPropertyDecl *ProtoProp = *P; 1398 DeclContext::lookup_result R 1399 = CatDecl->lookup(ProtoProp->getDeclName()); 1400 for (unsigned I = 0, N = R.size(); I != N; ++I) { 1401 if (ObjCPropertyDecl *CatProp = dyn_cast<ObjCPropertyDecl>(R[I])) { 1402 if (CatProp != ProtoProp) { 1403 // Property protocol already exist in class. Diagnose any mismatch. 1404 DiagnosePropertyMismatch(CatProp, ProtoProp, 1405 PDecl->getIdentifier()); 1406 } 1407 } 1408 } 1409 } 1410 return; 1411 } 1412 1413 // Class 1414 // FIXME: We should perform this check when the property in the class 1415 // is declared. 1416 ObjCInterfaceDecl *IDecl = cast<ObjCInterfaceDecl>(CDecl); 1417 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1418 E = PDecl->prop_end(); P != E; ++P) { 1419 ObjCPropertyDecl *ProtoProp = *P; 1420 DeclContext::lookup_result R 1421 = IDecl->lookup(ProtoProp->getDeclName()); 1422 for (unsigned I = 0, N = R.size(); I != N; ++I) { 1423 if (ObjCPropertyDecl *ClassProp = dyn_cast<ObjCPropertyDecl>(R[I])) { 1424 if (ClassProp != ProtoProp) { 1425 // Property protocol already exist in class. Diagnose any mismatch. 1426 DiagnosePropertyMismatch(ClassProp, ProtoProp, 1427 PDecl->getIdentifier()); 1428 } 1429 } 1430 } 1431 } 1432 } 1433 1434 /// isPropertyReadonly - Return true if property is readonly, by searching 1435 /// for the property in the class and in its categories and implementations 1436 /// 1437 bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, 1438 ObjCInterfaceDecl *IDecl) { 1439 // by far the most common case. 1440 if (!PDecl->isReadOnly()) 1441 return false; 1442 // Even if property is ready only, if interface has a user defined setter, 1443 // it is not considered read only. 1444 if (IDecl->getInstanceMethod(PDecl->getSetterName())) 1445 return false; 1446 1447 // Main class has the property as 'readonly'. Must search 1448 // through the category list to see if the property's 1449 // attribute has been over-ridden to 'readwrite'. 1450 for (ObjCInterfaceDecl::visible_categories_iterator 1451 Cat = IDecl->visible_categories_begin(), 1452 CatEnd = IDecl->visible_categories_end(); 1453 Cat != CatEnd; ++Cat) { 1454 if (Cat->getInstanceMethod(PDecl->getSetterName())) 1455 return false; 1456 ObjCPropertyDecl *P = 1457 Cat->FindPropertyDeclaration(PDecl->getIdentifier()); 1458 if (P && !P->isReadOnly()) 1459 return false; 1460 } 1461 1462 // Also, check for definition of a setter method in the implementation if 1463 // all else failed. 1464 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { 1465 if (ObjCImplementationDecl *IMD = 1466 dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { 1467 if (IMD->getInstanceMethod(PDecl->getSetterName())) 1468 return false; 1469 } else if (ObjCCategoryImplDecl *CIMD = 1470 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { 1471 if (CIMD->getInstanceMethod(PDecl->getSetterName())) 1472 return false; 1473 } 1474 } 1475 // Lastly, look through the implementation (if one is in scope). 1476 if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) 1477 if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) 1478 return false; 1479 // If all fails, look at the super class. 1480 if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) 1481 return isPropertyReadonly(PDecl, SIDecl); 1482 return true; 1483 } 1484 1485 /// CollectImmediateProperties - This routine collects all properties in 1486 /// the class and its conforming protocols; but not those in its super class. 1487 void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, 1488 ObjCContainerDecl::PropertyMap &PropMap, 1489 ObjCContainerDecl::PropertyMap &SuperPropMap) { 1490 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { 1491 for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), 1492 E = IDecl->prop_end(); P != E; ++P) { 1493 ObjCPropertyDecl *Prop = *P; 1494 PropMap[Prop->getIdentifier()] = Prop; 1495 } 1496 // scan through class's protocols. 1497 for (ObjCInterfaceDecl::all_protocol_iterator 1498 PI = IDecl->all_referenced_protocol_begin(), 1499 E = IDecl->all_referenced_protocol_end(); PI != E; ++PI) 1500 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1501 } 1502 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1503 if (!CATDecl->IsClassExtension()) 1504 for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), 1505 E = CATDecl->prop_end(); P != E; ++P) { 1506 ObjCPropertyDecl *Prop = *P; 1507 PropMap[Prop->getIdentifier()] = Prop; 1508 } 1509 // scan through class's protocols. 1510 for (ObjCCategoryDecl::protocol_iterator PI = CATDecl->protocol_begin(), 1511 E = CATDecl->protocol_end(); PI != E; ++PI) 1512 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1513 } 1514 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { 1515 for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), 1516 E = PDecl->prop_end(); P != E; ++P) { 1517 ObjCPropertyDecl *Prop = *P; 1518 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()]; 1519 // Exclude property for protocols which conform to class's super-class, 1520 // as super-class has to implement the property. 1521 if (!PropertyFromSuper || 1522 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { 1523 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; 1524 if (!PropEntry) 1525 PropEntry = Prop; 1526 } 1527 } 1528 // scan through protocol's protocols. 1529 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), 1530 E = PDecl->protocol_end(); PI != E; ++PI) 1531 CollectImmediateProperties((*PI), PropMap, SuperPropMap); 1532 } 1533 } 1534 1535 /// CollectSuperClassPropertyImplementations - This routine collects list of 1536 /// properties to be implemented in super class(s) and also coming from their 1537 /// conforming protocols. 1538 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, 1539 ObjCInterfaceDecl::PropertyMap &PropMap) { 1540 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { 1541 ObjCInterfaceDecl::PropertyDeclOrder PO; 1542 while (SDecl) { 1543 SDecl->collectPropertiesToImplement(PropMap, PO); 1544 SDecl = SDecl->getSuperClass(); 1545 } 1546 } 1547 } 1548 1549 /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is 1550 /// an ivar synthesized for 'Method' and 'Method' is a property accessor 1551 /// declared in class 'IFace'. 1552 bool 1553 Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, 1554 ObjCMethodDecl *Method, ObjCIvarDecl *IV) { 1555 if (!IV->getSynthesize()) 1556 return false; 1557 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(), 1558 Method->isInstanceMethod()); 1559 if (!IMD || !IMD->isPropertyAccessor()) 1560 return false; 1561 1562 // look up a property declaration whose one of its accessors is implemented 1563 // by this method. 1564 for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(), 1565 E = IFace->prop_end(); P != E; ++P) { 1566 ObjCPropertyDecl *property = *P; 1567 if ((property->getGetterName() == IMD->getSelector() || 1568 property->getSetterName() == IMD->getSelector()) && 1569 (property->getPropertyIvarDecl() == IV)) 1570 return true; 1571 } 1572 return false; 1573 } 1574 1575 1576 /// \brief Default synthesizes all properties which must be synthesized 1577 /// in class's \@implementation. 1578 void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl, 1579 ObjCInterfaceDecl *IDecl) { 1580 1581 ObjCInterfaceDecl::PropertyMap PropMap; 1582 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder; 1583 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder); 1584 if (PropMap.empty()) 1585 return; 1586 ObjCInterfaceDecl::PropertyMap SuperPropMap; 1587 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap); 1588 1589 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) { 1590 ObjCPropertyDecl *Prop = PropertyOrder[i]; 1591 // If property to be implemented in the super class, ignore. 1592 if (SuperPropMap[Prop->getIdentifier()]) { 1593 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()]; 1594 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) && 1595 (PropInSuperClass->getPropertyAttributes() & 1596 ObjCPropertyDecl::OBJC_PR_readonly) && 1597 !IMPDecl->getInstanceMethod(Prop->getSetterName()) && 1598 !IDecl->HasUserDeclaredSetterMethod(Prop)) { 1599 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property) 1600 << Prop->getIdentifier()->getName(); 1601 Diag(PropInSuperClass->getLocation(), diag::note_property_declare); 1602 } 1603 continue; 1604 } 1605 // Is there a matching property synthesize/dynamic? 1606 if (Prop->isInvalidDecl() || 1607 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional) 1608 continue; 1609 if (ObjCPropertyImplDecl *PID = 1610 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) { 1611 if (PID->getPropertyDecl() != Prop) { 1612 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property) 1613 << Prop->getIdentifier()->getName(); 1614 if (!PID->getLocation().isInvalid()) 1615 Diag(PID->getLocation(), diag::note_property_synthesize); 1616 } 1617 continue; 1618 } 1619 // Property may have been synthesized by user. 1620 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier())) 1621 continue; 1622 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) { 1623 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly) 1624 continue; 1625 if (IMPDecl->getInstanceMethod(Prop->getSetterName())) 1626 continue; 1627 } 1628 if (isa<ObjCProtocolDecl>(Prop->getDeclContext())) { 1629 // We won't auto-synthesize properties declared in protocols. 1630 Diag(IMPDecl->getLocation(), 1631 diag::warn_auto_synthesizing_protocol_property); 1632 Diag(Prop->getLocation(), diag::note_property_declare); 1633 continue; 1634 } 1635 1636 // We use invalid SourceLocations for the synthesized ivars since they 1637 // aren't really synthesized at a particular location; they just exist. 1638 // Saying that they are located at the @implementation isn't really going 1639 // to help users. 1640 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( 1641 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(), 1642 true, 1643 /* property = */ Prop->getIdentifier(), 1644 /* ivar = */ Prop->getDefaultSynthIvarName(Context), 1645 Prop->getLocation())); 1646 if (PIDecl) { 1647 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis); 1648 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation); 1649 } 1650 } 1651 } 1652 1653 void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) { 1654 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile()) 1655 return; 1656 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D); 1657 if (!IC) 1658 return; 1659 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) 1660 if (!IDecl->isObjCRequiresPropertyDefs()) 1661 DefaultSynthesizeProperties(S, IC, IDecl); 1662 } 1663 1664 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl, 1665 ObjCContainerDecl *CDecl) { 1666 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; 1667 ObjCInterfaceDecl *IDecl; 1668 // Gather properties which need not be implemented in this class 1669 // or category. 1670 if (!(IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))) 1671 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { 1672 // For categories, no need to implement properties declared in 1673 // its primary class (and its super classes) if property is 1674 // declared in one of those containers. 1675 if ((IDecl = C->getClassInterface())) { 1676 ObjCInterfaceDecl::PropertyDeclOrder PO; 1677 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO); 1678 } 1679 } 1680 if (IDecl) 1681 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap); 1682 1683 ObjCContainerDecl::PropertyMap PropMap; 1684 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap); 1685 if (PropMap.empty()) 1686 return; 1687 1688 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; 1689 for (ObjCImplDecl::propimpl_iterator 1690 I = IMPDecl->propimpl_begin(), 1691 EI = IMPDecl->propimpl_end(); I != EI; ++I) 1692 PropImplMap.insert(I->getPropertyDecl()); 1693 1694 SelectorSet InsMap; 1695 // Collect property accessors implemented in current implementation. 1696 for (ObjCImplementationDecl::instmeth_iterator 1697 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) 1698 InsMap.insert((*I)->getSelector()); 1699 1700 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); 1701 ObjCInterfaceDecl *PrimaryClass = 0; 1702 if (C && !C->IsClassExtension()) 1703 if ((PrimaryClass = C->getClassInterface())) 1704 // Report unimplemented properties in the category as well. 1705 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { 1706 // When reporting on missing setter/getters, do not report when 1707 // setter/getter is implemented in category's primary class 1708 // implementation. 1709 for (ObjCImplementationDecl::instmeth_iterator 1710 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) 1711 InsMap.insert((*I)->getSelector()); 1712 } 1713 1714 for (ObjCContainerDecl::PropertyMap::iterator 1715 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { 1716 ObjCPropertyDecl *Prop = P->second; 1717 // Is there a matching propery synthesize/dynamic? 1718 if (Prop->isInvalidDecl() || 1719 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || 1720 PropImplMap.count(Prop) || 1721 Prop->getAvailability() == AR_Unavailable) 1722 continue; 1723 // When reporting on missing property getter implementation in 1724 // categories, do not report when they are declared in primary class, 1725 // class's protocol, or one of it super classes. This is because, 1726 // the class is going to implement them. 1727 if (!InsMap.count(Prop->getGetterName()) && 1728 (PrimaryClass == 0 || 1729 !PrimaryClass->lookupPropertyAccessor(Prop->getGetterName(), C))) { 1730 Diag(IMPDecl->getLocation(), 1731 isa<ObjCCategoryDecl>(CDecl) ? 1732 diag::warn_setter_getter_impl_required_in_category : 1733 diag::warn_setter_getter_impl_required) 1734 << Prop->getDeclName() << Prop->getGetterName(); 1735 Diag(Prop->getLocation(), 1736 diag::note_property_declare); 1737 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) 1738 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1739 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1740 Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1741 1742 } 1743 // When reporting on missing property setter implementation in 1744 // categories, do not report when they are declared in primary class, 1745 // class's protocol, or one of it super classes. This is because, 1746 // the class is going to implement them. 1747 if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName()) && 1748 (PrimaryClass == 0 || 1749 !PrimaryClass->lookupPropertyAccessor(Prop->getSetterName(), C))) { 1750 Diag(IMPDecl->getLocation(), 1751 isa<ObjCCategoryDecl>(CDecl) ? 1752 diag::warn_setter_getter_impl_required_in_category : 1753 diag::warn_setter_getter_impl_required) 1754 << Prop->getDeclName() << Prop->getSetterName(); 1755 Diag(Prop->getLocation(), 1756 diag::note_property_declare); 1757 if (LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCRuntime.isNonFragile()) 1758 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl)) 1759 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) 1760 Diag(RID->getLocation(), diag::note_suppressed_class_declare); 1761 } 1762 } 1763 } 1764 1765 void 1766 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, 1767 ObjCContainerDecl* IDecl) { 1768 // Rules apply in non-GC mode only 1769 if (getLangOpts().getGC() != LangOptions::NonGC) 1770 return; 1771 for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), 1772 E = IDecl->prop_end(); 1773 I != E; ++I) { 1774 ObjCPropertyDecl *Property = *I; 1775 ObjCMethodDecl *GetterMethod = 0; 1776 ObjCMethodDecl *SetterMethod = 0; 1777 bool LookedUpGetterSetter = false; 1778 1779 unsigned Attributes = Property->getPropertyAttributes(); 1780 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); 1781 1782 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) && 1783 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) { 1784 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); 1785 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); 1786 LookedUpGetterSetter = true; 1787 if (GetterMethod) { 1788 Diag(GetterMethod->getLocation(), 1789 diag::warn_default_atomic_custom_getter_setter) 1790 << Property->getIdentifier() << 0; 1791 Diag(Property->getLocation(), diag::note_property_declare); 1792 } 1793 if (SetterMethod) { 1794 Diag(SetterMethod->getLocation(), 1795 diag::warn_default_atomic_custom_getter_setter) 1796 << Property->getIdentifier() << 1; 1797 Diag(Property->getLocation(), diag::note_property_declare); 1798 } 1799 } 1800 1801 // We only care about readwrite atomic property. 1802 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || 1803 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) 1804 continue; 1805 if (const ObjCPropertyImplDecl *PIDecl 1806 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { 1807 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) 1808 continue; 1809 if (!LookedUpGetterSetter) { 1810 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName()); 1811 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName()); 1812 LookedUpGetterSetter = true; 1813 } 1814 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { 1815 SourceLocation MethodLoc = 1816 (GetterMethod ? GetterMethod->getLocation() 1817 : SetterMethod->getLocation()); 1818 Diag(MethodLoc, diag::warn_atomic_property_rule) 1819 << Property->getIdentifier() << (GetterMethod != 0) 1820 << (SetterMethod != 0); 1821 // fixit stuff. 1822 if (!AttributesAsWritten) { 1823 if (Property->getLParenLoc().isValid()) { 1824 // @property () ... case. 1825 SourceRange PropSourceRange(Property->getAtLoc(), 1826 Property->getLParenLoc()); 1827 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << 1828 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic"); 1829 } 1830 else { 1831 //@property id etc. 1832 SourceLocation endLoc = 1833 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); 1834 endLoc = endLoc.getLocWithOffset(-1); 1835 SourceRange PropSourceRange(Property->getAtLoc(), endLoc); 1836 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << 1837 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) "); 1838 } 1839 } 1840 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) { 1841 // @property () ... case. 1842 SourceLocation endLoc = Property->getLParenLoc(); 1843 SourceRange PropSourceRange(Property->getAtLoc(), endLoc); 1844 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) << 1845 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, "); 1846 } 1847 else 1848 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest); 1849 Diag(Property->getLocation(), diag::note_property_declare); 1850 } 1851 } 1852 } 1853 } 1854 1855 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) { 1856 if (getLangOpts().getGC() == LangOptions::GCOnly) 1857 return; 1858 1859 for (ObjCImplementationDecl::propimpl_iterator 1860 i = D->propimpl_begin(), e = D->propimpl_end(); i != e; ++i) { 1861 ObjCPropertyImplDecl *PID = *i; 1862 if (PID->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize) 1863 continue; 1864 1865 const ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1866 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && 1867 !D->getInstanceMethod(PD->getGetterName())) { 1868 ObjCMethodDecl *method = PD->getGetterMethodDecl(); 1869 if (!method) 1870 continue; 1871 ObjCMethodFamily family = method->getMethodFamily(); 1872 if (family == OMF_alloc || family == OMF_copy || 1873 family == OMF_mutableCopy || family == OMF_new) { 1874 if (getLangOpts().ObjCAutoRefCount) 1875 Diag(PID->getLocation(), diag::err_ownin_getter_rule); 1876 else 1877 Diag(PID->getLocation(), diag::warn_owning_getter_rule); 1878 Diag(PD->getLocation(), diag::note_property_declare); 1879 } 1880 } 1881 } 1882 } 1883 1884 /// AddPropertyAttrs - Propagates attributes from a property to the 1885 /// implicitly-declared getter or setter for that property. 1886 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, 1887 ObjCPropertyDecl *Property) { 1888 // Should we just clone all attributes over? 1889 for (Decl::attr_iterator A = Property->attr_begin(), 1890 AEnd = Property->attr_end(); 1891 A != AEnd; ++A) { 1892 if (isa<DeprecatedAttr>(*A) || 1893 isa<UnavailableAttr>(*A) || 1894 isa<AvailabilityAttr>(*A)) 1895 PropertyMethod->addAttr((*A)->clone(S.Context)); 1896 } 1897 } 1898 1899 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods 1900 /// have the property type and issue diagnostics if they don't. 1901 /// Also synthesize a getter/setter method if none exist (and update the 1902 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized 1903 /// methods is the "right" thing to do. 1904 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, 1905 ObjCContainerDecl *CD, 1906 ObjCPropertyDecl *redeclaredProperty, 1907 ObjCContainerDecl *lexicalDC) { 1908 1909 ObjCMethodDecl *GetterMethod, *SetterMethod; 1910 1911 GetterMethod = CD->getInstanceMethod(property->getGetterName()); 1912 SetterMethod = CD->getInstanceMethod(property->getSetterName()); 1913 DiagnosePropertyAccessorMismatch(property, GetterMethod, 1914 property->getLocation()); 1915 1916 if (SetterMethod) { 1917 ObjCPropertyDecl::PropertyAttributeKind CAttr = 1918 property->getPropertyAttributes(); 1919 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && 1920 Context.getCanonicalType(SetterMethod->getResultType()) != 1921 Context.VoidTy) 1922 Diag(SetterMethod->getLocation(), diag::err_setter_type_void); 1923 if (SetterMethod->param_size() != 1 || 1924 !Context.hasSameUnqualifiedType( 1925 (*SetterMethod->param_begin())->getType().getNonReferenceType(), 1926 property->getType().getNonReferenceType())) { 1927 Diag(property->getLocation(), 1928 diag::warn_accessor_property_type_mismatch) 1929 << property->getDeclName() 1930 << SetterMethod->getSelector(); 1931 Diag(SetterMethod->getLocation(), diag::note_declared_at); 1932 } 1933 } 1934 1935 // Synthesize getter/setter methods if none exist. 1936 // Find the default getter and if one not found, add one. 1937 // FIXME: The synthesized property we set here is misleading. We almost always 1938 // synthesize these methods unless the user explicitly provided prototypes 1939 // (which is odd, but allowed). Sema should be typechecking that the 1940 // declarations jive in that situation (which it is not currently). 1941 if (!GetterMethod) { 1942 // No instance method of same name as property getter name was found. 1943 // Declare a getter method and add it to the list of methods 1944 // for this class. 1945 SourceLocation Loc = redeclaredProperty ? 1946 redeclaredProperty->getLocation() : 1947 property->getLocation(); 1948 1949 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc, 1950 property->getGetterName(), 1951 property->getType(), 0, CD, /*isInstance=*/true, 1952 /*isVariadic=*/false, /*isPropertyAccessor=*/true, 1953 /*isImplicitlyDeclared=*/true, /*isDefined=*/false, 1954 (property->getPropertyImplementation() == 1955 ObjCPropertyDecl::Optional) ? 1956 ObjCMethodDecl::Optional : 1957 ObjCMethodDecl::Required); 1958 CD->addDecl(GetterMethod); 1959 1960 AddPropertyAttrs(*this, GetterMethod, property); 1961 1962 // FIXME: Eventually this shouldn't be needed, as the lexical context 1963 // and the real context should be the same. 1964 if (lexicalDC) 1965 GetterMethod->setLexicalDeclContext(lexicalDC); 1966 if (property->hasAttr<NSReturnsNotRetainedAttr>()) 1967 GetterMethod->addAttr( 1968 ::new (Context) NSReturnsNotRetainedAttr(Loc, Context)); 1969 1970 if (getLangOpts().ObjCAutoRefCount) 1971 CheckARCMethodDecl(GetterMethod); 1972 } else 1973 // A user declared getter will be synthesize when @synthesize of 1974 // the property with the same name is seen in the @implementation 1975 GetterMethod->setPropertyAccessor(true); 1976 property->setGetterMethodDecl(GetterMethod); 1977 1978 // Skip setter if property is read-only. 1979 if (!property->isReadOnly()) { 1980 // Find the default setter and if one not found, add one. 1981 if (!SetterMethod) { 1982 // No instance method of same name as property setter name was found. 1983 // Declare a setter method and add it to the list of methods 1984 // for this class. 1985 SourceLocation Loc = redeclaredProperty ? 1986 redeclaredProperty->getLocation() : 1987 property->getLocation(); 1988 1989 SetterMethod = 1990 ObjCMethodDecl::Create(Context, Loc, Loc, 1991 property->getSetterName(), Context.VoidTy, 0, 1992 CD, /*isInstance=*/true, /*isVariadic=*/false, 1993 /*isPropertyAccessor=*/true, 1994 /*isImplicitlyDeclared=*/true, 1995 /*isDefined=*/false, 1996 (property->getPropertyImplementation() == 1997 ObjCPropertyDecl::Optional) ? 1998 ObjCMethodDecl::Optional : 1999 ObjCMethodDecl::Required); 2000 2001 // Invent the arguments for the setter. We don't bother making a 2002 // nice name for the argument. 2003 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, 2004 Loc, Loc, 2005 property->getIdentifier(), 2006 property->getType().getUnqualifiedType(), 2007 /*TInfo=*/0, 2008 SC_None, 2009 0); 2010 SetterMethod->setMethodParams(Context, Argument, None); 2011 2012 AddPropertyAttrs(*this, SetterMethod, property); 2013 2014 CD->addDecl(SetterMethod); 2015 // FIXME: Eventually this shouldn't be needed, as the lexical context 2016 // and the real context should be the same. 2017 if (lexicalDC) 2018 SetterMethod->setLexicalDeclContext(lexicalDC); 2019 2020 // It's possible for the user to have set a very odd custom 2021 // setter selector that causes it to have a method family. 2022 if (getLangOpts().ObjCAutoRefCount) 2023 CheckARCMethodDecl(SetterMethod); 2024 } else 2025 // A user declared setter will be synthesize when @synthesize of 2026 // the property with the same name is seen in the @implementation 2027 SetterMethod->setPropertyAccessor(true); 2028 property->setSetterMethodDecl(SetterMethod); 2029 } 2030 // Add any synthesized methods to the global pool. This allows us to 2031 // handle the following, which is supported by GCC (and part of the design). 2032 // 2033 // @interface Foo 2034 // @property double bar; 2035 // @end 2036 // 2037 // void thisIsUnfortunate() { 2038 // id foo; 2039 // double bar = [foo bar]; 2040 // } 2041 // 2042 if (GetterMethod) 2043 AddInstanceMethodToGlobalPool(GetterMethod); 2044 if (SetterMethod) 2045 AddInstanceMethodToGlobalPool(SetterMethod); 2046 2047 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD); 2048 if (!CurrentClass) { 2049 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD)) 2050 CurrentClass = Cat->getClassInterface(); 2051 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD)) 2052 CurrentClass = Impl->getClassInterface(); 2053 } 2054 if (GetterMethod) 2055 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown); 2056 if (SetterMethod) 2057 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown); 2058 } 2059 2060 void Sema::CheckObjCPropertyAttributes(Decl *PDecl, 2061 SourceLocation Loc, 2062 unsigned &Attributes, 2063 bool propertyInPrimaryClass) { 2064 // FIXME: Improve the reported location. 2065 if (!PDecl || PDecl->isInvalidDecl()) 2066 return; 2067 2068 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl); 2069 QualType PropertyTy = PropertyDecl->getType(); 2070 2071 if (getLangOpts().ObjCAutoRefCount && 2072 (Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2073 PropertyTy->isObjCRetainableType()) { 2074 // 'readonly' property with no obvious lifetime. 2075 // its life time will be determined by its backing ivar. 2076 unsigned rel = (ObjCDeclSpec::DQ_PR_unsafe_unretained | 2077 ObjCDeclSpec::DQ_PR_copy | 2078 ObjCDeclSpec::DQ_PR_retain | 2079 ObjCDeclSpec::DQ_PR_strong | 2080 ObjCDeclSpec::DQ_PR_weak | 2081 ObjCDeclSpec::DQ_PR_assign); 2082 if ((Attributes & rel) == 0) 2083 return; 2084 } 2085 2086 if (propertyInPrimaryClass) { 2087 // we postpone most property diagnosis until class's implementation 2088 // because, its readonly attribute may be overridden in its class 2089 // extensions making other attributes, which make no sense, to make sense. 2090 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2091 (Attributes & ObjCDeclSpec::DQ_PR_readwrite)) 2092 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2093 << "readonly" << "readwrite"; 2094 } 2095 // readonly and readwrite/assign/retain/copy conflict. 2096 else if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2097 (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | 2098 ObjCDeclSpec::DQ_PR_assign | 2099 ObjCDeclSpec::DQ_PR_unsafe_unretained | 2100 ObjCDeclSpec::DQ_PR_copy | 2101 ObjCDeclSpec::DQ_PR_retain | 2102 ObjCDeclSpec::DQ_PR_strong))) { 2103 const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? 2104 "readwrite" : 2105 (Attributes & ObjCDeclSpec::DQ_PR_assign) ? 2106 "assign" : 2107 (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) ? 2108 "unsafe_unretained" : 2109 (Attributes & ObjCDeclSpec::DQ_PR_copy) ? 2110 "copy" : "retain"; 2111 2112 Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? 2113 diag::err_objc_property_attr_mutually_exclusive : 2114 diag::warn_objc_property_attr_mutually_exclusive) 2115 << "readonly" << which; 2116 } 2117 2118 // Check for copy or retain on non-object types. 2119 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2120 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) && 2121 !PropertyTy->isObjCRetainableType() && 2122 !PropertyDecl->getAttr<ObjCNSObjectAttr>()) { 2123 Diag(Loc, diag::err_objc_property_requires_object) 2124 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" : 2125 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)"); 2126 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy | 2127 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong); 2128 PropertyDecl->setInvalidDecl(); 2129 } 2130 2131 // Check for more than one of { assign, copy, retain }. 2132 if (Attributes & ObjCDeclSpec::DQ_PR_assign) { 2133 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2134 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2135 << "assign" << "copy"; 2136 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2137 } 2138 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2139 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2140 << "assign" << "retain"; 2141 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2142 } 2143 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2144 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2145 << "assign" << "strong"; 2146 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2147 } 2148 if (getLangOpts().ObjCAutoRefCount && 2149 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2150 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2151 << "assign" << "weak"; 2152 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2153 } 2154 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) { 2155 if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2156 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2157 << "unsafe_unretained" << "copy"; 2158 Attributes &= ~ObjCDeclSpec::DQ_PR_copy; 2159 } 2160 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2161 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2162 << "unsafe_unretained" << "retain"; 2163 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2164 } 2165 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2166 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2167 << "unsafe_unretained" << "strong"; 2168 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2169 } 2170 if (getLangOpts().ObjCAutoRefCount && 2171 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2172 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2173 << "unsafe_unretained" << "weak"; 2174 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2175 } 2176 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { 2177 if (Attributes & ObjCDeclSpec::DQ_PR_retain) { 2178 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2179 << "copy" << "retain"; 2180 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2181 } 2182 if (Attributes & ObjCDeclSpec::DQ_PR_strong) { 2183 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2184 << "copy" << "strong"; 2185 Attributes &= ~ObjCDeclSpec::DQ_PR_strong; 2186 } 2187 if (Attributes & ObjCDeclSpec::DQ_PR_weak) { 2188 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2189 << "copy" << "weak"; 2190 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2191 } 2192 } 2193 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2194 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2195 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2196 << "retain" << "weak"; 2197 Attributes &= ~ObjCDeclSpec::DQ_PR_retain; 2198 } 2199 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) && 2200 (Attributes & ObjCDeclSpec::DQ_PR_weak)) { 2201 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2202 << "strong" << "weak"; 2203 Attributes &= ~ObjCDeclSpec::DQ_PR_weak; 2204 } 2205 2206 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) && 2207 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) { 2208 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) 2209 << "atomic" << "nonatomic"; 2210 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic; 2211 } 2212 2213 // Warn if user supplied no assignment attribute, property is 2214 // readwrite, and this is an object type. 2215 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | 2216 ObjCDeclSpec::DQ_PR_unsafe_unretained | 2217 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong | 2218 ObjCDeclSpec::DQ_PR_weak)) && 2219 PropertyTy->isObjCObjectPointerType()) { 2220 if (getLangOpts().ObjCAutoRefCount) 2221 // With arc, @property definitions should default to (strong) when 2222 // not specified; including when property is 'readonly'. 2223 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong); 2224 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) { 2225 bool isAnyClassTy = 2226 (PropertyTy->isObjCClassType() || 2227 PropertyTy->isObjCQualifiedClassType()); 2228 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to 2229 // issue any warning. 2230 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) 2231 ; 2232 else if (propertyInPrimaryClass) { 2233 // Don't issue warning on property with no life time in class 2234 // extension as it is inherited from property in primary class. 2235 // Skip this warning in gc-only mode. 2236 if (getLangOpts().getGC() != LangOptions::GCOnly) 2237 Diag(Loc, diag::warn_objc_property_no_assignment_attribute); 2238 2239 // If non-gc code warn that this is likely inappropriate. 2240 if (getLangOpts().getGC() == LangOptions::NonGC) 2241 Diag(Loc, diag::warn_objc_property_default_assign_on_object); 2242 } 2243 } 2244 2245 // FIXME: Implement warning dependent on NSCopying being 2246 // implemented. See also: 2247 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> 2248 // (please trim this list while you are at it). 2249 } 2250 2251 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) 2252 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly) 2253 && getLangOpts().getGC() == LangOptions::GCOnly 2254 && PropertyTy->isBlockPointerType()) 2255 Diag(Loc, diag::warn_objc_property_copy_missing_on_block); 2256 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) && 2257 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2258 !(Attributes & ObjCDeclSpec::DQ_PR_strong) && 2259 PropertyTy->isBlockPointerType()) 2260 Diag(Loc, diag::warn_objc_property_retain_of_block); 2261 2262 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && 2263 (Attributes & ObjCDeclSpec::DQ_PR_setter)) 2264 Diag(Loc, diag::warn_objc_readonly_property_has_setter); 2265 2266 } 2267