| 1 | //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements semantic analysis for Objective C @property and |
| 10 | // @synthesize declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTMutationListener.h" |
| 15 | #include "clang/AST/DeclObjC.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/ExprObjC.h" |
| 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Lex/Lexer.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Sema/Initialization.h" |
| 22 | #include "clang/Sema/SemaObjC.h" |
| 23 | #include "llvm/ADT/DenseSet.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Grammar actions. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// getImpliedARCOwnership - Given a set of property attributes and a |
| 32 | /// type, infer an expected lifetime. The type's ownership qualification |
| 33 | /// is not considered. |
| 34 | /// |
| 35 | /// Returns OCL_None if the attributes as stated do not imply an ownership. |
| 36 | /// Never returns OCL_Autoreleasing. |
| 37 | static Qualifiers::ObjCLifetime |
| 38 | getImpliedARCOwnership(ObjCPropertyAttribute::Kind attrs, QualType type) { |
| 39 | // retain, strong, copy, weak, and unsafe_unretained are only legal |
| 40 | // on properties of retainable pointer type. |
| 41 | if (attrs & |
| 42 | (ObjCPropertyAttribute::kind_retain | ObjCPropertyAttribute::kind_strong | |
| 43 | ObjCPropertyAttribute::kind_copy)) { |
| 44 | return Qualifiers::OCL_Strong; |
| 45 | } else if (attrs & ObjCPropertyAttribute::kind_weak) { |
| 46 | return Qualifiers::OCL_Weak; |
| 47 | } else if (attrs & ObjCPropertyAttribute::kind_unsafe_unretained) { |
| 48 | return Qualifiers::OCL_ExplicitNone; |
| 49 | } |
| 50 | |
| 51 | // assign can appear on other types, so we have to check the |
| 52 | // property type. |
| 53 | if (attrs & ObjCPropertyAttribute::kind_assign && |
| 54 | type->isObjCRetainableType()) { |
| 55 | return Qualifiers::OCL_ExplicitNone; |
| 56 | } |
| 57 | |
| 58 | return Qualifiers::OCL_None; |
| 59 | } |
| 60 | |
| 61 | /// Check the internal consistency of a property declaration with |
| 62 | /// an explicit ownership qualifier. |
| 63 | static void checkPropertyDeclWithOwnership(Sema &S, |
| 64 | ObjCPropertyDecl *property) { |
| 65 | if (property->isInvalidDecl()) return; |
| 66 | |
| 67 | ObjCPropertyAttribute::Kind propertyKind = property->getPropertyAttributes(); |
| 68 | Qualifiers::ObjCLifetime propertyLifetime |
| 69 | = property->getType().getObjCLifetime(); |
| 70 | |
| 71 | assert(propertyLifetime != Qualifiers::OCL_None); |
| 72 | |
| 73 | Qualifiers::ObjCLifetime expectedLifetime |
| 74 | = getImpliedARCOwnership(attrs: propertyKind, type: property->getType()); |
| 75 | if (!expectedLifetime) { |
| 76 | // We have a lifetime qualifier but no dominating property |
| 77 | // attribute. That's okay, but restore reasonable invariants by |
| 78 | // setting the property attribute according to the lifetime |
| 79 | // qualifier. |
| 80 | ObjCPropertyAttribute::Kind attr; |
| 81 | if (propertyLifetime == Qualifiers::OCL_Strong) { |
| 82 | attr = ObjCPropertyAttribute::kind_strong; |
| 83 | } else if (propertyLifetime == Qualifiers::OCL_Weak) { |
| 84 | attr = ObjCPropertyAttribute::kind_weak; |
| 85 | } else { |
| 86 | assert(propertyLifetime == Qualifiers::OCL_ExplicitNone); |
| 87 | attr = ObjCPropertyAttribute::kind_unsafe_unretained; |
| 88 | } |
| 89 | property->setPropertyAttributes(attr); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | if (propertyLifetime == expectedLifetime) return; |
| 94 | |
| 95 | property->setInvalidDecl(); |
| 96 | S.Diag(Loc: property->getLocation(), |
| 97 | DiagID: diag::err_arc_inconsistent_property_ownership) |
| 98 | << property->getDeclName() |
| 99 | << expectedLifetime |
| 100 | << propertyLifetime; |
| 101 | } |
| 102 | |
| 103 | /// Check this Objective-C property against a property declared in the |
| 104 | /// given protocol. |
| 105 | static void |
| 106 | CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop, |
| 107 | ObjCProtocolDecl *Proto, |
| 108 | llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) { |
| 109 | // Have we seen this protocol before? |
| 110 | if (!Known.insert(Ptr: Proto).second) |
| 111 | return; |
| 112 | |
| 113 | // Look for a property with the same name. |
| 114 | if (ObjCPropertyDecl *ProtoProp = Proto->getProperty( |
| 115 | Id: Prop->getIdentifier(), IsInstance: Prop->isInstanceProperty())) { |
| 116 | S.ObjC().DiagnosePropertyMismatch(Property: Prop, SuperProperty: ProtoProp, Name: Proto->getIdentifier(), |
| 117 | OverridingProtocolProperty: true); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Check this property against any protocols we inherit. |
| 122 | for (auto *P : Proto->protocols()) |
| 123 | CheckPropertyAgainstProtocol(S, Prop, Proto: P, Known); |
| 124 | } |
| 125 | |
| 126 | static unsigned deducePropertyOwnershipFromType(Sema &S, QualType T) { |
| 127 | // In GC mode, just look for the __weak qualifier. |
| 128 | if (S.getLangOpts().getGC() != LangOptions::NonGC) { |
| 129 | if (T.isObjCGCWeak()) |
| 130 | return ObjCPropertyAttribute::kind_weak; |
| 131 | |
| 132 | // In ARC/MRC, look for an explicit ownership qualifier. |
| 133 | // For some reason, this only applies to __weak. |
| 134 | } else if (auto ownership = T.getObjCLifetime()) { |
| 135 | switch (ownership) { |
| 136 | case Qualifiers::OCL_Weak: |
| 137 | return ObjCPropertyAttribute::kind_weak; |
| 138 | case Qualifiers::OCL_Strong: |
| 139 | return ObjCPropertyAttribute::kind_strong; |
| 140 | case Qualifiers::OCL_ExplicitNone: |
| 141 | return ObjCPropertyAttribute::kind_unsafe_unretained; |
| 142 | case Qualifiers::OCL_Autoreleasing: |
| 143 | case Qualifiers::OCL_None: |
| 144 | return 0; |
| 145 | } |
| 146 | llvm_unreachable("bad qualifier" ); |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static const unsigned OwnershipMask = |
| 153 | (ObjCPropertyAttribute::kind_assign | ObjCPropertyAttribute::kind_retain | |
| 154 | ObjCPropertyAttribute::kind_copy | ObjCPropertyAttribute::kind_weak | |
| 155 | ObjCPropertyAttribute::kind_strong | |
| 156 | ObjCPropertyAttribute::kind_unsafe_unretained); |
| 157 | |
| 158 | static unsigned getOwnershipRule(unsigned attr) { |
| 159 | unsigned result = attr & OwnershipMask; |
| 160 | |
| 161 | // From an ownership perspective, assign and unsafe_unretained are |
| 162 | // identical; make sure one also implies the other. |
| 163 | if (result & (ObjCPropertyAttribute::kind_assign | |
| 164 | ObjCPropertyAttribute::kind_unsafe_unretained)) { |
| 165 | result |= ObjCPropertyAttribute::kind_assign | |
| 166 | ObjCPropertyAttribute::kind_unsafe_unretained; |
| 167 | } |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | Decl *SemaObjC::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 173 | SourceLocation LParenLoc, FieldDeclarator &FD, |
| 174 | ObjCDeclSpec &ODS, Selector GetterSel, |
| 175 | Selector SetterSel, |
| 176 | tok::ObjCKeywordKind MethodImplKind, |
| 177 | DeclContext *lexicalDC) { |
| 178 | unsigned Attributes = ODS.getPropertyAttributes(); |
| 179 | FD.D.setObjCWeakProperty((Attributes & ObjCPropertyAttribute::kind_weak) != |
| 180 | 0); |
| 181 | TypeSourceInfo *TSI = SemaRef.GetTypeForDeclarator(D&: FD.D); |
| 182 | QualType T = TSI->getType(); |
| 183 | if (T.getPointerAuth().isPresent()) { |
| 184 | Diag(Loc: AtLoc, DiagID: diag::err_ptrauth_qualifier_invalid) << T << 2; |
| 185 | } |
| 186 | if (!getOwnershipRule(attr: Attributes)) { |
| 187 | Attributes |= deducePropertyOwnershipFromType(S&: SemaRef, T); |
| 188 | } |
| 189 | bool isReadWrite = ((Attributes & ObjCPropertyAttribute::kind_readwrite) || |
| 190 | // default is readwrite! |
| 191 | !(Attributes & ObjCPropertyAttribute::kind_readonly)); |
| 192 | |
| 193 | // Proceed with constructing the ObjCPropertyDecls. |
| 194 | ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(Val: SemaRef.CurContext); |
| 195 | ObjCPropertyDecl *Res = nullptr; |
| 196 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(Val: ClassDecl)) { |
| 197 | if (CDecl->IsClassExtension()) { |
| 198 | Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc, |
| 199 | FD, |
| 200 | GetterSel, GetterNameLoc: ODS.getGetterNameLoc(), |
| 201 | SetterSel, SetterNameLoc: ODS.getSetterNameLoc(), |
| 202 | isReadWrite, Attributes, |
| 203 | AttributesAsWritten: ODS.getPropertyAttributes(), |
| 204 | T, TSI, MethodImplKind); |
| 205 | if (!Res) |
| 206 | return nullptr; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (!Res) { |
| 211 | Res = CreatePropertyDecl(S, CDecl: ClassDecl, AtLoc, LParenLoc, FD, |
| 212 | GetterSel, GetterNameLoc: ODS.getGetterNameLoc(), SetterSel, |
| 213 | SetterNameLoc: ODS.getSetterNameLoc(), isReadWrite, Attributes, |
| 214 | AttributesAsWritten: ODS.getPropertyAttributes(), T, TSI, |
| 215 | MethodImplKind); |
| 216 | if (lexicalDC) |
| 217 | Res->setLexicalDeclContext(lexicalDC); |
| 218 | } |
| 219 | |
| 220 | // Validate the attributes on the @property. |
| 221 | CheckObjCPropertyAttributes(PropertyPtrTy: Res, Loc: AtLoc, Attributes, |
| 222 | propertyInPrimaryClass: (isa<ObjCInterfaceDecl>(Val: ClassDecl) || |
| 223 | isa<ObjCProtocolDecl>(Val: ClassDecl))); |
| 224 | |
| 225 | // Check consistency if the type has explicit ownership qualification. |
| 226 | if (Res->getType().getObjCLifetime()) |
| 227 | checkPropertyDeclWithOwnership(S&: SemaRef, property: Res); |
| 228 | |
| 229 | llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos; |
| 230 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Val: ClassDecl)) { |
| 231 | // For a class, compare the property against a property in our superclass. |
| 232 | bool FoundInSuper = false; |
| 233 | ObjCInterfaceDecl *CurrentInterfaceDecl = IFace; |
| 234 | while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) { |
| 235 | if (ObjCPropertyDecl *SuperProp = Super->getProperty( |
| 236 | Id: Res->getIdentifier(), IsInstance: Res->isInstanceProperty())) { |
| 237 | DiagnosePropertyMismatch(Property: Res, SuperProperty: SuperProp, Name: Super->getIdentifier(), OverridingProtocolProperty: false); |
| 238 | FoundInSuper = true; |
| 239 | break; |
| 240 | } |
| 241 | CurrentInterfaceDecl = Super; |
| 242 | } |
| 243 | |
| 244 | if (FoundInSuper) { |
| 245 | // Also compare the property against a property in our protocols. |
| 246 | for (auto *P : CurrentInterfaceDecl->protocols()) { |
| 247 | CheckPropertyAgainstProtocol(S&: SemaRef, Prop: Res, Proto: P, Known&: KnownProtos); |
| 248 | } |
| 249 | } else { |
| 250 | // Slower path: look in all protocols we referenced. |
| 251 | for (auto *P : IFace->all_referenced_protocols()) { |
| 252 | CheckPropertyAgainstProtocol(S&: SemaRef, Prop: Res, Proto: P, Known&: KnownProtos); |
| 253 | } |
| 254 | } |
| 255 | } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(Val: ClassDecl)) { |
| 256 | // We don't check if class extension. Because properties in class extension |
| 257 | // are meant to override some of the attributes and checking has already done |
| 258 | // when property in class extension is constructed. |
| 259 | if (!Cat->IsClassExtension()) |
| 260 | for (auto *P : Cat->protocols()) |
| 261 | CheckPropertyAgainstProtocol(S&: SemaRef, Prop: Res, Proto: P, Known&: KnownProtos); |
| 262 | } else { |
| 263 | ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(Val: ClassDecl); |
| 264 | for (auto *P : Proto->protocols()) |
| 265 | CheckPropertyAgainstProtocol(S&: SemaRef, Prop: Res, Proto: P, Known&: KnownProtos); |
| 266 | } |
| 267 | |
| 268 | SemaRef.ActOnDocumentableDecl(D: Res); |
| 269 | return Res; |
| 270 | } |
| 271 | |
| 272 | static ObjCPropertyAttribute::Kind |
| 273 | makePropertyAttributesAsWritten(unsigned Attributes) { |
| 274 | unsigned attributesAsWritten = 0; |
| 275 | if (Attributes & ObjCPropertyAttribute::kind_readonly) |
| 276 | attributesAsWritten |= ObjCPropertyAttribute::kind_readonly; |
| 277 | if (Attributes & ObjCPropertyAttribute::kind_readwrite) |
| 278 | attributesAsWritten |= ObjCPropertyAttribute::kind_readwrite; |
| 279 | if (Attributes & ObjCPropertyAttribute::kind_getter) |
| 280 | attributesAsWritten |= ObjCPropertyAttribute::kind_getter; |
| 281 | if (Attributes & ObjCPropertyAttribute::kind_setter) |
| 282 | attributesAsWritten |= ObjCPropertyAttribute::kind_setter; |
| 283 | if (Attributes & ObjCPropertyAttribute::kind_assign) |
| 284 | attributesAsWritten |= ObjCPropertyAttribute::kind_assign; |
| 285 | if (Attributes & ObjCPropertyAttribute::kind_retain) |
| 286 | attributesAsWritten |= ObjCPropertyAttribute::kind_retain; |
| 287 | if (Attributes & ObjCPropertyAttribute::kind_strong) |
| 288 | attributesAsWritten |= ObjCPropertyAttribute::kind_strong; |
| 289 | if (Attributes & ObjCPropertyAttribute::kind_weak) |
| 290 | attributesAsWritten |= ObjCPropertyAttribute::kind_weak; |
| 291 | if (Attributes & ObjCPropertyAttribute::kind_copy) |
| 292 | attributesAsWritten |= ObjCPropertyAttribute::kind_copy; |
| 293 | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
| 294 | attributesAsWritten |= ObjCPropertyAttribute::kind_unsafe_unretained; |
| 295 | if (Attributes & ObjCPropertyAttribute::kind_nonatomic) |
| 296 | attributesAsWritten |= ObjCPropertyAttribute::kind_nonatomic; |
| 297 | if (Attributes & ObjCPropertyAttribute::kind_atomic) |
| 298 | attributesAsWritten |= ObjCPropertyAttribute::kind_atomic; |
| 299 | if (Attributes & ObjCPropertyAttribute::kind_class) |
| 300 | attributesAsWritten |= ObjCPropertyAttribute::kind_class; |
| 301 | if (Attributes & ObjCPropertyAttribute::kind_direct) |
| 302 | attributesAsWritten |= ObjCPropertyAttribute::kind_direct; |
| 303 | |
| 304 | return (ObjCPropertyAttribute::Kind)attributesAsWritten; |
| 305 | } |
| 306 | |
| 307 | static bool LocPropertyAttribute( ASTContext &Context, const char *attrName, |
| 308 | SourceLocation LParenLoc, SourceLocation &Loc) { |
| 309 | if (LParenLoc.isMacroID()) |
| 310 | return false; |
| 311 | |
| 312 | SourceManager &SM = Context.getSourceManager(); |
| 313 | FileIDAndOffset locInfo = SM.getDecomposedLoc(Loc: LParenLoc); |
| 314 | // Try to load the file buffer. |
| 315 | bool invalidTemp = false; |
| 316 | StringRef file = SM.getBufferData(FID: locInfo.first, Invalid: &invalidTemp); |
| 317 | if (invalidTemp) |
| 318 | return false; |
| 319 | const char *tokenBegin = file.data() + locInfo.second; |
| 320 | |
| 321 | // Lex from the start of the given location. |
| 322 | Lexer lexer(SM.getLocForStartOfFile(FID: locInfo.first), |
| 323 | Context.getLangOpts(), |
| 324 | file.begin(), tokenBegin, file.end()); |
| 325 | Token Tok; |
| 326 | do { |
| 327 | lexer.LexFromRawLexer(Result&: Tok); |
| 328 | if (Tok.is(K: tok::raw_identifier) && Tok.getRawIdentifier() == attrName) { |
| 329 | Loc = Tok.getLocation(); |
| 330 | return true; |
| 331 | } |
| 332 | } while (Tok.isNot(K: tok::r_paren)); |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /// Check for a mismatch in the atomicity of the given properties. |
| 337 | static void checkAtomicPropertyMismatch(Sema &S, |
| 338 | ObjCPropertyDecl *OldProperty, |
| 339 | ObjCPropertyDecl *NewProperty, |
| 340 | bool PropagateAtomicity) { |
| 341 | // If the atomicity of both matches, we're done. |
| 342 | bool OldIsAtomic = (OldProperty->getPropertyAttributes() & |
| 343 | ObjCPropertyAttribute::kind_nonatomic) == 0; |
| 344 | bool NewIsAtomic = (NewProperty->getPropertyAttributes() & |
| 345 | ObjCPropertyAttribute::kind_nonatomic) == 0; |
| 346 | if (OldIsAtomic == NewIsAtomic) return; |
| 347 | |
| 348 | // Determine whether the given property is readonly and implicitly |
| 349 | // atomic. |
| 350 | auto isImplicitlyReadonlyAtomic = [](ObjCPropertyDecl *Property) -> bool { |
| 351 | // Is it readonly? |
| 352 | auto Attrs = Property->getPropertyAttributes(); |
| 353 | if ((Attrs & ObjCPropertyAttribute::kind_readonly) == 0) |
| 354 | return false; |
| 355 | |
| 356 | // Is it nonatomic? |
| 357 | if (Attrs & ObjCPropertyAttribute::kind_nonatomic) |
| 358 | return false; |
| 359 | |
| 360 | // Was 'atomic' specified directly? |
| 361 | if (Property->getPropertyAttributesAsWritten() & |
| 362 | ObjCPropertyAttribute::kind_atomic) |
| 363 | return false; |
| 364 | |
| 365 | return true; |
| 366 | }; |
| 367 | |
| 368 | // If we're allowed to propagate atomicity, and the new property did |
| 369 | // not specify atomicity at all, propagate. |
| 370 | const unsigned AtomicityMask = (ObjCPropertyAttribute::kind_atomic | |
| 371 | ObjCPropertyAttribute::kind_nonatomic); |
| 372 | if (PropagateAtomicity && |
| 373 | ((NewProperty->getPropertyAttributesAsWritten() & AtomicityMask) == 0)) { |
| 374 | unsigned Attrs = NewProperty->getPropertyAttributes(); |
| 375 | Attrs = Attrs & ~AtomicityMask; |
| 376 | if (OldIsAtomic) |
| 377 | Attrs |= ObjCPropertyAttribute::kind_atomic; |
| 378 | else |
| 379 | Attrs |= ObjCPropertyAttribute::kind_nonatomic; |
| 380 | |
| 381 | NewProperty->overwritePropertyAttributes(PRVal: Attrs); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // One of the properties is atomic; if it's a readonly property, and |
| 386 | // 'atomic' wasn't explicitly specified, we're okay. |
| 387 | if ((OldIsAtomic && isImplicitlyReadonlyAtomic(OldProperty)) || |
| 388 | (NewIsAtomic && isImplicitlyReadonlyAtomic(NewProperty))) |
| 389 | return; |
| 390 | |
| 391 | // Diagnose the conflict. |
| 392 | const IdentifierInfo *OldContextName; |
| 393 | auto *OldDC = OldProperty->getDeclContext(); |
| 394 | if (auto Category = dyn_cast<ObjCCategoryDecl>(Val: OldDC)) |
| 395 | OldContextName = Category->getClassInterface()->getIdentifier(); |
| 396 | else |
| 397 | OldContextName = cast<ObjCContainerDecl>(Val: OldDC)->getIdentifier(); |
| 398 | |
| 399 | S.Diag(Loc: NewProperty->getLocation(), DiagID: diag::warn_property_attribute) |
| 400 | << NewProperty->getDeclName() << "atomic" |
| 401 | << OldContextName; |
| 402 | S.Diag(Loc: OldProperty->getLocation(), DiagID: diag::note_property_declare); |
| 403 | } |
| 404 | |
| 405 | ObjCPropertyDecl *SemaObjC::HandlePropertyInClassExtension( |
| 406 | Scope *S, SourceLocation AtLoc, SourceLocation LParenLoc, |
| 407 | FieldDeclarator &FD, Selector GetterSel, SourceLocation GetterNameLoc, |
| 408 | Selector SetterSel, SourceLocation SetterNameLoc, const bool isReadWrite, |
| 409 | unsigned &Attributes, const unsigned AttributesAsWritten, QualType T, |
| 410 | TypeSourceInfo *TSI, tok::ObjCKeywordKind MethodImplKind) { |
| 411 | ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(Val: SemaRef.CurContext); |
| 412 | // Diagnose if this property is already in continuation class. |
| 413 | DeclContext *DC = SemaRef.CurContext; |
| 414 | const IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
| 415 | ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface(); |
| 416 | |
| 417 | // We need to look in the @interface to see if the @property was |
| 418 | // already declared. |
| 419 | if (!CCPrimary) { |
| 420 | Diag(Loc: CDecl->getLocation(), DiagID: diag::err_continuation_class); |
| 421 | return nullptr; |
| 422 | } |
| 423 | |
| 424 | bool isClassProperty = |
| 425 | (AttributesAsWritten & ObjCPropertyAttribute::kind_class) || |
| 426 | (Attributes & ObjCPropertyAttribute::kind_class); |
| 427 | |
| 428 | // Find the property in the extended class's primary class or |
| 429 | // extensions. |
| 430 | ObjCPropertyDecl *PIDecl = CCPrimary->FindPropertyVisibleInPrimaryClass( |
| 431 | PropertyId, QueryKind: ObjCPropertyDecl::getQueryKind(isClassProperty)); |
| 432 | |
| 433 | // If we found a property in an extension, complain. |
| 434 | if (PIDecl && isa<ObjCCategoryDecl>(Val: PIDecl->getDeclContext())) { |
| 435 | Diag(Loc: AtLoc, DiagID: diag::err_duplicate_property); |
| 436 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 437 | return nullptr; |
| 438 | } |
| 439 | |
| 440 | // Check for consistency with the previous declaration, if there is one. |
| 441 | if (PIDecl) { |
| 442 | // A readonly property declared in the primary class can be refined |
| 443 | // by adding a readwrite property within an extension. |
| 444 | // Anything else is an error. |
| 445 | if (!(PIDecl->isReadOnly() && isReadWrite)) { |
| 446 | // Tailor the diagnostics for the common case where a readwrite |
| 447 | // property is declared both in the @interface and the continuation. |
| 448 | // This is a common error where the user often intended the original |
| 449 | // declaration to be readonly. |
| 450 | unsigned diag = |
| 451 | (Attributes & ObjCPropertyAttribute::kind_readwrite) && |
| 452 | (PIDecl->getPropertyAttributesAsWritten() & |
| 453 | ObjCPropertyAttribute::kind_readwrite) |
| 454 | ? diag::err_use_continuation_class_redeclaration_readwrite |
| 455 | : diag::err_use_continuation_class; |
| 456 | Diag(Loc: AtLoc, DiagID: diag) |
| 457 | << CCPrimary->getDeclName(); |
| 458 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 459 | return nullptr; |
| 460 | } |
| 461 | |
| 462 | // Check for consistency of getters. |
| 463 | if (PIDecl->getGetterName() != GetterSel) { |
| 464 | // If the getter was written explicitly, complain. |
| 465 | if (AttributesAsWritten & ObjCPropertyAttribute::kind_getter) { |
| 466 | Diag(Loc: AtLoc, DiagID: diag::warn_property_redecl_getter_mismatch) |
| 467 | << PIDecl->getGetterName() << GetterSel; |
| 468 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 469 | } |
| 470 | |
| 471 | // Always adopt the getter from the original declaration. |
| 472 | GetterSel = PIDecl->getGetterName(); |
| 473 | Attributes |= ObjCPropertyAttribute::kind_getter; |
| 474 | } |
| 475 | |
| 476 | // Check consistency of ownership. |
| 477 | unsigned ExistingOwnership |
| 478 | = getOwnershipRule(attr: PIDecl->getPropertyAttributes()); |
| 479 | unsigned NewOwnership = getOwnershipRule(attr: Attributes); |
| 480 | if (ExistingOwnership && NewOwnership != ExistingOwnership) { |
| 481 | // If the ownership was written explicitly, complain. |
| 482 | if (getOwnershipRule(attr: AttributesAsWritten)) { |
| 483 | Diag(Loc: AtLoc, DiagID: diag::warn_property_attr_mismatch); |
| 484 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 485 | } |
| 486 | |
| 487 | // Take the ownership from the original property. |
| 488 | Attributes = (Attributes & ~OwnershipMask) | ExistingOwnership; |
| 489 | } |
| 490 | |
| 491 | // If the redeclaration is 'weak' but the original property is not, |
| 492 | if ((Attributes & ObjCPropertyAttribute::kind_weak) && |
| 493 | !(PIDecl->getPropertyAttributesAsWritten() & |
| 494 | ObjCPropertyAttribute::kind_weak) && |
| 495 | PIDecl->getType()->getAs<ObjCObjectPointerType>() && |
| 496 | PIDecl->getType().getObjCLifetime() == Qualifiers::OCL_None) { |
| 497 | Diag(Loc: AtLoc, DiagID: diag::warn_property_implicitly_mismatched); |
| 498 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Create a new ObjCPropertyDecl with the DeclContext being |
| 503 | // the class extension. |
| 504 | ObjCPropertyDecl *PDecl = CreatePropertyDecl(S, CDecl, AtLoc, LParenLoc, |
| 505 | FD, GetterSel, GetterNameLoc, |
| 506 | SetterSel, SetterNameLoc, |
| 507 | isReadWrite, |
| 508 | Attributes, AttributesAsWritten, |
| 509 | T, TSI, MethodImplKind, lexicalDC: DC); |
| 510 | ASTContext &Context = getASTContext(); |
| 511 | // If there was no declaration of a property with the same name in |
| 512 | // the primary class, we're done. |
| 513 | if (!PIDecl) { |
| 514 | ProcessPropertyDecl(property: PDecl); |
| 515 | return PDecl; |
| 516 | } |
| 517 | |
| 518 | if (!Context.hasSameType(T1: PIDecl->getType(), T2: PDecl->getType())) { |
| 519 | bool IncompatibleObjC = false; |
| 520 | QualType ConvertedType; |
| 521 | // Relax the strict type matching for property type in continuation class. |
| 522 | // Allow property object type of continuation class to be different as long |
| 523 | // as it narrows the object type in its primary class property. Note that |
| 524 | // this conversion is safe only because the wider type is for a 'readonly' |
| 525 | // property in primary class and 'narrowed' type for a 'readwrite' property |
| 526 | // in continuation class. |
| 527 | QualType PrimaryClassPropertyT = Context.getCanonicalType(T: PIDecl->getType()); |
| 528 | QualType ClassExtPropertyT = Context.getCanonicalType(T: PDecl->getType()); |
| 529 | if (!isa<ObjCObjectPointerType>(Val: PrimaryClassPropertyT) || |
| 530 | !isa<ObjCObjectPointerType>(Val: ClassExtPropertyT) || |
| 531 | (!SemaRef.isObjCPointerConversion(FromType: ClassExtPropertyT, |
| 532 | ToType: PrimaryClassPropertyT, ConvertedType, |
| 533 | IncompatibleObjC)) || |
| 534 | IncompatibleObjC) { |
| 535 | Diag(Loc: AtLoc, |
| 536 | DiagID: diag::err_type_mismatch_continuation_class) << PDecl->getType(); |
| 537 | Diag(Loc: PIDecl->getLocation(), DiagID: diag::note_property_declare); |
| 538 | return nullptr; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Check that atomicity of property in class extension matches the previous |
| 543 | // declaration. |
| 544 | checkAtomicPropertyMismatch(S&: SemaRef, OldProperty: PIDecl, NewProperty: PDecl, PropagateAtomicity: true); |
| 545 | |
| 546 | // Make sure getter/setter are appropriately synthesized. |
| 547 | ProcessPropertyDecl(property: PDecl); |
| 548 | return PDecl; |
| 549 | } |
| 550 | |
| 551 | ObjCPropertyDecl *SemaObjC::CreatePropertyDecl( |
| 552 | Scope *S, ObjCContainerDecl *CDecl, SourceLocation AtLoc, |
| 553 | SourceLocation LParenLoc, FieldDeclarator &FD, Selector GetterSel, |
| 554 | SourceLocation GetterNameLoc, Selector SetterSel, |
| 555 | SourceLocation SetterNameLoc, const bool isReadWrite, |
| 556 | const unsigned Attributes, const unsigned AttributesAsWritten, QualType T, |
| 557 | TypeSourceInfo *TInfo, tok::ObjCKeywordKind MethodImplKind, |
| 558 | DeclContext *lexicalDC) { |
| 559 | ASTContext &Context = getASTContext(); |
| 560 | const IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
| 561 | |
| 562 | // Property defaults to 'assign' if it is readwrite, unless this is ARC |
| 563 | // and the type is retainable. |
| 564 | bool isAssign; |
| 565 | if (Attributes & (ObjCPropertyAttribute::kind_assign | |
| 566 | ObjCPropertyAttribute::kind_unsafe_unretained)) { |
| 567 | isAssign = true; |
| 568 | } else if (getOwnershipRule(attr: Attributes) || !isReadWrite) { |
| 569 | isAssign = false; |
| 570 | } else { |
| 571 | isAssign = (!getLangOpts().ObjCAutoRefCount || |
| 572 | !T->isObjCRetainableType()); |
| 573 | } |
| 574 | |
| 575 | // Issue a warning if property is 'assign' as default and its |
| 576 | // object, which is gc'able conforms to NSCopying protocol |
| 577 | if (getLangOpts().getGC() != LangOptions::NonGC && isAssign && |
| 578 | !(Attributes & ObjCPropertyAttribute::kind_assign)) { |
| 579 | if (const ObjCObjectPointerType *ObjPtrTy = |
| 580 | T->getAs<ObjCObjectPointerType>()) { |
| 581 | ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface(); |
| 582 | if (IDecl) |
| 583 | if (ObjCProtocolDecl* PNSCopying = |
| 584 | LookupProtocol(II: &Context.Idents.get(Name: "NSCopying" ), IdLoc: AtLoc)) |
| 585 | if (IDecl->ClassImplementsProtocol(lProto: PNSCopying, lookupCategory: true)) |
| 586 | Diag(Loc: AtLoc, DiagID: diag::warn_implements_nscopying) << PropertyId; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if (T->isObjCObjectType()) { |
| 591 | SourceLocation StarLoc = TInfo->getTypeLoc().getEndLoc(); |
| 592 | StarLoc = SemaRef.getLocForEndOfToken(Loc: StarLoc); |
| 593 | Diag(Loc: FD.D.getIdentifierLoc(), DiagID: diag::err_statically_allocated_object) |
| 594 | << FixItHint::CreateInsertion(InsertionLoc: StarLoc, Code: "*" ); |
| 595 | T = Context.getObjCObjectPointerType(OIT: T); |
| 596 | SourceLocation TLoc = TInfo->getTypeLoc().getBeginLoc(); |
| 597 | TInfo = Context.getTrivialTypeSourceInfo(T, Loc: TLoc); |
| 598 | } |
| 599 | |
| 600 | DeclContext *DC = CDecl; |
| 601 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(C&: Context, DC, |
| 602 | L: FD.D.getIdentifierLoc(), |
| 603 | Id: PropertyId, AtLocation: AtLoc, |
| 604 | LParenLocation: LParenLoc, T, TSI: TInfo); |
| 605 | |
| 606 | bool isClassProperty = |
| 607 | (AttributesAsWritten & ObjCPropertyAttribute::kind_class) || |
| 608 | (Attributes & ObjCPropertyAttribute::kind_class); |
| 609 | // Class property and instance property can have the same name. |
| 610 | if (ObjCPropertyDecl *prevDecl = ObjCPropertyDecl::findPropertyDecl( |
| 611 | DC, propertyID: PropertyId, queryKind: ObjCPropertyDecl::getQueryKind(isClassProperty))) { |
| 612 | Diag(Loc: PDecl->getLocation(), DiagID: diag::err_duplicate_property); |
| 613 | Diag(Loc: prevDecl->getLocation(), DiagID: diag::note_property_declare); |
| 614 | PDecl->setInvalidDecl(); |
| 615 | } |
| 616 | else { |
| 617 | DC->addDecl(D: PDecl); |
| 618 | if (lexicalDC) |
| 619 | PDecl->setLexicalDeclContext(lexicalDC); |
| 620 | } |
| 621 | |
| 622 | if (T->isArrayType() || T->isFunctionType()) { |
| 623 | Diag(Loc: AtLoc, DiagID: diag::err_property_type) << T; |
| 624 | PDecl->setInvalidDecl(); |
| 625 | } |
| 626 | |
| 627 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 628 | // selector names in anticipation of declaration of setter/getter methods. |
| 629 | PDecl->setGetterName(Sel: GetterSel, Loc: GetterNameLoc); |
| 630 | PDecl->setSetterName(Sel: SetterSel, Loc: SetterNameLoc); |
| 631 | PDecl->setPropertyAttributesAsWritten( |
| 632 | makePropertyAttributesAsWritten(Attributes: AttributesAsWritten)); |
| 633 | |
| 634 | SemaRef.ProcessDeclAttributes(S, D: PDecl, PD: FD.D); |
| 635 | |
| 636 | if (Attributes & ObjCPropertyAttribute::kind_readonly) |
| 637 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readonly); |
| 638 | |
| 639 | if (Attributes & ObjCPropertyAttribute::kind_getter) |
| 640 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_getter); |
| 641 | |
| 642 | if (Attributes & ObjCPropertyAttribute::kind_setter) |
| 643 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_setter); |
| 644 | |
| 645 | if (isReadWrite) |
| 646 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_readwrite); |
| 647 | |
| 648 | if (Attributes & ObjCPropertyAttribute::kind_retain) |
| 649 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_retain); |
| 650 | |
| 651 | if (Attributes & ObjCPropertyAttribute::kind_strong) |
| 652 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
| 653 | |
| 654 | if (Attributes & ObjCPropertyAttribute::kind_weak) |
| 655 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_weak); |
| 656 | |
| 657 | if (Attributes & ObjCPropertyAttribute::kind_copy) |
| 658 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_copy); |
| 659 | |
| 660 | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
| 661 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained); |
| 662 | |
| 663 | if (isAssign) |
| 664 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign); |
| 665 | |
| 666 | // In the semantic attributes, one of nonatomic or atomic is always set. |
| 667 | if (Attributes & ObjCPropertyAttribute::kind_nonatomic) |
| 668 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nonatomic); |
| 669 | else |
| 670 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_atomic); |
| 671 | |
| 672 | // 'unsafe_unretained' is alias for 'assign'. |
| 673 | if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) |
| 674 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign); |
| 675 | if (isAssign) |
| 676 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_unsafe_unretained); |
| 677 | |
| 678 | if (MethodImplKind == tok::objc_required) |
| 679 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 680 | else if (MethodImplKind == tok::objc_optional) |
| 681 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
| 682 | |
| 683 | if (Attributes & ObjCPropertyAttribute::kind_nullability) |
| 684 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_nullability); |
| 685 | |
| 686 | if (Attributes & ObjCPropertyAttribute::kind_null_resettable) |
| 687 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_null_resettable); |
| 688 | |
| 689 | if (Attributes & ObjCPropertyAttribute::kind_class) |
| 690 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_class); |
| 691 | |
| 692 | if ((Attributes & ObjCPropertyAttribute::kind_direct) || |
| 693 | CDecl->hasAttr<ObjCDirectMembersAttr>()) { |
| 694 | if (isa<ObjCProtocolDecl>(Val: CDecl)) { |
| 695 | Diag(Loc: PDecl->getLocation(), DiagID: diag::err_objc_direct_on_protocol) << true; |
| 696 | } else if (getLangOpts().ObjCRuntime.allowsDirectDispatch()) { |
| 697 | PDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_direct); |
| 698 | } else { |
| 699 | Diag(Loc: PDecl->getLocation(), DiagID: diag::warn_objc_direct_property_ignored) |
| 700 | << PDecl->getDeclName(); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return PDecl; |
| 705 | } |
| 706 | |
| 707 | static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, |
| 708 | ObjCPropertyDecl *property, |
| 709 | ObjCIvarDecl *ivar) { |
| 710 | if (property->isInvalidDecl() || ivar->isInvalidDecl()) return; |
| 711 | |
| 712 | QualType ivarType = ivar->getType(); |
| 713 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
| 714 | |
| 715 | // The lifetime implied by the property's attributes. |
| 716 | Qualifiers::ObjCLifetime propertyLifetime = |
| 717 | getImpliedARCOwnership(attrs: property->getPropertyAttributes(), |
| 718 | type: property->getType()); |
| 719 | |
| 720 | // We're fine if they match. |
| 721 | if (propertyLifetime == ivarLifetime) return; |
| 722 | |
| 723 | // None isn't a valid lifetime for an object ivar in ARC, and |
| 724 | // __autoreleasing is never valid; don't diagnose twice. |
| 725 | if ((ivarLifetime == Qualifiers::OCL_None && |
| 726 | S.getLangOpts().ObjCAutoRefCount) || |
| 727 | ivarLifetime == Qualifiers::OCL_Autoreleasing) |
| 728 | return; |
| 729 | |
| 730 | // If the ivar is private, and it's implicitly __unsafe_unretained |
| 731 | // because of its type, then pretend it was actually implicitly |
| 732 | // __strong. This is only sound because we're processing the |
| 733 | // property implementation before parsing any method bodies. |
| 734 | if (ivarLifetime == Qualifiers::OCL_ExplicitNone && |
| 735 | propertyLifetime == Qualifiers::OCL_Strong && |
| 736 | ivar->getAccessControl() == ObjCIvarDecl::Private) { |
| 737 | SplitQualType split = ivarType.split(); |
| 738 | if (split.Quals.hasObjCLifetime()) { |
| 739 | assert(ivarType->isObjCARCImplicitlyUnretainedType()); |
| 740 | split.Quals.setObjCLifetime(Qualifiers::OCL_Strong); |
| 741 | ivarType = S.Context.getQualifiedType(split); |
| 742 | ivar->setType(ivarType); |
| 743 | return; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | switch (propertyLifetime) { |
| 748 | case Qualifiers::OCL_Strong: |
| 749 | S.Diag(Loc: ivar->getLocation(), DiagID: diag::err_arc_strong_property_ownership) |
| 750 | << property->getDeclName() |
| 751 | << ivar->getDeclName() |
| 752 | << ivarLifetime; |
| 753 | break; |
| 754 | |
| 755 | case Qualifiers::OCL_Weak: |
| 756 | S.Diag(Loc: ivar->getLocation(), DiagID: diag::err_weak_property) |
| 757 | << property->getDeclName() |
| 758 | << ivar->getDeclName(); |
| 759 | break; |
| 760 | |
| 761 | case Qualifiers::OCL_ExplicitNone: |
| 762 | S.Diag(Loc: ivar->getLocation(), DiagID: diag::err_arc_assign_property_ownership) |
| 763 | << property->getDeclName() << ivar->getDeclName() |
| 764 | << ((property->getPropertyAttributesAsWritten() & |
| 765 | ObjCPropertyAttribute::kind_assign) != 0); |
| 766 | break; |
| 767 | |
| 768 | case Qualifiers::OCL_Autoreleasing: |
| 769 | llvm_unreachable("properties cannot be autoreleasing" ); |
| 770 | |
| 771 | case Qualifiers::OCL_None: |
| 772 | // Any other property should be ignored. |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | S.Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 777 | if (propertyImplLoc.isValid()) |
| 778 | S.Diag(Loc: propertyImplLoc, DiagID: diag::note_property_synthesize); |
| 779 | } |
| 780 | |
| 781 | /// setImpliedPropertyAttributeForReadOnlyProperty - |
| 782 | /// This routine evaludates life-time attributes for a 'readonly' |
| 783 | /// property with no known lifetime of its own, using backing |
| 784 | /// 'ivar's attribute, if any. If no backing 'ivar', property's |
| 785 | /// life-time is assumed 'strong'. |
| 786 | static void setImpliedPropertyAttributeForReadOnlyProperty( |
| 787 | ObjCPropertyDecl *property, ObjCIvarDecl *ivar) { |
| 788 | Qualifiers::ObjCLifetime propertyLifetime = |
| 789 | getImpliedARCOwnership(attrs: property->getPropertyAttributes(), |
| 790 | type: property->getType()); |
| 791 | if (propertyLifetime != Qualifiers::OCL_None) |
| 792 | return; |
| 793 | |
| 794 | if (!ivar) { |
| 795 | // if no backing ivar, make property 'strong'. |
| 796 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
| 797 | return; |
| 798 | } |
| 799 | // property assumes owenership of backing ivar. |
| 800 | QualType ivarType = ivar->getType(); |
| 801 | Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime(); |
| 802 | if (ivarLifetime == Qualifiers::OCL_Strong) |
| 803 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
| 804 | else if (ivarLifetime == Qualifiers::OCL_Weak) |
| 805 | property->setPropertyAttributes(ObjCPropertyAttribute::kind_weak); |
| 806 | } |
| 807 | |
| 808 | static bool isIncompatiblePropertyAttribute(unsigned Attr1, unsigned Attr2, |
| 809 | ObjCPropertyAttribute::Kind Kind) { |
| 810 | return (Attr1 & Kind) != (Attr2 & Kind); |
| 811 | } |
| 812 | |
| 813 | static bool areIncompatiblePropertyAttributes(unsigned Attr1, unsigned Attr2, |
| 814 | unsigned Kinds) { |
| 815 | return ((Attr1 & Kinds) != 0) != ((Attr2 & Kinds) != 0); |
| 816 | } |
| 817 | |
| 818 | /// SelectPropertyForSynthesisFromProtocols - Finds the most appropriate |
| 819 | /// property declaration that should be synthesised in all of the inherited |
| 820 | /// protocols. It also diagnoses properties declared in inherited protocols with |
| 821 | /// mismatched types or attributes, since any of them can be candidate for |
| 822 | /// synthesis. |
| 823 | static ObjCPropertyDecl * |
| 824 | SelectPropertyForSynthesisFromProtocols(Sema &S, SourceLocation AtLoc, |
| 825 | ObjCInterfaceDecl *ClassDecl, |
| 826 | ObjCPropertyDecl *Property) { |
| 827 | assert(isa<ObjCProtocolDecl>(Property->getDeclContext()) && |
| 828 | "Expected a property from a protocol" ); |
| 829 | ObjCInterfaceDecl::ProtocolPropertySet ProtocolSet; |
| 830 | ObjCInterfaceDecl::PropertyDeclOrder Properties; |
| 831 | for (const auto *PI : ClassDecl->all_referenced_protocols()) { |
| 832 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
| 833 | PDecl->collectInheritedProtocolProperties(Property, PS&: ProtocolSet, |
| 834 | PO&: Properties); |
| 835 | } |
| 836 | if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass()) { |
| 837 | while (SDecl) { |
| 838 | for (const auto *PI : SDecl->all_referenced_protocols()) { |
| 839 | if (const ObjCProtocolDecl *PDecl = PI->getDefinition()) |
| 840 | PDecl->collectInheritedProtocolProperties(Property, PS&: ProtocolSet, |
| 841 | PO&: Properties); |
| 842 | } |
| 843 | SDecl = SDecl->getSuperClass(); |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if (Properties.empty()) |
| 848 | return Property; |
| 849 | |
| 850 | ObjCPropertyDecl *OriginalProperty = Property; |
| 851 | size_t SelectedIndex = 0; |
| 852 | for (const auto &Prop : llvm::enumerate(First&: Properties)) { |
| 853 | // Select the 'readwrite' property if such property exists. |
| 854 | if (Property->isReadOnly() && !Prop.value()->isReadOnly()) { |
| 855 | Property = Prop.value(); |
| 856 | SelectedIndex = Prop.index(); |
| 857 | } |
| 858 | } |
| 859 | if (Property != OriginalProperty) { |
| 860 | // Check that the old property is compatible with the new one. |
| 861 | Properties[SelectedIndex] = OriginalProperty; |
| 862 | } |
| 863 | |
| 864 | QualType RHSType = S.Context.getCanonicalType(T: Property->getType()); |
| 865 | unsigned OriginalAttributes = Property->getPropertyAttributesAsWritten(); |
| 866 | enum MismatchKind { |
| 867 | IncompatibleType = 0, |
| 868 | HasNoExpectedAttribute, |
| 869 | HasUnexpectedAttribute, |
| 870 | DifferentGetter, |
| 871 | DifferentSetter |
| 872 | }; |
| 873 | // Represents a property from another protocol that conflicts with the |
| 874 | // selected declaration. |
| 875 | struct MismatchingProperty { |
| 876 | const ObjCPropertyDecl *Prop; |
| 877 | MismatchKind Kind; |
| 878 | StringRef AttributeName; |
| 879 | }; |
| 880 | SmallVector<MismatchingProperty, 4> Mismatches; |
| 881 | for (ObjCPropertyDecl *Prop : Properties) { |
| 882 | // Verify the property attributes. |
| 883 | unsigned Attr = Prop->getPropertyAttributesAsWritten(); |
| 884 | if (Attr != OriginalAttributes) { |
| 885 | auto Diag = [&](bool OriginalHasAttribute, StringRef AttributeName) { |
| 886 | MismatchKind Kind = OriginalHasAttribute ? HasNoExpectedAttribute |
| 887 | : HasUnexpectedAttribute; |
| 888 | Mismatches.push_back(Elt: {.Prop: Prop, .Kind: Kind, .AttributeName: AttributeName}); |
| 889 | }; |
| 890 | // The ownership might be incompatible unless the property has no explicit |
| 891 | // ownership. |
| 892 | bool HasOwnership = |
| 893 | (Attr & (ObjCPropertyAttribute::kind_retain | |
| 894 | ObjCPropertyAttribute::kind_strong | |
| 895 | ObjCPropertyAttribute::kind_copy | |
| 896 | ObjCPropertyAttribute::kind_assign | |
| 897 | ObjCPropertyAttribute::kind_unsafe_unretained | |
| 898 | ObjCPropertyAttribute::kind_weak)) != 0; |
| 899 | if (HasOwnership && |
| 900 | isIncompatiblePropertyAttribute(Attr1: OriginalAttributes, Attr2: Attr, |
| 901 | Kind: ObjCPropertyAttribute::kind_copy)) { |
| 902 | Diag(OriginalAttributes & ObjCPropertyAttribute::kind_copy, "copy" ); |
| 903 | continue; |
| 904 | } |
| 905 | if (HasOwnership && areIncompatiblePropertyAttributes( |
| 906 | Attr1: OriginalAttributes, Attr2: Attr, |
| 907 | Kinds: ObjCPropertyAttribute::kind_retain | |
| 908 | ObjCPropertyAttribute::kind_strong)) { |
| 909 | Diag(OriginalAttributes & (ObjCPropertyAttribute::kind_retain | |
| 910 | ObjCPropertyAttribute::kind_strong), |
| 911 | "retain (or strong)" ); |
| 912 | continue; |
| 913 | } |
| 914 | if (isIncompatiblePropertyAttribute(Attr1: OriginalAttributes, Attr2: Attr, |
| 915 | Kind: ObjCPropertyAttribute::kind_atomic)) { |
| 916 | Diag(OriginalAttributes & ObjCPropertyAttribute::kind_atomic, "atomic" ); |
| 917 | continue; |
| 918 | } |
| 919 | } |
| 920 | if (Property->getGetterName() != Prop->getGetterName()) { |
| 921 | Mismatches.push_back(Elt: {.Prop: Prop, .Kind: DifferentGetter, .AttributeName: "" }); |
| 922 | continue; |
| 923 | } |
| 924 | if (!Property->isReadOnly() && !Prop->isReadOnly() && |
| 925 | Property->getSetterName() != Prop->getSetterName()) { |
| 926 | Mismatches.push_back(Elt: {.Prop: Prop, .Kind: DifferentSetter, .AttributeName: "" }); |
| 927 | continue; |
| 928 | } |
| 929 | QualType LHSType = S.Context.getCanonicalType(T: Prop->getType()); |
| 930 | if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
| 931 | bool IncompatibleObjC = false; |
| 932 | QualType ConvertedType; |
| 933 | if (!S.isObjCPointerConversion(FromType: RHSType, ToType: LHSType, ConvertedType, IncompatibleObjC) |
| 934 | || IncompatibleObjC) { |
| 935 | Mismatches.push_back(Elt: {.Prop: Prop, .Kind: IncompatibleType, .AttributeName: "" }); |
| 936 | continue; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if (Mismatches.empty()) |
| 942 | return Property; |
| 943 | |
| 944 | // Diagnose incompability. |
| 945 | { |
| 946 | bool HasIncompatibleAttributes = false; |
| 947 | for (const auto &Note : Mismatches) |
| 948 | HasIncompatibleAttributes = |
| 949 | Note.Kind != IncompatibleType ? true : HasIncompatibleAttributes; |
| 950 | // Promote the warning to an error if there are incompatible attributes or |
| 951 | // incompatible types together with readwrite/readonly incompatibility. |
| 952 | auto Diag = S.Diag(Loc: Property->getLocation(), |
| 953 | DiagID: Property != OriginalProperty || HasIncompatibleAttributes |
| 954 | ? diag::err_protocol_property_mismatch |
| 955 | : diag::warn_protocol_property_mismatch); |
| 956 | Diag << Mismatches[0].Kind; |
| 957 | switch (Mismatches[0].Kind) { |
| 958 | case IncompatibleType: |
| 959 | Diag << Property->getType(); |
| 960 | break; |
| 961 | case HasNoExpectedAttribute: |
| 962 | case HasUnexpectedAttribute: |
| 963 | Diag << Mismatches[0].AttributeName; |
| 964 | break; |
| 965 | case DifferentGetter: |
| 966 | Diag << Property->getGetterName(); |
| 967 | break; |
| 968 | case DifferentSetter: |
| 969 | Diag << Property->getSetterName(); |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | for (const auto &Note : Mismatches) { |
| 974 | auto Diag = |
| 975 | S.Diag(Loc: Note.Prop->getLocation(), DiagID: diag::note_protocol_property_declare) |
| 976 | << Note.Kind; |
| 977 | switch (Note.Kind) { |
| 978 | case IncompatibleType: |
| 979 | Diag << Note.Prop->getType(); |
| 980 | break; |
| 981 | case HasNoExpectedAttribute: |
| 982 | case HasUnexpectedAttribute: |
| 983 | Diag << Note.AttributeName; |
| 984 | break; |
| 985 | case DifferentGetter: |
| 986 | Diag << Note.Prop->getGetterName(); |
| 987 | break; |
| 988 | case DifferentSetter: |
| 989 | Diag << Note.Prop->getSetterName(); |
| 990 | break; |
| 991 | } |
| 992 | } |
| 993 | if (AtLoc.isValid()) |
| 994 | S.Diag(Loc: AtLoc, DiagID: diag::note_property_synthesize); |
| 995 | |
| 996 | return Property; |
| 997 | } |
| 998 | |
| 999 | /// Determine whether any storage attributes were written on the property. |
| 1000 | static bool hasWrittenStorageAttribute(ObjCPropertyDecl *Prop, |
| 1001 | ObjCPropertyQueryKind QueryKind) { |
| 1002 | if (Prop->getPropertyAttributesAsWritten() & OwnershipMask) return true; |
| 1003 | |
| 1004 | // If this is a readwrite property in a class extension that refines |
| 1005 | // a readonly property in the original class definition, check it as |
| 1006 | // well. |
| 1007 | |
| 1008 | // If it's a readonly property, we're not interested. |
| 1009 | if (Prop->isReadOnly()) return false; |
| 1010 | |
| 1011 | // Is it declared in an extension? |
| 1012 | auto Category = dyn_cast<ObjCCategoryDecl>(Val: Prop->getDeclContext()); |
| 1013 | if (!Category || !Category->IsClassExtension()) return false; |
| 1014 | |
| 1015 | // Find the corresponding property in the primary class definition. |
| 1016 | auto OrigClass = Category->getClassInterface(); |
| 1017 | for (auto *Found : OrigClass->lookup(Name: Prop->getDeclName())) { |
| 1018 | if (ObjCPropertyDecl *OrigProp = dyn_cast<ObjCPropertyDecl>(Val: Found)) |
| 1019 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
| 1020 | } |
| 1021 | |
| 1022 | // Look through all of the protocols. |
| 1023 | for (const auto *Proto : OrigClass->all_referenced_protocols()) { |
| 1024 | if (ObjCPropertyDecl *OrigProp = Proto->FindPropertyDeclaration( |
| 1025 | PropertyId: Prop->getIdentifier(), QueryKind)) |
| 1026 | return OrigProp->getPropertyAttributesAsWritten() & OwnershipMask; |
| 1027 | } |
| 1028 | |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | /// Create a synthesized property accessor stub inside the \@implementation. |
| 1033 | static ObjCMethodDecl * |
| 1034 | RedeclarePropertyAccessor(ASTContext &Context, ObjCImplementationDecl *Impl, |
| 1035 | ObjCMethodDecl *AccessorDecl, SourceLocation AtLoc, |
| 1036 | SourceLocation PropertyLoc) { |
| 1037 | ObjCMethodDecl *Decl = AccessorDecl; |
| 1038 | ObjCMethodDecl *ImplDecl = ObjCMethodDecl::Create( |
| 1039 | C&: Context, beginLoc: AtLoc.isValid() ? AtLoc : Decl->getBeginLoc(), |
| 1040 | endLoc: PropertyLoc.isValid() ? PropertyLoc : Decl->getEndLoc(), |
| 1041 | SelInfo: Decl->getSelector(), T: Decl->getReturnType(), |
| 1042 | ReturnTInfo: Decl->getReturnTypeSourceInfo(), contextDecl: Impl, isInstance: Decl->isInstanceMethod(), |
| 1043 | isVariadic: Decl->isVariadic(), isPropertyAccessor: Decl->isPropertyAccessor(), |
| 1044 | /* isSynthesized*/ isSynthesizedAccessorStub: true, isImplicitlyDeclared: Decl->isImplicit(), isDefined: Decl->isDefined(), |
| 1045 | impControl: Decl->getImplementationControl(), HasRelatedResultType: Decl->hasRelatedResultType()); |
| 1046 | ImplDecl->getMethodFamily(); |
| 1047 | if (Decl->hasAttrs()) |
| 1048 | ImplDecl->setAttrs(Decl->getAttrs()); |
| 1049 | ImplDecl->setSelfDecl(Decl->getSelfDecl()); |
| 1050 | ImplDecl->setCmdDecl(Decl->getCmdDecl()); |
| 1051 | SmallVector<SourceLocation, 1> SelLocs; |
| 1052 | Decl->getSelectorLocs(SelLocs); |
| 1053 | ImplDecl->setMethodParams(C&: Context, Params: Decl->parameters(), SelLocs); |
| 1054 | ImplDecl->setLexicalDeclContext(Impl); |
| 1055 | ImplDecl->setDefined(false); |
| 1056 | return ImplDecl; |
| 1057 | } |
| 1058 | |
| 1059 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1060 | /// builds the AST node for a property implementation declaration; declared |
| 1061 | /// as \@synthesize or \@dynamic. |
| 1062 | /// |
| 1063 | Decl *SemaObjC::ActOnPropertyImplDecl( |
| 1064 | Scope *S, SourceLocation AtLoc, SourceLocation PropertyLoc, bool Synthesize, |
| 1065 | IdentifierInfo *PropertyId, IdentifierInfo *PropertyIvar, |
| 1066 | SourceLocation PropertyIvarLoc, ObjCPropertyQueryKind QueryKind) { |
| 1067 | ASTContext &Context = getASTContext(); |
| 1068 | ObjCContainerDecl *ClassImpDecl = |
| 1069 | dyn_cast<ObjCContainerDecl>(Val: SemaRef.CurContext); |
| 1070 | // Make sure we have a context for the property implementation declaration. |
| 1071 | if (!ClassImpDecl) { |
| 1072 | Diag(Loc: AtLoc, DiagID: diag::err_missing_property_context); |
| 1073 | return nullptr; |
| 1074 | } |
| 1075 | if (PropertyIvarLoc.isInvalid()) |
| 1076 | PropertyIvarLoc = PropertyLoc; |
| 1077 | SourceLocation PropertyDiagLoc = PropertyLoc; |
| 1078 | if (PropertyDiagLoc.isInvalid()) |
| 1079 | PropertyDiagLoc = ClassImpDecl->getBeginLoc(); |
| 1080 | ObjCPropertyDecl *property = nullptr; |
| 1081 | ObjCInterfaceDecl *IDecl = nullptr; |
| 1082 | // Find the class or category class where this property must have |
| 1083 | // a declaration. |
| 1084 | ObjCImplementationDecl *IC = nullptr; |
| 1085 | ObjCCategoryImplDecl *CatImplClass = nullptr; |
| 1086 | if ((IC = dyn_cast<ObjCImplementationDecl>(Val: ClassImpDecl))) { |
| 1087 | IDecl = IC->getClassInterface(); |
| 1088 | // We always synthesize an interface for an implementation |
| 1089 | // without an interface decl. So, IDecl is always non-zero. |
| 1090 | assert(IDecl && |
| 1091 | "ActOnPropertyImplDecl - @implementation without @interface" ); |
| 1092 | |
| 1093 | // Look for this property declaration in the @implementation's @interface |
| 1094 | property = IDecl->FindPropertyDeclaration(PropertyId, QueryKind); |
| 1095 | if (!property) { |
| 1096 | Diag(Loc: PropertyLoc, DiagID: diag::err_bad_property_decl) << IDecl->getDeclName(); |
| 1097 | return nullptr; |
| 1098 | } |
| 1099 | if (property->isClassProperty() && Synthesize) { |
| 1100 | Diag(Loc: PropertyLoc, DiagID: diag::err_synthesize_on_class_property) << PropertyId; |
| 1101 | return nullptr; |
| 1102 | } |
| 1103 | unsigned PIkind = property->getPropertyAttributesAsWritten(); |
| 1104 | if ((PIkind & (ObjCPropertyAttribute::kind_atomic | |
| 1105 | ObjCPropertyAttribute::kind_nonatomic)) == 0) { |
| 1106 | if (AtLoc.isValid()) |
| 1107 | Diag(Loc: AtLoc, DiagID: diag::warn_implicit_atomic_property); |
| 1108 | else |
| 1109 | Diag(Loc: IC->getLocation(), DiagID: diag::warn_auto_implicit_atomic_property); |
| 1110 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1111 | } |
| 1112 | |
| 1113 | if (const ObjCCategoryDecl *CD = |
| 1114 | dyn_cast<ObjCCategoryDecl>(Val: property->getDeclContext())) { |
| 1115 | if (!CD->IsClassExtension()) { |
| 1116 | Diag(Loc: PropertyLoc, DiagID: diag::err_category_property) << CD->getDeclName(); |
| 1117 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1118 | return nullptr; |
| 1119 | } |
| 1120 | } |
| 1121 | if (Synthesize && (PIkind & ObjCPropertyAttribute::kind_readonly) && |
| 1122 | property->hasAttr<IBOutletAttr>() && !AtLoc.isValid()) { |
| 1123 | bool ReadWriteProperty = false; |
| 1124 | // Search into the class extensions and see if 'readonly property is |
| 1125 | // redeclared 'readwrite', then no warning is to be issued. |
| 1126 | for (auto *Ext : IDecl->known_extensions()) { |
| 1127 | DeclContext::lookup_result R = Ext->lookup(Name: property->getDeclName()); |
| 1128 | if (auto *ExtProp = R.find_first<ObjCPropertyDecl>()) { |
| 1129 | PIkind = ExtProp->getPropertyAttributesAsWritten(); |
| 1130 | if (PIkind & ObjCPropertyAttribute::kind_readwrite) { |
| 1131 | ReadWriteProperty = true; |
| 1132 | break; |
| 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (!ReadWriteProperty) { |
| 1138 | Diag(Loc: property->getLocation(), DiagID: diag::warn_auto_readonly_iboutlet_property) |
| 1139 | << property; |
| 1140 | SourceLocation readonlyLoc; |
| 1141 | if (LocPropertyAttribute(Context, attrName: "readonly" , |
| 1142 | LParenLoc: property->getLParenLoc(), Loc&: readonlyLoc)) { |
| 1143 | SourceLocation endLoc = |
| 1144 | readonlyLoc.getLocWithOffset(Offset: strlen(s: "readonly" )-1); |
| 1145 | SourceRange ReadonlySourceRange(readonlyLoc, endLoc); |
| 1146 | Diag(Loc: property->getLocation(), |
| 1147 | DiagID: diag::note_auto_readonly_iboutlet_fixup_suggest) << |
| 1148 | FixItHint::CreateReplacement(RemoveRange: ReadonlySourceRange, Code: "readwrite" ); |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | if (Synthesize && isa<ObjCProtocolDecl>(Val: property->getDeclContext())) |
| 1153 | property = SelectPropertyForSynthesisFromProtocols(S&: SemaRef, AtLoc, ClassDecl: IDecl, |
| 1154 | Property: property); |
| 1155 | |
| 1156 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(Val: ClassImpDecl))) { |
| 1157 | if (Synthesize) { |
| 1158 | Diag(Loc: AtLoc, DiagID: diag::err_synthesize_category_decl); |
| 1159 | return nullptr; |
| 1160 | } |
| 1161 | IDecl = CatImplClass->getClassInterface(); |
| 1162 | if (!IDecl) { |
| 1163 | Diag(Loc: AtLoc, DiagID: diag::err_missing_property_interface); |
| 1164 | return nullptr; |
| 1165 | } |
| 1166 | ObjCCategoryDecl *Category = |
| 1167 | IDecl->FindCategoryDeclaration(CategoryId: CatImplClass->getIdentifier()); |
| 1168 | |
| 1169 | // If category for this implementation not found, it is an error which |
| 1170 | // has already been reported eralier. |
| 1171 | if (!Category) |
| 1172 | return nullptr; |
| 1173 | // Look for this property declaration in @implementation's category |
| 1174 | property = Category->FindPropertyDeclaration(PropertyId, QueryKind); |
| 1175 | if (!property) { |
| 1176 | Diag(Loc: PropertyLoc, DiagID: diag::err_bad_category_property_decl) |
| 1177 | << Category->getDeclName(); |
| 1178 | return nullptr; |
| 1179 | } |
| 1180 | } else { |
| 1181 | Diag(Loc: AtLoc, DiagID: diag::err_bad_property_context); |
| 1182 | return nullptr; |
| 1183 | } |
| 1184 | ObjCIvarDecl *Ivar = nullptr; |
| 1185 | bool CompleteTypeErr = false; |
| 1186 | bool compat = true; |
| 1187 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1188 | if (Synthesize) { |
| 1189 | // @synthesize |
| 1190 | if (!PropertyIvar) |
| 1191 | PropertyIvar = PropertyId; |
| 1192 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
| 1193 | ObjCInterfaceDecl *ClassDeclared; |
| 1194 | Ivar = IDecl->lookupInstanceVariable(IVarName: PropertyIvar, ClassDeclared); |
| 1195 | QualType PropType = property->getType(); |
| 1196 | QualType PropertyIvarType = PropType.getNonReferenceType(); |
| 1197 | |
| 1198 | if (SemaRef.RequireCompleteType(Loc: PropertyDiagLoc, T: PropertyIvarType, |
| 1199 | DiagID: diag::err_incomplete_synthesized_property, |
| 1200 | Args: property->getDeclName())) { |
| 1201 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1202 | CompleteTypeErr = true; |
| 1203 | } |
| 1204 | |
| 1205 | if (getLangOpts().ObjCAutoRefCount && |
| 1206 | (property->getPropertyAttributesAsWritten() & |
| 1207 | ObjCPropertyAttribute::kind_readonly) && |
| 1208 | PropertyIvarType->isObjCRetainableType()) { |
| 1209 | setImpliedPropertyAttributeForReadOnlyProperty(property, ivar: Ivar); |
| 1210 | } |
| 1211 | |
| 1212 | ObjCPropertyAttribute::Kind kind = property->getPropertyAttributes(); |
| 1213 | |
| 1214 | bool isARCWeak = false; |
| 1215 | if (kind & ObjCPropertyAttribute::kind_weak) { |
| 1216 | // Add GC __weak to the ivar type if the property is weak. |
| 1217 | if (getLangOpts().getGC() != LangOptions::NonGC) { |
| 1218 | assert(!getLangOpts().ObjCAutoRefCount); |
| 1219 | if (PropertyIvarType.isObjCGCStrong()) { |
| 1220 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_gc_weak_property_strong_type); |
| 1221 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1222 | } else { |
| 1223 | PropertyIvarType = |
| 1224 | Context.getObjCGCQualType(T: PropertyIvarType, gcAttr: Qualifiers::Weak); |
| 1225 | } |
| 1226 | |
| 1227 | // Otherwise, check whether ARC __weak is enabled and works with |
| 1228 | // the property type. |
| 1229 | } else { |
| 1230 | if (!getLangOpts().ObjCWeak) { |
| 1231 | // Only complain here when synthesizing an ivar. |
| 1232 | if (!Ivar) { |
| 1233 | Diag(Loc: PropertyDiagLoc, |
| 1234 | DiagID: getLangOpts().ObjCWeakRuntime |
| 1235 | ? diag::err_synthesizing_arc_weak_property_disabled |
| 1236 | : diag::err_synthesizing_arc_weak_property_no_runtime); |
| 1237 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1238 | } |
| 1239 | CompleteTypeErr = true; // suppress later diagnostics about the ivar |
| 1240 | } else { |
| 1241 | isARCWeak = true; |
| 1242 | if (const ObjCObjectPointerType *ObjT = |
| 1243 | PropertyIvarType->getAs<ObjCObjectPointerType>()) { |
| 1244 | const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl(); |
| 1245 | if (ObjI && ObjI->isArcWeakrefUnavailable()) { |
| 1246 | Diag(Loc: property->getLocation(), |
| 1247 | DiagID: diag::err_arc_weak_unavailable_property) |
| 1248 | << PropertyIvarType; |
| 1249 | Diag(Loc: ClassImpDecl->getLocation(), DiagID: diag::note_implemented_by_class) |
| 1250 | << ClassImpDecl->getName(); |
| 1251 | } |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | if (AtLoc.isInvalid()) { |
| 1258 | // Check when default synthesizing a property that there is |
| 1259 | // an ivar matching property name and issue warning; since this |
| 1260 | // is the most common case of not using an ivar used for backing |
| 1261 | // property in non-default synthesis case. |
| 1262 | ObjCInterfaceDecl *ClassDeclared=nullptr; |
| 1263 | ObjCIvarDecl *originalIvar = |
| 1264 | IDecl->lookupInstanceVariable(IVarName: property->getIdentifier(), |
| 1265 | ClassDeclared); |
| 1266 | if (originalIvar) { |
| 1267 | Diag(Loc: PropertyDiagLoc, |
| 1268 | DiagID: diag::warn_autosynthesis_property_ivar_match) |
| 1269 | << PropertyId << (Ivar == nullptr) << PropertyIvar |
| 1270 | << originalIvar->getIdentifier(); |
| 1271 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1272 | Diag(Loc: originalIvar->getLocation(), DiagID: diag::note_ivar_decl); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | if (!Ivar) { |
| 1277 | // In ARC, give the ivar a lifetime qualifier based on the |
| 1278 | // property attributes. |
| 1279 | if ((getLangOpts().ObjCAutoRefCount || isARCWeak) && |
| 1280 | !PropertyIvarType.getObjCLifetime() && |
| 1281 | PropertyIvarType->isObjCRetainableType()) { |
| 1282 | |
| 1283 | // It's an error if we have to do this and the user didn't |
| 1284 | // explicitly write an ownership attribute on the property. |
| 1285 | if (!hasWrittenStorageAttribute(Prop: property, QueryKind) && |
| 1286 | !(kind & ObjCPropertyAttribute::kind_strong)) { |
| 1287 | Diag(Loc: PropertyDiagLoc, |
| 1288 | DiagID: diag::err_arc_objc_property_default_assign_on_object); |
| 1289 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1290 | } else { |
| 1291 | Qualifiers::ObjCLifetime lifetime = |
| 1292 | getImpliedARCOwnership(attrs: kind, type: PropertyIvarType); |
| 1293 | assert(lifetime && "no lifetime for property?" ); |
| 1294 | |
| 1295 | Qualifiers qs; |
| 1296 | qs.addObjCLifetime(type: lifetime); |
| 1297 | PropertyIvarType = Context.getQualifiedType(T: PropertyIvarType, Qs: qs); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | Ivar = ObjCIvarDecl::Create(C&: Context, DC: ClassImpDecl, |
| 1302 | StartLoc: PropertyIvarLoc,IdLoc: PropertyIvarLoc, Id: PropertyIvar, |
| 1303 | T: PropertyIvarType, /*TInfo=*/nullptr, |
| 1304 | ac: ObjCIvarDecl::Private, |
| 1305 | BW: (Expr *)nullptr, synthesized: true); |
| 1306 | if (SemaRef.RequireNonAbstractType(Loc: PropertyIvarLoc, T: PropertyIvarType, |
| 1307 | DiagID: diag::err_abstract_type_in_decl, |
| 1308 | Args: Sema::AbstractSynthesizedIvarType)) { |
| 1309 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1310 | // An abstract type is as bad as an incomplete type. |
| 1311 | CompleteTypeErr = true; |
| 1312 | } |
| 1313 | if (!CompleteTypeErr) { |
| 1314 | const RecordType *RecordTy = PropertyIvarType->getAs<RecordType>(); |
| 1315 | if (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()) { |
| 1316 | Diag(Loc: PropertyIvarLoc, DiagID: diag::err_synthesize_variable_sized_ivar) |
| 1317 | << PropertyIvarType; |
| 1318 | CompleteTypeErr = true; // suppress later diagnostics about the ivar |
| 1319 | } |
| 1320 | } |
| 1321 | if (CompleteTypeErr) |
| 1322 | Ivar->setInvalidDecl(); |
| 1323 | ClassImpDecl->addDecl(D: Ivar); |
| 1324 | IDecl->makeDeclVisibleInContext(D: Ivar); |
| 1325 | |
| 1326 | if (getLangOpts().ObjCRuntime.isFragile()) |
| 1327 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_missing_property_ivar_decl) |
| 1328 | << PropertyId; |
| 1329 | // Note! I deliberately want it to fall thru so, we have a |
| 1330 | // a property implementation and to avoid future warnings. |
| 1331 | } else if (getLangOpts().ObjCRuntime.isNonFragile() && |
| 1332 | !declaresSameEntity(D1: ClassDeclared, D2: IDecl)) { |
| 1333 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_ivar_in_superclass_use) |
| 1334 | << property->getDeclName() << Ivar->getDeclName() |
| 1335 | << ClassDeclared->getDeclName(); |
| 1336 | Diag(Loc: Ivar->getLocation(), DiagID: diag::note_previous_access_declaration) |
| 1337 | << Ivar << Ivar->getName(); |
| 1338 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 1339 | } |
| 1340 | property->setPropertyIvarDecl(Ivar); |
| 1341 | |
| 1342 | QualType IvarType = Context.getCanonicalType(T: Ivar->getType()); |
| 1343 | |
| 1344 | // Check that type of property and its ivar are type compatible. |
| 1345 | if (!Context.hasSameType(T1: PropertyIvarType, T2: IvarType)) { |
| 1346 | if (isa<ObjCObjectPointerType>(Val: PropertyIvarType) |
| 1347 | && isa<ObjCObjectPointerType>(Val: IvarType)) |
| 1348 | compat = Context.canAssignObjCInterfaces( |
| 1349 | LHSOPT: PropertyIvarType->castAs<ObjCObjectPointerType>(), |
| 1350 | RHSOPT: IvarType->castAs<ObjCObjectPointerType>()); |
| 1351 | else { |
| 1352 | compat = SemaRef.IsAssignConvertCompatible( |
| 1353 | ConvTy: SemaRef.CheckAssignmentConstraints(Loc: PropertyIvarLoc, |
| 1354 | LHSType: PropertyIvarType, RHSType: IvarType)); |
| 1355 | } |
| 1356 | if (!compat) { |
| 1357 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_property_ivar_type) |
| 1358 | << property->getDeclName() << PropType |
| 1359 | << Ivar->getDeclName() << IvarType; |
| 1360 | Diag(Loc: Ivar->getLocation(), DiagID: diag::note_ivar_decl); |
| 1361 | // Note! I deliberately want it to fall thru so, we have a |
| 1362 | // a property implementation and to avoid future warnings. |
| 1363 | } |
| 1364 | else { |
| 1365 | // FIXME! Rules for properties are somewhat different that those |
| 1366 | // for assignments. Use a new routine to consolidate all cases; |
| 1367 | // specifically for property redeclarations as well as for ivars. |
| 1368 | QualType lhsType =Context.getCanonicalType(T: PropertyIvarType).getUnqualifiedType(); |
| 1369 | QualType rhsType =Context.getCanonicalType(T: IvarType).getUnqualifiedType(); |
| 1370 | if (lhsType != rhsType && |
| 1371 | lhsType->isArithmeticType()) { |
| 1372 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_property_ivar_type) |
| 1373 | << property->getDeclName() << PropType |
| 1374 | << Ivar->getDeclName() << IvarType; |
| 1375 | Diag(Loc: Ivar->getLocation(), DiagID: diag::note_ivar_decl); |
| 1376 | // Fall thru - see previous comment |
| 1377 | } |
| 1378 | } |
| 1379 | // __weak is explicit. So it works on Canonical type. |
| 1380 | if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
| 1381 | getLangOpts().getGC() != LangOptions::NonGC)) { |
| 1382 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_weak_property) |
| 1383 | << property->getDeclName() << Ivar->getDeclName(); |
| 1384 | Diag(Loc: Ivar->getLocation(), DiagID: diag::note_ivar_decl); |
| 1385 | // Fall thru - see previous comment |
| 1386 | } |
| 1387 | // Fall thru - see previous comment |
| 1388 | if ((property->getType()->isObjCObjectPointerType() || |
| 1389 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
| 1390 | getLangOpts().getGC() != LangOptions::NonGC) { |
| 1391 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_strong_property) |
| 1392 | << property->getDeclName() << Ivar->getDeclName(); |
| 1393 | // Fall thru - see previous comment |
| 1394 | } |
| 1395 | } |
| 1396 | if (getLangOpts().ObjCAutoRefCount || isARCWeak || |
| 1397 | Ivar->getType().getObjCLifetime()) |
| 1398 | checkARCPropertyImpl(S&: SemaRef, propertyImplLoc: PropertyLoc, property, ivar: Ivar); |
| 1399 | } else if (PropertyIvar) |
| 1400 | // @dynamic |
| 1401 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_dynamic_property_ivar_decl); |
| 1402 | |
| 1403 | assert (property && "ActOnPropertyImplDecl - property declaration missing" ); |
| 1404 | ObjCPropertyImplDecl *PIDecl = ObjCPropertyImplDecl::Create( |
| 1405 | C&: Context, DC: SemaRef.CurContext, atLoc: AtLoc, L: PropertyLoc, property, |
| 1406 | PK: (Synthesize ? ObjCPropertyImplDecl::Synthesize |
| 1407 | : ObjCPropertyImplDecl::Dynamic), |
| 1408 | ivarDecl: Ivar, ivarLoc: PropertyIvarLoc); |
| 1409 | |
| 1410 | if (CompleteTypeErr || !compat) |
| 1411 | PIDecl->setInvalidDecl(); |
| 1412 | |
| 1413 | if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) { |
| 1414 | getterMethod->createImplicitParams(Context, ID: IDecl); |
| 1415 | |
| 1416 | // Redeclare the getter within the implementation as DeclContext. |
| 1417 | if (Synthesize) { |
| 1418 | // If the method hasn't been overridden, create a synthesized implementation. |
| 1419 | ObjCMethodDecl *OMD = ClassImpDecl->getMethod( |
| 1420 | Sel: getterMethod->getSelector(), isInstance: getterMethod->isInstanceMethod()); |
| 1421 | if (!OMD) |
| 1422 | OMD = RedeclarePropertyAccessor(Context, Impl: IC, AccessorDecl: getterMethod, AtLoc, |
| 1423 | PropertyLoc); |
| 1424 | PIDecl->setGetterMethodDecl(OMD); |
| 1425 | } |
| 1426 | |
| 1427 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 1428 | Ivar->getType()->isRecordType()) { |
| 1429 | // For Objective-C++, need to synthesize the AST for the IVAR object to be |
| 1430 | // returned by the getter as it must conform to C++'s copy-return rules. |
| 1431 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 1432 | Sema::SynthesizedFunctionScope Scope(SemaRef, getterMethod); |
| 1433 | ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl(); |
| 1434 | DeclRefExpr *SelfExpr = new (Context) |
| 1435 | DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue, |
| 1436 | PropertyDiagLoc); |
| 1437 | SemaRef.MarkDeclRefReferenced(E: SelfExpr); |
| 1438 | Expr *LoadSelfExpr = ImplicitCastExpr::Create( |
| 1439 | Context, T: SelfDecl->getType(), Kind: CK_LValueToRValue, Operand: SelfExpr, BasePath: nullptr, |
| 1440 | Cat: VK_PRValue, FPO: FPOptionsOverride()); |
| 1441 | Expr *IvarRefExpr = |
| 1442 | new (Context) ObjCIvarRefExpr(Ivar, |
| 1443 | Ivar->getUsageType(objectType: SelfDecl->getType()), |
| 1444 | PropertyDiagLoc, |
| 1445 | Ivar->getLocation(), |
| 1446 | LoadSelfExpr, true, true); |
| 1447 | ExprResult Res = SemaRef.PerformCopyInitialization( |
| 1448 | Entity: InitializedEntity::InitializeResult(ReturnLoc: PropertyDiagLoc, |
| 1449 | Type: getterMethod->getReturnType()), |
| 1450 | EqualLoc: PropertyDiagLoc, Init: IvarRefExpr); |
| 1451 | if (!Res.isInvalid()) { |
| 1452 | Expr *ResExpr = Res.getAs<Expr>(); |
| 1453 | if (ResExpr) |
| 1454 | ResExpr = SemaRef.MaybeCreateExprWithCleanups(SubExpr: ResExpr); |
| 1455 | PIDecl->setGetterCXXConstructor(ResExpr); |
| 1456 | } |
| 1457 | } |
| 1458 | if (property->hasAttr<NSReturnsNotRetainedAttr>() && |
| 1459 | !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1460 | Diag(Loc: getterMethod->getLocation(), |
| 1461 | DiagID: diag::warn_property_getter_owning_mismatch); |
| 1462 | Diag(Loc: property->getLocation(), DiagID: diag::note_property_declare); |
| 1463 | } |
| 1464 | if (getLangOpts().ObjCAutoRefCount && Synthesize) |
| 1465 | switch (getterMethod->getMethodFamily()) { |
| 1466 | case OMF_retain: |
| 1467 | case OMF_retainCount: |
| 1468 | case OMF_release: |
| 1469 | case OMF_autorelease: |
| 1470 | Diag(Loc: getterMethod->getLocation(), DiagID: diag::err_arc_illegal_method_def) |
| 1471 | << 1 << getterMethod->getSelector(); |
| 1472 | break; |
| 1473 | default: |
| 1474 | break; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) { |
| 1479 | setterMethod->createImplicitParams(Context, ID: IDecl); |
| 1480 | |
| 1481 | // Redeclare the setter within the implementation as DeclContext. |
| 1482 | if (Synthesize) { |
| 1483 | ObjCMethodDecl *OMD = ClassImpDecl->getMethod( |
| 1484 | Sel: setterMethod->getSelector(), isInstance: setterMethod->isInstanceMethod()); |
| 1485 | if (!OMD) |
| 1486 | OMD = RedeclarePropertyAccessor(Context, Impl: IC, AccessorDecl: setterMethod, |
| 1487 | AtLoc, PropertyLoc); |
| 1488 | PIDecl->setSetterMethodDecl(OMD); |
| 1489 | } |
| 1490 | |
| 1491 | if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr && |
| 1492 | Ivar->getType()->isRecordType()) { |
| 1493 | // FIXME. Eventually we want to do this for Objective-C as well. |
| 1494 | Sema::SynthesizedFunctionScope Scope(SemaRef, setterMethod); |
| 1495 | ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl(); |
| 1496 | DeclRefExpr *SelfExpr = new (Context) |
| 1497 | DeclRefExpr(Context, SelfDecl, false, SelfDecl->getType(), VK_LValue, |
| 1498 | PropertyDiagLoc); |
| 1499 | SemaRef.MarkDeclRefReferenced(E: SelfExpr); |
| 1500 | Expr *LoadSelfExpr = ImplicitCastExpr::Create( |
| 1501 | Context, T: SelfDecl->getType(), Kind: CK_LValueToRValue, Operand: SelfExpr, BasePath: nullptr, |
| 1502 | Cat: VK_PRValue, FPO: FPOptionsOverride()); |
| 1503 | Expr *lhs = |
| 1504 | new (Context) ObjCIvarRefExpr(Ivar, |
| 1505 | Ivar->getUsageType(objectType: SelfDecl->getType()), |
| 1506 | PropertyDiagLoc, |
| 1507 | Ivar->getLocation(), |
| 1508 | LoadSelfExpr, true, true); |
| 1509 | ObjCMethodDecl::param_iterator P = setterMethod->param_begin(); |
| 1510 | ParmVarDecl *Param = (*P); |
| 1511 | QualType T = Param->getType().getNonReferenceType(); |
| 1512 | DeclRefExpr *rhs = new (Context) |
| 1513 | DeclRefExpr(Context, Param, false, T, VK_LValue, PropertyDiagLoc); |
| 1514 | SemaRef.MarkDeclRefReferenced(E: rhs); |
| 1515 | ExprResult Res = |
| 1516 | SemaRef.BuildBinOp(S, OpLoc: PropertyDiagLoc, Opc: BO_Assign, LHSExpr: lhs, RHSExpr: rhs); |
| 1517 | if (property->getPropertyAttributes() & |
| 1518 | ObjCPropertyAttribute::kind_atomic) { |
| 1519 | Expr *callExpr = Res.getAs<Expr>(); |
| 1520 | if (const CXXOperatorCallExpr *CXXCE = |
| 1521 | dyn_cast_or_null<CXXOperatorCallExpr>(Val: callExpr)) |
| 1522 | if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee()) |
| 1523 | if (!FuncDecl->isTrivial()) |
| 1524 | if (property->getType()->isReferenceType()) { |
| 1525 | Diag(Loc: PropertyDiagLoc, |
| 1526 | DiagID: diag::err_atomic_property_nontrivial_assign_op) |
| 1527 | << property->getType(); |
| 1528 | Diag(Loc: FuncDecl->getBeginLoc(), DiagID: diag::note_callee_decl) |
| 1529 | << FuncDecl; |
| 1530 | } |
| 1531 | } |
| 1532 | PIDecl->setSetterCXXAssignment(Res.getAs<Expr>()); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | if (IC) { |
| 1537 | if (Synthesize) |
| 1538 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1539 | IC->FindPropertyImplIvarDecl(ivarId: PropertyIvar)) { |
| 1540 | Diag(Loc: PropertyLoc, DiagID: diag::err_duplicate_ivar_use) |
| 1541 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1542 | << PropertyIvar; |
| 1543 | Diag(Loc: PPIDecl->getLocation(), DiagID: diag::note_previous_use); |
| 1544 | } |
| 1545 | |
| 1546 | if (ObjCPropertyImplDecl *PPIDecl |
| 1547 | = IC->FindPropertyImplDecl(propertyId: PropertyId, queryKind: QueryKind)) { |
| 1548 | Diag(Loc: PropertyLoc, DiagID: diag::err_property_implemented) << PropertyId; |
| 1549 | Diag(Loc: PPIDecl->getLocation(), DiagID: diag::note_previous_declaration); |
| 1550 | return nullptr; |
| 1551 | } |
| 1552 | IC->addPropertyImplementation(property: PIDecl); |
| 1553 | if (getLangOpts().ObjCDefaultSynthProperties && |
| 1554 | getLangOpts().ObjCRuntime.isNonFragile() && |
| 1555 | !IDecl->isObjCRequiresPropertyDefs()) { |
| 1556 | // Diagnose if an ivar was lazily synthesdized due to a previous |
| 1557 | // use and if 1) property is @dynamic or 2) property is synthesized |
| 1558 | // but it requires an ivar of different name. |
| 1559 | ObjCInterfaceDecl *ClassDeclared=nullptr; |
| 1560 | ObjCIvarDecl *Ivar = nullptr; |
| 1561 | if (!Synthesize) |
| 1562 | Ivar = IDecl->lookupInstanceVariable(IVarName: PropertyId, ClassDeclared); |
| 1563 | else { |
| 1564 | if (PropertyIvar && PropertyIvar != PropertyId) |
| 1565 | Ivar = IDecl->lookupInstanceVariable(IVarName: PropertyId, ClassDeclared); |
| 1566 | } |
| 1567 | // Issue diagnostics only if Ivar belongs to current class. |
| 1568 | if (Ivar && Ivar->getSynthesize() && |
| 1569 | declaresSameEntity(D1: IC->getClassInterface(), D2: ClassDeclared)) { |
| 1570 | Diag(Loc: Ivar->getLocation(), DiagID: diag::err_undeclared_var_use) |
| 1571 | << PropertyId; |
| 1572 | Ivar->setInvalidDecl(); |
| 1573 | } |
| 1574 | } |
| 1575 | } else { |
| 1576 | if (Synthesize) |
| 1577 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1578 | CatImplClass->FindPropertyImplIvarDecl(ivarId: PropertyIvar)) { |
| 1579 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_duplicate_ivar_use) |
| 1580 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1581 | << PropertyIvar; |
| 1582 | Diag(Loc: PPIDecl->getLocation(), DiagID: diag::note_previous_use); |
| 1583 | } |
| 1584 | |
| 1585 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1586 | CatImplClass->FindPropertyImplDecl(propertyId: PropertyId, queryKind: QueryKind)) { |
| 1587 | Diag(Loc: PropertyDiagLoc, DiagID: diag::err_property_implemented) << PropertyId; |
| 1588 | Diag(Loc: PPIDecl->getLocation(), DiagID: diag::note_previous_declaration); |
| 1589 | return nullptr; |
| 1590 | } |
| 1591 | CatImplClass->addPropertyImplementation(property: PIDecl); |
| 1592 | } |
| 1593 | |
| 1594 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic && |
| 1595 | PIDecl->getPropertyDecl() && |
| 1596 | PIDecl->getPropertyDecl()->isDirectProperty()) { |
| 1597 | Diag(Loc: PropertyLoc, DiagID: diag::err_objc_direct_dynamic_property); |
| 1598 | Diag(Loc: PIDecl->getPropertyDecl()->getLocation(), |
| 1599 | DiagID: diag::note_previous_declaration); |
| 1600 | return nullptr; |
| 1601 | } |
| 1602 | |
| 1603 | return PIDecl; |
| 1604 | } |
| 1605 | |
| 1606 | //===----------------------------------------------------------------------===// |
| 1607 | // Helper methods. |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | |
| 1610 | /// DiagnosePropertyMismatch - Compares two properties for their |
| 1611 | /// attributes and types and warns on a variety of inconsistencies. |
| 1612 | /// |
| 1613 | void SemaObjC::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 1614 | ObjCPropertyDecl *SuperProperty, |
| 1615 | const IdentifierInfo *inheritedName, |
| 1616 | bool OverridingProtocolProperty) { |
| 1617 | ASTContext &Context = getASTContext(); |
| 1618 | ObjCPropertyAttribute::Kind CAttr = Property->getPropertyAttributes(); |
| 1619 | ObjCPropertyAttribute::Kind SAttr = SuperProperty->getPropertyAttributes(); |
| 1620 | |
| 1621 | // We allow readonly properties without an explicit ownership |
| 1622 | // (assign/unsafe_unretained/weak/retain/strong/copy) in super class |
| 1623 | // to be overridden by a property with any explicit ownership in the subclass. |
| 1624 | if (!OverridingProtocolProperty && |
| 1625 | !getOwnershipRule(attr: SAttr) && getOwnershipRule(attr: CAttr)) |
| 1626 | ; |
| 1627 | else { |
| 1628 | if ((CAttr & ObjCPropertyAttribute::kind_readonly) && |
| 1629 | (SAttr & ObjCPropertyAttribute::kind_readwrite)) |
| 1630 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_readonly_property) |
| 1631 | << Property->getDeclName() << inheritedName; |
| 1632 | if ((CAttr & ObjCPropertyAttribute::kind_copy) != |
| 1633 | (SAttr & ObjCPropertyAttribute::kind_copy)) |
| 1634 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_property_attribute) |
| 1635 | << Property->getDeclName() << "copy" << inheritedName; |
| 1636 | else if (!(SAttr & ObjCPropertyAttribute::kind_readonly)) { |
| 1637 | unsigned CAttrRetain = (CAttr & (ObjCPropertyAttribute::kind_retain | |
| 1638 | ObjCPropertyAttribute::kind_strong)); |
| 1639 | unsigned SAttrRetain = (SAttr & (ObjCPropertyAttribute::kind_retain | |
| 1640 | ObjCPropertyAttribute::kind_strong)); |
| 1641 | bool CStrong = (CAttrRetain != 0); |
| 1642 | bool SStrong = (SAttrRetain != 0); |
| 1643 | if (CStrong != SStrong) |
| 1644 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_property_attribute) |
| 1645 | << Property->getDeclName() << "retain (or strong)" << inheritedName; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // Check for nonatomic; note that nonatomic is effectively |
| 1650 | // meaningless for readonly properties, so don't diagnose if the |
| 1651 | // atomic property is 'readonly'. |
| 1652 | checkAtomicPropertyMismatch(S&: SemaRef, OldProperty: SuperProperty, NewProperty: Property, PropagateAtomicity: false); |
| 1653 | // Readonly properties from protocols can be implemented as "readwrite" |
| 1654 | // with a custom setter name. |
| 1655 | if (Property->getSetterName() != SuperProperty->getSetterName() && |
| 1656 | !(SuperProperty->isReadOnly() && |
| 1657 | isa<ObjCProtocolDecl>(Val: SuperProperty->getDeclContext()))) { |
| 1658 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_property_attribute) |
| 1659 | << Property->getDeclName() << "setter" << inheritedName; |
| 1660 | Diag(Loc: SuperProperty->getLocation(), DiagID: diag::note_property_declare); |
| 1661 | } |
| 1662 | if (Property->getGetterName() != SuperProperty->getGetterName()) { |
| 1663 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_property_attribute) |
| 1664 | << Property->getDeclName() << "getter" << inheritedName; |
| 1665 | Diag(Loc: SuperProperty->getLocation(), DiagID: diag::note_property_declare); |
| 1666 | } |
| 1667 | |
| 1668 | QualType LHSType = |
| 1669 | Context.getCanonicalType(T: SuperProperty->getType()); |
| 1670 | QualType RHSType = |
| 1671 | Context.getCanonicalType(T: Property->getType()); |
| 1672 | |
| 1673 | if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) { |
| 1674 | // Do cases not handled in above. |
| 1675 | // FIXME. For future support of covariant property types, revisit this. |
| 1676 | bool IncompatibleObjC = false; |
| 1677 | QualType ConvertedType; |
| 1678 | if (!SemaRef.isObjCPointerConversion(FromType: RHSType, ToType: LHSType, ConvertedType, |
| 1679 | IncompatibleObjC) || |
| 1680 | IncompatibleObjC) { |
| 1681 | Diag(Loc: Property->getLocation(), DiagID: diag::warn_property_types_are_incompatible) |
| 1682 | << Property->getType() << SuperProperty->getType() << inheritedName; |
| 1683 | Diag(Loc: SuperProperty->getLocation(), DiagID: diag::note_property_declare); |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | bool SemaObjC::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 1689 | ObjCMethodDecl *GetterMethod, |
| 1690 | SourceLocation Loc) { |
| 1691 | ASTContext &Context = getASTContext(); |
| 1692 | if (!GetterMethod) |
| 1693 | return false; |
| 1694 | QualType GetterType = GetterMethod->getReturnType().getNonReferenceType(); |
| 1695 | QualType PropertyRValueType = |
| 1696 | property->getType().getNonReferenceType().getAtomicUnqualifiedType(); |
| 1697 | bool compat = Context.hasSameType(T1: PropertyRValueType, T2: GetterType); |
| 1698 | if (!compat) { |
| 1699 | const ObjCObjectPointerType *propertyObjCPtr = nullptr; |
| 1700 | const ObjCObjectPointerType *getterObjCPtr = nullptr; |
| 1701 | if ((propertyObjCPtr = |
| 1702 | PropertyRValueType->getAs<ObjCObjectPointerType>()) && |
| 1703 | (getterObjCPtr = GetterType->getAs<ObjCObjectPointerType>())) |
| 1704 | compat = Context.canAssignObjCInterfaces(LHSOPT: getterObjCPtr, RHSOPT: propertyObjCPtr); |
| 1705 | else if (!SemaRef.IsAssignConvertCompatible( |
| 1706 | ConvTy: SemaRef.CheckAssignmentConstraints(Loc, LHSType: GetterType, |
| 1707 | RHSType: PropertyRValueType))) { |
| 1708 | Diag(Loc, DiagID: diag::err_property_accessor_type) |
| 1709 | << property->getDeclName() << PropertyRValueType |
| 1710 | << GetterMethod->getSelector() << GetterType; |
| 1711 | Diag(Loc: GetterMethod->getLocation(), DiagID: diag::note_declared_at); |
| 1712 | return true; |
| 1713 | } else { |
| 1714 | compat = true; |
| 1715 | QualType lhsType = Context.getCanonicalType(T: PropertyRValueType); |
| 1716 | QualType rhsType =Context.getCanonicalType(T: GetterType).getUnqualifiedType(); |
| 1717 | if (lhsType != rhsType && lhsType->isArithmeticType()) |
| 1718 | compat = false; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | if (!compat) { |
| 1723 | Diag(Loc, DiagID: diag::warn_accessor_property_type_mismatch) |
| 1724 | << property->getDeclName() |
| 1725 | << GetterMethod->getSelector(); |
| 1726 | Diag(Loc: GetterMethod->getLocation(), DiagID: diag::note_declared_at); |
| 1727 | return true; |
| 1728 | } |
| 1729 | |
| 1730 | return false; |
| 1731 | } |
| 1732 | |
| 1733 | /// CollectImmediateProperties - This routine collects all properties in |
| 1734 | /// the class and its conforming protocols; but not those in its super class. |
| 1735 | static void |
| 1736 | CollectImmediateProperties(ObjCContainerDecl *CDecl, |
| 1737 | ObjCContainerDecl::PropertyMap &PropMap, |
| 1738 | ObjCContainerDecl::PropertyMap &SuperPropMap, |
| 1739 | bool CollectClassPropsOnly = false, |
| 1740 | bool IncludeProtocols = true) { |
| 1741 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(Val: CDecl)) { |
| 1742 | for (auto *Prop : IDecl->properties()) { |
| 1743 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1744 | continue; |
| 1745 | PropMap[std::make_pair(x: Prop->getIdentifier(), y: Prop->isClassProperty())] = |
| 1746 | Prop; |
| 1747 | } |
| 1748 | |
| 1749 | // Collect the properties from visible extensions. |
| 1750 | for (auto *Ext : IDecl->visible_extensions()) |
| 1751 | CollectImmediateProperties(CDecl: Ext, PropMap, SuperPropMap, |
| 1752 | CollectClassPropsOnly, IncludeProtocols); |
| 1753 | |
| 1754 | if (IncludeProtocols) { |
| 1755 | // Scan through class's protocols. |
| 1756 | for (auto *PI : IDecl->all_referenced_protocols()) |
| 1757 | CollectImmediateProperties(CDecl: PI, PropMap, SuperPropMap, |
| 1758 | CollectClassPropsOnly); |
| 1759 | } |
| 1760 | } |
| 1761 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(Val: CDecl)) { |
| 1762 | for (auto *Prop : CATDecl->properties()) { |
| 1763 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1764 | continue; |
| 1765 | PropMap[std::make_pair(x: Prop->getIdentifier(), y: Prop->isClassProperty())] = |
| 1766 | Prop; |
| 1767 | } |
| 1768 | if (IncludeProtocols) { |
| 1769 | // Scan through class's protocols. |
| 1770 | for (auto *PI : CATDecl->protocols()) |
| 1771 | CollectImmediateProperties(CDecl: PI, PropMap, SuperPropMap, |
| 1772 | CollectClassPropsOnly); |
| 1773 | } |
| 1774 | } |
| 1775 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(Val: CDecl)) { |
| 1776 | for (auto *Prop : PDecl->properties()) { |
| 1777 | if (CollectClassPropsOnly && !Prop->isClassProperty()) |
| 1778 | continue; |
| 1779 | ObjCPropertyDecl *PropertyFromSuper = |
| 1780 | SuperPropMap[std::make_pair(x: Prop->getIdentifier(), |
| 1781 | y: Prop->isClassProperty())]; |
| 1782 | // Exclude property for protocols which conform to class's super-class, |
| 1783 | // as super-class has to implement the property. |
| 1784 | if (!PropertyFromSuper || |
| 1785 | PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) { |
| 1786 | ObjCPropertyDecl *&PropEntry = |
| 1787 | PropMap[std::make_pair(x: Prop->getIdentifier(), |
| 1788 | y: Prop->isClassProperty())]; |
| 1789 | if (!PropEntry) |
| 1790 | PropEntry = Prop; |
| 1791 | } |
| 1792 | } |
| 1793 | // Scan through protocol's protocols. |
| 1794 | for (auto *PI : PDecl->protocols()) |
| 1795 | CollectImmediateProperties(CDecl: PI, PropMap, SuperPropMap, |
| 1796 | CollectClassPropsOnly); |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /// CollectSuperClassPropertyImplementations - This routine collects list of |
| 1801 | /// properties to be implemented in super class(s) and also coming from their |
| 1802 | /// conforming protocols. |
| 1803 | static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl, |
| 1804 | ObjCInterfaceDecl::PropertyMap &PropMap) { |
| 1805 | if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) { |
| 1806 | while (SDecl) { |
| 1807 | SDecl->collectPropertiesToImplement(PM&: PropMap); |
| 1808 | SDecl = SDecl->getSuperClass(); |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is |
| 1814 | /// an ivar synthesized for 'Method' and 'Method' is a property accessor |
| 1815 | /// declared in class 'IFace'. |
| 1816 | bool SemaObjC::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace, |
| 1817 | ObjCMethodDecl *Method, |
| 1818 | ObjCIvarDecl *IV) { |
| 1819 | if (!IV->getSynthesize()) |
| 1820 | return false; |
| 1821 | ObjCMethodDecl *IMD = IFace->lookupMethod(Sel: Method->getSelector(), |
| 1822 | isInstance: Method->isInstanceMethod()); |
| 1823 | if (!IMD || !IMD->isPropertyAccessor()) |
| 1824 | return false; |
| 1825 | |
| 1826 | // look up a property declaration whose one of its accessors is implemented |
| 1827 | // by this method. |
| 1828 | for (const auto *Property : IFace->instance_properties()) { |
| 1829 | if ((Property->getGetterName() == IMD->getSelector() || |
| 1830 | Property->getSetterName() == IMD->getSelector()) && |
| 1831 | (Property->getPropertyIvarDecl() == IV)) |
| 1832 | return true; |
| 1833 | } |
| 1834 | // Also look up property declaration in class extension whose one of its |
| 1835 | // accessors is implemented by this method. |
| 1836 | for (const auto *Ext : IFace->known_extensions()) |
| 1837 | for (const auto *Property : Ext->instance_properties()) |
| 1838 | if ((Property->getGetterName() == IMD->getSelector() || |
| 1839 | Property->getSetterName() == IMD->getSelector()) && |
| 1840 | (Property->getPropertyIvarDecl() == IV)) |
| 1841 | return true; |
| 1842 | return false; |
| 1843 | } |
| 1844 | |
| 1845 | static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl, |
| 1846 | ObjCPropertyDecl *Prop) { |
| 1847 | bool SuperClassImplementsGetter = false; |
| 1848 | bool SuperClassImplementsSetter = false; |
| 1849 | if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) |
| 1850 | SuperClassImplementsSetter = true; |
| 1851 | |
| 1852 | while (IDecl->getSuperClass()) { |
| 1853 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 1854 | if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Sel: Prop->getGetterName())) |
| 1855 | SuperClassImplementsGetter = true; |
| 1856 | |
| 1857 | if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Sel: Prop->getSetterName())) |
| 1858 | SuperClassImplementsSetter = true; |
| 1859 | if (SuperClassImplementsGetter && SuperClassImplementsSetter) |
| 1860 | return true; |
| 1861 | IDecl = IDecl->getSuperClass(); |
| 1862 | } |
| 1863 | return false; |
| 1864 | } |
| 1865 | |
| 1866 | /// Default synthesizes all properties which must be synthesized |
| 1867 | /// in class's \@implementation. |
| 1868 | void SemaObjC::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl *IMPDecl, |
| 1869 | ObjCInterfaceDecl *IDecl, |
| 1870 | SourceLocation AtEnd) { |
| 1871 | ASTContext &Context = getASTContext(); |
| 1872 | ObjCInterfaceDecl::PropertyMap PropMap; |
| 1873 | IDecl->collectPropertiesToImplement(PM&: PropMap); |
| 1874 | if (PropMap.empty()) |
| 1875 | return; |
| 1876 | ObjCInterfaceDecl::PropertyMap SuperPropMap; |
| 1877 | CollectSuperClassPropertyImplementations(CDecl: IDecl, PropMap&: SuperPropMap); |
| 1878 | |
| 1879 | for (const auto &PropEntry : PropMap) { |
| 1880 | ObjCPropertyDecl *Prop = PropEntry.second; |
| 1881 | // Is there a matching property synthesize/dynamic? |
| 1882 | if (Prop->isInvalidDecl() || |
| 1883 | Prop->isClassProperty() || |
| 1884 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 1885 | continue; |
| 1886 | // Property may have been synthesized by user. |
| 1887 | if (IMPDecl->FindPropertyImplDecl( |
| 1888 | propertyId: Prop->getIdentifier(), queryKind: Prop->getQueryKind())) |
| 1889 | continue; |
| 1890 | ObjCMethodDecl *ImpMethod = IMPDecl->getInstanceMethod(Sel: Prop->getGetterName()); |
| 1891 | if (ImpMethod && !ImpMethod->getBody()) { |
| 1892 | if (Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_readonly) |
| 1893 | continue; |
| 1894 | ImpMethod = IMPDecl->getInstanceMethod(Sel: Prop->getSetterName()); |
| 1895 | if (ImpMethod && !ImpMethod->getBody()) |
| 1896 | continue; |
| 1897 | } |
| 1898 | if (ObjCPropertyImplDecl *PID = |
| 1899 | IMPDecl->FindPropertyImplIvarDecl(ivarId: Prop->getIdentifier())) { |
| 1900 | Diag(Loc: Prop->getLocation(), DiagID: diag::warn_no_autosynthesis_shared_ivar_property) |
| 1901 | << Prop->getIdentifier(); |
| 1902 | if (PID->getLocation().isValid()) |
| 1903 | Diag(Loc: PID->getLocation(), DiagID: diag::note_property_synthesize); |
| 1904 | continue; |
| 1905 | } |
| 1906 | ObjCPropertyDecl *PropInSuperClass = |
| 1907 | SuperPropMap[std::make_pair(x: Prop->getIdentifier(), |
| 1908 | y: Prop->isClassProperty())]; |
| 1909 | if (ObjCProtocolDecl *Proto = |
| 1910 | dyn_cast<ObjCProtocolDecl>(Val: Prop->getDeclContext())) { |
| 1911 | // We won't auto-synthesize properties declared in protocols. |
| 1912 | // Suppress the warning if class's superclass implements property's |
| 1913 | // getter and implements property's setter (if readwrite property). |
| 1914 | // Or, if property is going to be implemented in its super class. |
| 1915 | if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) { |
| 1916 | Diag(Loc: IMPDecl->getLocation(), |
| 1917 | DiagID: diag::warn_auto_synthesizing_protocol_property) |
| 1918 | << Prop << Proto; |
| 1919 | Diag(Loc: Prop->getLocation(), DiagID: diag::note_property_declare); |
| 1920 | std::string FixIt = |
| 1921 | (Twine("@synthesize " ) + Prop->getName() + ";\n\n" ).str(); |
| 1922 | Diag(Loc: AtEnd, DiagID: diag::note_add_synthesize_directive) |
| 1923 | << FixItHint::CreateInsertion(InsertionLoc: AtEnd, Code: FixIt); |
| 1924 | } |
| 1925 | continue; |
| 1926 | } |
| 1927 | // If property to be implemented in the super class, ignore. |
| 1928 | if (PropInSuperClass) { |
| 1929 | if ((Prop->getPropertyAttributes() & |
| 1930 | ObjCPropertyAttribute::kind_readwrite) && |
| 1931 | (PropInSuperClass->getPropertyAttributes() & |
| 1932 | ObjCPropertyAttribute::kind_readonly) && |
| 1933 | !IMPDecl->getInstanceMethod(Sel: Prop->getSetterName()) && |
| 1934 | !IDecl->HasUserDeclaredSetterMethod(P: Prop)) { |
| 1935 | Diag(Loc: Prop->getLocation(), DiagID: diag::warn_no_autosynthesis_property) |
| 1936 | << Prop->getIdentifier(); |
| 1937 | Diag(Loc: PropInSuperClass->getLocation(), DiagID: diag::note_property_declare); |
| 1938 | } else { |
| 1939 | Diag(Loc: Prop->getLocation(), DiagID: diag::warn_autosynthesis_property_in_superclass) |
| 1940 | << Prop->getIdentifier(); |
| 1941 | Diag(Loc: PropInSuperClass->getLocation(), DiagID: diag::note_property_declare); |
| 1942 | Diag(Loc: IMPDecl->getLocation(), DiagID: diag::note_while_in_implementation); |
| 1943 | } |
| 1944 | continue; |
| 1945 | } |
| 1946 | // We use invalid SourceLocations for the synthesized ivars since they |
| 1947 | // aren't really synthesized at a particular location; they just exist. |
| 1948 | // Saying that they are located at the @implementation isn't really going |
| 1949 | // to help users. |
| 1950 | ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>( |
| 1951 | Val: ActOnPropertyImplDecl(S, AtLoc: SourceLocation(), PropertyLoc: SourceLocation(), |
| 1952 | Synthesize: true, |
| 1953 | /* property = */ PropertyId: Prop->getIdentifier(), |
| 1954 | /* ivar = */ PropertyIvar: Prop->getDefaultSynthIvarName(Ctx&: Context), |
| 1955 | PropertyIvarLoc: Prop->getLocation(), QueryKind: Prop->getQueryKind())); |
| 1956 | if (PIDecl && !Prop->isUnavailable()) { |
| 1957 | Diag(Loc: Prop->getLocation(), DiagID: diag::warn_missing_explicit_synthesis); |
| 1958 | Diag(Loc: IMPDecl->getLocation(), DiagID: diag::note_while_in_implementation); |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | void SemaObjC::DefaultSynthesizeProperties(Scope *S, Decl *D, |
| 1964 | SourceLocation AtEnd) { |
| 1965 | if (!getLangOpts().ObjCDefaultSynthProperties || |
| 1966 | getLangOpts().ObjCRuntime.isFragile()) |
| 1967 | return; |
| 1968 | ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(Val: D); |
| 1969 | if (!IC) |
| 1970 | return; |
| 1971 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
| 1972 | if (!IDecl->isObjCRequiresPropertyDefs()) |
| 1973 | DefaultSynthesizeProperties(S, IMPDecl: IC, IDecl, AtEnd); |
| 1974 | } |
| 1975 | |
| 1976 | static void DiagnoseUnimplementedAccessor( |
| 1977 | Sema &S, ObjCInterfaceDecl *PrimaryClass, Selector Method, |
| 1978 | ObjCImplDecl *IMPDecl, ObjCContainerDecl *CDecl, ObjCCategoryDecl *C, |
| 1979 | ObjCPropertyDecl *Prop, |
| 1980 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> &SMap) { |
| 1981 | // Check to see if we have a corresponding selector in SMap and with the |
| 1982 | // right method type. |
| 1983 | auto I = llvm::find_if(Range&: SMap, P: [&](const ObjCMethodDecl *x) { |
| 1984 | return x->getSelector() == Method && |
| 1985 | x->isClassMethod() == Prop->isClassProperty(); |
| 1986 | }); |
| 1987 | // When reporting on missing property setter/getter implementation in |
| 1988 | // categories, do not report when they are declared in primary class, |
| 1989 | // class's protocol, or one of it super classes. This is because, |
| 1990 | // the class is going to implement them. |
| 1991 | if (I == SMap.end() && |
| 1992 | (PrimaryClass == nullptr || |
| 1993 | !PrimaryClass->lookupPropertyAccessor(Sel: Method, Cat: C, |
| 1994 | IsClassProperty: Prop->isClassProperty()))) { |
| 1995 | unsigned diag = |
| 1996 | isa<ObjCCategoryDecl>(Val: CDecl) |
| 1997 | ? (Prop->isClassProperty() |
| 1998 | ? diag::warn_impl_required_in_category_for_class_property |
| 1999 | : diag::warn_setter_getter_impl_required_in_category) |
| 2000 | : (Prop->isClassProperty() |
| 2001 | ? diag::warn_impl_required_for_class_property |
| 2002 | : diag::warn_setter_getter_impl_required); |
| 2003 | S.Diag(Loc: IMPDecl->getLocation(), DiagID: diag) << Prop->getDeclName() << Method; |
| 2004 | S.Diag(Loc: Prop->getLocation(), DiagID: diag::note_property_declare); |
| 2005 | if (S.LangOpts.ObjCDefaultSynthProperties && |
| 2006 | S.LangOpts.ObjCRuntime.isNonFragile()) |
| 2007 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(Val: CDecl)) |
| 2008 | if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs()) |
| 2009 | S.Diag(Loc: RID->getLocation(), DiagID: diag::note_suppressed_class_declare); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | void SemaObjC::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl *IMPDecl, |
| 2014 | ObjCContainerDecl *CDecl, |
| 2015 | bool SynthesizeProperties) { |
| 2016 | ObjCContainerDecl::PropertyMap PropMap; |
| 2017 | ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(Val: CDecl); |
| 2018 | |
| 2019 | // Since we don't synthesize class properties, we should emit diagnose even |
| 2020 | // if SynthesizeProperties is true. |
| 2021 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 2022 | // Gather properties which need not be implemented in this class |
| 2023 | // or category. |
| 2024 | if (!IDecl) |
| 2025 | if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(Val: CDecl)) { |
| 2026 | // For categories, no need to implement properties declared in |
| 2027 | // its primary class (and its super classes) if property is |
| 2028 | // declared in one of those containers. |
| 2029 | if ((IDecl = C->getClassInterface())) { |
| 2030 | IDecl->collectPropertiesToImplement(PM&: NoNeedToImplPropMap); |
| 2031 | } |
| 2032 | } |
| 2033 | if (IDecl) |
| 2034 | CollectSuperClassPropertyImplementations(CDecl: IDecl, PropMap&: NoNeedToImplPropMap); |
| 2035 | |
| 2036 | // When SynthesizeProperties is true, we only check class properties. |
| 2037 | CollectImmediateProperties(CDecl, PropMap, SuperPropMap&: NoNeedToImplPropMap, |
| 2038 | CollectClassPropsOnly: SynthesizeProperties/*CollectClassPropsOnly*/); |
| 2039 | |
| 2040 | // Scan the @interface to see if any of the protocols it adopts |
| 2041 | // require an explicit implementation, via attribute |
| 2042 | // 'objc_protocol_requires_explicit_implementation'. |
| 2043 | if (IDecl) { |
| 2044 | std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap; |
| 2045 | |
| 2046 | for (auto *PDecl : IDecl->all_referenced_protocols()) { |
| 2047 | if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2048 | continue; |
| 2049 | // Lazily construct a set of all the properties in the @interface |
| 2050 | // of the class, without looking at the superclass. We cannot |
| 2051 | // use the call to CollectImmediateProperties() above as that |
| 2052 | // utilizes information from the super class's properties as well |
| 2053 | // as scans the adopted protocols. This work only triggers for protocols |
| 2054 | // with the attribute, which is very rare, and only occurs when |
| 2055 | // analyzing the @implementation. |
| 2056 | if (!LazyMap) { |
| 2057 | ObjCContainerDecl::PropertyMap NoNeedToImplPropMap; |
| 2058 | LazyMap.reset(p: new ObjCContainerDecl::PropertyMap()); |
| 2059 | CollectImmediateProperties(CDecl, PropMap&: *LazyMap, SuperPropMap&: NoNeedToImplPropMap, |
| 2060 | /* CollectClassPropsOnly */ false, |
| 2061 | /* IncludeProtocols */ false); |
| 2062 | } |
| 2063 | // Add the properties of 'PDecl' to the list of properties that |
| 2064 | // need to be implemented. |
| 2065 | for (auto *PropDecl : PDecl->properties()) { |
| 2066 | if ((*LazyMap)[std::make_pair(x: PropDecl->getIdentifier(), |
| 2067 | y: PropDecl->isClassProperty())]) |
| 2068 | continue; |
| 2069 | PropMap[std::make_pair(x: PropDecl->getIdentifier(), |
| 2070 | y: PropDecl->isClassProperty())] = PropDecl; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | if (PropMap.empty()) |
| 2076 | return; |
| 2077 | |
| 2078 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 2079 | for (const auto *I : IMPDecl->property_impls()) |
| 2080 | PropImplMap.insert(V: I->getPropertyDecl()); |
| 2081 | |
| 2082 | // Collect property accessors implemented in current implementation. |
| 2083 | llvm::SmallPtrSet<const ObjCMethodDecl *, 8> InsMap(llvm::from_range, |
| 2084 | IMPDecl->methods()); |
| 2085 | |
| 2086 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(Val: CDecl); |
| 2087 | ObjCInterfaceDecl *PrimaryClass = nullptr; |
| 2088 | if (C && !C->IsClassExtension()) |
| 2089 | if ((PrimaryClass = C->getClassInterface())) |
| 2090 | // Report unimplemented properties in the category as well. |
| 2091 | if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) { |
| 2092 | // When reporting on missing setter/getters, do not report when |
| 2093 | // setter/getter is implemented in category's primary class |
| 2094 | // implementation. |
| 2095 | InsMap.insert_range(R: IMP->methods()); |
| 2096 | } |
| 2097 | |
| 2098 | for (ObjCContainerDecl::PropertyMap::iterator |
| 2099 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 2100 | ObjCPropertyDecl *Prop = P->second; |
| 2101 | // Is there a matching property synthesize/dynamic? |
| 2102 | if (Prop->isInvalidDecl() || |
| 2103 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 2104 | PropImplMap.count(V: Prop) || |
| 2105 | Prop->getAvailability() == AR_Unavailable) |
| 2106 | continue; |
| 2107 | |
| 2108 | // Diagnose unimplemented getters and setters. |
| 2109 | DiagnoseUnimplementedAccessor(S&: SemaRef, PrimaryClass, Method: Prop->getGetterName(), |
| 2110 | IMPDecl, CDecl, C, Prop, SMap&: InsMap); |
| 2111 | if (!Prop->isReadOnly()) |
| 2112 | DiagnoseUnimplementedAccessor(S&: SemaRef, PrimaryClass, |
| 2113 | Method: Prop->getSetterName(), IMPDecl, CDecl, C, |
| 2114 | Prop, SMap&: InsMap); |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | void SemaObjC::diagnoseNullResettableSynthesizedSetters( |
| 2119 | const ObjCImplDecl *impDecl) { |
| 2120 | for (const auto *propertyImpl : impDecl->property_impls()) { |
| 2121 | const auto *property = propertyImpl->getPropertyDecl(); |
| 2122 | // Warn about null_resettable properties with synthesized setters, |
| 2123 | // because the setter won't properly handle nil. |
| 2124 | if (propertyImpl->getPropertyImplementation() == |
| 2125 | ObjCPropertyImplDecl::Synthesize && |
| 2126 | (property->getPropertyAttributes() & |
| 2127 | ObjCPropertyAttribute::kind_null_resettable) && |
| 2128 | property->getGetterMethodDecl() && property->getSetterMethodDecl()) { |
| 2129 | auto *getterImpl = propertyImpl->getGetterMethodDecl(); |
| 2130 | auto *setterImpl = propertyImpl->getSetterMethodDecl(); |
| 2131 | if ((!getterImpl || getterImpl->isSynthesizedAccessorStub()) && |
| 2132 | (!setterImpl || setterImpl->isSynthesizedAccessorStub())) { |
| 2133 | SourceLocation loc = propertyImpl->getLocation(); |
| 2134 | if (loc.isInvalid()) |
| 2135 | loc = impDecl->getBeginLoc(); |
| 2136 | |
| 2137 | Diag(Loc: loc, DiagID: diag::warn_null_resettable_setter) |
| 2138 | << setterImpl->getSelector() << property->getDeclName(); |
| 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | void SemaObjC::AtomicPropertySetterGetterRules(ObjCImplDecl *IMPDecl, |
| 2145 | ObjCInterfaceDecl *IDecl) { |
| 2146 | // Rules apply in non-GC mode only |
| 2147 | if (getLangOpts().getGC() != LangOptions::NonGC) |
| 2148 | return; |
| 2149 | ObjCContainerDecl::PropertyMap PM; |
| 2150 | for (auto *Prop : IDecl->properties()) |
| 2151 | PM[std::make_pair(x: Prop->getIdentifier(), y: Prop->isClassProperty())] = Prop; |
| 2152 | for (const auto *Ext : IDecl->known_extensions()) |
| 2153 | for (auto *Prop : Ext->properties()) |
| 2154 | PM[std::make_pair(x: Prop->getIdentifier(), y: Prop->isClassProperty())] = Prop; |
| 2155 | |
| 2156 | for (ObjCContainerDecl::PropertyMap::iterator I = PM.begin(), E = PM.end(); |
| 2157 | I != E; ++I) { |
| 2158 | const ObjCPropertyDecl *Property = I->second; |
| 2159 | ObjCMethodDecl *GetterMethod = nullptr; |
| 2160 | ObjCMethodDecl *SetterMethod = nullptr; |
| 2161 | |
| 2162 | unsigned Attributes = Property->getPropertyAttributes(); |
| 2163 | unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten(); |
| 2164 | |
| 2165 | if (!(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic) && |
| 2166 | !(AttributesAsWritten & ObjCPropertyAttribute::kind_nonatomic)) { |
| 2167 | GetterMethod = Property->isClassProperty() ? |
| 2168 | IMPDecl->getClassMethod(Sel: Property->getGetterName()) : |
| 2169 | IMPDecl->getInstanceMethod(Sel: Property->getGetterName()); |
| 2170 | SetterMethod = Property->isClassProperty() ? |
| 2171 | IMPDecl->getClassMethod(Sel: Property->getSetterName()) : |
| 2172 | IMPDecl->getInstanceMethod(Sel: Property->getSetterName()); |
| 2173 | if (GetterMethod && GetterMethod->isSynthesizedAccessorStub()) |
| 2174 | GetterMethod = nullptr; |
| 2175 | if (SetterMethod && SetterMethod->isSynthesizedAccessorStub()) |
| 2176 | SetterMethod = nullptr; |
| 2177 | if (GetterMethod) { |
| 2178 | Diag(Loc: GetterMethod->getLocation(), |
| 2179 | DiagID: diag::warn_default_atomic_custom_getter_setter) |
| 2180 | << Property->getIdentifier() << 0; |
| 2181 | Diag(Loc: Property->getLocation(), DiagID: diag::note_property_declare); |
| 2182 | } |
| 2183 | if (SetterMethod) { |
| 2184 | Diag(Loc: SetterMethod->getLocation(), |
| 2185 | DiagID: diag::warn_default_atomic_custom_getter_setter) |
| 2186 | << Property->getIdentifier() << 1; |
| 2187 | Diag(Loc: Property->getLocation(), DiagID: diag::note_property_declare); |
| 2188 | } |
| 2189 | } |
| 2190 | |
| 2191 | // We only care about readwrite atomic property. |
| 2192 | if ((Attributes & ObjCPropertyAttribute::kind_nonatomic) || |
| 2193 | !(Attributes & ObjCPropertyAttribute::kind_readwrite)) |
| 2194 | continue; |
| 2195 | if (const ObjCPropertyImplDecl *PIDecl = IMPDecl->FindPropertyImplDecl( |
| 2196 | propertyId: Property->getIdentifier(), queryKind: Property->getQueryKind())) { |
| 2197 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 2198 | continue; |
| 2199 | GetterMethod = PIDecl->getGetterMethodDecl(); |
| 2200 | SetterMethod = PIDecl->getSetterMethodDecl(); |
| 2201 | if (GetterMethod && GetterMethod->isSynthesizedAccessorStub()) |
| 2202 | GetterMethod = nullptr; |
| 2203 | if (SetterMethod && SetterMethod->isSynthesizedAccessorStub()) |
| 2204 | SetterMethod = nullptr; |
| 2205 | if ((bool)GetterMethod ^ (bool)SetterMethod) { |
| 2206 | SourceLocation MethodLoc = |
| 2207 | (GetterMethod ? GetterMethod->getLocation() |
| 2208 | : SetterMethod->getLocation()); |
| 2209 | Diag(Loc: MethodLoc, DiagID: diag::warn_atomic_property_rule) |
| 2210 | << Property->getIdentifier() << (GetterMethod != nullptr) |
| 2211 | << (SetterMethod != nullptr); |
| 2212 | // fixit stuff. |
| 2213 | if (Property->getLParenLoc().isValid() && |
| 2214 | !(AttributesAsWritten & ObjCPropertyAttribute::kind_atomic)) { |
| 2215 | // @property () ... case. |
| 2216 | SourceLocation AfterLParen = |
| 2217 | SemaRef.getLocForEndOfToken(Loc: Property->getLParenLoc()); |
| 2218 | StringRef NonatomicStr = AttributesAsWritten? "nonatomic, " |
| 2219 | : "nonatomic" ; |
| 2220 | Diag(Loc: Property->getLocation(), |
| 2221 | DiagID: diag::note_atomic_property_fixup_suggest) |
| 2222 | << FixItHint::CreateInsertion(InsertionLoc: AfterLParen, Code: NonatomicStr); |
| 2223 | } else if (Property->getLParenLoc().isInvalid()) { |
| 2224 | //@property id etc. |
| 2225 | SourceLocation startLoc = |
| 2226 | Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc(); |
| 2227 | Diag(Loc: Property->getLocation(), |
| 2228 | DiagID: diag::note_atomic_property_fixup_suggest) |
| 2229 | << FixItHint::CreateInsertion(InsertionLoc: startLoc, Code: "(nonatomic) " ); |
| 2230 | } else |
| 2231 | Diag(Loc: MethodLoc, DiagID: diag::note_atomic_property_fixup_suggest); |
| 2232 | Diag(Loc: Property->getLocation(), DiagID: diag::note_property_declare); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | |
| 2238 | void SemaObjC::DiagnoseOwningPropertyGetterSynthesis( |
| 2239 | const ObjCImplementationDecl *D) { |
| 2240 | if (getLangOpts().getGC() == LangOptions::GCOnly) |
| 2241 | return; |
| 2242 | |
| 2243 | for (const auto *PID : D->property_impls()) { |
| 2244 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2245 | if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() && |
| 2246 | !PD->isClassProperty()) { |
| 2247 | ObjCMethodDecl *IM = PID->getGetterMethodDecl(); |
| 2248 | if (IM && !IM->isSynthesizedAccessorStub()) |
| 2249 | continue; |
| 2250 | ObjCMethodDecl *method = PD->getGetterMethodDecl(); |
| 2251 | if (!method) |
| 2252 | continue; |
| 2253 | ObjCMethodFamily family = method->getMethodFamily(); |
| 2254 | if (family == OMF_alloc || family == OMF_copy || |
| 2255 | family == OMF_mutableCopy || family == OMF_new) { |
| 2256 | if (getLangOpts().ObjCAutoRefCount) |
| 2257 | Diag(Loc: PD->getLocation(), DiagID: diag::err_cocoa_naming_owned_rule); |
| 2258 | else |
| 2259 | Diag(Loc: PD->getLocation(), DiagID: diag::warn_cocoa_naming_owned_rule); |
| 2260 | |
| 2261 | // Look for a getter explicitly declared alongside the property. |
| 2262 | // If we find one, use its location for the note. |
| 2263 | SourceLocation noteLoc = PD->getLocation(); |
| 2264 | SourceLocation fixItLoc; |
| 2265 | for (auto *getterRedecl : method->redecls()) { |
| 2266 | if (getterRedecl->isImplicit()) |
| 2267 | continue; |
| 2268 | if (getterRedecl->getDeclContext() != PD->getDeclContext()) |
| 2269 | continue; |
| 2270 | noteLoc = getterRedecl->getLocation(); |
| 2271 | fixItLoc = getterRedecl->getEndLoc(); |
| 2272 | } |
| 2273 | |
| 2274 | Preprocessor &PP = SemaRef.getPreprocessor(); |
| 2275 | TokenValue tokens[] = { |
| 2276 | tok::kw___attribute, tok::l_paren, tok::l_paren, |
| 2277 | PP.getIdentifierInfo(Name: "objc_method_family" ), tok::l_paren, |
| 2278 | PP.getIdentifierInfo(Name: "none" ), tok::r_paren, |
| 2279 | tok::r_paren, tok::r_paren |
| 2280 | }; |
| 2281 | StringRef spelling = "__attribute__((objc_method_family(none)))" ; |
| 2282 | StringRef macroName = PP.getLastMacroWithSpelling(Loc: noteLoc, Tokens: tokens); |
| 2283 | if (!macroName.empty()) |
| 2284 | spelling = macroName; |
| 2285 | |
| 2286 | auto noteDiag = Diag(Loc: noteLoc, DiagID: diag::note_cocoa_naming_declare_family) |
| 2287 | << method->getDeclName() << spelling; |
| 2288 | if (fixItLoc.isValid()) { |
| 2289 | SmallString<64> fixItText(" " ); |
| 2290 | fixItText += spelling; |
| 2291 | noteDiag << FixItHint::CreateInsertion(InsertionLoc: fixItLoc, Code: fixItText); |
| 2292 | } |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | |
| 2298 | void SemaObjC::DiagnoseMissingDesignatedInitOverrides( |
| 2299 | const ObjCImplementationDecl *ImplD, const ObjCInterfaceDecl *IFD) { |
| 2300 | assert(IFD->hasDesignatedInitializers()); |
| 2301 | const ObjCInterfaceDecl *SuperD = IFD->getSuperClass(); |
| 2302 | if (!SuperD) |
| 2303 | return; |
| 2304 | |
| 2305 | SelectorSet InitSelSet; |
| 2306 | for (const auto *I : ImplD->instance_methods()) |
| 2307 | if (I->getMethodFamily() == OMF_init) |
| 2308 | InitSelSet.insert(Ptr: I->getSelector()); |
| 2309 | |
| 2310 | SmallVector<const ObjCMethodDecl *, 8> DesignatedInits; |
| 2311 | SuperD->getDesignatedInitializers(Methods&: DesignatedInits); |
| 2312 | for (SmallVector<const ObjCMethodDecl *, 8>::iterator |
| 2313 | I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) { |
| 2314 | const ObjCMethodDecl *MD = *I; |
| 2315 | if (!InitSelSet.count(Ptr: MD->getSelector())) { |
| 2316 | // Don't emit a diagnostic if the overriding method in the subclass is |
| 2317 | // marked as unavailable. |
| 2318 | bool Ignore = false; |
| 2319 | if (auto *IMD = IFD->getInstanceMethod(Sel: MD->getSelector())) { |
| 2320 | Ignore = IMD->isUnavailable(); |
| 2321 | } else { |
| 2322 | // Check the methods declared in the class extensions too. |
| 2323 | for (auto *Ext : IFD->visible_extensions()) |
| 2324 | if (auto *IMD = Ext->getInstanceMethod(Sel: MD->getSelector())) { |
| 2325 | Ignore = IMD->isUnavailable(); |
| 2326 | break; |
| 2327 | } |
| 2328 | } |
| 2329 | if (!Ignore) { |
| 2330 | Diag(Loc: ImplD->getLocation(), |
| 2331 | DiagID: diag::warn_objc_implementation_missing_designated_init_override) |
| 2332 | << MD->getSelector(); |
| 2333 | Diag(Loc: MD->getLocation(), DiagID: diag::note_objc_designated_init_marked_here); |
| 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | /// AddPropertyAttrs - Propagates attributes from a property to the |
| 2340 | /// implicitly-declared getter or setter for that property. |
| 2341 | static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod, |
| 2342 | ObjCPropertyDecl *Property) { |
| 2343 | // Should we just clone all attributes over? |
| 2344 | for (const auto *A : Property->attrs()) { |
| 2345 | if (isa<DeprecatedAttr>(Val: A) || |
| 2346 | isa<UnavailableAttr>(Val: A) || |
| 2347 | isa<AvailabilityAttr>(Val: A)) |
| 2348 | PropertyMethod->addAttr(A: A->clone(C&: S.Context)); |
| 2349 | } |
| 2350 | } |
| 2351 | |
| 2352 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 2353 | /// have the property type and issue diagnostics if they don't. |
| 2354 | /// Also synthesize a getter/setter method if none exist (and update the |
| 2355 | /// appropriate lookup tables. |
| 2356 | void SemaObjC::ProcessPropertyDecl(ObjCPropertyDecl *property) { |
| 2357 | ASTContext &Context = getASTContext(); |
| 2358 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 2359 | ObjCContainerDecl *CD = cast<ObjCContainerDecl>(Val: property->getDeclContext()); |
| 2360 | if (CD->isInvalidDecl()) |
| 2361 | return; |
| 2362 | |
| 2363 | bool IsClassProperty = property->isClassProperty(); |
| 2364 | GetterMethod = IsClassProperty ? |
| 2365 | CD->getClassMethod(Sel: property->getGetterName()) : |
| 2366 | CD->getInstanceMethod(Sel: property->getGetterName()); |
| 2367 | |
| 2368 | // if setter or getter is not found in class extension, it might be |
| 2369 | // in the primary class. |
| 2370 | if (!GetterMethod) |
| 2371 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Val: CD)) |
| 2372 | if (CatDecl->IsClassExtension()) |
| 2373 | GetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
| 2374 | getClassMethod(Sel: property->getGetterName()) : |
| 2375 | CatDecl->getClassInterface()-> |
| 2376 | getInstanceMethod(Sel: property->getGetterName()); |
| 2377 | |
| 2378 | SetterMethod = IsClassProperty ? |
| 2379 | CD->getClassMethod(Sel: property->getSetterName()) : |
| 2380 | CD->getInstanceMethod(Sel: property->getSetterName()); |
| 2381 | if (!SetterMethod) |
| 2382 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Val: CD)) |
| 2383 | if (CatDecl->IsClassExtension()) |
| 2384 | SetterMethod = IsClassProperty ? CatDecl->getClassInterface()-> |
| 2385 | getClassMethod(Sel: property->getSetterName()) : |
| 2386 | CatDecl->getClassInterface()-> |
| 2387 | getInstanceMethod(Sel: property->getSetterName()); |
| 2388 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
| 2389 | Loc: property->getLocation()); |
| 2390 | |
| 2391 | // synthesizing accessors must not result in a direct method that is not |
| 2392 | // monomorphic |
| 2393 | if (!GetterMethod) { |
| 2394 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Val: CD)) { |
| 2395 | auto *ExistingGetter = CatDecl->getClassInterface()->lookupMethod( |
| 2396 | Sel: property->getGetterName(), isInstance: !IsClassProperty, shallowCategoryLookup: true, followSuper: false, C: CatDecl); |
| 2397 | if (ExistingGetter) { |
| 2398 | if (ExistingGetter->isDirectMethod() || property->isDirectProperty()) { |
| 2399 | Diag(Loc: property->getLocation(), DiagID: diag::err_objc_direct_duplicate_decl) |
| 2400 | << property->isDirectProperty() << 1 /* property */ |
| 2401 | << ExistingGetter->isDirectMethod() |
| 2402 | << ExistingGetter->getDeclName(); |
| 2403 | Diag(Loc: ExistingGetter->getLocation(), DiagID: diag::note_previous_declaration); |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | if (!property->isReadOnly() && !SetterMethod) { |
| 2410 | if (const ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(Val: CD)) { |
| 2411 | auto *ExistingSetter = CatDecl->getClassInterface()->lookupMethod( |
| 2412 | Sel: property->getSetterName(), isInstance: !IsClassProperty, shallowCategoryLookup: true, followSuper: false, C: CatDecl); |
| 2413 | if (ExistingSetter) { |
| 2414 | if (ExistingSetter->isDirectMethod() || property->isDirectProperty()) { |
| 2415 | Diag(Loc: property->getLocation(), DiagID: diag::err_objc_direct_duplicate_decl) |
| 2416 | << property->isDirectProperty() << 1 /* property */ |
| 2417 | << ExistingSetter->isDirectMethod() |
| 2418 | << ExistingSetter->getDeclName(); |
| 2419 | Diag(Loc: ExistingSetter->getLocation(), DiagID: diag::note_previous_declaration); |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | if (!property->isReadOnly() && SetterMethod) { |
| 2426 | if (Context.getCanonicalType(T: SetterMethod->getReturnType()) != |
| 2427 | Context.VoidTy) |
| 2428 | Diag(Loc: SetterMethod->getLocation(), DiagID: diag::err_setter_type_void); |
| 2429 | if (SetterMethod->param_size() != 1 || |
| 2430 | !Context.hasSameUnqualifiedType( |
| 2431 | T1: (*SetterMethod->param_begin())->getType().getNonReferenceType(), |
| 2432 | T2: property->getType().getNonReferenceType())) { |
| 2433 | Diag(Loc: property->getLocation(), |
| 2434 | DiagID: diag::warn_accessor_property_type_mismatch) |
| 2435 | << property->getDeclName() |
| 2436 | << SetterMethod->getSelector(); |
| 2437 | Diag(Loc: SetterMethod->getLocation(), DiagID: diag::note_declared_at); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | // Synthesize getter/setter methods if none exist. |
| 2442 | // Find the default getter and if one not found, add one. |
| 2443 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 2444 | // synthesize these methods unless the user explicitly provided prototypes |
| 2445 | // (which is odd, but allowed). Sema should be typechecking that the |
| 2446 | // declarations jive in that situation (which it is not currently). |
| 2447 | if (!GetterMethod) { |
| 2448 | // No instance/class method of same name as property getter name was found. |
| 2449 | // Declare a getter method and add it to the list of methods |
| 2450 | // for this class. |
| 2451 | SourceLocation Loc = property->getLocation(); |
| 2452 | |
| 2453 | // The getter returns the declared property type with all qualifiers |
| 2454 | // removed. |
| 2455 | QualType resultTy = property->getType().getAtomicUnqualifiedType(); |
| 2456 | |
| 2457 | // If the property is null_resettable, the getter returns nonnull. |
| 2458 | if (property->getPropertyAttributes() & |
| 2459 | ObjCPropertyAttribute::kind_null_resettable) { |
| 2460 | QualType modifiedTy = resultTy; |
| 2461 | if (auto nullability = AttributedType::stripOuterNullability(T&: modifiedTy)) { |
| 2462 | if (*nullability == NullabilityKind::Unspecified) |
| 2463 | resultTy = Context.getAttributedType(nullability: NullabilityKind::NonNull, |
| 2464 | modifiedType: modifiedTy, equivalentType: modifiedTy); |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | GetterMethod = ObjCMethodDecl::Create( |
| 2469 | C&: Context, beginLoc: Loc, endLoc: Loc, SelInfo: property->getGetterName(), T: resultTy, ReturnTInfo: nullptr, contextDecl: CD, |
| 2470 | isInstance: !IsClassProperty, /*isVariadic=*/false, |
| 2471 | /*isPropertyAccessor=*/true, /*isSynthesizedAccessorStub=*/false, |
| 2472 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 2473 | impControl: (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 2474 | ? ObjCImplementationControl::Optional |
| 2475 | : ObjCImplementationControl::Required); |
| 2476 | CD->addDecl(D: GetterMethod); |
| 2477 | |
| 2478 | AddPropertyAttrs(S&: SemaRef, PropertyMethod: GetterMethod, Property: property); |
| 2479 | |
| 2480 | if (property->isDirectProperty()) |
| 2481 | GetterMethod->addAttr(A: ObjCDirectAttr::CreateImplicit(Ctx&: Context, Range: Loc)); |
| 2482 | |
| 2483 | if (property->hasAttr<NSReturnsNotRetainedAttr>()) |
| 2484 | GetterMethod->addAttr(A: NSReturnsNotRetainedAttr::CreateImplicit(Ctx&: Context, |
| 2485 | Range: Loc)); |
| 2486 | |
| 2487 | if (property->hasAttr<ObjCReturnsInnerPointerAttr>()) |
| 2488 | GetterMethod->addAttr( |
| 2489 | A: ObjCReturnsInnerPointerAttr::CreateImplicit(Ctx&: Context, Range: Loc)); |
| 2490 | |
| 2491 | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
| 2492 | GetterMethod->addAttr(A: SectionAttr::CreateImplicit( |
| 2493 | Ctx&: Context, Name: SA->getName(), Range: Loc, S: SectionAttr::GNU_section)); |
| 2494 | |
| 2495 | SemaRef.ProcessAPINotes(D: GetterMethod); |
| 2496 | |
| 2497 | if (getLangOpts().ObjCAutoRefCount) |
| 2498 | CheckARCMethodDecl(method: GetterMethod); |
| 2499 | } else |
| 2500 | // A user declared getter will be synthesize when @synthesize of |
| 2501 | // the property with the same name is seen in the @implementation |
| 2502 | GetterMethod->setPropertyAccessor(true); |
| 2503 | |
| 2504 | GetterMethod->createImplicitParams(Context, |
| 2505 | ID: GetterMethod->getClassInterface()); |
| 2506 | property->setGetterMethodDecl(GetterMethod); |
| 2507 | |
| 2508 | // Skip setter if property is read-only. |
| 2509 | if (!property->isReadOnly()) { |
| 2510 | // Find the default setter and if one not found, add one. |
| 2511 | if (!SetterMethod) { |
| 2512 | // No instance/class method of same name as property setter name was |
| 2513 | // found. |
| 2514 | // Declare a setter method and add it to the list of methods |
| 2515 | // for this class. |
| 2516 | SourceLocation Loc = property->getLocation(); |
| 2517 | |
| 2518 | SetterMethod = ObjCMethodDecl::Create( |
| 2519 | C&: Context, beginLoc: Loc, endLoc: Loc, SelInfo: property->getSetterName(), T: Context.VoidTy, ReturnTInfo: nullptr, |
| 2520 | contextDecl: CD, isInstance: !IsClassProperty, |
| 2521 | /*isVariadic=*/false, |
| 2522 | /*isPropertyAccessor=*/true, |
| 2523 | /*isSynthesizedAccessorStub=*/false, |
| 2524 | /*isImplicitlyDeclared=*/true, |
| 2525 | /*isDefined=*/false, |
| 2526 | impControl: (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) |
| 2527 | ? ObjCImplementationControl::Optional |
| 2528 | : ObjCImplementationControl::Required); |
| 2529 | |
| 2530 | // Remove all qualifiers from the setter's parameter type. |
| 2531 | QualType paramTy = |
| 2532 | property->getType().getUnqualifiedType().getAtomicUnqualifiedType(); |
| 2533 | |
| 2534 | // If the property is null_resettable, the setter accepts a |
| 2535 | // nullable value. |
| 2536 | if (property->getPropertyAttributes() & |
| 2537 | ObjCPropertyAttribute::kind_null_resettable) { |
| 2538 | QualType modifiedTy = paramTy; |
| 2539 | if (auto nullability = AttributedType::stripOuterNullability(T&: modifiedTy)){ |
| 2540 | if (*nullability == NullabilityKind::Unspecified) |
| 2541 | paramTy = Context.getAttributedType(nullability: NullabilityKind::Nullable, |
| 2542 | modifiedType: modifiedTy, equivalentType: modifiedTy); |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | // Invent the arguments for the setter. We don't bother making a |
| 2547 | // nice name for the argument. |
| 2548 | ParmVarDecl *Argument = ParmVarDecl::Create(C&: Context, DC: SetterMethod, |
| 2549 | StartLoc: Loc, IdLoc: Loc, |
| 2550 | Id: property->getIdentifier(), |
| 2551 | T: paramTy, |
| 2552 | /*TInfo=*/nullptr, |
| 2553 | S: SC_None, |
| 2554 | DefArg: nullptr); |
| 2555 | SetterMethod->setMethodParams(C&: Context, Params: Argument, SelLocs: {}); |
| 2556 | |
| 2557 | AddPropertyAttrs(S&: SemaRef, PropertyMethod: SetterMethod, Property: property); |
| 2558 | |
| 2559 | if (property->isDirectProperty()) |
| 2560 | SetterMethod->addAttr(A: ObjCDirectAttr::CreateImplicit(Ctx&: Context, Range: Loc)); |
| 2561 | |
| 2562 | CD->addDecl(D: SetterMethod); |
| 2563 | if (const SectionAttr *SA = property->getAttr<SectionAttr>()) |
| 2564 | SetterMethod->addAttr(A: SectionAttr::CreateImplicit( |
| 2565 | Ctx&: Context, Name: SA->getName(), Range: Loc, S: SectionAttr::GNU_section)); |
| 2566 | |
| 2567 | SemaRef.ProcessAPINotes(D: SetterMethod); |
| 2568 | |
| 2569 | // It's possible for the user to have set a very odd custom |
| 2570 | // setter selector that causes it to have a method family. |
| 2571 | if (getLangOpts().ObjCAutoRefCount) |
| 2572 | CheckARCMethodDecl(method: SetterMethod); |
| 2573 | } else |
| 2574 | // A user declared setter will be synthesize when @synthesize of |
| 2575 | // the property with the same name is seen in the @implementation |
| 2576 | SetterMethod->setPropertyAccessor(true); |
| 2577 | |
| 2578 | SetterMethod->createImplicitParams(Context, |
| 2579 | ID: SetterMethod->getClassInterface()); |
| 2580 | property->setSetterMethodDecl(SetterMethod); |
| 2581 | } |
| 2582 | // Add any synthesized methods to the global pool. This allows us to |
| 2583 | // handle the following, which is supported by GCC (and part of the design). |
| 2584 | // |
| 2585 | // @interface Foo |
| 2586 | // @property double bar; |
| 2587 | // @end |
| 2588 | // |
| 2589 | // void thisIsUnfortunate() { |
| 2590 | // id foo; |
| 2591 | // double bar = [foo bar]; |
| 2592 | // } |
| 2593 | // |
| 2594 | if (!IsClassProperty) { |
| 2595 | if (GetterMethod) |
| 2596 | AddInstanceMethodToGlobalPool(Method: GetterMethod); |
| 2597 | if (SetterMethod) |
| 2598 | AddInstanceMethodToGlobalPool(Method: SetterMethod); |
| 2599 | } else { |
| 2600 | if (GetterMethod) |
| 2601 | AddFactoryMethodToGlobalPool(Method: GetterMethod); |
| 2602 | if (SetterMethod) |
| 2603 | AddFactoryMethodToGlobalPool(Method: SetterMethod); |
| 2604 | } |
| 2605 | |
| 2606 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(Val: CD); |
| 2607 | if (!CurrentClass) { |
| 2608 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(Val: CD)) |
| 2609 | CurrentClass = Cat->getClassInterface(); |
| 2610 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Val: CD)) |
| 2611 | CurrentClass = Impl->getClassInterface(); |
| 2612 | } |
| 2613 | if (GetterMethod) |
| 2614 | CheckObjCMethodOverrides(ObjCMethod: GetterMethod, CurrentClass, RTC: SemaObjC::RTC_Unknown); |
| 2615 | if (SetterMethod) |
| 2616 | CheckObjCMethodOverrides(ObjCMethod: SetterMethod, CurrentClass, RTC: SemaObjC::RTC_Unknown); |
| 2617 | } |
| 2618 | |
| 2619 | void SemaObjC::CheckObjCPropertyAttributes(Decl *PDecl, SourceLocation Loc, |
| 2620 | unsigned &Attributes, |
| 2621 | bool propertyInPrimaryClass) { |
| 2622 | // FIXME: Improve the reported location. |
| 2623 | if (!PDecl || PDecl->isInvalidDecl()) |
| 2624 | return; |
| 2625 | |
| 2626 | if ((Attributes & ObjCPropertyAttribute::kind_readonly) && |
| 2627 | (Attributes & ObjCPropertyAttribute::kind_readwrite)) |
| 2628 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2629 | << "readonly" << "readwrite" ; |
| 2630 | |
| 2631 | ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(Val: PDecl); |
| 2632 | QualType PropertyTy = PropertyDecl->getType(); |
| 2633 | |
| 2634 | // Check for copy or retain on non-object types. |
| 2635 | if ((Attributes & |
| 2636 | (ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy | |
| 2637 | ObjCPropertyAttribute::kind_retain | |
| 2638 | ObjCPropertyAttribute::kind_strong)) && |
| 2639 | !PropertyTy->isObjCRetainableType() && |
| 2640 | !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) { |
| 2641 | Diag(Loc, DiagID: diag::err_objc_property_requires_object) |
| 2642 | << (Attributes & ObjCPropertyAttribute::kind_weak |
| 2643 | ? "weak" |
| 2644 | : Attributes & ObjCPropertyAttribute::kind_copy |
| 2645 | ? "copy" |
| 2646 | : "retain (or strong)" ); |
| 2647 | Attributes &= |
| 2648 | ~(ObjCPropertyAttribute::kind_weak | ObjCPropertyAttribute::kind_copy | |
| 2649 | ObjCPropertyAttribute::kind_retain | |
| 2650 | ObjCPropertyAttribute::kind_strong); |
| 2651 | PropertyDecl->setInvalidDecl(); |
| 2652 | } |
| 2653 | |
| 2654 | // Check for assign on object types. |
| 2655 | if ((Attributes & ObjCPropertyAttribute::kind_assign) && |
| 2656 | !(Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) && |
| 2657 | PropertyTy->isObjCRetainableType() && |
| 2658 | !PropertyTy->isObjCARCImplicitlyUnretainedType()) { |
| 2659 | Diag(Loc, DiagID: diag::warn_objc_property_assign_on_object); |
| 2660 | } |
| 2661 | |
| 2662 | // Check for more than one of { assign, copy, retain }. |
| 2663 | if (Attributes & ObjCPropertyAttribute::kind_assign) { |
| 2664 | if (Attributes & ObjCPropertyAttribute::kind_copy) { |
| 2665 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2666 | << "assign" << "copy" ; |
| 2667 | Attributes &= ~ObjCPropertyAttribute::kind_copy; |
| 2668 | } |
| 2669 | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
| 2670 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2671 | << "assign" << "retain" ; |
| 2672 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
| 2673 | } |
| 2674 | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
| 2675 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2676 | << "assign" << "strong" ; |
| 2677 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
| 2678 | } |
| 2679 | if (getLangOpts().ObjCAutoRefCount && |
| 2680 | (Attributes & ObjCPropertyAttribute::kind_weak)) { |
| 2681 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2682 | << "assign" << "weak" ; |
| 2683 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
| 2684 | } |
| 2685 | if (PropertyDecl->hasAttr<IBOutletCollectionAttr>()) |
| 2686 | Diag(Loc, DiagID: diag::warn_iboutletcollection_property_assign); |
| 2687 | } else if (Attributes & ObjCPropertyAttribute::kind_unsafe_unretained) { |
| 2688 | if (Attributes & ObjCPropertyAttribute::kind_copy) { |
| 2689 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2690 | << "unsafe_unretained" << "copy" ; |
| 2691 | Attributes &= ~ObjCPropertyAttribute::kind_copy; |
| 2692 | } |
| 2693 | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
| 2694 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2695 | << "unsafe_unretained" << "retain" ; |
| 2696 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
| 2697 | } |
| 2698 | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
| 2699 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2700 | << "unsafe_unretained" << "strong" ; |
| 2701 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
| 2702 | } |
| 2703 | if (getLangOpts().ObjCAutoRefCount && |
| 2704 | (Attributes & ObjCPropertyAttribute::kind_weak)) { |
| 2705 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2706 | << "unsafe_unretained" << "weak" ; |
| 2707 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
| 2708 | } |
| 2709 | } else if (Attributes & ObjCPropertyAttribute::kind_copy) { |
| 2710 | if (Attributes & ObjCPropertyAttribute::kind_retain) { |
| 2711 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2712 | << "copy" << "retain" ; |
| 2713 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
| 2714 | } |
| 2715 | if (Attributes & ObjCPropertyAttribute::kind_strong) { |
| 2716 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2717 | << "copy" << "strong" ; |
| 2718 | Attributes &= ~ObjCPropertyAttribute::kind_strong; |
| 2719 | } |
| 2720 | if (Attributes & ObjCPropertyAttribute::kind_weak) { |
| 2721 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2722 | << "copy" << "weak" ; |
| 2723 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
| 2724 | } |
| 2725 | } else if ((Attributes & ObjCPropertyAttribute::kind_retain) && |
| 2726 | (Attributes & ObjCPropertyAttribute::kind_weak)) { |
| 2727 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) << "retain" |
| 2728 | << "weak" ; |
| 2729 | Attributes &= ~ObjCPropertyAttribute::kind_retain; |
| 2730 | } else if ((Attributes & ObjCPropertyAttribute::kind_strong) && |
| 2731 | (Attributes & ObjCPropertyAttribute::kind_weak)) { |
| 2732 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) << "strong" |
| 2733 | << "weak" ; |
| 2734 | Attributes &= ~ObjCPropertyAttribute::kind_weak; |
| 2735 | } |
| 2736 | |
| 2737 | if (Attributes & ObjCPropertyAttribute::kind_weak) { |
| 2738 | // 'weak' and 'nonnull' are mutually exclusive. |
| 2739 | if (auto nullability = PropertyTy->getNullability()) { |
| 2740 | if (*nullability == NullabilityKind::NonNull) |
| 2741 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) |
| 2742 | << "nonnull" << "weak" ; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | if ((Attributes & ObjCPropertyAttribute::kind_atomic) && |
| 2747 | (Attributes & ObjCPropertyAttribute::kind_nonatomic)) { |
| 2748 | Diag(Loc, DiagID: diag::err_objc_property_attr_mutually_exclusive) << "atomic" |
| 2749 | << "nonatomic" ; |
| 2750 | Attributes &= ~ObjCPropertyAttribute::kind_atomic; |
| 2751 | } |
| 2752 | |
| 2753 | // Warn if user supplied no assignment attribute, property is |
| 2754 | // readwrite, and this is an object type. |
| 2755 | if (!getOwnershipRule(attr: Attributes) && PropertyTy->isObjCRetainableType()) { |
| 2756 | if (Attributes & ObjCPropertyAttribute::kind_readonly) { |
| 2757 | // do nothing |
| 2758 | } else if (getLangOpts().ObjCAutoRefCount) { |
| 2759 | // With arc, @property definitions should default to strong when |
| 2760 | // not specified. |
| 2761 | PropertyDecl->setPropertyAttributes(ObjCPropertyAttribute::kind_strong); |
| 2762 | } else if (PropertyTy->isObjCObjectPointerType()) { |
| 2763 | bool isAnyClassTy = (PropertyTy->isObjCClassType() || |
| 2764 | PropertyTy->isObjCQualifiedClassType()); |
| 2765 | // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to |
| 2766 | // issue any warning. |
| 2767 | if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC) |
| 2768 | ; |
| 2769 | else if (propertyInPrimaryClass) { |
| 2770 | // Don't issue warning on property with no life time in class |
| 2771 | // extension as it is inherited from property in primary class. |
| 2772 | // Skip this warning in gc-only mode. |
| 2773 | if (getLangOpts().getGC() != LangOptions::GCOnly) |
| 2774 | Diag(Loc, DiagID: diag::warn_objc_property_no_assignment_attribute); |
| 2775 | |
| 2776 | // If non-gc code warn that this is likely inappropriate. |
| 2777 | if (getLangOpts().getGC() == LangOptions::NonGC) |
| 2778 | Diag(Loc, DiagID: diag::warn_objc_property_default_assign_on_object); |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | // FIXME: Implement warning dependent on NSCopying being |
| 2783 | // implemented. |
| 2784 | } |
| 2785 | |
| 2786 | if (!(Attributes & ObjCPropertyAttribute::kind_copy) && |
| 2787 | !(Attributes & ObjCPropertyAttribute::kind_readonly) && |
| 2788 | getLangOpts().getGC() == LangOptions::GCOnly && |
| 2789 | PropertyTy->isBlockPointerType()) |
| 2790 | Diag(Loc, DiagID: diag::warn_objc_property_copy_missing_on_block); |
| 2791 | else if ((Attributes & ObjCPropertyAttribute::kind_retain) && |
| 2792 | !(Attributes & ObjCPropertyAttribute::kind_readonly) && |
| 2793 | !(Attributes & ObjCPropertyAttribute::kind_strong) && |
| 2794 | PropertyTy->isBlockPointerType()) |
| 2795 | Diag(Loc, DiagID: diag::warn_objc_property_retain_of_block); |
| 2796 | |
| 2797 | if ((Attributes & ObjCPropertyAttribute::kind_readonly) && |
| 2798 | (Attributes & ObjCPropertyAttribute::kind_setter)) |
| 2799 | Diag(Loc, DiagID: diag::warn_objc_readonly_property_has_setter); |
| 2800 | } |
| 2801 | |