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