1 //===--- SemaObjCProperty.cpp - Semantic Analysis for ObjC @property ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements semantic analysis for Objective C @property and
11 // @synthesize declarations.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/SemaInternal.h"
16 #include "clang/AST/ASTMutationListener.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/ExprObjC.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Lex/Lexer.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Initialization.h"
24 #include "llvm/ADT/DenseSet.h"
25 #include "llvm/ADT/SmallString.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 /// getImpliedARCOwnership - Given a set of property attributes and a
34 /// type, infer an expected lifetime. The type's ownership qualification
35 /// is not considered.
37 /// Returns OCL_None if the attributes as stated do not imply an ownership.
38 /// Never returns OCL_Autoreleasing.
39 static Qualifiers::ObjCLifetime getImpliedARCOwnership(
40 ObjCPropertyDecl::PropertyAttributeKind attrs,
42 // retain, strong, copy, weak, and unsafe_unretained are only legal
43 // on properties of retainable pointer type.
44 if (attrs & (ObjCPropertyDecl::OBJC_PR_retain |
45 ObjCPropertyDecl::OBJC_PR_strong |
46 ObjCPropertyDecl::OBJC_PR_copy)) {
47 return Qualifiers::OCL_Strong;
48 } else if (attrs & ObjCPropertyDecl::OBJC_PR_weak) {
49 return Qualifiers::OCL_Weak;
50 } else if (attrs & ObjCPropertyDecl::OBJC_PR_unsafe_unretained) {
51 return Qualifiers::OCL_ExplicitNone;
54 // assign can appear on other types, so we have to check the
56 if (attrs & ObjCPropertyDecl::OBJC_PR_assign &&
57 type->isObjCRetainableType()) {
58 return Qualifiers::OCL_ExplicitNone;
61 return Qualifiers::OCL_None;
64 /// Check the internal consistency of a property declaration.
65 static void checkARCPropertyDecl(Sema &S, ObjCPropertyDecl *property) {
66 if (property->isInvalidDecl()) return;
68 ObjCPropertyDecl::PropertyAttributeKind propertyKind
69 = property->getPropertyAttributes();
70 Qualifiers::ObjCLifetime propertyLifetime
71 = property->getType().getObjCLifetime();
73 // Nothing to do if we don't have a lifetime.
74 if (propertyLifetime == Qualifiers::OCL_None) return;
76 Qualifiers::ObjCLifetime expectedLifetime
77 = getImpliedARCOwnership(propertyKind, property->getType());
78 if (!expectedLifetime) {
79 // We have a lifetime qualifier but no dominating property
80 // attribute. That's okay, but restore reasonable invariants by
81 // setting the property attribute according to the lifetime
83 ObjCPropertyDecl::PropertyAttributeKind attr;
84 if (propertyLifetime == Qualifiers::OCL_Strong) {
85 attr = ObjCPropertyDecl::OBJC_PR_strong;
86 } else if (propertyLifetime == Qualifiers::OCL_Weak) {
87 attr = ObjCPropertyDecl::OBJC_PR_weak;
89 assert(propertyLifetime == Qualifiers::OCL_ExplicitNone);
90 attr = ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
92 property->setPropertyAttributes(attr);
96 if (propertyLifetime == expectedLifetime) return;
98 property->setInvalidDecl();
99 S.Diag(property->getLocation(),
100 diag::err_arc_inconsistent_property_ownership)
101 << property->getDeclName()
106 /// \brief Check this Objective-C property against a property declared in the
109 CheckPropertyAgainstProtocol(Sema &S, ObjCPropertyDecl *Prop,
110 ObjCProtocolDecl *Proto,
111 llvm::SmallPtrSetImpl<ObjCProtocolDecl *> &Known) {
112 // Have we seen this protocol before?
113 if (!Known.insert(Proto).second)
116 // Look for a property with the same name.
117 DeclContext::lookup_result R = Proto->lookup(Prop->getDeclName());
118 for (unsigned I = 0, N = R.size(); I != N; ++I) {
119 if (ObjCPropertyDecl *ProtoProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
120 S.DiagnosePropertyMismatch(Prop, ProtoProp, Proto->getIdentifier(), true);
125 // Check this property against any protocols we inherit.
126 for (auto *P : Proto->protocols())
127 CheckPropertyAgainstProtocol(S, Prop, P, Known);
130 Decl *Sema::ActOnProperty(Scope *S, SourceLocation AtLoc,
131 SourceLocation LParenLoc,
136 bool *isOverridingProperty,
137 tok::ObjCKeywordKind MethodImplKind,
138 DeclContext *lexicalDC) {
139 unsigned Attributes = ODS.getPropertyAttributes();
140 FD.D.setObjCWeakProperty((Attributes & ObjCDeclSpec::DQ_PR_weak) != 0);
141 TypeSourceInfo *TSI = GetTypeForDeclarator(FD.D, S);
142 QualType T = TSI->getType();
143 Attributes |= deduceWeakPropertyFromType(T);
144 bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) ||
145 // default is readwrite!
146 !(Attributes & ObjCDeclSpec::DQ_PR_readonly));
147 // property is defaulted to 'assign' if it is readwrite and is
148 // not retain or copy
149 bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) ||
151 !(Attributes & ObjCDeclSpec::DQ_PR_retain) &&
152 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
153 !(Attributes & ObjCDeclSpec::DQ_PR_copy) &&
154 !(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
155 !(Attributes & ObjCDeclSpec::DQ_PR_weak)));
157 // Proceed with constructing the ObjCPropertyDecls.
158 ObjCContainerDecl *ClassDecl = cast<ObjCContainerDecl>(CurContext);
159 ObjCPropertyDecl *Res = nullptr;
160 if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
161 if (CDecl->IsClassExtension()) {
162 Res = HandlePropertyInClassExtension(S, AtLoc, LParenLoc,
163 FD, GetterSel, SetterSel,
164 isAssign, isReadWrite,
166 ODS.getPropertyAttributes(),
167 isOverridingProperty, T, TSI,
175 Res = CreatePropertyDecl(S, ClassDecl, AtLoc, LParenLoc, FD,
176 GetterSel, SetterSel, isAssign, isReadWrite,
177 Attributes, ODS.getPropertyAttributes(),
178 T, TSI, MethodImplKind);
180 Res->setLexicalDeclContext(lexicalDC);
183 // Validate the attributes on the @property.
184 CheckObjCPropertyAttributes(Res, AtLoc, Attributes,
185 (isa<ObjCInterfaceDecl>(ClassDecl) ||
186 isa<ObjCProtocolDecl>(ClassDecl)));
188 if (getLangOpts().ObjCAutoRefCount)
189 checkARCPropertyDecl(*this, Res);
191 llvm::SmallPtrSet<ObjCProtocolDecl *, 16> KnownProtos;
192 if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
193 // For a class, compare the property against a property in our superclass.
194 bool FoundInSuper = false;
195 ObjCInterfaceDecl *CurrentInterfaceDecl = IFace;
196 while (ObjCInterfaceDecl *Super = CurrentInterfaceDecl->getSuperClass()) {
197 DeclContext::lookup_result R = Super->lookup(Res->getDeclName());
198 for (unsigned I = 0, N = R.size(); I != N; ++I) {
199 if (ObjCPropertyDecl *SuperProp = dyn_cast<ObjCPropertyDecl>(R[I])) {
200 DiagnosePropertyMismatch(Res, SuperProp, Super->getIdentifier(), false);
208 CurrentInterfaceDecl = Super;
212 // Also compare the property against a property in our protocols.
213 for (auto *P : CurrentInterfaceDecl->protocols()) {
214 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
217 // Slower path: look in all protocols we referenced.
218 for (auto *P : IFace->all_referenced_protocols()) {
219 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
222 } else if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
223 for (auto *P : Cat->protocols())
224 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
226 ObjCProtocolDecl *Proto = cast<ObjCProtocolDecl>(ClassDecl);
227 for (auto *P : Proto->protocols())
228 CheckPropertyAgainstProtocol(*this, Res, P, KnownProtos);
231 ActOnDocumentableDecl(Res);
235 static ObjCPropertyDecl::PropertyAttributeKind
236 makePropertyAttributesAsWritten(unsigned Attributes) {
237 unsigned attributesAsWritten = 0;
238 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
239 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readonly;
240 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
241 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_readwrite;
242 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
243 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_getter;
244 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
245 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_setter;
246 if (Attributes & ObjCDeclSpec::DQ_PR_assign)
247 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_assign;
248 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
249 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_retain;
250 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
251 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_strong;
252 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
253 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_weak;
254 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
255 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_copy;
256 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
257 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_unsafe_unretained;
258 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
259 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_nonatomic;
260 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
261 attributesAsWritten |= ObjCPropertyDecl::OBJC_PR_atomic;
263 return (ObjCPropertyDecl::PropertyAttributeKind)attributesAsWritten;
266 static bool LocPropertyAttribute( ASTContext &Context, const char *attrName,
267 SourceLocation LParenLoc, SourceLocation &Loc) {
268 if (LParenLoc.isMacroID())
271 SourceManager &SM = Context.getSourceManager();
272 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(LParenLoc);
273 // Try to load the file buffer.
274 bool invalidTemp = false;
275 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
278 const char *tokenBegin = file.data() + locInfo.second;
280 // Lex from the start of the given location.
281 Lexer lexer(SM.getLocForStartOfFile(locInfo.first),
282 Context.getLangOpts(),
283 file.begin(), tokenBegin, file.end());
286 lexer.LexFromRawLexer(Tok);
287 if (Tok.is(tok::raw_identifier) && Tok.getRawIdentifier() == attrName) {
288 Loc = Tok.getLocation();
291 } while (Tok.isNot(tok::r_paren));
296 static unsigned getOwnershipRule(unsigned attr) {
297 return attr & (ObjCPropertyDecl::OBJC_PR_assign |
298 ObjCPropertyDecl::OBJC_PR_retain |
299 ObjCPropertyDecl::OBJC_PR_copy |
300 ObjCPropertyDecl::OBJC_PR_weak |
301 ObjCPropertyDecl::OBJC_PR_strong |
302 ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
306 Sema::HandlePropertyInClassExtension(Scope *S,
307 SourceLocation AtLoc,
308 SourceLocation LParenLoc,
310 Selector GetterSel, Selector SetterSel,
312 const bool isReadWrite,
313 const unsigned Attributes,
314 const unsigned AttributesAsWritten,
315 bool *isOverridingProperty,
318 tok::ObjCKeywordKind MethodImplKind) {
319 ObjCCategoryDecl *CDecl = cast<ObjCCategoryDecl>(CurContext);
320 // Diagnose if this property is already in continuation class.
321 DeclContext *DC = CurContext;
322 IdentifierInfo *PropertyId = FD.D.getIdentifier();
323 ObjCInterfaceDecl *CCPrimary = CDecl->getClassInterface();
326 // Check for duplicate declaration of this property in current and
327 // other class extensions.
328 for (const auto *Ext : CCPrimary->known_extensions()) {
329 if (ObjCPropertyDecl *prevDecl
330 = ObjCPropertyDecl::findPropertyDecl(Ext, PropertyId)) {
331 Diag(AtLoc, diag::err_duplicate_property);
332 Diag(prevDecl->getLocation(), diag::note_property_declare);
338 // Create a new ObjCPropertyDecl with the DeclContext being
339 // the class extension.
340 // FIXME. We should really be using CreatePropertyDecl for this.
341 ObjCPropertyDecl *PDecl =
342 ObjCPropertyDecl::Create(Context, DC, FD.D.getIdentifierLoc(),
343 PropertyId, AtLoc, LParenLoc, T, TSI);
344 PDecl->setPropertyAttributesAsWritten(
345 makePropertyAttributesAsWritten(AttributesAsWritten));
346 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
347 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
348 if (Attributes & ObjCDeclSpec::DQ_PR_readwrite)
349 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
350 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
351 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
352 if (Attributes & ObjCDeclSpec::DQ_PR_atomic)
353 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
354 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
355 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
356 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
357 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
359 // Set setter/getter selector name. Needed later.
360 PDecl->setGetterName(GetterSel);
361 PDecl->setSetterName(SetterSel);
362 ProcessDeclAttributes(S, PDecl, FD.D);
365 // We need to look in the @interface to see if the @property was
368 Diag(CDecl->getLocation(), diag::err_continuation_class);
369 *isOverridingProperty = true;
373 // Find the property in continuation class's primary class only.
374 ObjCPropertyDecl *PIDecl =
375 CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId);
378 // No matching property found in the primary class. Just fall thru
379 // and add property to continuation class's primary class.
380 ObjCPropertyDecl *PrimaryPDecl =
381 CreatePropertyDecl(S, CCPrimary, AtLoc, LParenLoc,
382 FD, GetterSel, SetterSel, isAssign, isReadWrite,
383 Attributes,AttributesAsWritten, T, TSI, MethodImplKind,
386 // A case of continuation class adding a new property in the class. This
387 // is not what it was meant for. However, gcc supports it and so should we.
388 // Make sure setter/getters are declared here.
389 ProcessPropertyDecl(PrimaryPDecl, CCPrimary,
390 /* redeclaredProperty = */ nullptr,
391 /* lexicalDC = */ CDecl);
392 PDecl->setGetterMethodDecl(PrimaryPDecl->getGetterMethodDecl());
393 PDecl->setSetterMethodDecl(PrimaryPDecl->getSetterMethodDecl());
394 if (ASTMutationListener *L = Context.getASTMutationListener())
395 L->AddedObjCPropertyInClassExtension(PrimaryPDecl, /*OrigProp=*/nullptr,
399 if (!Context.hasSameType(PIDecl->getType(), PDecl->getType())) {
400 bool IncompatibleObjC = false;
401 QualType ConvertedType;
402 // Relax the strict type matching for property type in continuation class.
403 // Allow property object type of continuation class to be different as long
404 // as it narrows the object type in its primary class property. Note that
405 // this conversion is safe only because the wider type is for a 'readonly'
406 // property in primary class and 'narrowed' type for a 'readwrite' property
407 // in continuation class.
408 QualType PrimaryClassPropertyT = Context.getCanonicalType(PIDecl->getType());
409 QualType ClassExtPropertyT = Context.getCanonicalType(PDecl->getType());
410 if (!isa<ObjCObjectPointerType>(PrimaryClassPropertyT) ||
411 !isa<ObjCObjectPointerType>(ClassExtPropertyT) ||
412 (!isObjCPointerConversion(ClassExtPropertyT, PrimaryClassPropertyT,
413 ConvertedType, IncompatibleObjC))
414 || IncompatibleObjC) {
416 diag::err_type_mismatch_continuation_class) << PDecl->getType();
417 Diag(PIDecl->getLocation(), diag::note_property_declare);
422 // The property 'PIDecl's readonly attribute will be over-ridden
423 // with continuation class's readwrite property attribute!
424 unsigned PIkind = PIDecl->getPropertyAttributesAsWritten();
425 if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) {
426 PIkind &= ~ObjCPropertyDecl::OBJC_PR_readonly;
427 PIkind |= ObjCPropertyDecl::OBJC_PR_readwrite;
428 PIkind |= deduceWeakPropertyFromType(PIDecl->getType());
429 unsigned ClassExtensionMemoryModel = getOwnershipRule(Attributes);
430 unsigned PrimaryClassMemoryModel = getOwnershipRule(PIkind);
431 if (PrimaryClassMemoryModel && ClassExtensionMemoryModel &&
432 (PrimaryClassMemoryModel != ClassExtensionMemoryModel)) {
433 Diag(AtLoc, diag::warn_property_attr_mismatch);
434 Diag(PIDecl->getLocation(), diag::note_property_declare);
436 else if (getLangOpts().ObjCAutoRefCount) {
437 QualType PrimaryPropertyQT =
438 Context.getCanonicalType(PIDecl->getType()).getUnqualifiedType();
439 if (isa<ObjCObjectPointerType>(PrimaryPropertyQT)) {
440 bool PropertyIsWeak = ((PIkind & ObjCPropertyDecl::OBJC_PR_weak) != 0);
441 Qualifiers::ObjCLifetime PrimaryPropertyLifeTime =
442 PrimaryPropertyQT.getObjCLifetime();
443 if (PrimaryPropertyLifeTime == Qualifiers::OCL_None &&
444 (Attributes & ObjCDeclSpec::DQ_PR_weak) &&
446 Diag(AtLoc, diag::warn_property_implicitly_mismatched);
447 Diag(PIDecl->getLocation(), diag::note_property_declare);
452 DeclContext *DC = cast<DeclContext>(CCPrimary);
453 if (!ObjCPropertyDecl::findPropertyDecl(DC,
454 PIDecl->getDeclName().getAsIdentifierInfo())) {
455 // In mrr mode, 'readwrite' property must have an explicit
456 // memory attribute. If none specified, select the default (assign).
457 if (!getLangOpts().ObjCAutoRefCount) {
458 if (!(PIkind & (ObjCDeclSpec::DQ_PR_assign |
459 ObjCDeclSpec::DQ_PR_retain |
460 ObjCDeclSpec::DQ_PR_strong |
461 ObjCDeclSpec::DQ_PR_copy |
462 ObjCDeclSpec::DQ_PR_unsafe_unretained |
463 ObjCDeclSpec::DQ_PR_weak)))
464 PIkind |= ObjCPropertyDecl::OBJC_PR_assign;
467 // Protocol is not in the primary class. Must build one for it.
468 ObjCDeclSpec ProtocolPropertyODS;
469 // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind
470 // and ObjCPropertyDecl::PropertyAttributeKind have identical
471 // values. Should consolidate both into one enum type.
473 setPropertyAttributes((ObjCDeclSpec::ObjCPropertyAttributeKind)
475 // Must re-establish the context from class extension to primary
477 ContextRAII SavedContext(*this, CCPrimary);
479 Decl *ProtocolPtrTy =
480 ActOnProperty(S, AtLoc, LParenLoc, FD, ProtocolPropertyODS,
481 PIDecl->getGetterName(),
482 PIDecl->getSetterName(),
483 isOverridingProperty,
485 /* lexicalDC = */ CDecl);
486 PIDecl = cast<ObjCPropertyDecl>(ProtocolPtrTy);
488 PIDecl->makeitReadWriteAttribute();
489 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
490 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
491 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
492 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
493 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
494 PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
495 PIDecl->setSetterName(SetterSel);
497 // Tailor the diagnostics for the common case where a readwrite
498 // property is declared both in the @interface and the continuation.
499 // This is a common error where the user often intended the original
500 // declaration to be readonly.
502 (Attributes & ObjCDeclSpec::DQ_PR_readwrite) &&
503 (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite)
504 ? diag::err_use_continuation_class_redeclaration_readwrite
505 : diag::err_use_continuation_class;
507 << CCPrimary->getDeclName();
508 Diag(PIDecl->getLocation(), diag::note_property_declare);
511 *isOverridingProperty = true;
512 // Make sure setter decl is synthesized, and added to primary class's list.
513 ProcessPropertyDecl(PIDecl, CCPrimary, PDecl, CDecl);
514 PDecl->setGetterMethodDecl(PIDecl->getGetterMethodDecl());
515 PDecl->setSetterMethodDecl(PIDecl->getSetterMethodDecl());
516 if (ASTMutationListener *L = Context.getASTMutationListener())
517 L->AddedObjCPropertyInClassExtension(PDecl, PIDecl, CDecl);
521 ObjCPropertyDecl *Sema::CreatePropertyDecl(Scope *S,
522 ObjCContainerDecl *CDecl,
523 SourceLocation AtLoc,
524 SourceLocation LParenLoc,
529 const bool isReadWrite,
530 const unsigned Attributes,
531 const unsigned AttributesAsWritten,
533 TypeSourceInfo *TInfo,
534 tok::ObjCKeywordKind MethodImplKind,
535 DeclContext *lexicalDC){
536 IdentifierInfo *PropertyId = FD.D.getIdentifier();
538 // Issue a warning if property is 'assign' as default and its object, which is
539 // gc'able conforms to NSCopying protocol
540 if (getLangOpts().getGC() != LangOptions::NonGC &&
541 isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign))
542 if (const ObjCObjectPointerType *ObjPtrTy =
543 T->getAs<ObjCObjectPointerType>()) {
544 ObjCInterfaceDecl *IDecl = ObjPtrTy->getObjectType()->getInterface();
546 if (ObjCProtocolDecl* PNSCopying =
547 LookupProtocol(&Context.Idents.get("NSCopying"), AtLoc))
548 if (IDecl->ClassImplementsProtocol(PNSCopying, true))
549 Diag(AtLoc, diag::warn_implements_nscopying) << PropertyId;
552 if (T->isObjCObjectType()) {
553 SourceLocation StarLoc = TInfo->getTypeLoc().getLocEnd();
554 StarLoc = getLocForEndOfToken(StarLoc);
555 Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object)
556 << FixItHint::CreateInsertion(StarLoc, "*");
557 T = Context.getObjCObjectPointerType(T);
558 SourceLocation TLoc = TInfo->getTypeLoc().getLocStart();
559 TInfo = Context.getTrivialTypeSourceInfo(T, TLoc);
562 DeclContext *DC = cast<DeclContext>(CDecl);
563 ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC,
564 FD.D.getIdentifierLoc(),
566 LParenLoc, T, TInfo);
568 if (ObjCPropertyDecl *prevDecl =
569 ObjCPropertyDecl::findPropertyDecl(DC, PropertyId)) {
570 Diag(PDecl->getLocation(), diag::err_duplicate_property);
571 Diag(prevDecl->getLocation(), diag::note_property_declare);
572 PDecl->setInvalidDecl();
577 PDecl->setLexicalDeclContext(lexicalDC);
580 if (T->isArrayType() || T->isFunctionType()) {
581 Diag(AtLoc, diag::err_property_type) << T;
582 PDecl->setInvalidDecl();
585 ProcessDeclAttributes(S, PDecl, FD.D);
587 // Regardless of setter/getter attribute, we save the default getter/setter
588 // selector names in anticipation of declaration of setter/getter methods.
589 PDecl->setGetterName(GetterSel);
590 PDecl->setSetterName(SetterSel);
591 PDecl->setPropertyAttributesAsWritten(
592 makePropertyAttributesAsWritten(AttributesAsWritten));
594 if (Attributes & ObjCDeclSpec::DQ_PR_readonly)
595 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly);
597 if (Attributes & ObjCDeclSpec::DQ_PR_getter)
598 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter);
600 if (Attributes & ObjCDeclSpec::DQ_PR_setter)
601 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter);
604 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite);
606 if (Attributes & ObjCDeclSpec::DQ_PR_retain)
607 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain);
609 if (Attributes & ObjCDeclSpec::DQ_PR_strong)
610 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
612 if (Attributes & ObjCDeclSpec::DQ_PR_weak)
613 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
615 if (Attributes & ObjCDeclSpec::DQ_PR_copy)
616 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
618 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
619 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
622 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
624 // In the semantic attributes, one of nonatomic or atomic is always set.
625 if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)
626 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic);
628 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_atomic);
630 // 'unsafe_unretained' is alias for 'assign'.
631 if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained)
632 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign);
634 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_unsafe_unretained);
636 if (MethodImplKind == tok::objc_required)
637 PDecl->setPropertyImplementation(ObjCPropertyDecl::Required);
638 else if (MethodImplKind == tok::objc_optional)
639 PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional);
641 if (Attributes & ObjCDeclSpec::DQ_PR_nullability)
642 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability);
644 if (Attributes & ObjCDeclSpec::DQ_PR_null_resettable)
645 PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_null_resettable);
650 static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
651 ObjCPropertyDecl *property,
652 ObjCIvarDecl *ivar) {
653 if (property->isInvalidDecl() || ivar->isInvalidDecl()) return;
655 QualType ivarType = ivar->getType();
656 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
658 // The lifetime implied by the property's attributes.
659 Qualifiers::ObjCLifetime propertyLifetime =
660 getImpliedARCOwnership(property->getPropertyAttributes(),
661 property->getType());
663 // We're fine if they match.
664 if (propertyLifetime == ivarLifetime) return;
666 // These aren't valid lifetimes for object ivars; don't diagnose twice.
667 if (ivarLifetime == Qualifiers::OCL_None ||
668 ivarLifetime == Qualifiers::OCL_Autoreleasing)
671 // If the ivar is private, and it's implicitly __unsafe_unretained
672 // becaues of its type, then pretend it was actually implicitly
673 // __strong. This is only sound because we're processing the
674 // property implementation before parsing any method bodies.
675 if (ivarLifetime == Qualifiers::OCL_ExplicitNone &&
676 propertyLifetime == Qualifiers::OCL_Strong &&
677 ivar->getAccessControl() == ObjCIvarDecl::Private) {
678 SplitQualType split = ivarType.split();
679 if (split.Quals.hasObjCLifetime()) {
680 assert(ivarType->isObjCARCImplicitlyUnretainedType());
681 split.Quals.setObjCLifetime(Qualifiers::OCL_Strong);
682 ivarType = S.Context.getQualifiedType(split);
683 ivar->setType(ivarType);
688 switch (propertyLifetime) {
689 case Qualifiers::OCL_Strong:
690 S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
691 << property->getDeclName()
692 << ivar->getDeclName()
696 case Qualifiers::OCL_Weak:
697 S.Diag(ivar->getLocation(), diag::error_weak_property)
698 << property->getDeclName()
699 << ivar->getDeclName();
702 case Qualifiers::OCL_ExplicitNone:
703 S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
704 << property->getDeclName()
705 << ivar->getDeclName()
706 << ((property->getPropertyAttributesAsWritten()
707 & ObjCPropertyDecl::OBJC_PR_assign) != 0);
710 case Qualifiers::OCL_Autoreleasing:
711 llvm_unreachable("properties cannot be autoreleasing");
713 case Qualifiers::OCL_None:
714 // Any other property should be ignored.
718 S.Diag(property->getLocation(), diag::note_property_declare);
719 if (propertyImplLoc.isValid())
720 S.Diag(propertyImplLoc, diag::note_property_synthesize);
723 /// setImpliedPropertyAttributeForReadOnlyProperty -
724 /// This routine evaludates life-time attributes for a 'readonly'
725 /// property with no known lifetime of its own, using backing
726 /// 'ivar's attribute, if any. If no backing 'ivar', property's
727 /// life-time is assumed 'strong'.
728 static void setImpliedPropertyAttributeForReadOnlyProperty(
729 ObjCPropertyDecl *property, ObjCIvarDecl *ivar) {
730 Qualifiers::ObjCLifetime propertyLifetime =
731 getImpliedARCOwnership(property->getPropertyAttributes(),
732 property->getType());
733 if (propertyLifetime != Qualifiers::OCL_None)
737 // if no backing ivar, make property 'strong'.
738 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
741 // property assumes owenership of backing ivar.
742 QualType ivarType = ivar->getType();
743 Qualifiers::ObjCLifetime ivarLifetime = ivarType.getObjCLifetime();
744 if (ivarLifetime == Qualifiers::OCL_Strong)
745 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
746 else if (ivarLifetime == Qualifiers::OCL_Weak)
747 property->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_weak);
751 /// DiagnosePropertyMismatchDeclInProtocols - diagnose properties declared
752 /// in inherited protocols with mismatched types. Since any of them can
753 /// be candidate for synthesis.
755 DiagnosePropertyMismatchDeclInProtocols(Sema &S, SourceLocation AtLoc,
756 ObjCInterfaceDecl *ClassDecl,
757 ObjCPropertyDecl *Property) {
758 ObjCInterfaceDecl::ProtocolPropertyMap PropMap;
759 for (const auto *PI : ClassDecl->all_referenced_protocols()) {
760 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
761 PDecl->collectInheritedProtocolProperties(Property, PropMap);
763 if (ObjCInterfaceDecl *SDecl = ClassDecl->getSuperClass())
765 for (const auto *PI : SDecl->all_referenced_protocols()) {
766 if (const ObjCProtocolDecl *PDecl = PI->getDefinition())
767 PDecl->collectInheritedProtocolProperties(Property, PropMap);
769 SDecl = SDecl->getSuperClass();
775 QualType RHSType = S.Context.getCanonicalType(Property->getType());
776 bool FirsTime = true;
777 for (ObjCInterfaceDecl::ProtocolPropertyMap::iterator
778 I = PropMap.begin(), E = PropMap.end(); I != E; I++) {
779 ObjCPropertyDecl *Prop = I->second;
780 QualType LHSType = S.Context.getCanonicalType(Prop->getType());
781 if (!S.Context.propertyTypesAreCompatible(LHSType, RHSType)) {
782 bool IncompatibleObjC = false;
783 QualType ConvertedType;
784 if (!S.isObjCPointerConversion(RHSType, LHSType, ConvertedType, IncompatibleObjC)
785 || IncompatibleObjC) {
787 S.Diag(Property->getLocation(), diag::warn_protocol_property_mismatch)
788 << Property->getType();
791 S.Diag(Prop->getLocation(), diag::note_protocol_property_declare)
796 if (!FirsTime && AtLoc.isValid())
797 S.Diag(AtLoc, diag::note_property_synthesize);
800 /// ActOnPropertyImplDecl - This routine performs semantic checks and
801 /// builds the AST node for a property implementation declaration; declared
802 /// as \@synthesize or \@dynamic.
804 Decl *Sema::ActOnPropertyImplDecl(Scope *S,
805 SourceLocation AtLoc,
806 SourceLocation PropertyLoc,
808 IdentifierInfo *PropertyId,
809 IdentifierInfo *PropertyIvar,
810 SourceLocation PropertyIvarLoc) {
811 ObjCContainerDecl *ClassImpDecl =
812 dyn_cast<ObjCContainerDecl>(CurContext);
813 // Make sure we have a context for the property implementation declaration.
815 Diag(AtLoc, diag::error_missing_property_context);
818 if (PropertyIvarLoc.isInvalid())
819 PropertyIvarLoc = PropertyLoc;
820 SourceLocation PropertyDiagLoc = PropertyLoc;
821 if (PropertyDiagLoc.isInvalid())
822 PropertyDiagLoc = ClassImpDecl->getLocStart();
823 ObjCPropertyDecl *property = nullptr;
824 ObjCInterfaceDecl *IDecl = nullptr;
825 // Find the class or category class where this property must have
827 ObjCImplementationDecl *IC = nullptr;
828 ObjCCategoryImplDecl *CatImplClass = nullptr;
829 if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) {
830 IDecl = IC->getClassInterface();
831 // We always synthesize an interface for an implementation
832 // without an interface decl. So, IDecl is always non-zero.
834 "ActOnPropertyImplDecl - @implementation without @interface");
836 // Look for this property declaration in the @implementation's @interface
837 property = IDecl->FindPropertyDeclaration(PropertyId);
839 Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName();
842 unsigned PIkind = property->getPropertyAttributesAsWritten();
843 if ((PIkind & (ObjCPropertyDecl::OBJC_PR_atomic |
844 ObjCPropertyDecl::OBJC_PR_nonatomic) ) == 0) {
846 Diag(AtLoc, diag::warn_implicit_atomic_property);
848 Diag(IC->getLocation(), diag::warn_auto_implicit_atomic_property);
849 Diag(property->getLocation(), diag::note_property_declare);
852 if (const ObjCCategoryDecl *CD =
853 dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
854 if (!CD->IsClassExtension()) {
855 Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
856 Diag(property->getLocation(), diag::note_property_declare);
861 (PIkind & ObjCPropertyDecl::OBJC_PR_readonly) &&
862 property->hasAttr<IBOutletAttr>() &&
864 bool ReadWriteProperty = false;
865 // Search into the class extensions and see if 'readonly property is
866 // redeclared 'readwrite', then no warning is to be issued.
867 for (auto *Ext : IDecl->known_extensions()) {
868 DeclContext::lookup_result R = Ext->lookup(property->getDeclName());
870 if (ObjCPropertyDecl *ExtProp = dyn_cast<ObjCPropertyDecl>(R[0])) {
871 PIkind = ExtProp->getPropertyAttributesAsWritten();
872 if (PIkind & ObjCPropertyDecl::OBJC_PR_readwrite) {
873 ReadWriteProperty = true;
879 if (!ReadWriteProperty) {
880 Diag(property->getLocation(), diag::warn_auto_readonly_iboutlet_property)
882 SourceLocation readonlyLoc;
883 if (LocPropertyAttribute(Context, "readonly",
884 property->getLParenLoc(), readonlyLoc)) {
885 SourceLocation endLoc =
886 readonlyLoc.getLocWithOffset(strlen("readonly")-1);
887 SourceRange ReadonlySourceRange(readonlyLoc, endLoc);
888 Diag(property->getLocation(),
889 diag::note_auto_readonly_iboutlet_fixup_suggest) <<
890 FixItHint::CreateReplacement(ReadonlySourceRange, "readwrite");
894 if (Synthesize && isa<ObjCProtocolDecl>(property->getDeclContext()))
895 DiagnosePropertyMismatchDeclInProtocols(*this, AtLoc, IDecl, property);
897 } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) {
899 Diag(AtLoc, diag::error_synthesize_category_decl);
902 IDecl = CatImplClass->getClassInterface();
904 Diag(AtLoc, diag::error_missing_property_interface);
907 ObjCCategoryDecl *Category =
908 IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier());
910 // If category for this implementation not found, it is an error which
911 // has already been reported eralier.
914 // Look for this property declaration in @implementation's category
915 property = Category->FindPropertyDeclaration(PropertyId);
917 Diag(PropertyLoc, diag::error_bad_category_property_decl)
918 << Category->getDeclName();
922 Diag(AtLoc, diag::error_bad_property_context);
925 ObjCIvarDecl *Ivar = nullptr;
926 bool CompleteTypeErr = false;
928 // Check that we have a valid, previously declared ivar for @synthesize
932 PropertyIvar = PropertyId;
933 // Check that this is a previously declared 'ivar' in 'IDecl' interface
934 ObjCInterfaceDecl *ClassDeclared;
935 Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared);
936 QualType PropType = property->getType();
937 QualType PropertyIvarType = PropType.getNonReferenceType();
939 if (RequireCompleteType(PropertyDiagLoc, PropertyIvarType,
940 diag::err_incomplete_synthesized_property,
941 property->getDeclName())) {
942 Diag(property->getLocation(), diag::note_property_declare);
943 CompleteTypeErr = true;
946 if (getLangOpts().ObjCAutoRefCount &&
947 (property->getPropertyAttributesAsWritten() &
948 ObjCPropertyDecl::OBJC_PR_readonly) &&
949 PropertyIvarType->isObjCRetainableType()) {
950 setImpliedPropertyAttributeForReadOnlyProperty(property, Ivar);
953 ObjCPropertyDecl::PropertyAttributeKind kind
954 = property->getPropertyAttributes();
956 // Add GC __weak to the ivar type if the property is weak.
957 if ((kind & ObjCPropertyDecl::OBJC_PR_weak) &&
958 getLangOpts().getGC() != LangOptions::NonGC) {
959 assert(!getLangOpts().ObjCAutoRefCount);
960 if (PropertyIvarType.isObjCGCStrong()) {
961 Diag(PropertyDiagLoc, diag::err_gc_weak_property_strong_type);
962 Diag(property->getLocation(), diag::note_property_declare);
965 Context.getObjCGCQualType(PropertyIvarType, Qualifiers::Weak);
968 if (AtLoc.isInvalid()) {
969 // Check when default synthesizing a property that there is
970 // an ivar matching property name and issue warning; since this
971 // is the most common case of not using an ivar used for backing
972 // property in non-default synthesis case.
973 ObjCInterfaceDecl *ClassDeclared=nullptr;
974 ObjCIvarDecl *originalIvar =
975 IDecl->lookupInstanceVariable(property->getIdentifier(),
978 Diag(PropertyDiagLoc,
979 diag::warn_autosynthesis_property_ivar_match)
980 << PropertyId << (Ivar == nullptr) << PropertyIvar
981 << originalIvar->getIdentifier();
982 Diag(property->getLocation(), diag::note_property_declare);
983 Diag(originalIvar->getLocation(), diag::note_ivar_decl);
988 // In ARC, give the ivar a lifetime qualifier based on the
989 // property attributes.
990 if (getLangOpts().ObjCAutoRefCount &&
991 !PropertyIvarType.getObjCLifetime() &&
992 PropertyIvarType->isObjCRetainableType()) {
994 // It's an error if we have to do this and the user didn't
995 // explicitly write an ownership attribute on the property.
996 if (!property->hasWrittenStorageAttribute() &&
997 !(kind & ObjCPropertyDecl::OBJC_PR_strong)) {
998 Diag(PropertyDiagLoc,
999 diag::err_arc_objc_property_default_assign_on_object);
1000 Diag(property->getLocation(), diag::note_property_declare);
1002 Qualifiers::ObjCLifetime lifetime =
1003 getImpliedARCOwnership(kind, PropertyIvarType);
1004 assert(lifetime && "no lifetime for property?");
1005 if (lifetime == Qualifiers::OCL_Weak) {
1007 if (const ObjCObjectPointerType *ObjT =
1008 PropertyIvarType->getAs<ObjCObjectPointerType>()) {
1009 const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl();
1010 if (ObjI && ObjI->isArcWeakrefUnavailable()) {
1011 Diag(property->getLocation(),
1012 diag::err_arc_weak_unavailable_property) << PropertyIvarType;
1013 Diag(ClassImpDecl->getLocation(), diag::note_implemented_by_class)
1014 << ClassImpDecl->getName();
1018 if (!err && !getLangOpts().ObjCARCWeak) {
1019 Diag(PropertyDiagLoc, diag::err_arc_weak_no_runtime);
1020 Diag(property->getLocation(), diag::note_property_declare);
1025 qs.addObjCLifetime(lifetime);
1026 PropertyIvarType = Context.getQualifiedType(PropertyIvarType, qs);
1030 if (kind & ObjCPropertyDecl::OBJC_PR_weak &&
1031 !getLangOpts().ObjCAutoRefCount &&
1032 getLangOpts().getGC() == LangOptions::NonGC) {
1033 Diag(PropertyDiagLoc, diag::error_synthesize_weak_non_arc_or_gc);
1034 Diag(property->getLocation(), diag::note_property_declare);
1037 Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl,
1038 PropertyIvarLoc,PropertyIvarLoc, PropertyIvar,
1039 PropertyIvarType, /*Dinfo=*/nullptr,
1040 ObjCIvarDecl::Private,
1041 (Expr *)nullptr, true);
1042 if (RequireNonAbstractType(PropertyIvarLoc,
1044 diag::err_abstract_type_in_decl,
1045 AbstractSynthesizedIvarType)) {
1046 Diag(property->getLocation(), diag::note_property_declare);
1047 Ivar->setInvalidDecl();
1048 } else if (CompleteTypeErr)
1049 Ivar->setInvalidDecl();
1050 ClassImpDecl->addDecl(Ivar);
1051 IDecl->makeDeclVisibleInContext(Ivar);
1053 if (getLangOpts().ObjCRuntime.isFragile())
1054 Diag(PropertyDiagLoc, diag::error_missing_property_ivar_decl)
1056 // Note! I deliberately want it to fall thru so, we have a
1057 // a property implementation and to avoid future warnings.
1058 } else if (getLangOpts().ObjCRuntime.isNonFragile() &&
1059 !declaresSameEntity(ClassDeclared, IDecl)) {
1060 Diag(PropertyDiagLoc, diag::error_ivar_in_superclass_use)
1061 << property->getDeclName() << Ivar->getDeclName()
1062 << ClassDeclared->getDeclName();
1063 Diag(Ivar->getLocation(), diag::note_previous_access_declaration)
1064 << Ivar << Ivar->getName();
1065 // Note! I deliberately want it to fall thru so more errors are caught.
1067 property->setPropertyIvarDecl(Ivar);
1069 QualType IvarType = Context.getCanonicalType(Ivar->getType());
1071 // Check that type of property and its ivar are type compatible.
1072 if (!Context.hasSameType(PropertyIvarType, IvarType)) {
1073 if (isa<ObjCObjectPointerType>(PropertyIvarType)
1074 && isa<ObjCObjectPointerType>(IvarType))
1076 Context.canAssignObjCInterfaces(
1077 PropertyIvarType->getAs<ObjCObjectPointerType>(),
1078 IvarType->getAs<ObjCObjectPointerType>());
1080 compat = (CheckAssignmentConstraints(PropertyIvarLoc, PropertyIvarType,
1085 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1086 << property->getDeclName() << PropType
1087 << Ivar->getDeclName() << IvarType;
1088 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1089 // Note! I deliberately want it to fall thru so, we have a
1090 // a property implementation and to avoid future warnings.
1093 // FIXME! Rules for properties are somewhat different that those
1094 // for assignments. Use a new routine to consolidate all cases;
1095 // specifically for property redeclarations as well as for ivars.
1096 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1097 QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType();
1098 if (lhsType != rhsType &&
1099 lhsType->isArithmeticType()) {
1100 Diag(PropertyDiagLoc, diag::error_property_ivar_type)
1101 << property->getDeclName() << PropType
1102 << Ivar->getDeclName() << IvarType;
1103 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1104 // Fall thru - see previous comment
1107 // __weak is explicit. So it works on Canonical type.
1108 if ((PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() &&
1109 getLangOpts().getGC() != LangOptions::NonGC)) {
1110 Diag(PropertyDiagLoc, diag::error_weak_property)
1111 << property->getDeclName() << Ivar->getDeclName();
1112 Diag(Ivar->getLocation(), diag::note_ivar_decl);
1113 // Fall thru - see previous comment
1115 // Fall thru - see previous comment
1116 if ((property->getType()->isObjCObjectPointerType() ||
1117 PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() &&
1118 getLangOpts().getGC() != LangOptions::NonGC) {
1119 Diag(PropertyDiagLoc, diag::error_strong_property)
1120 << property->getDeclName() << Ivar->getDeclName();
1121 // Fall thru - see previous comment
1124 if (getLangOpts().ObjCAutoRefCount)
1125 checkARCPropertyImpl(*this, PropertyLoc, property, Ivar);
1126 } else if (PropertyIvar)
1128 Diag(PropertyDiagLoc, diag::error_dynamic_property_ivar_decl);
1130 assert (property && "ActOnPropertyImplDecl - property declaration missing");
1131 ObjCPropertyImplDecl *PIDecl =
1132 ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc,
1135 ObjCPropertyImplDecl::Synthesize
1136 : ObjCPropertyImplDecl::Dynamic),
1137 Ivar, PropertyIvarLoc);
1139 if (CompleteTypeErr || !compat)
1140 PIDecl->setInvalidDecl();
1142 if (ObjCMethodDecl *getterMethod = property->getGetterMethodDecl()) {
1143 getterMethod->createImplicitParams(Context, IDecl);
1144 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1145 Ivar->getType()->isRecordType()) {
1146 // For Objective-C++, need to synthesize the AST for the IVAR object to be
1147 // returned by the getter as it must conform to C++'s copy-return rules.
1148 // FIXME. Eventually we want to do this for Objective-C as well.
1149 SynthesizedFunctionScope Scope(*this, getterMethod);
1150 ImplicitParamDecl *SelfDecl = getterMethod->getSelfDecl();
1151 DeclRefExpr *SelfExpr =
1152 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
1153 VK_LValue, PropertyDiagLoc);
1154 MarkDeclRefReferenced(SelfExpr);
1155 Expr *LoadSelfExpr =
1156 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1157 CK_LValueToRValue, SelfExpr, nullptr,
1160 new (Context) ObjCIvarRefExpr(Ivar,
1161 Ivar->getUsageType(SelfDecl->getType()),
1163 Ivar->getLocation(),
1164 LoadSelfExpr, true, true);
1165 ExprResult Res = PerformCopyInitialization(
1166 InitializedEntity::InitializeResult(PropertyDiagLoc,
1167 getterMethod->getReturnType(),
1169 PropertyDiagLoc, IvarRefExpr);
1170 if (!Res.isInvalid()) {
1171 Expr *ResExpr = Res.getAs<Expr>();
1173 ResExpr = MaybeCreateExprWithCleanups(ResExpr);
1174 PIDecl->setGetterCXXConstructor(ResExpr);
1177 if (property->hasAttr<NSReturnsNotRetainedAttr>() &&
1178 !getterMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
1179 Diag(getterMethod->getLocation(),
1180 diag::warn_property_getter_owning_mismatch);
1181 Diag(property->getLocation(), diag::note_property_declare);
1183 if (getLangOpts().ObjCAutoRefCount && Synthesize)
1184 switch (getterMethod->getMethodFamily()) {
1186 case OMF_retainCount:
1188 case OMF_autorelease:
1189 Diag(getterMethod->getLocation(), diag::err_arc_illegal_method_def)
1190 << 1 << getterMethod->getSelector();
1196 if (ObjCMethodDecl *setterMethod = property->getSetterMethodDecl()) {
1197 setterMethod->createImplicitParams(Context, IDecl);
1198 if (getLangOpts().CPlusPlus && Synthesize && !CompleteTypeErr &&
1199 Ivar->getType()->isRecordType()) {
1200 // FIXME. Eventually we want to do this for Objective-C as well.
1201 SynthesizedFunctionScope Scope(*this, setterMethod);
1202 ImplicitParamDecl *SelfDecl = setterMethod->getSelfDecl();
1203 DeclRefExpr *SelfExpr =
1204 new (Context) DeclRefExpr(SelfDecl, false, SelfDecl->getType(),
1205 VK_LValue, PropertyDiagLoc);
1206 MarkDeclRefReferenced(SelfExpr);
1207 Expr *LoadSelfExpr =
1208 ImplicitCastExpr::Create(Context, SelfDecl->getType(),
1209 CK_LValueToRValue, SelfExpr, nullptr,
1212 new (Context) ObjCIvarRefExpr(Ivar,
1213 Ivar->getUsageType(SelfDecl->getType()),
1215 Ivar->getLocation(),
1216 LoadSelfExpr, true, true);
1217 ObjCMethodDecl::param_iterator P = setterMethod->param_begin();
1218 ParmVarDecl *Param = (*P);
1219 QualType T = Param->getType().getNonReferenceType();
1220 DeclRefExpr *rhs = new (Context) DeclRefExpr(Param, false, T,
1221 VK_LValue, PropertyDiagLoc);
1222 MarkDeclRefReferenced(rhs);
1223 ExprResult Res = BuildBinOp(S, PropertyDiagLoc,
1224 BO_Assign, lhs, rhs);
1225 if (property->getPropertyAttributes() &
1226 ObjCPropertyDecl::OBJC_PR_atomic) {
1227 Expr *callExpr = Res.getAs<Expr>();
1228 if (const CXXOperatorCallExpr *CXXCE =
1229 dyn_cast_or_null<CXXOperatorCallExpr>(callExpr))
1230 if (const FunctionDecl *FuncDecl = CXXCE->getDirectCallee())
1231 if (!FuncDecl->isTrivial())
1232 if (property->getType()->isReferenceType()) {
1233 Diag(PropertyDiagLoc,
1234 diag::err_atomic_property_nontrivial_assign_op)
1235 << property->getType();
1236 Diag(FuncDecl->getLocStart(),
1237 diag::note_callee_decl) << FuncDecl;
1240 PIDecl->setSetterCXXAssignment(Res.getAs<Expr>());
1246 if (ObjCPropertyImplDecl *PPIDecl =
1247 IC->FindPropertyImplIvarDecl(PropertyIvar)) {
1248 Diag(PropertyLoc, diag::error_duplicate_ivar_use)
1249 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1251 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1254 if (ObjCPropertyImplDecl *PPIDecl
1255 = IC->FindPropertyImplDecl(PropertyId)) {
1256 Diag(PropertyLoc, diag::error_property_implemented) << PropertyId;
1257 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1260 IC->addPropertyImplementation(PIDecl);
1261 if (getLangOpts().ObjCDefaultSynthProperties &&
1262 getLangOpts().ObjCRuntime.isNonFragile() &&
1263 !IDecl->isObjCRequiresPropertyDefs()) {
1264 // Diagnose if an ivar was lazily synthesdized due to a previous
1265 // use and if 1) property is @dynamic or 2) property is synthesized
1266 // but it requires an ivar of different name.
1267 ObjCInterfaceDecl *ClassDeclared=nullptr;
1268 ObjCIvarDecl *Ivar = nullptr;
1270 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1272 if (PropertyIvar && PropertyIvar != PropertyId)
1273 Ivar = IDecl->lookupInstanceVariable(PropertyId, ClassDeclared);
1275 // Issue diagnostics only if Ivar belongs to current class.
1276 if (Ivar && Ivar->getSynthesize() &&
1277 declaresSameEntity(IC->getClassInterface(), ClassDeclared)) {
1278 Diag(Ivar->getLocation(), diag::err_undeclared_var_use)
1280 Ivar->setInvalidDecl();
1285 if (ObjCPropertyImplDecl *PPIDecl =
1286 CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) {
1287 Diag(PropertyDiagLoc, diag::error_duplicate_ivar_use)
1288 << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier()
1290 Diag(PPIDecl->getLocation(), diag::note_previous_use);
1293 if (ObjCPropertyImplDecl *PPIDecl =
1294 CatImplClass->FindPropertyImplDecl(PropertyId)) {
1295 Diag(PropertyDiagLoc, diag::error_property_implemented) << PropertyId;
1296 Diag(PPIDecl->getLocation(), diag::note_previous_declaration);
1299 CatImplClass->addPropertyImplementation(PIDecl);
1305 //===----------------------------------------------------------------------===//
1307 //===----------------------------------------------------------------------===//
1309 /// DiagnosePropertyMismatch - Compares two properties for their
1310 /// attributes and types and warns on a variety of inconsistencies.
1313 Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property,
1314 ObjCPropertyDecl *SuperProperty,
1315 const IdentifierInfo *inheritedName,
1316 bool OverridingProtocolProperty) {
1317 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1318 Property->getPropertyAttributes();
1319 ObjCPropertyDecl::PropertyAttributeKind SAttr =
1320 SuperProperty->getPropertyAttributes();
1322 // We allow readonly properties without an explicit ownership
1323 // (assign/unsafe_unretained/weak/retain/strong/copy) in super class
1324 // to be overridden by a property with any explicit ownership in the subclass.
1325 if (!OverridingProtocolProperty &&
1326 !getOwnershipRule(SAttr) && getOwnershipRule(CAttr))
1329 if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly)
1330 && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite))
1331 Diag(Property->getLocation(), diag::warn_readonly_property)
1332 << Property->getDeclName() << inheritedName;
1333 if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy)
1334 != (SAttr & ObjCPropertyDecl::OBJC_PR_copy))
1335 Diag(Property->getLocation(), diag::warn_property_attribute)
1336 << Property->getDeclName() << "copy" << inheritedName;
1337 else if (!(SAttr & ObjCPropertyDecl::OBJC_PR_readonly)){
1338 unsigned CAttrRetain =
1340 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1341 unsigned SAttrRetain =
1343 (ObjCPropertyDecl::OBJC_PR_retain | ObjCPropertyDecl::OBJC_PR_strong));
1344 bool CStrong = (CAttrRetain != 0);
1345 bool SStrong = (SAttrRetain != 0);
1346 if (CStrong != SStrong)
1347 Diag(Property->getLocation(), diag::warn_property_attribute)
1348 << Property->getDeclName() << "retain (or strong)" << inheritedName;
1352 if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)
1353 != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
1354 Diag(Property->getLocation(), diag::warn_property_attribute)
1355 << Property->getDeclName() << "atomic" << inheritedName;
1356 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1358 if (Property->getSetterName() != SuperProperty->getSetterName()) {
1359 Diag(Property->getLocation(), diag::warn_property_attribute)
1360 << Property->getDeclName() << "setter" << inheritedName;
1361 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1363 if (Property->getGetterName() != SuperProperty->getGetterName()) {
1364 Diag(Property->getLocation(), diag::warn_property_attribute)
1365 << Property->getDeclName() << "getter" << inheritedName;
1366 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1370 Context.getCanonicalType(SuperProperty->getType());
1372 Context.getCanonicalType(Property->getType());
1374 if (!Context.propertyTypesAreCompatible(LHSType, RHSType)) {
1375 // Do cases not handled in above.
1376 // FIXME. For future support of covariant property types, revisit this.
1377 bool IncompatibleObjC = false;
1378 QualType ConvertedType;
1379 if (!isObjCPointerConversion(RHSType, LHSType,
1380 ConvertedType, IncompatibleObjC) ||
1382 Diag(Property->getLocation(), diag::warn_property_types_are_incompatible)
1383 << Property->getType() << SuperProperty->getType() << inheritedName;
1384 Diag(SuperProperty->getLocation(), diag::note_property_declare);
1389 bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
1390 ObjCMethodDecl *GetterMethod,
1391 SourceLocation Loc) {
1394 QualType GetterType = GetterMethod->getReturnType().getNonReferenceType();
1395 QualType PropertyIvarType = property->getType().getNonReferenceType();
1396 bool compat = Context.hasSameType(PropertyIvarType, GetterType);
1398 if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
1399 isa<ObjCObjectPointerType>(GetterType))
1401 Context.canAssignObjCInterfaces(
1402 GetterType->getAs<ObjCObjectPointerType>(),
1403 PropertyIvarType->getAs<ObjCObjectPointerType>());
1404 else if (CheckAssignmentConstraints(Loc, GetterType, PropertyIvarType)
1406 Diag(Loc, diag::error_property_accessor_type)
1407 << property->getDeclName() << PropertyIvarType
1408 << GetterMethod->getSelector() << GetterType;
1409 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1413 QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
1414 QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
1415 if (lhsType != rhsType && lhsType->isArithmeticType())
1421 Diag(Loc, diag::warn_accessor_property_type_mismatch)
1422 << property->getDeclName()
1423 << GetterMethod->getSelector();
1424 Diag(GetterMethod->getLocation(), diag::note_declared_at);
1431 /// CollectImmediateProperties - This routine collects all properties in
1432 /// the class and its conforming protocols; but not those in its super class.
1433 static void CollectImmediateProperties(ObjCContainerDecl *CDecl,
1434 ObjCContainerDecl::PropertyMap &PropMap,
1435 ObjCContainerDecl::PropertyMap &SuperPropMap,
1436 bool IncludeProtocols = true) {
1438 if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1439 for (auto *Prop : IDecl->properties())
1440 PropMap[Prop->getIdentifier()] = Prop;
1441 if (IncludeProtocols) {
1442 // Scan through class's protocols.
1443 for (auto *PI : IDecl->all_referenced_protocols())
1444 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1447 if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1448 if (!CATDecl->IsClassExtension())
1449 for (auto *Prop : CATDecl->properties())
1450 PropMap[Prop->getIdentifier()] = Prop;
1451 if (IncludeProtocols) {
1452 // Scan through class's protocols.
1453 for (auto *PI : CATDecl->protocols())
1454 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1457 else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1458 for (auto *Prop : PDecl->properties()) {
1459 ObjCPropertyDecl *PropertyFromSuper = SuperPropMap[Prop->getIdentifier()];
1460 // Exclude property for protocols which conform to class's super-class,
1461 // as super-class has to implement the property.
1462 if (!PropertyFromSuper ||
1463 PropertyFromSuper->getIdentifier() != Prop->getIdentifier()) {
1464 ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()];
1469 // scan through protocol's protocols.
1470 for (auto *PI : PDecl->protocols())
1471 CollectImmediateProperties(PI, PropMap, SuperPropMap);
1475 /// CollectSuperClassPropertyImplementations - This routine collects list of
1476 /// properties to be implemented in super class(s) and also coming from their
1477 /// conforming protocols.
1478 static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
1479 ObjCInterfaceDecl::PropertyMap &PropMap) {
1480 if (ObjCInterfaceDecl *SDecl = CDecl->getSuperClass()) {
1481 ObjCInterfaceDecl::PropertyDeclOrder PO;
1483 SDecl->collectPropertiesToImplement(PropMap, PO);
1484 SDecl = SDecl->getSuperClass();
1489 /// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
1490 /// an ivar synthesized for 'Method' and 'Method' is a property accessor
1491 /// declared in class 'IFace'.
1493 Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
1494 ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
1495 if (!IV->getSynthesize())
1497 ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
1498 Method->isInstanceMethod());
1499 if (!IMD || !IMD->isPropertyAccessor())
1502 // look up a property declaration whose one of its accessors is implemented
1504 for (const auto *Property : IFace->properties()) {
1505 if ((Property->getGetterName() == IMD->getSelector() ||
1506 Property->getSetterName() == IMD->getSelector()) &&
1507 (Property->getPropertyIvarDecl() == IV))
1513 static bool SuperClassImplementsProperty(ObjCInterfaceDecl *IDecl,
1514 ObjCPropertyDecl *Prop) {
1515 bool SuperClassImplementsGetter = false;
1516 bool SuperClassImplementsSetter = false;
1517 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1518 SuperClassImplementsSetter = true;
1520 while (IDecl->getSuperClass()) {
1521 ObjCInterfaceDecl *SDecl = IDecl->getSuperClass();
1522 if (!SuperClassImplementsGetter && SDecl->getInstanceMethod(Prop->getGetterName()))
1523 SuperClassImplementsGetter = true;
1525 if (!SuperClassImplementsSetter && SDecl->getInstanceMethod(Prop->getSetterName()))
1526 SuperClassImplementsSetter = true;
1527 if (SuperClassImplementsGetter && SuperClassImplementsSetter)
1529 IDecl = IDecl->getSuperClass();
1534 /// \brief Default synthesizes all properties which must be synthesized
1535 /// in class's \@implementation.
1536 void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,
1537 ObjCInterfaceDecl *IDecl) {
1539 ObjCInterfaceDecl::PropertyMap PropMap;
1540 ObjCInterfaceDecl::PropertyDeclOrder PropertyOrder;
1541 IDecl->collectPropertiesToImplement(PropMap, PropertyOrder);
1542 if (PropMap.empty())
1544 ObjCInterfaceDecl::PropertyMap SuperPropMap;
1545 CollectSuperClassPropertyImplementations(IDecl, SuperPropMap);
1547 for (unsigned i = 0, e = PropertyOrder.size(); i != e; i++) {
1548 ObjCPropertyDecl *Prop = PropertyOrder[i];
1549 // Is there a matching property synthesize/dynamic?
1550 if (Prop->isInvalidDecl() ||
1551 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional)
1553 // Property may have been synthesized by user.
1554 if (IMPDecl->FindPropertyImplDecl(Prop->getIdentifier()))
1556 if (IMPDecl->getInstanceMethod(Prop->getGetterName())) {
1557 if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readonly)
1559 if (IMPDecl->getInstanceMethod(Prop->getSetterName()))
1562 if (ObjCPropertyImplDecl *PID =
1563 IMPDecl->FindPropertyImplIvarDecl(Prop->getIdentifier())) {
1564 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_shared_ivar_property)
1565 << Prop->getIdentifier();
1566 if (!PID->getLocation().isInvalid())
1567 Diag(PID->getLocation(), diag::note_property_synthesize);
1570 ObjCPropertyDecl *PropInSuperClass = SuperPropMap[Prop->getIdentifier()];
1571 if (ObjCProtocolDecl *Proto =
1572 dyn_cast<ObjCProtocolDecl>(Prop->getDeclContext())) {
1573 // We won't auto-synthesize properties declared in protocols.
1574 // Suppress the warning if class's superclass implements property's
1575 // getter and implements property's setter (if readwrite property).
1576 // Or, if property is going to be implemented in its super class.
1577 if (!SuperClassImplementsProperty(IDecl, Prop) && !PropInSuperClass) {
1578 Diag(IMPDecl->getLocation(),
1579 diag::warn_auto_synthesizing_protocol_property)
1581 Diag(Prop->getLocation(), diag::note_property_declare);
1585 // If property to be implemented in the super class, ignore.
1586 if (PropInSuperClass) {
1587 if ((Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_readwrite) &&
1588 (PropInSuperClass->getPropertyAttributes() &
1589 ObjCPropertyDecl::OBJC_PR_readonly) &&
1590 !IMPDecl->getInstanceMethod(Prop->getSetterName()) &&
1591 !IDecl->HasUserDeclaredSetterMethod(Prop)) {
1592 Diag(Prop->getLocation(), diag::warn_no_autosynthesis_property)
1593 << Prop->getIdentifier();
1594 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1597 Diag(Prop->getLocation(), diag::warn_autosynthesis_property_in_superclass)
1598 << Prop->getIdentifier();
1599 Diag(PropInSuperClass->getLocation(), diag::note_property_declare);
1600 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1604 // We use invalid SourceLocations for the synthesized ivars since they
1605 // aren't really synthesized at a particular location; they just exist.
1606 // Saying that they are located at the @implementation isn't really going
1608 ObjCPropertyImplDecl *PIDecl = dyn_cast_or_null<ObjCPropertyImplDecl>(
1609 ActOnPropertyImplDecl(S, SourceLocation(), SourceLocation(),
1611 /* property = */ Prop->getIdentifier(),
1612 /* ivar = */ Prop->getDefaultSynthIvarName(Context),
1613 Prop->getLocation()));
1615 Diag(Prop->getLocation(), diag::warn_missing_explicit_synthesis);
1616 Diag(IMPDecl->getLocation(), diag::note_while_in_implementation);
1621 void Sema::DefaultSynthesizeProperties(Scope *S, Decl *D) {
1622 if (!LangOpts.ObjCDefaultSynthProperties || LangOpts.ObjCRuntime.isFragile())
1624 ObjCImplementationDecl *IC=dyn_cast_or_null<ObjCImplementationDecl>(D);
1627 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface())
1628 if (!IDecl->isObjCRequiresPropertyDefs())
1629 DefaultSynthesizeProperties(S, IC, IDecl);
1632 static void DiagnoseUnimplementedAccessor(Sema &S,
1633 ObjCInterfaceDecl *PrimaryClass,
1635 ObjCImplDecl* IMPDecl,
1636 ObjCContainerDecl *CDecl,
1637 ObjCCategoryDecl *C,
1638 ObjCPropertyDecl *Prop,
1639 Sema::SelectorSet &SMap) {
1640 // When reporting on missing property setter/getter implementation in
1641 // categories, do not report when they are declared in primary class,
1642 // class's protocol, or one of it super classes. This is because,
1643 // the class is going to implement them.
1644 if (!SMap.count(Method) &&
1645 (PrimaryClass == nullptr ||
1646 !PrimaryClass->lookupPropertyAccessor(Method, C))) {
1647 S.Diag(IMPDecl->getLocation(),
1648 isa<ObjCCategoryDecl>(CDecl) ?
1649 diag::warn_setter_getter_impl_required_in_category :
1650 diag::warn_setter_getter_impl_required)
1651 << Prop->getDeclName() << Method;
1652 S.Diag(Prop->getLocation(),
1653 diag::note_property_declare);
1654 if (S.LangOpts.ObjCDefaultSynthProperties &&
1655 S.LangOpts.ObjCRuntime.isNonFragile())
1656 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(CDecl))
1657 if (const ObjCInterfaceDecl *RID = ID->isObjCRequiresPropertyDefs())
1658 S.Diag(RID->getLocation(), diag::note_suppressed_class_declare);
1662 void Sema::DiagnoseUnimplementedProperties(Scope *S, ObjCImplDecl* IMPDecl,
1663 ObjCContainerDecl *CDecl,
1664 bool SynthesizeProperties) {
1665 ObjCContainerDecl::PropertyMap PropMap;
1666 ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1668 if (!SynthesizeProperties) {
1669 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1670 // Gather properties which need not be implemented in this class
1673 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
1674 // For categories, no need to implement properties declared in
1675 // its primary class (and its super classes) if property is
1676 // declared in one of those containers.
1677 if ((IDecl = C->getClassInterface())) {
1678 ObjCInterfaceDecl::PropertyDeclOrder PO;
1679 IDecl->collectPropertiesToImplement(NoNeedToImplPropMap, PO);
1683 CollectSuperClassPropertyImplementations(IDecl, NoNeedToImplPropMap);
1685 CollectImmediateProperties(CDecl, PropMap, NoNeedToImplPropMap);
1688 // Scan the @interface to see if any of the protocols it adopts
1689 // require an explicit implementation, via attribute
1690 // 'objc_protocol_requires_explicit_implementation'.
1692 std::unique_ptr<ObjCContainerDecl::PropertyMap> LazyMap;
1694 for (auto *PDecl : IDecl->all_referenced_protocols()) {
1695 if (!PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1697 // Lazily construct a set of all the properties in the @interface
1698 // of the class, without looking at the superclass. We cannot
1699 // use the call to CollectImmediateProperties() above as that
1700 // utilizes information from the super class's properties as well
1701 // as scans the adopted protocols. This work only triggers for protocols
1702 // with the attribute, which is very rare, and only occurs when
1703 // analyzing the @implementation.
1705 ObjCContainerDecl::PropertyMap NoNeedToImplPropMap;
1706 LazyMap.reset(new ObjCContainerDecl::PropertyMap());
1707 CollectImmediateProperties(CDecl, *LazyMap, NoNeedToImplPropMap,
1708 /* IncludeProtocols */ false);
1710 // Add the properties of 'PDecl' to the list of properties that
1711 // need to be implemented.
1712 for (auto *PropDecl : PDecl->properties()) {
1713 if ((*LazyMap)[PropDecl->getIdentifier()])
1715 PropMap[PropDecl->getIdentifier()] = PropDecl;
1720 if (PropMap.empty())
1723 llvm::DenseSet<ObjCPropertyDecl *> PropImplMap;
1724 for (const auto *I : IMPDecl->property_impls())
1725 PropImplMap.insert(I->getPropertyDecl());
1728 // Collect property accessors implemented in current implementation.
1729 for (const auto *I : IMPDecl->instance_methods())
1730 InsMap.insert(I->getSelector());
1732 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1733 ObjCInterfaceDecl *PrimaryClass = nullptr;
1734 if (C && !C->IsClassExtension())
1735 if ((PrimaryClass = C->getClassInterface()))
1736 // Report unimplemented properties in the category as well.
1737 if (ObjCImplDecl *IMP = PrimaryClass->getImplementation()) {
1738 // When reporting on missing setter/getters, do not report when
1739 // setter/getter is implemented in category's primary class
1741 for (const auto *I : IMP->instance_methods())
1742 InsMap.insert(I->getSelector());
1745 for (ObjCContainerDecl::PropertyMap::iterator
1746 P = PropMap.begin(), E = PropMap.end(); P != E; ++P) {
1747 ObjCPropertyDecl *Prop = P->second;
1748 // Is there a matching propery synthesize/dynamic?
1749 if (Prop->isInvalidDecl() ||
1750 Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional ||
1751 PropImplMap.count(Prop) ||
1752 Prop->getAvailability() == AR_Unavailable)
1755 // Diagnose unimplemented getters and setters.
1756 DiagnoseUnimplementedAccessor(*this,
1757 PrimaryClass, Prop->getGetterName(), IMPDecl, CDecl, C, Prop, InsMap);
1758 if (!Prop->isReadOnly())
1759 DiagnoseUnimplementedAccessor(*this,
1760 PrimaryClass, Prop->getSetterName(),
1761 IMPDecl, CDecl, C, Prop, InsMap);
1765 void Sema::diagnoseNullResettableSynthesizedSetters(const ObjCImplDecl *impDecl) {
1766 for (const auto *propertyImpl : impDecl->property_impls()) {
1767 const auto *property = propertyImpl->getPropertyDecl();
1769 // Warn about null_resettable properties with synthesized setters,
1770 // because the setter won't properly handle nil.
1771 if (propertyImpl->getPropertyImplementation()
1772 == ObjCPropertyImplDecl::Synthesize &&
1773 (property->getPropertyAttributes() &
1774 ObjCPropertyDecl::OBJC_PR_null_resettable) &&
1775 property->getGetterMethodDecl() &&
1776 property->getSetterMethodDecl()) {
1777 auto *getterMethod = property->getGetterMethodDecl();
1778 auto *setterMethod = property->getSetterMethodDecl();
1779 if (!impDecl->getInstanceMethod(setterMethod->getSelector()) &&
1780 !impDecl->getInstanceMethod(getterMethod->getSelector())) {
1781 SourceLocation loc = propertyImpl->getLocation();
1782 if (loc.isInvalid())
1783 loc = impDecl->getLocStart();
1785 Diag(loc, diag::warn_null_resettable_setter)
1786 << setterMethod->getSelector() << property->getDeclName();
1793 Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl,
1794 ObjCContainerDecl* IDecl) {
1795 // Rules apply in non-GC mode only
1796 if (getLangOpts().getGC() != LangOptions::NonGC)
1798 for (const auto *Property : IDecl->properties()) {
1799 ObjCMethodDecl *GetterMethod = nullptr;
1800 ObjCMethodDecl *SetterMethod = nullptr;
1801 bool LookedUpGetterSetter = false;
1803 unsigned Attributes = Property->getPropertyAttributes();
1804 unsigned AttributesAsWritten = Property->getPropertyAttributesAsWritten();
1806 if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic) &&
1807 !(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
1808 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1809 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1810 LookedUpGetterSetter = true;
1812 Diag(GetterMethod->getLocation(),
1813 diag::warn_default_atomic_custom_getter_setter)
1814 << Property->getIdentifier() << 0;
1815 Diag(Property->getLocation(), diag::note_property_declare);
1818 Diag(SetterMethod->getLocation(),
1819 diag::warn_default_atomic_custom_getter_setter)
1820 << Property->getIdentifier() << 1;
1821 Diag(Property->getLocation(), diag::note_property_declare);
1825 // We only care about readwrite atomic property.
1826 if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) ||
1827 !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite))
1829 if (const ObjCPropertyImplDecl *PIDecl
1830 = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) {
1831 if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic)
1833 if (!LookedUpGetterSetter) {
1834 GetterMethod = IMPDecl->getInstanceMethod(Property->getGetterName());
1835 SetterMethod = IMPDecl->getInstanceMethod(Property->getSetterName());
1837 if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) {
1838 SourceLocation MethodLoc =
1839 (GetterMethod ? GetterMethod->getLocation()
1840 : SetterMethod->getLocation());
1841 Diag(MethodLoc, diag::warn_atomic_property_rule)
1842 << Property->getIdentifier() << (GetterMethod != nullptr)
1843 << (SetterMethod != nullptr);
1845 if (!AttributesAsWritten) {
1846 if (Property->getLParenLoc().isValid()) {
1847 // @property () ... case.
1848 SourceRange PropSourceRange(Property->getAtLoc(),
1849 Property->getLParenLoc());
1850 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1851 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic");
1855 SourceLocation endLoc =
1856 Property->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
1857 endLoc = endLoc.getLocWithOffset(-1);
1858 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1859 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1860 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic) ");
1863 else if (!(AttributesAsWritten & ObjCPropertyDecl::OBJC_PR_atomic)) {
1864 // @property () ... case.
1865 SourceLocation endLoc = Property->getLParenLoc();
1866 SourceRange PropSourceRange(Property->getAtLoc(), endLoc);
1867 Diag(Property->getLocation(), diag::note_atomic_property_fixup_suggest) <<
1868 FixItHint::CreateReplacement(PropSourceRange, "@property (nonatomic, ");
1871 Diag(MethodLoc, diag::note_atomic_property_fixup_suggest);
1872 Diag(Property->getLocation(), diag::note_property_declare);
1878 void Sema::DiagnoseOwningPropertyGetterSynthesis(const ObjCImplementationDecl *D) {
1879 if (getLangOpts().getGC() == LangOptions::GCOnly)
1882 for (const auto *PID : D->property_impls()) {
1883 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
1884 if (PD && !PD->hasAttr<NSReturnsNotRetainedAttr>() &&
1885 !D->getInstanceMethod(PD->getGetterName())) {
1886 ObjCMethodDecl *method = PD->getGetterMethodDecl();
1889 ObjCMethodFamily family = method->getMethodFamily();
1890 if (family == OMF_alloc || family == OMF_copy ||
1891 family == OMF_mutableCopy || family == OMF_new) {
1892 if (getLangOpts().ObjCAutoRefCount)
1893 Diag(PD->getLocation(), diag::err_cocoa_naming_owned_rule);
1895 Diag(PD->getLocation(), diag::warn_cocoa_naming_owned_rule);
1897 // Look for a getter explicitly declared alongside the property.
1898 // If we find one, use its location for the note.
1899 SourceLocation noteLoc = PD->getLocation();
1900 SourceLocation fixItLoc;
1901 for (auto *getterRedecl : method->redecls()) {
1902 if (getterRedecl->isImplicit())
1904 if (getterRedecl->getDeclContext() != PD->getDeclContext())
1906 noteLoc = getterRedecl->getLocation();
1907 fixItLoc = getterRedecl->getLocEnd();
1910 Preprocessor &PP = getPreprocessor();
1911 TokenValue tokens[] = {
1912 tok::kw___attribute, tok::l_paren, tok::l_paren,
1913 PP.getIdentifierInfo("objc_method_family"), tok::l_paren,
1914 PP.getIdentifierInfo("none"), tok::r_paren,
1915 tok::r_paren, tok::r_paren
1917 StringRef spelling = "__attribute__((objc_method_family(none)))";
1918 StringRef macroName = PP.getLastMacroWithSpelling(noteLoc, tokens);
1919 if (!macroName.empty())
1920 spelling = macroName;
1922 auto noteDiag = Diag(noteLoc, diag::note_cocoa_naming_declare_family)
1923 << method->getDeclName() << spelling;
1924 if (fixItLoc.isValid()) {
1925 SmallString<64> fixItText(" ");
1926 fixItText += spelling;
1927 noteDiag << FixItHint::CreateInsertion(fixItLoc, fixItText);
1934 void Sema::DiagnoseMissingDesignatedInitOverrides(
1935 const ObjCImplementationDecl *ImplD,
1936 const ObjCInterfaceDecl *IFD) {
1937 assert(IFD->hasDesignatedInitializers());
1938 const ObjCInterfaceDecl *SuperD = IFD->getSuperClass();
1942 SelectorSet InitSelSet;
1943 for (const auto *I : ImplD->instance_methods())
1944 if (I->getMethodFamily() == OMF_init)
1945 InitSelSet.insert(I->getSelector());
1947 SmallVector<const ObjCMethodDecl *, 8> DesignatedInits;
1948 SuperD->getDesignatedInitializers(DesignatedInits);
1949 for (SmallVector<const ObjCMethodDecl *, 8>::iterator
1950 I = DesignatedInits.begin(), E = DesignatedInits.end(); I != E; ++I) {
1951 const ObjCMethodDecl *MD = *I;
1952 if (!InitSelSet.count(MD->getSelector())) {
1953 Diag(ImplD->getLocation(),
1954 diag::warn_objc_implementation_missing_designated_init_override)
1955 << MD->getSelector();
1956 Diag(MD->getLocation(), diag::note_objc_designated_init_marked_here);
1961 /// AddPropertyAttrs - Propagates attributes from a property to the
1962 /// implicitly-declared getter or setter for that property.
1963 static void AddPropertyAttrs(Sema &S, ObjCMethodDecl *PropertyMethod,
1964 ObjCPropertyDecl *Property) {
1965 // Should we just clone all attributes over?
1966 for (const auto *A : Property->attrs()) {
1967 if (isa<DeprecatedAttr>(A) ||
1968 isa<UnavailableAttr>(A) ||
1969 isa<AvailabilityAttr>(A))
1970 PropertyMethod->addAttr(A->clone(S.Context));
1974 /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods
1975 /// have the property type and issue diagnostics if they don't.
1976 /// Also synthesize a getter/setter method if none exist (and update the
1977 /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized
1978 /// methods is the "right" thing to do.
1979 void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property,
1980 ObjCContainerDecl *CD,
1981 ObjCPropertyDecl *redeclaredProperty,
1982 ObjCContainerDecl *lexicalDC) {
1984 ObjCMethodDecl *GetterMethod, *SetterMethod;
1986 if (CD->isInvalidDecl())
1989 GetterMethod = CD->getInstanceMethod(property->getGetterName());
1990 SetterMethod = CD->getInstanceMethod(property->getSetterName());
1991 DiagnosePropertyAccessorMismatch(property, GetterMethod,
1992 property->getLocation());
1995 ObjCPropertyDecl::PropertyAttributeKind CAttr =
1996 property->getPropertyAttributes();
1997 if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) &&
1998 Context.getCanonicalType(SetterMethod->getReturnType()) !=
2000 Diag(SetterMethod->getLocation(), diag::err_setter_type_void);
2001 if (SetterMethod->param_size() != 1 ||
2002 !Context.hasSameUnqualifiedType(
2003 (*SetterMethod->param_begin())->getType().getNonReferenceType(),
2004 property->getType().getNonReferenceType())) {
2005 Diag(property->getLocation(),
2006 diag::warn_accessor_property_type_mismatch)
2007 << property->getDeclName()
2008 << SetterMethod->getSelector();
2009 Diag(SetterMethod->getLocation(), diag::note_declared_at);
2013 // Synthesize getter/setter methods if none exist.
2014 // Find the default getter and if one not found, add one.
2015 // FIXME: The synthesized property we set here is misleading. We almost always
2016 // synthesize these methods unless the user explicitly provided prototypes
2017 // (which is odd, but allowed). Sema should be typechecking that the
2018 // declarations jive in that situation (which it is not currently).
2019 if (!GetterMethod) {
2020 // No instance method of same name as property getter name was found.
2021 // Declare a getter method and add it to the list of methods
2023 SourceLocation Loc = redeclaredProperty ?
2024 redeclaredProperty->getLocation() :
2025 property->getLocation();
2027 // If the property is null_resettable, the getter returns nonnull.
2028 QualType resultTy = property->getType();
2029 if (property->getPropertyAttributes() &
2030 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2031 QualType modifiedTy = resultTy;
2032 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)) {
2033 if (*nullability == NullabilityKind::Unspecified)
2034 resultTy = Context.getAttributedType(AttributedType::attr_nonnull,
2035 modifiedTy, modifiedTy);
2039 GetterMethod = ObjCMethodDecl::Create(Context, Loc, Loc,
2040 property->getGetterName(),
2041 resultTy, nullptr, CD,
2042 /*isInstance=*/true, /*isVariadic=*/false,
2043 /*isPropertyAccessor=*/true,
2044 /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
2045 (property->getPropertyImplementation() ==
2046 ObjCPropertyDecl::Optional) ?
2047 ObjCMethodDecl::Optional :
2048 ObjCMethodDecl::Required);
2049 CD->addDecl(GetterMethod);
2051 AddPropertyAttrs(*this, GetterMethod, property);
2053 // FIXME: Eventually this shouldn't be needed, as the lexical context
2054 // and the real context should be the same.
2056 GetterMethod->setLexicalDeclContext(lexicalDC);
2057 if (property->hasAttr<NSReturnsNotRetainedAttr>())
2058 GetterMethod->addAttr(NSReturnsNotRetainedAttr::CreateImplicit(Context,
2061 if (property->hasAttr<ObjCReturnsInnerPointerAttr>())
2062 GetterMethod->addAttr(
2063 ObjCReturnsInnerPointerAttr::CreateImplicit(Context, Loc));
2065 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2066 GetterMethod->addAttr(
2067 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2068 SA->getName(), Loc));
2070 if (getLangOpts().ObjCAutoRefCount)
2071 CheckARCMethodDecl(GetterMethod);
2073 // A user declared getter will be synthesize when @synthesize of
2074 // the property with the same name is seen in the @implementation
2075 GetterMethod->setPropertyAccessor(true);
2076 property->setGetterMethodDecl(GetterMethod);
2078 // Skip setter if property is read-only.
2079 if (!property->isReadOnly()) {
2080 // Find the default setter and if one not found, add one.
2081 if (!SetterMethod) {
2082 // No instance method of same name as property setter name was found.
2083 // Declare a setter method and add it to the list of methods
2085 SourceLocation Loc = redeclaredProperty ?
2086 redeclaredProperty->getLocation() :
2087 property->getLocation();
2090 ObjCMethodDecl::Create(Context, Loc, Loc,
2091 property->getSetterName(), Context.VoidTy,
2092 nullptr, CD, /*isInstance=*/true,
2093 /*isVariadic=*/false,
2094 /*isPropertyAccessor=*/true,
2095 /*isImplicitlyDeclared=*/true,
2096 /*isDefined=*/false,
2097 (property->getPropertyImplementation() ==
2098 ObjCPropertyDecl::Optional) ?
2099 ObjCMethodDecl::Optional :
2100 ObjCMethodDecl::Required);
2102 // If the property is null_resettable, the setter accepts a
2104 QualType paramTy = property->getType().getUnqualifiedType();
2105 if (property->getPropertyAttributes() &
2106 ObjCPropertyDecl::OBJC_PR_null_resettable) {
2107 QualType modifiedTy = paramTy;
2108 if (auto nullability = AttributedType::stripOuterNullability(modifiedTy)){
2109 if (*nullability == NullabilityKind::Unspecified)
2110 paramTy = Context.getAttributedType(AttributedType::attr_nullable,
2111 modifiedTy, modifiedTy);
2115 // Invent the arguments for the setter. We don't bother making a
2116 // nice name for the argument.
2117 ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod,
2119 property->getIdentifier(),
2124 SetterMethod->setMethodParams(Context, Argument, None);
2126 AddPropertyAttrs(*this, SetterMethod, property);
2128 CD->addDecl(SetterMethod);
2129 // FIXME: Eventually this shouldn't be needed, as the lexical context
2130 // and the real context should be the same.
2132 SetterMethod->setLexicalDeclContext(lexicalDC);
2133 if (const SectionAttr *SA = property->getAttr<SectionAttr>())
2134 SetterMethod->addAttr(
2135 SectionAttr::CreateImplicit(Context, SectionAttr::GNU_section,
2136 SA->getName(), Loc));
2137 // It's possible for the user to have set a very odd custom
2138 // setter selector that causes it to have a method family.
2139 if (getLangOpts().ObjCAutoRefCount)
2140 CheckARCMethodDecl(SetterMethod);
2142 // A user declared setter will be synthesize when @synthesize of
2143 // the property with the same name is seen in the @implementation
2144 SetterMethod->setPropertyAccessor(true);
2145 property->setSetterMethodDecl(SetterMethod);
2147 // Add any synthesized methods to the global pool. This allows us to
2148 // handle the following, which is supported by GCC (and part of the design).
2151 // @property double bar;
2154 // void thisIsUnfortunate() {
2156 // double bar = [foo bar];
2160 AddInstanceMethodToGlobalPool(GetterMethod);
2162 AddInstanceMethodToGlobalPool(SetterMethod);
2164 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(CD);
2165 if (!CurrentClass) {
2166 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CD))
2167 CurrentClass = Cat->getClassInterface();
2168 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(CD))
2169 CurrentClass = Impl->getClassInterface();
2172 CheckObjCMethodOverrides(GetterMethod, CurrentClass, Sema::RTC_Unknown);
2174 CheckObjCMethodOverrides(SetterMethod, CurrentClass, Sema::RTC_Unknown);
2177 void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
2179 unsigned &Attributes,
2180 bool propertyInPrimaryClass) {
2181 // FIXME: Improve the reported location.
2182 if (!PDecl || PDecl->isInvalidDecl())
2185 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2186 (Attributes & ObjCDeclSpec::DQ_PR_readwrite))
2187 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2188 << "readonly" << "readwrite";
2190 ObjCPropertyDecl *PropertyDecl = cast<ObjCPropertyDecl>(PDecl);
2191 QualType PropertyTy = PropertyDecl->getType();
2192 unsigned PropertyOwnership = getOwnershipRule(Attributes);
2194 // 'readonly' property with no obvious lifetime.
2195 // its life time will be determined by its backing ivar.
2196 if (getLangOpts().ObjCAutoRefCount &&
2197 Attributes & ObjCDeclSpec::DQ_PR_readonly &&
2198 PropertyTy->isObjCRetainableType() &&
2202 // Check for copy or retain on non-object types.
2203 if ((Attributes & (ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2204 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong)) &&
2205 !PropertyTy->isObjCRetainableType() &&
2206 !PropertyDecl->hasAttr<ObjCNSObjectAttr>()) {
2207 Diag(Loc, diag::err_objc_property_requires_object)
2208 << (Attributes & ObjCDeclSpec::DQ_PR_weak ? "weak" :
2209 Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain (or strong)");
2210 Attributes &= ~(ObjCDeclSpec::DQ_PR_weak | ObjCDeclSpec::DQ_PR_copy |
2211 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong);
2212 PropertyDecl->setInvalidDecl();
2215 // Check for more than one of { assign, copy, retain }.
2216 if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
2217 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2218 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2219 << "assign" << "copy";
2220 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2222 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2223 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2224 << "assign" << "retain";
2225 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2227 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2228 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2229 << "assign" << "strong";
2230 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2232 if (getLangOpts().ObjCAutoRefCount &&
2233 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2234 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2235 << "assign" << "weak";
2236 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2238 if (PropertyDecl->hasAttr<IBOutletCollectionAttr>())
2239 Diag(Loc, diag::warn_iboutletcollection_property_assign);
2240 } else if (Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) {
2241 if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2242 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2243 << "unsafe_unretained" << "copy";
2244 Attributes &= ~ObjCDeclSpec::DQ_PR_copy;
2246 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2247 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2248 << "unsafe_unretained" << "retain";
2249 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2251 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2252 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2253 << "unsafe_unretained" << "strong";
2254 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2256 if (getLangOpts().ObjCAutoRefCount &&
2257 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2258 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2259 << "unsafe_unretained" << "weak";
2260 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2262 } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
2263 if (Attributes & ObjCDeclSpec::DQ_PR_retain) {
2264 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2265 << "copy" << "retain";
2266 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2268 if (Attributes & ObjCDeclSpec::DQ_PR_strong) {
2269 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2270 << "copy" << "strong";
2271 Attributes &= ~ObjCDeclSpec::DQ_PR_strong;
2273 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2274 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2275 << "copy" << "weak";
2276 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2279 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2280 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2281 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2282 << "retain" << "weak";
2283 Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
2285 else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2286 (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
2287 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2288 << "strong" << "weak";
2289 Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
2292 if (Attributes & ObjCDeclSpec::DQ_PR_weak) {
2293 // 'weak' and 'nonnull' are mutually exclusive.
2294 if (auto nullability = PropertyTy->getNullability(Context)) {
2295 if (*nullability == NullabilityKind::NonNull)
2296 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2297 << "nonnull" << "weak";
2300 Context.getAttributedType(
2301 AttributedType::getNullabilityAttrKind(NullabilityKind::Nullable),
2302 PropertyTy, PropertyTy);
2303 TypeSourceInfo *TSInfo = PropertyDecl->getTypeSourceInfo();
2304 PropertyDecl->setType(PropertyTy, TSInfo);
2308 if ((Attributes & ObjCDeclSpec::DQ_PR_atomic) &&
2309 (Attributes & ObjCDeclSpec::DQ_PR_nonatomic)) {
2310 Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
2311 << "atomic" << "nonatomic";
2312 Attributes &= ~ObjCDeclSpec::DQ_PR_atomic;
2315 // Warn if user supplied no assignment attribute, property is
2316 // readwrite, and this is an object type.
2317 if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy |
2318 ObjCDeclSpec::DQ_PR_unsafe_unretained |
2319 ObjCDeclSpec::DQ_PR_retain | ObjCDeclSpec::DQ_PR_strong |
2320 ObjCDeclSpec::DQ_PR_weak)) &&
2321 PropertyTy->isObjCObjectPointerType()) {
2322 if (getLangOpts().ObjCAutoRefCount)
2323 // With arc, @property definitions should default to (strong) when
2324 // not specified; including when property is 'readonly'.
2325 PropertyDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_strong);
2326 else if (!(Attributes & ObjCDeclSpec::DQ_PR_readonly)) {
2328 (PropertyTy->isObjCClassType() ||
2329 PropertyTy->isObjCQualifiedClassType());
2330 // In non-gc, non-arc mode, 'Class' is treated as a 'void *' no need to
2331 // issue any warning.
2332 if (isAnyClassTy && getLangOpts().getGC() == LangOptions::NonGC)
2334 else if (propertyInPrimaryClass) {
2335 // Don't issue warning on property with no life time in class
2336 // extension as it is inherited from property in primary class.
2337 // Skip this warning in gc-only mode.
2338 if (getLangOpts().getGC() != LangOptions::GCOnly)
2339 Diag(Loc, diag::warn_objc_property_no_assignment_attribute);
2341 // If non-gc code warn that this is likely inappropriate.
2342 if (getLangOpts().getGC() == LangOptions::NonGC)
2343 Diag(Loc, diag::warn_objc_property_default_assign_on_object);
2347 // FIXME: Implement warning dependent on NSCopying being
2348 // implemented. See also:
2349 // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496>
2350 // (please trim this list while you are at it).
2353 if (!(Attributes & ObjCDeclSpec::DQ_PR_copy)
2354 &&!(Attributes & ObjCDeclSpec::DQ_PR_readonly)
2355 && getLangOpts().getGC() == LangOptions::GCOnly
2356 && PropertyTy->isBlockPointerType())
2357 Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
2358 else if ((Attributes & ObjCDeclSpec::DQ_PR_retain) &&
2359 !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2360 !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
2361 PropertyTy->isBlockPointerType())
2362 Diag(Loc, diag::warn_objc_property_retain_of_block);
2364 if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
2365 (Attributes & ObjCDeclSpec::DQ_PR_setter))
2366 Diag(Loc, diag::warn_objc_readonly_property_has_setter);